Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Feb 9, 2024
1 parent 093c4a6 commit d71f828
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 109 deletions.
68 changes: 54 additions & 14 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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{})
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
88 changes: 4 additions & 84 deletions contracts/camino_smart_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@ package contracts

import (
"context"
"crypto/rand"
"math/big"
"strings"
"testing"

"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"

Expand All @@ -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")

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -634,20 +614,17 @@ 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)
}()

sim.Commit(true)

ac := sim.Blockchain().AdminController()

latestHeader, state := getLatestHeaderAndState(t, sim)

bf := ac.GetFixedBaseFee(latestHeader, state)
Expand Down Expand Up @@ -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 := &params.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 := &ETHChain{backend: backend, chainConfig: chainConfig}
backend.SetEtherbase(blackholeAddr)

return chain
}

func makeGenesisAllocation() core.GenesisAlloc {
alloc := make(core.GenesisAlloc)

Expand Down
12 changes: 8 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions scripts/versions.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d71f828

Please sign in to comment.