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

sync subnet-evm to 981830ed + avalanchego v1.12.0-initial-poc.9 #686

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## How this works

## How this was tested

## Need to be documented?

## Need to update RELEASES.md?
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[Avalanche](https://docs.avax.network/intro) is a network composed of multiple blockchains.
Each blockchain is an instance of a Virtual Machine (VM), much like an object in an object-oriented language is an instance of a class.
That is, the VM defines the behavior of the blockchain.
Coreth (from core Ethereum) is the [Virtual Machine (VM)](https://docs.avax.network/learn/avalanche/virtual-machines) that defines the Contract Chain (C-Chain).
Coreth (from core Ethereum) is the [Virtual Machine (VM)](https://docs.avax.network/learn/virtual-machines) that defines the Contract Chain (C-Chain).
This chain implements the Ethereum Virtual Machine and supports Solidity smart contracts as well as most other Ethereum client functionality.

## Building
Expand Down
18 changes: 9 additions & 9 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ type CacheConfig struct {
// triedbConfig derives the configures for trie database.
func (c *CacheConfig) triedbConfig() *triedb.Config {
config := &triedb.Config{Preimages: c.Preimages}
if c.StateScheme == rawdb.HashScheme {
if c.StateScheme == rawdb.HashScheme || c.StateScheme == "" {
config.HashDB = &hashdb.Config{
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
StatsPrefix: trieCleanCacheStatsNamespace,
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
StatsPrefix: trieCleanCacheStatsNamespace,
ReferenceRootAtomicallyOnUpdate: true,
}
}
if c.StateScheme == rawdb.PathScheme {
Expand Down Expand Up @@ -1161,9 +1162,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// diff layer for the block.
var err error
if bc.snaps == nil {
_, err = state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), true)
_, err = state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()))
} else {
_, err = state.CommitWithSnap(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash(), true)
_, err = state.CommitWithSnap(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash())
}
if err != nil {
return err
Expand Down Expand Up @@ -1695,9 +1696,9 @@ func (bc *BlockChain) reprocessBlock(parent *types.Block, current *types.Block)
// If snapshots are enabled, call CommitWithSnaps to explicitly create a snapshot
// diff layer for the block.
if bc.snaps == nil {
return statedb.Commit(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), false)
return statedb.Commit(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()))
}
return statedb.CommitWithSnap(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), bc.snaps, current.Hash(), current.ParentHash(), false)
return statedb.CommitWithSnap(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), bc.snaps, current.Hash(), current.ParentHash())
}

// initSnapshot instantiates a Snapshot instance and adds it to [bc]
Expand Down Expand Up @@ -1838,8 +1839,7 @@ func (bc *BlockChain) reprocessState(current *types.Block, reexec uint64) error
// Flatten snapshot if initialized, holding a reference to the state root until the next block
// is processed.
if err := bc.flattenSnapshot(func() error {
triedb.Reference(root, common.Hash{})
if previousRoot != (common.Hash{}) {
if previousRoot != (common.Hash{}) && previousRoot != root {
triedb.Dereference(previousRoot)
}
previousRoot = root
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
}

// Write state changes to db
root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number), false)
root, err := statedb.Commit(b.header.Number.Uint64(), config.IsEIP158(b.header.Number))
if err != nil {
panic(fmt.Sprintf("state write error: %v", err))
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *triedb.Database) *types.Blo
}
}

