Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add stateful precompiles support #2633

Merged
merged 32 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f9f2c05
chore: cleanup Makefile and go.mod
fbac Aug 5, 2024
e449a1b
feat: introduce stateful precompiles initial support
fbac Aug 5, 2024
4ca59cb
chore: remove simapp as a dependency
fbac Aug 5, 2024
d8256c9
fix: adapt to testutil to changes
fbac Aug 7, 2024
5f2e9c8
Merge branch 'develop' into feat/stateful-precompiled-contracts
fbac Aug 7, 2024
d987a02
fix: linting
fbac Aug 8, 2024
12eb98b
Merge branch 'develop' into feat/stateful-precompiled-contracts
fbac Aug 8, 2024
ac03a5d
chore: add changelog and remove unused vars
fbac Aug 8, 2024
a6fa3e7
fix: create aggregateAllKeys function
fbac Aug 8, 2024
871cdd1
feat: add e2e test stateful contract (#2703)
Aug 21, 2024
796112a
resolve conflicts
fbac Aug 21, 2024
42110e0
formatting
fbac Aug 21, 2024
0488bad
new generated mocks
fbac Aug 21, 2024
e531669
skip precompile tests during upgrade
fbac Aug 21, 2024
e8af6b8
skip precompile
fbac Aug 23, 2024
56f4a1a
Merge branch 'develop' into feat/stateful-precompiled-contracts
skosito Aug 26, 2024
48035a4
try to fix generate
skosito Aug 26, 2024
16f2b0f
debug script
skosito Aug 26, 2024
de59aa8
install solc in generate files
skosito Aug 26, 2024
a8566a0
install abigen
skosito Aug 26, 2024
9f2d914
cleanup action
skosito Aug 27, 2024
e83aa70
PR comments pt1
skosito Aug 27, 2024
8a91d5f
group prototype unit tests a bit
skosito Aug 27, 2024
ac9f98e
PR comment missing test
skosito Aug 27, 2024
45370d4
cleanup action a bit
skosito Aug 27, 2024
c0a1d60
add solc checks to script
skosito Aug 27, 2024
e3649c1
cleanup not needed changes
skosito Aug 27, 2024
4aba6e0
revert ibc change in test
skosito Aug 27, 2024
c840542
remove new test
skosito Aug 27, 2024
15c277f
Merge branch 'develop' into feat/stateful-precompiled-contracts
swift1337 Aug 27, 2024
ecdc35f
reduce simple operations gas required to 500
skosito Aug 27, 2024
fffd126
Merge branch 'develop' into feat/stateful-precompiled-contracts
skosito Aug 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
skosito marked this conversation as resolved.
Show resolved Hide resolved
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
}
skosito marked this conversation as resolved.
Show resolved Hide resolved
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
Loading