From d71f828d8ef564f03ce1f190d42cb911d34afbc5 Mon Sep 17 00:00:00 2001 From: evlekht Date: Fri, 9 Feb 2024 21:03:32 +0400 Subject: [PATCH] fixes --- accounts/abi/bind/backends/simulated.go | 68 ++++++++++++++---- contracts/camino_smart_contracts_test.go | 88 ++---------------------- core/blockchain.go | 12 ++-- core/blockchain_reader.go | 2 +- core/chain_makers.go | 1 - eth/backend.go | 5 +- params/config.go | 3 +- scripts/versions.sh | 4 +- 8 files changed, 74 insertions(+), 109 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 5681a8be92..16d191b3bb 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -41,12 +41,12 @@ import ( "github.com/ava-labs/coreth/accounts/abi/bind" "github.com/ava-labs/coreth/consensus/dummy" "github.com/ava-labs/coreth/core" - "github.com/ava-labs/coreth/core/admin" "github.com/ava-labs/coreth/core/bloombits" "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/core/vm" + "github.com/ava-labs/coreth/eth/ethadmin" "github.com/ava-labs/coreth/eth/filters" "github.com/ava-labs/coreth/ethdb" "github.com/ava-labs/coreth/interfaces" @@ -104,10 +104,49 @@ type SimulatedBackend struct { config *params.ChainConfig } +// NewSimulatedCaminoBackendWithDatabase creates a new binding backend based on the given database +// and uses a simulated blockchain for testing purposes. +// A simulated backend always uses chainID 1337. +func NewSimulatedBackendWithDatabaseAndChainConfig(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, cpcfg *params.ChainConfig) *SimulatedBackend { + cpcfg.ChainID = big.NewInt(1337) + genesis := core.Genesis{ + Config: cpcfg, + GasLimit: gasLimit, + Alloc: alloc, + InitialAdmin: addr, + } + if addr.String() != "0x0000000000000000000000000000000000000000" { + genesis.PreDeploy() + } + cacheConfig := &core.CacheConfig{} + blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, dummy.NewFaker(), vm.Config{}, common.Hash{}, false) + backend := &SimulatedBackend{ + database: database, + blockchain: blockchain, + config: genesis.Config, + } + adminCtrl := ethadmin.NewController(backend, blockchain.Config()) + blockchain.SetAdminController(adminCtrl) + + filterBackend := &filterBackend{database, blockchain, backend} + backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{}) + backend.events = filters.NewEventSystem(backend.filterSystem, false) + + backend.rollback(blockchain.CurrentBlock()) + return backend +} + +// NewSimulatedCaminoBackendWithDatabase creates a new binding backend based on the given database +// and uses a simulated blockchain for testing purposes. +// A simulated backend always uses chainID 1337. +func NewSimulatedBackendWithChainConfig(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, cpcfg *params.ChainConfig) *SimulatedBackend { + return NewSimulatedBackendWithDatabaseAndChainConfig(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, cpcfg) +} + // NewSimulatedBackendWithDatabase creates a new binding backend based on the given database // and uses a simulated blockchain for testing purposes. // A simulated backend always uses chainID 1337. -func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, ctrl admin.AdminController) *SimulatedBackend { +func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64, addr common.Address) *SimulatedBackend { cpcfg := params.TestChainConfig cpcfg.ChainID = big.NewInt(1337) genesis := core.Genesis{ @@ -119,15 +158,15 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis if addr.String() != "0x0000000000000000000000000000000000000000" { genesis.PreDeploy() } - genesis.MustCommit(database) cacheConfig := &core.CacheConfig{} - blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, dummy.NewFaker(), vm.Config{AdminContoller: ctrl}, common.Hash{}, false) - + blockchain, _ := core.NewBlockChain(database, cacheConfig, &genesis, dummy.NewFaker(), vm.Config{}, common.Hash{}, false) backend := &SimulatedBackend{ database: database, blockchain: blockchain, config: genesis.Config, } + adminCtrl := ethadmin.NewController(backend, blockchain.Config()) + blockchain.SetAdminController(adminCtrl) filterBackend := &filterBackend{database, blockchain, backend} backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{}) @@ -141,21 +180,14 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis // for testing purposes. // A simulated backend always uses chainID 1337. func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { - return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, common.Address{}, nil) + return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, common.Address{}) } // NewSimulatedBackendWithInitialAdmin creates a new binding backend using a simulated blockchain // for testing purposes. // A simulated backend always uses chainID 1337. func NewSimulatedBackendWithInitialAdmin(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address) *SimulatedBackend { - return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, nil) -} - -// NewSimulatedBackendWithInitialAdmin creates a new binding backend using a simulated blockchain -// for testing purposes. -// A simulated backend always uses chainID 1337. -func NewSimulatedBackendWithInitialAdminAndAdminController(alloc core.GenesisAlloc, gasLimit uint64, addr common.Address, ctrl admin.AdminController) *SimulatedBackend { - return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr, ctrl) + return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit, addr) } // Close terminates the underlying blockchain's update loop. @@ -869,6 +901,14 @@ func (b *SimulatedBackend) Blockchain() *core.BlockChain { return b.blockchain } +func (b *SimulatedBackend) StateByHeader(ctx context.Context, header *types.Header) (*state.StateDB, error) { + return b.blockchain.StateAt(header.Root) +} + +func (b *SimulatedBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { + return b.blockchain.SubscribeChainHeadEvent(ch) +} + // callMsg implements core.Message to allow passing it as a transaction simulator. type callMsg struct { interfaces.CallMsg diff --git a/contracts/camino_smart_contracts_test.go b/contracts/camino_smart_contracts_test.go index 9ea30cf797..24fcc3dc8f 100644 --- a/contracts/camino_smart_contracts_test.go +++ b/contracts/camino_smart_contracts_test.go @@ -5,7 +5,6 @@ package contracts import ( "context" - "crypto/rand" "math/big" "strings" "testing" @@ -13,24 +12,15 @@ import ( "github.com/ava-labs/coreth/interfaces" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" - "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/coreth/accounts/abi" "github.com/ava-labs/coreth/accounts/abi/bind" "github.com/ava-labs/coreth/accounts/abi/bind/backends" - "github.com/ava-labs/coreth/accounts/keystore" - "github.com/ava-labs/coreth/consensus/dummy" "github.com/ava-labs/coreth/core" - "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/coreth/core/types" - "github.com/ava-labs/coreth/eth" - "github.com/ava-labs/coreth/eth/ethadmin" - "github.com/ava-labs/coreth/eth/ethconfig" - "github.com/ava-labs/coreth/node" "github.com/ava-labs/coreth/params" "github.com/ava-labs/coreth/vmerrs" @@ -51,14 +41,12 @@ var ( gasFeeKey, _ = crypto.HexToECDSA("04214cc61e1feaf005aa25b7771d33ca5c4aea959d21fe9a1429f822fa024171") blacklistKey, _ = crypto.HexToECDSA("b32d5aa5b8f4028218538c8c5b14b5c14f3f2e35b236e4bbbff09b669e69e46c") dummyKey, _ = crypto.HexToECDSA("62802c57c0e3c24ae0ce354f7d19f7659ddbe506547b00e9e6a722980d2fed3d") - blackHoleKey, _ = crypto.HexToECDSA("50cdff9c21002158e2a18b73c2504f8982332e05b5c3b26d3cffdd2f1291796a") adminAddr = crypto.PubkeyToAddress(adminKey.PublicKey) kycAddr = crypto.PubkeyToAddress(kycKey.PublicKey) gasFeeAddr = crypto.PubkeyToAddress(gasFeeKey.PublicKey) blacklistAddr = crypto.PubkeyToAddress(blacklistKey.PublicKey) dummyAddr = crypto.PubkeyToAddress(dummyKey.PublicKey) - blackholeAddr = crypto.PubkeyToAddress(blackHoleKey.PublicKey) AdminProxyAddr = common.HexToAddress("0x010000000000000000000000000000000000000a") @@ -68,11 +56,6 @@ var ( BLACKLIST_ROLE = big.NewInt(8) ) -type ETHChain struct { - backend *eth.Ethereum - chainConfig *params.ChainConfig -} - // Src code of factory contract: // // SPDX-License-Identifier: MIT @@ -129,11 +112,8 @@ func TestDeployContract(t *testing.T) { // Generate GenesisAlloc alloc := makeGenesisAllocation() - ethChain := newETHChain(t) - ac := ethadmin.NewController(ethChain.backend.APIBackend, ethChain.chainConfig) - // Generate SimulatedBackend - sim := backends.NewSimulatedBackendWithInitialAdminAndAdminController(alloc, gasLimit, adminAddr, ac) + sim := backends.NewSimulatedBackendWithChainConfig(alloc, gasLimit, adminAddr, params.TestCaminoChainConfig) defer func() { err := sim.Close() assert.NoError(t, err) @@ -634,13 +614,8 @@ func TestEthAdmin(t *testing.T) { // Generate GenesisAlloc alloc := makeGenesisAllocation() - // Create a bew Eth chain to generate an AdminController from its backend - // Simulated backed will not do - ethChain := newETHChain(t) - ac := ethadmin.NewController(ethChain.backend.APIBackend, ethChain.chainConfig) - // Generate SimulatedBackend - sim := backends.NewSimulatedBackendWithInitialAdminAndAdminController(alloc, gasLimit, gasFeeAddr, ac) + sim := backends.NewSimulatedBackendWithInitialAdmin(alloc, gasLimit, gasFeeAddr) defer func() { err := sim.Close() assert.NoError(t, err) @@ -648,6 +623,8 @@ func TestEthAdmin(t *testing.T) { sim.Commit(true) + ac := sim.Blockchain().AdminController() + latestHeader, state := getLatestHeaderAndState(t, sim) bf := ac.GetFixedBaseFee(latestHeader, state) @@ -692,63 +669,6 @@ func getLatestHeaderAndState(t *testing.T, sim *backends.SimulatedBackend) (*typ return latestHeader, state } -// newETHChain creates an Ethereum blockchain with the given configs. -func newETHChain(t *testing.T) *ETHChain { - chainID := big.NewInt(1) - initialBalance := big.NewInt(1000000000000000000) - - fundedKey, err := keystore.NewKey(rand.Reader) - assert.NoError(t, err) - - // configure the chain - config := ethconfig.NewDefaultConfig() - chainConfig := ¶ms.ChainConfig{ - ChainID: chainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: true, - EIP150Block: big.NewInt(0), - EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - SunrisePhase0BlockTimestamp: big.NewInt(0), - ApricotPhase1BlockTimestamp: big.NewInt(0), - ApricotPhase2BlockTimestamp: big.NewInt(0), - ApricotPhase3BlockTimestamp: big.NewInt(0), - ApricotPhase4BlockTimestamp: big.NewInt(0), - ApricotPhase5BlockTimestamp: big.NewInt(0), - ApricotPhasePre6BlockTimestamp: big.NewInt(0), - ApricotPhase6BlockTimestamp: big.NewInt(0), - ApricotPhasePost6BlockTimestamp: big.NewInt(0), - BanffBlockTimestamp: big.NewInt(0), - } - - config.Genesis = &core.Genesis{ - Config: chainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: gasLimit, - Difficulty: big.NewInt(0), - Alloc: core.GenesisAlloc{fundedKey.Address: {Balance: initialBalance}}, - } - - node, err := node.New(&node.Config{}) - assert.NoError(t, err) - - backend, err := eth.New(node, &config, new(dummy.ConsensusCallbacks), rawdb.NewMemoryDatabase(), eth.DefaultSettings, common.Hash{}, &mockable.Clock{}) - assert.NoError(t, err) - - chain := ÐChain{backend: backend, chainConfig: chainConfig} - backend.SetEtherbase(blackholeAddr) - - return chain -} - func makeGenesisAllocation() core.GenesisAlloc { alloc := make(core.GenesisAlloc) diff --git a/core/blockchain.go b/core/blockchain.go index 03d739d0df..466e395790 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -200,9 +200,8 @@ var DefaultCacheConfig = &CacheConfig{ // included in the canonical one where as GetBlockByNumber always represents the // canonical chain. type BlockChain struct { - chainConfig *params.ChainConfig // Chain & network configuration - cacheConfig *CacheConfig // Cache configuration for pruning - adminCtrl admin.AdminController // Block based administrative control + chainConfig *params.ChainConfig // Chain & network configuration + cacheConfig *CacheConfig // Cache configuration for pruning db ethdb.Database // Low level persistent database to store final content in @@ -326,7 +325,6 @@ func NewBlockChain( bc := &BlockChain{ chainConfig: chainConfig, cacheConfig: cacheConfig, - adminCtrl: vmConfig.AdminContoller, db: db, stateCache: state.NewDatabaseWithConfig(db, &trie.Config{ Cache: cacheConfig.TrieCleanLimit, @@ -2079,3 +2077,9 @@ func (bc *BlockChain) ResetToStateSyncedBlock(block *types.Block) error { bc.initSnapshot(head) return nil } + +// SetAdminController sets the chain admin controller. Must be called right after chain creation and only once. +// Must be also set in other vmConfig instances for consistency, cause chain copies vmConfig by value. +func (bc *BlockChain) SetAdminController(adminCtrl admin.AdminController) { + bc.vmConfig.AdminContoller = adminCtrl +} diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index f434ef4454..b3d8eca0ea 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -263,7 +263,7 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig } // AdminController retrieves the chain's admin controller. -func (bc *BlockChain) AdminController() admin.AdminController { return bc.adminCtrl } +func (bc *BlockChain) AdminController() admin.AdminController { return bc.vmConfig.AdminContoller } // Engine retrieves the blockchain's consensus engine. func (bc *BlockChain) Engine() consensus.Engine { return bc.engine } diff --git a/core/chain_makers.go b/core/chain_makers.go index 155624eb52..b0e32cffc5 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -42,7 +42,6 @@ import ( "github.com/ava-labs/coreth/consensus" "github.com/ava-labs/coreth/consensus/dummy" - "github.com/ava-labs/coreth/consensus/misc" "github.com/ava-labs/coreth/core/admin" "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/state" diff --git a/eth/backend.go b/eth/backend.go index 67c0331294..6c9d8663e6 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -227,14 +227,15 @@ func New( eth: eth, } - vmConfig.AdminContoller = ethadmin.NewController(eth.APIBackend, chainConfig) - var err error eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, eth.engine, vmConfig, lastAcceptedHash, config.SkipUpgradeCheck) if err != nil { return nil, err } + vmConfig.AdminContoller = ethadmin.NewController(eth.APIBackend, eth.blockchain.Config()) + eth.blockchain.SetAdminController(vmConfig.AdminContoller) + if err := eth.handleOfflinePruning(cacheConfig, config.Genesis, vmConfig, lastAcceptedHash); err != nil { return nil, err } diff --git a/params/config.go b/params/config.go index 4de0d0f517..d9c44137e0 100644 --- a/params/config.go +++ b/params/config.go @@ -53,7 +53,7 @@ var ( AvalancheMainnetChainID = big.NewInt(43114) // AvalancheFujiChainID ... AvalancheFujiChainID = big.NewInt(43113) - // LocalChainID ... + // AvalancheLocalChainID ... AvalancheLocalChainID = big.NewInt(43112) // CaminoChainID ... CaminoChainID = big.NewInt(500) @@ -154,6 +154,7 @@ var ( } TestChainConfig = &ChainConfig{AvalancheContext{common.Hash{1}}, big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0)} + TestCaminoChainConfig = &ChainConfig{AvalancheContext{common.Hash{1}}, big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0)} TestLaunchConfig = &ChainConfig{AvalancheContext{common.Hash{1}}, big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil} TestApricotPhase1Config = &ChainConfig{AvalancheContext{common.Hash{1}}, big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil} TestApricotPhase2Config = &ChainConfig{AvalancheContext{common.Hash{1}}, big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil} diff --git a/scripts/versions.sh b/scripts/versions.sh index 8cc245c756..a8d262f46a 100644 --- a/scripts/versions.sh +++ b/scripts/versions.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Set up the versions to be used -coreth_version=${CORETH_VERSION:-'v1.1.0'} +coreth_version=${CORETH_VERSION:-'v1.1.0'} # caminoethvm version # Don't export them as they're used in the context of other calls -avalanche_version=${AVALANCHE_VERSION:-'v1.1.0'} +avalanche_version=${AVALANCHE_VERSION:-'v1.0.0-rc1'} # caminogo version