statedb.Commit(0, false, false)
statedb.Commit(0, false)
// Commit newly generated states into disk if it's not empty.
if root != types.EmptyRootHash {
if err := triedb.Commit(root, true); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestDump(t *testing.T) {
// write some of them to the trie
s.state.updateStateObject(obj1)
s.state.updateStateObject(obj2)
root, _ := s.state.Commit(0, false, false)
root, _ := s.state.Commit(0, false)

// check that DumpToCollector contains the state objects that are in trie
s.state, _ = New(root, tdb, nil)
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestIterativeDump(t *testing.T) {
// write some of them to the trie
s.state.updateStateObject(obj1)
s.state.updateStateObject(obj2)
root, _ := s.state.Commit(0, false, false)
root, _ := s.state.Commit(0, false)
s.state, _ = New(root, tdb, nil)

b := &bytes.Buffer{}
Expand All @@ -156,7 +156,7 @@ func TestNull(t *testing.T) {
var value common.Hash

s.state.SetState(address, common.Hash{}, value)
s.state.Commit(0, false, false)
s.state.Commit(0, false)

if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
t.Errorf("expected empty current value, got %x", value)
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestSnapshot2(t *testing.T) {
so0.deleted = false
state.setStateObject(so0)

root, _ := state.Commit(0, false, false)
root, _ := state.Commit(0, false)
state, _ = New(root, state.db, nil)

// and one with deleted == true
Expand Down
20 changes: 7 additions & 13 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,14 +1263,14 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A
}

// Commit writes the state to the underlying in-memory trie database.
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, referenceRoot bool) (common.Hash, error) {
return s.commit(block, deleteEmptyObjects, nil, common.Hash{}, common.Hash{}, referenceRoot)
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) {
return s.commit(block, deleteEmptyObjects, nil, common.Hash{}, common.Hash{})
}

// CommitWithSnap writes the state to the underlying in-memory trie database and
// generates a snapshot layer for the newly committed state.
func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash, referenceRoot bool) (common.Hash, error) {
return s.commit(block, deleteEmptyObjects, snaps, blockHash, parentHash, referenceRoot)
func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash) (common.Hash, error) {
return s.commit(block, deleteEmptyObjects, snaps, blockHash, parentHash)
}

// Once the state is committed, tries cached in stateDB (including account
Expand All @@ -1280,7 +1280,7 @@ func (s *StateDB) CommitWithSnap(block uint64, deleteEmptyObjects bool, snaps *s
//
// The associated block number of the state transition is also provided
// for more chain context.
func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash, referenceRoot bool) (common.Hash, error) {
func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.Tree, blockHash, parentHash common.Hash) (common.Hash, error) {
// Short circuit in case any database failure occurred earlier.
if s.dbErr != nil {
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
Expand Down Expand Up @@ -1389,14 +1389,8 @@ func (s *StateDB) commit(block uint64, deleteEmptyObjects bool, snaps *snapshot.
if root != origin {
start := time.Now()
set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete)
if referenceRoot {
if err := s.db.TrieDB().UpdateAndReferenceRoot(root, origin, block, nodes, set); err != nil {
return common.Hash{}, err
}
} else {
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
return common.Hash{}, err
}
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
return common.Hash{}, err
}
s.originalRoot = root
if metrics.EnabledExpensive {
Expand Down
2 changes: 1 addition & 1 deletion core/state/statedb_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (test *stateTest) run() bool {
} else {
state.IntermediateRoot(true) // call intermediateRoot at the transaction boundary
}
nroot, err := state.Commit(0, true, false) // call commit at the block boundary
nroot, err := state.Commit(0, true) // call commit at the block boundary
if err != nil {
panic(err)
}
Expand Down
36 changes: 18 additions & 18 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ func TestIntermediateLeaks(t *testing.T) {
}

// Commit and cross check the databases.
transRoot, err := transState.Commit(0, false, false)
transRoot, err := transState.Commit(0, false)
if err != nil {
t.Fatalf("failed to commit transition state: %v", err)
}
if err = transNdb.Commit(transRoot, false); err != nil {
t.Errorf("can not commit trie %v to persistent database", transRoot.Hex())
}

finalRoot, err := finalState.Commit(0, false, false)
finalRoot, err := finalState.Commit(0, false)
if err != nil {
t.Fatalf("failed to commit final state: %v", err)
}
Expand Down Expand Up @@ -543,7 +543,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
func TestTouchDelete(t *testing.T) {
s := newStateEnv()
s.state.getOrNewStateObject(common.Address{})
root, _ := s.state.Commit(0, false, false)
root, _ := s.state.Commit(0, false)
s.state, _ = NewWithSnapshot(root, s.state.db, s.state.snap)

snapshot := s.state.Snapshot()
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestCopyCommitCopy(t *testing.T) {
t.Fatalf("second copy committed storage slot mismatch: have %x, want %x", val, sval)
}
// Commit state, ensure states can be loaded from disk
root, _ := state.Commit(0, false, false)
root, _ := state.Commit(0, false)
state, _ = New(root, tdb, nil)
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
t.Fatalf("state post-commit balance mismatch: have %v, want %v", balance, 42)
Expand Down Expand Up @@ -745,7 +745,7 @@ func TestCommitCopy(t *testing.T) {
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
}
// Copy the committed state database, the copied one is not functional.
state.Commit(0, true, false)
state.Commit(0, true)
copied := state.Copy()
if balance := copied.GetBalance(addr); balance.Cmp(uint256.NewInt(0)) != 0 {
t.Fatalf("unexpected balance: have %v", balance)
Expand Down Expand Up @@ -779,7 +779,7 @@ func TestDeleteCreateRevert(t *testing.T) {
addr := common.BytesToAddress([]byte("so"))
state.SetBalance(addr, uint256.NewInt(1))

root, _ := state.Commit(0, false, false)
root, _ := state.Commit(0, false)
state, _ = NewWithSnapshot(root, state.db, state.snap)

// Simulate self-destructing in one transaction, then create-reverting in another
Expand All @@ -791,7 +791,7 @@ func TestDeleteCreateRevert(t *testing.T) {
state.RevertToSnapshot(id)

// Commit the entire state and make sure we don't crash and have the correct state
root, _ = state.Commit(0, true, false)
root, _ = state.Commit(0, true)
state, _ = NewWithSnapshot(root, state.db, state.snap)

if state.getStateObject(addr) != nil {
Expand Down Expand Up @@ -834,7 +834,7 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
a2 := common.BytesToAddress([]byte("another"))
state.SetBalance(a2, uint256.NewInt(100))
state.SetCode(a2, []byte{1, 2, 4})
root, _ = state.Commit(0, false, false)
root, _ = state.Commit(0, false)
t.Logf("root: %x", root)
// force-flush
tdb.Commit(root, false)
Expand All @@ -858,7 +858,7 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
}
// Modify the state
state.SetBalance(addr, uint256.NewInt(2))
root, err := state.Commit(0, false, false)
root, err := state.Commit(0, false)
if err == nil {
t.Fatalf("expected error, got root :%x", root)
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func TestMultiCoinOperations(t *testing.T) {
assetID := common.Hash{2}

s.state.getOrNewStateObject(addr)
root, _ := s.state.Commit(0, false, false)
root, _ := s.state.Commit(0, false)
s.state, _ = NewWithSnapshot(root, s.state.db, s.state.snap)

s.state.AddBalance(addr, new(uint256.Int))
Expand Down Expand Up @@ -1101,22 +1101,22 @@ func TestMultiCoinSnapshot(t *testing.T) {
assertBalances(10, 0, 0)

// Commit and get the new root
root, _ = stateDB.Commit(0, false, false)
root, _ = stateDB.Commit(0, false)
assertBalances(10, 0, 0)

// Create a new state from the latest root, add a multicoin balance, and
// commit it to the tree.
stateDB, _ = New(root, sdb, snapTree)
stateDB.AddBalanceMultiCoin(addr, assetID1, big.NewInt(10))
root, _ = stateDB.Commit(0, false, false)
root, _ = stateDB.Commit(0, false)
assertBalances(10, 10, 0)

// Add more layers than the cap and ensure the balances and layers are correct
for i := 0; i < 256; i++ {
stateDB, _ = New(root, sdb, snapTree)
stateDB.AddBalanceMultiCoin(addr, assetID1, big.NewInt(1))
stateDB.AddBalanceMultiCoin(addr, assetID2, big.NewInt(2))
root, _ = stateDB.Commit(0, false, false)
root, _ = stateDB.Commit(0, false)
}
assertBalances(10, 266, 512)

Expand All @@ -1125,7 +1125,7 @@ func TestMultiCoinSnapshot(t *testing.T) {
stateDB, _ = New(root, sdb, snapTree)
stateDB.AddBalance(addr, uint256.NewInt(1))
stateDB.AddBalanceMultiCoin(addr, assetID1, big.NewInt(1))
root, _ = stateDB.Commit(0, false, false)
root, _ = stateDB.Commit(0, false)
stateDB, _ = New(root, sdb, snapTree)
assertBalances(11, 267, 512)
}
Expand All @@ -1147,7 +1147,7 @@ func TestGenerateMultiCoinAccounts(t *testing.T) {
t.Fatal(err)
}
stateDB.SetBalanceMultiCoin(addr, assetID, assetBalance)
root, err := stateDB.Commit(0, false, false)
root, err := stateDB.Commit(0, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1207,7 +1207,7 @@ func TestFlushOrderDataLoss(t *testing.T) {
state.SetState(common.Address{a}, common.Hash{a, s}, common.Hash{a, s})
}
}
root, err := state.Commit(0, false, false)
root, err := state.Commit(0, false)
if err != nil {
t.Fatalf("failed to commit state trie: %v", err)
}
Expand Down Expand Up @@ -1286,7 +1286,7 @@ func TestResetObject(t *testing.T) {
state.CreateAccount(addr)
state.SetBalance(addr, uint256.NewInt(2))
state.SetState(addr, slotB, common.BytesToHash([]byte{0x2}))
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{}, false)
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{})

// Ensure the original account is wiped properly
snap := snaps.Snapshot(root)
Expand Down Expand Up @@ -1317,7 +1317,7 @@ func TestDeleteStorage(t *testing.T) {
value := common.Hash(uint256.NewInt(uint64(10 * i)).Bytes32())
state.SetState(addr, slot, value)
}
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{}, false)
root, _ := state.CommitWithSnap(0, true, snaps, common.Hash{}, common.Hash{})
// Init phase done, create two states, one with snap and one without
fastState, _ := New(root, db, snaps)
slowState, _ := New(root, db, nil)
Expand Down
2 changes: 1 addition & 1 deletion core/state/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func makeTestState(scheme string) (ethdb.Database, Database, *triedb.Database, c
}
accounts = append(accounts, acc)
}
root, _ := state.Commit(0, false, false)
root, _ := state.Commit(0, false)

// Return the generated state
return db, sdb, nodeDb, root, accounts
Expand Down
2 changes: 1 addition & 1 deletion core/test_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ func checkTxIndicesHelper(t *testing.T, expectedTail *uint64, indexedFrom uint64
require.EventuallyWithTf(t,
func(c *assert.CollectT) {
stored = *rawdb.ReadTxIndexTail(db)
require.Equalf(t, tailValue, stored, "expected tail to be %d, found %d", tailValue, stored)
assert.Equalf(c, tailValue, stored, "expected tail to be %d, found %d", tailValue, stored)
},
30*time.Second, 500*time.Millisecond, "expected tail to be %d eventually", tailValue)
}
Expand Down
Loading
Loading