Skip to content

Commit

Permalink
Use upstream statedb (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush authored Nov 29, 2024
1 parent d7a7fce commit 35736dd
Show file tree
Hide file tree
Showing 134 changed files with 710 additions and 17,456 deletions.
9 changes: 7 additions & 2 deletions accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@ func TestWaitDeployedCornerCases(t *testing.T) {
// Create a transaction to an account.
code := "6060604052600a8060106000396000f360606040526008565b00"
tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code))
tx, _ = types.SignTx(tx, types.LatestSigner(params.TestChainConfig), testKey)
tx, err := types.SignTx(tx, types.LatestSignerForChainID(big.NewInt(1337)), testKey)
if err != nil {
t.Fatalf("Failed to sign transaction: %s", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
backend.Client().SendTransaction(ctx, tx)
if err := backend.Client().SendTransaction(ctx, tx); err != nil {
t.Fatalf("Failed to send transaction: %s", err)
}
backend.Commit(true)
notContractCreation := errors.New("tx is not contract creation")
if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != notContractCreation.Error() {
Expand Down
2 changes: 1 addition & 1 deletion consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/trie"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ava-labs/libevm/trie"
)

// BlockValidator is responsible for validating block headers, uncles and
Expand Down
60 changes: 35 additions & 25 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ import (
"github.com/ava-labs/coreth/internal/version"
"github.com/ava-labs/coreth/metrics"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/coreth/triedb/hashdb"
"github.com/ava-labs/coreth/triedb/pathdb"
"github.com/ava-labs/libevm/common"
Expand All @@ -58,6 +56,8 @@ import (
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/event"
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/trie"
"github.com/ava-labs/libevm/triedb"
)

var (
Expand Down Expand Up @@ -177,18 +177,19 @@ 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 {
config.HashDB = &hashdb.Config{
if c.StateScheme == rawdb.HashScheme || c.StateScheme == "" {
config.DBOverride = hashdb.Config{
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
StatsPrefix: trieCleanCacheStatsNamespace,
}
ReferenceRoot: true, // Automatically reference root nodes when an update is made
}.BackendConstructor
}
if c.StateScheme == rawdb.PathScheme {
config.PathDB = &pathdb.Config{
config.DBOverride = pathdb.Config{
StateHistory: c.StateHistory,
CleanCacheSize: c.TrieCleanLimit * 1024 * 1024,
DirtyCacheSize: c.TrieDirtyLimit * 1024 * 1024,
}
}.BackendConstructor
}
return config
}
Expand Down Expand Up @@ -1125,8 +1126,8 @@ func (bc *BlockChain) newTip(block *types.Block) bool {
// canonical chain.
// writeBlockAndSetHead expects to be the last verification step during InsertBlock
// since it creates a reference that will only be cleaned up by Accept/Reject.
func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) error {
if err := bc.writeBlockWithState(block, receipts, state); err != nil {
func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, parentRoot common.Hash, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) error {
if err := bc.writeBlockWithState(block, parentRoot, receipts, state); err != nil {
return err
}

Expand All @@ -1143,7 +1144,7 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types

// writeBlockWithState writes the block and all associated state to the database,
// but it expects the chain mutex to be held.
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) error {
func (bc *BlockChain) writeBlockWithState(block *types.Block, parentRoot common.Hash, receipts []*types.Receipt, state *state.StateDB) error {
// Irrelevant of the canonical status, write the block itself to the database.
//
// Note all the components of block(hash->number map, header, body, receipts)
Expand All @@ -1157,14 +1158,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}

// Commit all cached state changes into underlying memory database.
// If snapshots are enabled, call CommitWithSnaps to explicitly create a snapshot
// diff layer for the block.
var err error
if bc.snaps == nil {
_, err = state.Commit(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), true)
} else {
_, err = state.CommitWithSnap(block.NumberU64(), bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash(), true)
}
_, err = bc.commitWithSnap(block, parentRoot, state)
if err != nil {
return err
}
Expand Down Expand Up @@ -1367,7 +1362,7 @@ func (bc *BlockChain) insertBlock(block *types.Block, writes bool) error {
// will be cleaned up in Accept/Reject so we need to ensure an error cannot occur
// later in verification, since that would cause the referenced root to never be dereferenced.
wstart := time.Now()
if err := bc.writeBlockAndSetHead(block, receipts, logs, statedb); err != nil {
if err := bc.writeBlockAndSetHead(block, parent.Root, receipts, logs, statedb); err != nil {
return err
}
// Update the metrics touched during block commit
Expand Down Expand Up @@ -1667,7 +1662,7 @@ func (bc *BlockChain) reprocessBlock(parent *types.Block, current *types.Block)
if snap == nil {
return common.Hash{}, fmt.Errorf("failed to get snapshot for parent root: %s", parentRoot)
}
statedb, err = state.NewWithSnapshot(parentRoot, bc.stateCache, snap)
statedb, err = state.New(parentRoot, bc.stateCache, bc.snaps)
}
if err != nil {
return common.Hash{}, fmt.Errorf("could not fetch state for (%s: %d): %v", parent.Hash().Hex(), parent.NumberU64(), err)
Expand All @@ -1692,12 +1687,28 @@ func (bc *BlockChain) reprocessBlock(parent *types.Block, current *types.Block)
log.Debug("Processed block", "block", current.Hash(), "number", current.NumberU64())

// Commit all cached state changes into underlying memory database.
// 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 bc.commitWithSnap(current, parentRoot, statedb)
}

func (bc *BlockChain) commitWithSnap(
current *types.Block, parentRoot common.Hash, statedb *state.StateDB,
) (common.Hash, error) {
// blockHashes must be passed through Commit since snapshots are based on the
// block hash.
blockHashes := snapshot.WithBlockHashes(current.Hash(), current.ParentHash())
root, err := statedb.Commit(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), blockHashes)
if err != nil {
return common.Hash{}, err
}
// Upstream does not perform a snapshot update if the root is the same as the
// parent root, however here the snapshots are based on the block hash, so
// this update is necessary. Note blockHashes are passed here as well.
if bc.snaps != nil && root == parentRoot {
if err := bc.snaps.Update(root, parentRoot, nil, nil, nil, blockHashes); err != nil {
return common.Hash{}, err
}
}
return statedb.CommitWithSnap(current.NumberU64(), bc.chainConfig.IsEIP158(current.Number()), bc.snaps, current.Hash(), current.ParentHash(), false)
return root, nil
}

// initSnapshot instantiates a Snapshot instance and adds it to [bc]
Expand Down Expand Up @@ -1838,7 +1849,6 @@ 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{}) {
triedb.Dereference(previousRoot)
}
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"github.com/ava-labs/coreth/core/state/snapshot"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/event"
"github.com/ava-labs/libevm/triedb"
)

// CurrentHeader retrieves the current head header of the canonical chain. The
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import (
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/triedb"
"github.com/stretchr/testify/require"
)

Expand Down
4 changes: 2 additions & 2 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/triedb"
"github.com/holiman/uint256"
)

Expand Down 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/chain_makers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import (
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/triedb"
)

func ExampleGenerateChain() {
Expand Down
13 changes: 12 additions & 1 deletion core/extstate/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

type VmStateDB interface {
vm.StateDB
Logs() []*types.Log

GetTxHash() common.Hash
GetLogData() (topics [][]common.Hash, data [][]byte)
GetBalanceMultiCoin(common.Address, common.Hash) *big.Int
AddBalanceMultiCoin(common.Address, common.Hash, *big.Int)
SubBalanceMultiCoin(common.Address, common.Hash, *big.Int)
Expand All @@ -36,6 +37,16 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
s.VmStateDB.Prepare(rules, sender, coinbase, dst, precompiles, list)
}

// GetLogData returns the underlying topics and data from each log included in the StateDB
// Test helper function.
func (s *StateDB) GetLogData() (topics [][]common.Hash, data [][]byte) {
for _, log := range s.Logs() {
topics = append(topics, log.Topics)
data = append(data, common.CopyBytes(log.Data))
}
return topics, data
}

// GetPredicateStorageSlots returns the storage slots associated with the address, index pair.
// A list of access tuples can be included within transaction types post EIP-2930. The address
// is declared directly on the access tuple and the index is the i'th occurrence of an access
Expand Down
62 changes: 31 additions & 31 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 35736dd

Please sign in to comment.