Skip to content

Commit

Permalink
feat!: add stateful precompiles support (#2633)
Browse files Browse the repository at this point in the history
* chore: cleanup Makefile and go.mod

* feat: introduce stateful precompiles initial support

* chore: remove simapp as a dependency

* fix: adapt to testutil to changes

* fix: linting

* chore: add changelog and remove unused vars

* fix: create aggregateAllKeys function

* feat: add e2e test stateful contract (#2703)

* feat: add test precompiled contract

* feat: add test stateful contract

* feat: enable contracts granularly

* feat: allow passing the app ctx to contracts

* revert ctx changes

* format ABI

* add first e2e test

* add RegularCaller contract to e2e

* add signer to deployment

* fix e2e, use regular abi

* minor rewording

* use idiomatic errors

* remove unused files

* rename and generate bindings automatically

* create a new e2e section for precompiles

* add bindings generator and makefile target

* add GetGasStabilityPoolBalance as prototype contract function

* include getGasStabilityPoolBalance function

* apply code review fixes

* include tests for precompiles package

* delete regularcaller

* ignore bindings from codecov

* introduce more unit testing

* add unit testing for prototype.go

* minor linting fixes

* remove ctx, keep fungible keeper private

* increase unit tests coverage

* add keys for new user_precompile

* add init unit test

* switch to double quotes

* avoid go:embed

* fixes derived from code review

* refactor some parts of bech32 functions

* formatting

* new generated mocks

* skip precompile tests during upgrade

* skip precompile

* try to fix generate

* debug script

* install solc in generate files

* install abigen

* cleanup action

* PR comments pt1

* group prototype unit tests a bit

* PR comment missing test

* cleanup action a bit

* add solc checks to script

* cleanup not needed changes

* revert ibc change in test

* remove new test

* reduce simple operations gas required to 500

---------

Co-authored-by: skosito <[email protected]>
Co-authored-by: Dmitry S <[email protected]>
  • Loading branch information
3 people authored Aug 27, 2024
1 parent 42eb4bd commit 949cc26
Show file tree
Hide file tree
Showing 148 changed files with 2,262 additions and 560 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/generate-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
run: |
echo "$HOME/go/bin" >> $GITHUB_PATH
- name: Install solc-select
run: |
pip3 install solc-select
- name: Generate Go code, docs and specs
env:
TEST_ENV: ${{ github.workspace }}
Expand Down
23 changes: 13 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.PHONY: build

PACKAGE_NAME := github.com/zeta-chain/node
VERSION := $(shell ./version.sh)
COMMIT := $(shell [ -z "${COMMIT_ID}" ] && git log -1 --format='%H' || echo ${COMMIT_ID} )
BUILDTIME := $(shell date -u +"%Y%m%d.%H%M%S" )
Expand All @@ -8,7 +9,9 @@ DOCKER ?= docker
# useful for setting profiles
DOCKER_COMPOSE ?= $(DOCKER) compose $(COMPOSE_ARGS)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf
GOFLAGS:=""
GOFLAGS := ""
GOLANG_CROSS_VERSION ?= v1.22.4
GOPATH ?= '$(HOME)/go'

ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=zetacore \
-X github.com/cosmos/cosmos-sdk/version.ServerName=zetacored \
Expand All @@ -23,7 +26,7 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=zetacore \

BUILD_FLAGS := -ldflags '$(ldflags)' -tags pebbledb,ledger

TEST_DIR?="./..."
TEST_DIR ?= "./..."
TEST_BUILD_FLAGS := -tags pebbledb,ledger
HSM_BUILD_FLAGS := -tags pebbledb,ledger,hsm_test

Expand Down Expand Up @@ -53,11 +56,11 @@ go.sum: go.mod
### Test commands ###
###############################################################################

test: clean-test-dir run-test

run-test:
@go test ${TEST_BUILD_FLAGS} ${TEST_DIR}

test :clean-test-dir run-test

test-hsm:
@go test ${HSM_BUILD_FLAGS} ${TEST_DIR}

Expand Down Expand Up @@ -199,8 +202,13 @@ mocks:
@bash ./scripts/mocks-generate.sh
.PHONY: mocks

precompiles:
@echo "--> Generating bindings for precompiled contracts"
@bash ./scripts/bindings-stateful-precompiles.sh
.PHONY: precompiles

# generate also includes Go code formatting
generate: proto-gen openapi specs typescript docs-zetacored mocks fmt
generate: proto-gen openapi specs typescript docs-zetacored mocks precompiles fmt
.PHONY: generate


Expand Down Expand Up @@ -294,7 +302,6 @@ zetanode-upgrade: zetanode
.PHONY: zetanode-upgrade
endif


start-upgrade-test: zetanode-upgrade
@echo "--> Starting upgrade test"
export LOCALNET_MODE=upgrade && \
Expand All @@ -307,7 +314,6 @@ start-upgrade-test-light: zetanode-upgrade
export UPGRADE_HEIGHT=90 && \
cd contrib/localnet/ && $(DOCKER_COMPOSE) --profile upgrade -f docker-compose.yml -f docker-compose-upgrade.yml up -d


start-upgrade-test-admin: zetanode-upgrade
@echo "--> Starting admin upgrade test"
export LOCALNET_MODE=upgrade && \
Expand All @@ -327,9 +333,6 @@ start-upgrade-import-mainnet-test: zetanode-upgrade
### GoReleaser ###
###############################################################################

PACKAGE_NAME := github.com/zeta-chain/node
GOLANG_CROSS_VERSION ?= v1.22.4
GOPATH ?= '$(HOME)/go'
release-dry-run:
docker run \
--rm \
Expand Down
2 changes: 1 addition & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE
package ante

import (
Expand Down
4 changes: 2 additions & 2 deletions app/ante/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE

// Copyright 2023 ZetaChain
// modified to exclude gentx transaction type from the min gas price check
Expand All @@ -26,7 +26,7 @@ import (
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
)

var (
Expand Down
8 changes: 4 additions & 4 deletions app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE

package ante

Expand All @@ -29,9 +29,9 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
ethante "github.com/evmos/ethermint/app/ante"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
ethante "github.com/zeta-chain/ethermint/app/ante"
ethermint "github.com/zeta-chain/ethermint/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"

observerkeeper "github.com/zeta-chain/zetacore/x/observer/keeper"
)
Expand Down
11 changes: 5 additions & 6 deletions app/ante/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE

package ante

Expand All @@ -24,10 +24,9 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/evmos/ethermint/x/evm/statedb"
evmtypes "github.com/evmos/ethermint/x/evm/types"
evm "github.com/evmos/ethermint/x/evm/vm"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
"github.com/zeta-chain/ethermint/x/evm/statedb"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"
)

// DynamicFeeEVMKeeper is a subset of EVMKeeper interface that supports dynamic fee checker
Expand All @@ -42,7 +41,7 @@ type EVMKeeper interface {
statedb.Keeper
DynamicFeeEVMKeeper

NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) evm.EVM
NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
Expand Down
80 changes: 54 additions & 26 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

cosmoserrors "cosmossdk.io/errors"
appparams "cosmossdk.io/simapp/params"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -78,22 +77,22 @@ import (
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
evmante "github.com/evmos/ethermint/app/ante"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm"
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/ethermint/x/evm/vm/geth"
"github.com/evmos/ethermint/x/feemarket"
feemarketkeeper "github.com/evmos/ethermint/x/feemarket/keeper"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
evmante "github.com/zeta-chain/ethermint/app/ante"
ethermint "github.com/zeta-chain/ethermint/types"
"github.com/zeta-chain/ethermint/x/evm"
evmkeeper "github.com/zeta-chain/ethermint/x/evm/keeper"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
"github.com/zeta-chain/ethermint/x/feemarket"
feemarketkeeper "github.com/zeta-chain/ethermint/x/feemarket/keeper"
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"

"github.com/zeta-chain/zetacore/app/ante"
"github.com/zeta-chain/zetacore/docs/openapi"
zetamempool "github.com/zeta-chain/zetacore/pkg/mempool"
"github.com/zeta-chain/zetacore/precompiles"
srvflags "github.com/zeta-chain/zetacore/server/flags"
authoritymodule "github.com/zeta-chain/zetacore/x/authority"
authoritykeeper "github.com/zeta-chain/zetacore/x/authority/keeper"
Expand Down Expand Up @@ -146,16 +145,11 @@ func init() {
var (
AccountAddressPrefix = "zeta"
NodeDir = ".zetacored"
DefaultNodeHome = os.ExpandEnv("$HOME/") + NodeDir

// AddrLen is the allowed length (in bytes) for an address.
//
// NOTE: In the SDK, the default value is 255.
AddrLen = 20
)

var (
// DefaultNodeHome default home directories for wasmd
DefaultNodeHome = os.ExpandEnv("$HOME/") + NodeDir

// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
Bech32PrefixAccAddr = AccountAddressPrefix
Expand Down Expand Up @@ -315,7 +309,7 @@ func New(
skipUpgradeHeights map[int64]bool,
homePath string,
invCheckPeriod uint,
encodingConfig appparams.EncodingConfig,
encodingConfig ethermint.EncodingConfig,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {
Expand Down Expand Up @@ -356,9 +350,9 @@ func New(
consensusparamtypes.StoreKey,
crisistypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
memKeys := sdk.NewMemoryStoreKeys(
//capabilitytypes.MemStoreKey,
// capabilitytypes.MemStoreKey,
)

app := &App{
Expand All @@ -368,7 +362,7 @@ func New(
interfaceRegistry: interfaceRegistry,
invCheckPeriod: invCheckPeriod,
keys: keys,
tkeys: tkeys,
tkeys: tKeys,
memKeys: memKeys,
}
if homePath == "" {
Expand All @@ -378,7 +372,7 @@ func New(
// get authority address
authAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()

app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])
app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tKeys[paramstypes.TStoreKey])
// set the BaseApp's parameter store
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authAddr)
bApp.SetParamStore(&app.ConsensusParamsKeeper)
Expand Down Expand Up @@ -556,25 +550,26 @@ func New(
appCodec,
authtypes.NewModuleAddress(govtypes.ModuleName),
keys[feemarkettypes.StoreKey],
tkeys[feemarkettypes.TransientKey],
tKeys[feemarkettypes.TransientKey],
feeSs,
app.ConsensusParamsKeeper,
)
evmSs := app.GetSubspace(evmtypes.ModuleName)

app.EvmKeeper = evmkeeper.NewKeeper(
appCodec,
keys[evmtypes.StoreKey],
tkeys[evmtypes.TransientKey],
tKeys[evmtypes.TransientKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
&app.FeeMarketKeeper,
nil,
geth.NewEVM,
tracer,
evmSs,
precompiles.StatefulContracts(&app.FungibleKeeper, appCodec, storetypes.TransientGasConfig()),
app.ConsensusParamsKeeper,
aggregateAllKeys(keys, tKeys, memKeys),
)

app.FungibleKeeper = *fungiblekeeper.NewKeeper(
Expand Down Expand Up @@ -813,7 +808,7 @@ func New(

// initialize stores
app.MountKVStores(keys)
app.MountTransientStores(tkeys)
app.MountTransientStores(tKeys)
app.MountMemoryStores(memKeys)

// initialize BaseApp
Expand Down Expand Up @@ -1055,9 +1050,42 @@ func (app *App) SimulationManager() *module.SimulationManager {

func (app *App) BlockedAddrs() map[string]bool {
blockList := make(map[string]bool)

for k, v := range blockedReceivingModAcc {
addr := authtypes.NewModuleAddress(k)
blockList[addr.String()] = v
}

// Each enabled precompiled stateful contract should be added as a BlockedAddrs.
// That way it's marked as non payable by the bank keeper.
for addr, enabled := range precompiles.EnabledStatefulContracts {
if enabled {
blockList[addr.String()] = enabled
}
}

return blockList
}

// aggregateAllKeys aggregates all the keys in a single map.
func aggregateAllKeys(
keys map[string]*storetypes.KVStoreKey,
tKeys map[string]*storetypes.TransientStoreKey,
memKeys map[string]*storetypes.MemoryStoreKey,
) map[string]storetypes.StoreKey {
allKeys := make(map[string]storetypes.StoreKey, len(keys)+len(tKeys)+len(memKeys))

for k, v := range keys {
allKeys[k] = v
}

for k, v := range tKeys {
allKeys[k] = v
}

for k, v := range memKeys {
allKeys[k] = v
}

return allKeys
}
6 changes: 3 additions & 3 deletions app/encoding.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package app

import (
"cosmossdk.io/simapp/params"
evmenc "github.com/evmos/ethermint/encoding"
evmenc "github.com/zeta-chain/ethermint/encoding"
ethermint "github.com/zeta-chain/ethermint/types"
)

// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
func MakeEncodingConfig() ethermint.EncodingConfig {
//encodingConfig := params.MakeEncodingConfig()
encodingConfig := evmenc.MakeConfig(ModuleBasics)
//std.RegisterLegacyAminoCodec(encodingConfig.Amino)
Expand Down
4 changes: 2 additions & 2 deletions app/init_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"

authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
Expand Down
2 changes: 1 addition & 1 deletion app/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethcfg "github.com/evmos/ethermint/cmd/config"
ethcfg "github.com/zeta-chain/ethermint/cmd/config"

cmdcfg "github.com/zeta-chain/zetacore/cmd/zetacored/config"
)
Expand Down
Loading

0 comments on commit 949cc26

Please sign in to comment.