Skip to content

Commit 949cc26

Browse files
Francisco de Borja Aranda Castillejoskositoswift1337
authored
feat!: add stateful precompiles support (#2633)
* 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]>
1 parent 42eb4bd commit 949cc26

File tree

148 files changed

+2262
-560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+2262
-560
lines changed

.github/workflows/generate-files.yml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ jobs:
1818
run: |
1919
echo "$HOME/go/bin" >> $GITHUB_PATH
2020
21+
- name: Install solc-select
22+
run: |
23+
pip3 install solc-select
24+
2125
- name: Generate Go code, docs and specs
2226
env:
2327
TEST_ENV: ${{ github.workspace }}

Makefile

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.PHONY: build
22

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

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

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

26-
TEST_DIR?="./..."
29+
TEST_DIR ?= "./..."
2730
TEST_BUILD_FLAGS := -tags pebbledb,ledger
2831
HSM_BUILD_FLAGS := -tags pebbledb,ledger,hsm_test
2932

@@ -53,11 +56,11 @@ go.sum: go.mod
5356
### Test commands ###
5457
###############################################################################
5558

59+
test: clean-test-dir run-test
60+
5661
run-test:
5762
@go test ${TEST_BUILD_FLAGS} ${TEST_DIR}
5863

59-
test :clean-test-dir run-test
60-
6164
test-hsm:
6265
@go test ${HSM_BUILD_FLAGS} ${TEST_DIR}
6366

@@ -199,8 +202,13 @@ mocks:
199202
@bash ./scripts/mocks-generate.sh
200203
.PHONY: mocks
201204

205+
precompiles:
206+
@echo "--> Generating bindings for precompiled contracts"
207+
@bash ./scripts/bindings-stateful-precompiles.sh
208+
.PHONY: precompiles
209+
202210
# generate also includes Go code formatting
203-
generate: proto-gen openapi specs typescript docs-zetacored mocks fmt
211+
generate: proto-gen openapi specs typescript docs-zetacored mocks precompiles fmt
204212
.PHONY: generate
205213

206214

@@ -294,7 +302,6 @@ zetanode-upgrade: zetanode
294302
.PHONY: zetanode-upgrade
295303
endif
296304

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

310-
311317
start-upgrade-test-admin: zetanode-upgrade
312318
@echo "--> Starting admin upgrade test"
313319
export LOCALNET_MODE=upgrade && \
@@ -327,9 +333,6 @@ start-upgrade-import-mainnet-test: zetanode-upgrade
327333
### GoReleaser ###
328334
###############################################################################
329335

330-
PACKAGE_NAME := github.com/zeta-chain/node
331-
GOLANG_CROSS_VERSION ?= v1.22.4
332-
GOPATH ?= '$(HOME)/go'
333336
release-dry-run:
334337
docker run \
335338
--rm \

app/ante/ante.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
15+
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE
1616
package ante
1717

1818
import (

app/ante/fees.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
15+
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE
1616

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

3232
var (

app/ante/handler_options.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
15+
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE
1616

1717
package ante
1818

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

3636
observerkeeper "github.com/zeta-chain/zetacore/x/observer/keeper"
3737
)

app/ante/interfaces.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
15+
// along with the Ethermint library. If not, see https://github.com/zeta-chain/ethermint/blob/main/LICENSE
1616

1717
package ante
1818

@@ -24,10 +24,9 @@ import (
2424
"github.com/ethereum/go-ethereum/core"
2525
"github.com/ethereum/go-ethereum/core/vm"
2626
"github.com/ethereum/go-ethereum/params"
27-
"github.com/evmos/ethermint/x/evm/statedb"
28-
evmtypes "github.com/evmos/ethermint/x/evm/types"
29-
evm "github.com/evmos/ethermint/x/evm/vm"
30-
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
27+
"github.com/zeta-chain/ethermint/x/evm/statedb"
28+
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
29+
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"
3130
)
3231

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

45-
NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) evm.EVM
44+
NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
4645
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
4746
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
4847
ResetTransientGasUsed(ctx sdk.Context)

app/app.go

+54-26
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
cosmoserrors "cosmossdk.io/errors"
10-
appparams "cosmossdk.io/simapp/params"
1110
dbm "github.com/cometbft/cometbft-db"
1211
abci "github.com/cometbft/cometbft/abci/types"
1312
tmjson "github.com/cometbft/cometbft/libs/json"
@@ -78,22 +77,22 @@ import (
7877
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
7978
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
8079
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
81-
evmante "github.com/evmos/ethermint/app/ante"
82-
ethermint "github.com/evmos/ethermint/types"
83-
"github.com/evmos/ethermint/x/evm"
84-
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
85-
evmtypes "github.com/evmos/ethermint/x/evm/types"
86-
"github.com/evmos/ethermint/x/evm/vm/geth"
87-
"github.com/evmos/ethermint/x/feemarket"
88-
feemarketkeeper "github.com/evmos/ethermint/x/feemarket/keeper"
89-
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
9080
"github.com/gorilla/mux"
9181
"github.com/rakyll/statik/fs"
9282
"github.com/spf13/cast"
83+
evmante "github.com/zeta-chain/ethermint/app/ante"
84+
ethermint "github.com/zeta-chain/ethermint/types"
85+
"github.com/zeta-chain/ethermint/x/evm"
86+
evmkeeper "github.com/zeta-chain/ethermint/x/evm/keeper"
87+
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
88+
"github.com/zeta-chain/ethermint/x/feemarket"
89+
feemarketkeeper "github.com/zeta-chain/ethermint/x/feemarket/keeper"
90+
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"
9391

9492
"github.com/zeta-chain/zetacore/app/ante"
9593
"github.com/zeta-chain/zetacore/docs/openapi"
9694
zetamempool "github.com/zeta-chain/zetacore/pkg/mempool"
95+
"github.com/zeta-chain/zetacore/precompiles"
9796
srvflags "github.com/zeta-chain/zetacore/server/flags"
9897
authoritymodule "github.com/zeta-chain/zetacore/x/authority"
9998
authoritykeeper "github.com/zeta-chain/zetacore/x/authority/keeper"
@@ -146,16 +145,11 @@ func init() {
146145
var (
147146
AccountAddressPrefix = "zeta"
148147
NodeDir = ".zetacored"
148+
DefaultNodeHome = os.ExpandEnv("$HOME/") + NodeDir
149149

150150
// AddrLen is the allowed length (in bytes) for an address.
151-
//
152151
// NOTE: In the SDK, the default value is 255.
153152
AddrLen = 20
154-
)
155-
156-
var (
157-
// DefaultNodeHome default home directories for wasmd
158-
DefaultNodeHome = os.ExpandEnv("$HOME/") + NodeDir
159153

160154
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
161155
Bech32PrefixAccAddr = AccountAddressPrefix
@@ -315,7 +309,7 @@ func New(
315309
skipUpgradeHeights map[int64]bool,
316310
homePath string,
317311
invCheckPeriod uint,
318-
encodingConfig appparams.EncodingConfig,
312+
encodingConfig ethermint.EncodingConfig,
319313
appOpts servertypes.AppOptions,
320314
baseAppOptions ...func(*baseapp.BaseApp),
321315
) *App {
@@ -356,9 +350,9 @@ func New(
356350
consensusparamtypes.StoreKey,
357351
crisistypes.StoreKey,
358352
)
359-
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
353+
tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
360354
memKeys := sdk.NewMemoryStoreKeys(
361-
//capabilitytypes.MemStoreKey,
355+
// capabilitytypes.MemStoreKey,
362356
)
363357

364358
app := &App{
@@ -368,7 +362,7 @@ func New(
368362
interfaceRegistry: interfaceRegistry,
369363
invCheckPeriod: invCheckPeriod,
370364
keys: keys,
371-
tkeys: tkeys,
365+
tkeys: tKeys,
372366
memKeys: memKeys,
373367
}
374368
if homePath == "" {
@@ -378,7 +372,7 @@ func New(
378372
// get authority address
379373
authAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
380374

381-
app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])
375+
app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tKeys[paramstypes.TStoreKey])
382376
// set the BaseApp's parameter store
383377
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authAddr)
384378
bApp.SetParamStore(&app.ConsensusParamsKeeper)
@@ -556,25 +550,26 @@ func New(
556550
appCodec,
557551
authtypes.NewModuleAddress(govtypes.ModuleName),
558552
keys[feemarkettypes.StoreKey],
559-
tkeys[feemarkettypes.TransientKey],
553+
tKeys[feemarkettypes.TransientKey],
560554
feeSs,
561555
app.ConsensusParamsKeeper,
562556
)
563557
evmSs := app.GetSubspace(evmtypes.ModuleName)
558+
564559
app.EvmKeeper = evmkeeper.NewKeeper(
565560
appCodec,
566561
keys[evmtypes.StoreKey],
567-
tkeys[evmtypes.TransientKey],
562+
tKeys[evmtypes.TransientKey],
568563
authtypes.NewModuleAddress(govtypes.ModuleName),
569564
app.AccountKeeper,
570565
app.BankKeeper,
571566
app.StakingKeeper,
572567
&app.FeeMarketKeeper,
573-
nil,
574-
geth.NewEVM,
575568
tracer,
576569
evmSs,
570+
precompiles.StatefulContracts(&app.FungibleKeeper, appCodec, storetypes.TransientGasConfig()),
577571
app.ConsensusParamsKeeper,
572+
aggregateAllKeys(keys, tKeys, memKeys),
578573
)
579574

580575
app.FungibleKeeper = *fungiblekeeper.NewKeeper(
@@ -813,7 +808,7 @@ func New(
813808

814809
// initialize stores
815810
app.MountKVStores(keys)
816-
app.MountTransientStores(tkeys)
811+
app.MountTransientStores(tKeys)
817812
app.MountMemoryStores(memKeys)
818813

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

10561051
func (app *App) BlockedAddrs() map[string]bool {
10571052
blockList := make(map[string]bool)
1053+
10581054
for k, v := range blockedReceivingModAcc {
10591055
addr := authtypes.NewModuleAddress(k)
10601056
blockList[addr.String()] = v
10611057
}
1058+
1059+
// Each enabled precompiled stateful contract should be added as a BlockedAddrs.
1060+
// That way it's marked as non payable by the bank keeper.
1061+
for addr, enabled := range precompiles.EnabledStatefulContracts {
1062+
if enabled {
1063+
blockList[addr.String()] = enabled
1064+
}
1065+
}
1066+
10621067
return blockList
10631068
}
1069+
1070+
// aggregateAllKeys aggregates all the keys in a single map.
1071+
func aggregateAllKeys(
1072+
keys map[string]*storetypes.KVStoreKey,
1073+
tKeys map[string]*storetypes.TransientStoreKey,
1074+
memKeys map[string]*storetypes.MemoryStoreKey,
1075+
) map[string]storetypes.StoreKey {
1076+
allKeys := make(map[string]storetypes.StoreKey, len(keys)+len(tKeys)+len(memKeys))
1077+
1078+
for k, v := range keys {
1079+
allKeys[k] = v
1080+
}
1081+
1082+
for k, v := range tKeys {
1083+
allKeys[k] = v
1084+
}
1085+
1086+
for k, v := range memKeys {
1087+
allKeys[k] = v
1088+
}
1089+
1090+
return allKeys
1091+
}

app/encoding.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package app
22

33
import (
4-
"cosmossdk.io/simapp/params"
5-
evmenc "github.com/evmos/ethermint/encoding"
4+
evmenc "github.com/zeta-chain/ethermint/encoding"
5+
ethermint "github.com/zeta-chain/ethermint/types"
66
)
77

88
// MakeEncodingConfig creates an EncodingConfig for testing
9-
func MakeEncodingConfig() params.EncodingConfig {
9+
func MakeEncodingConfig() ethermint.EncodingConfig {
1010
//encodingConfig := params.MakeEncodingConfig()
1111
encodingConfig := evmenc.MakeConfig(ModuleBasics)
1212
//std.RegisterLegacyAminoCodec(encodingConfig.Amino)

app/init_genesis.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
1717
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
1818
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
19-
evmtypes "github.com/evmos/ethermint/x/evm/types"
20-
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
19+
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
20+
feemarkettypes "github.com/zeta-chain/ethermint/x/feemarket/types"
2121

2222
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
2323
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"

app/prefix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package app
22

33
import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
5-
ethcfg "github.com/evmos/ethermint/cmd/config"
5+
ethcfg "github.com/zeta-chain/ethermint/cmd/config"
66

77
cmdcfg "github.com/zeta-chain/zetacore/cmd/zetacored/config"
88
)

0 commit comments

Comments
 (0)