Skip to content
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
314fe42
format: as subnet-evm
Sep 25, 2024
72a4b00
take changes up to d72ff25 from subnet-evm
Oct 2, 2024
b3647e3
complete use core/vm
Sep 26, 2024
20754f7
format: as coreth
Oct 2, 2024
6ff7f77
ci fixes
Sep 26, 2024
512ae03
use race free version
Sep 26, 2024
0baae1a
update version
Sep 26, 2024
4fe802b
rebase new libevm
Sep 27, 2024
df98c4e
remove WithExtras hack
Oct 1, 2024
8e223b9
cleanup
Oct 2, 2024
43dde81
fix genesis EthUpgrades
Oct 3, 2024
0976d50
update libevm
Oct 7, 2024
473a6ab
snapshot: initial refactoring for use with upstream statedb
Oct 7, 2024
95c4c16
refactor: inline CommitWithSnap
Oct 7, 2024
b82d5fa
remove unused args
Oct 7, 2024
ac1e437
reduce diff
Oct 7, 2024
2ebad51
handle snapshot update with root same as parent
Oct 7, 2024
6b6fb64
reference root refactor (use triedb)
Oct 8, 2024
b985d91
to fix on master
Oct 8, 2024
87f64eb
nit: reduce diff w/ upstream
Oct 8, 2024
e379625
no mc funds were used in genesis (mainnet/fuji)
Oct 8, 2024
2d0ea28
minor improvement
Oct 8, 2024
a963103
use libevm rebased for arr4n/state-db-extra-payload-handling
Oct 8, 2024
dac5b3e
remove trie/ package
Oct 8, 2024
27f02f1
use libevm for triedb
Oct 9, 2024
4704b1c
update version e2e
Oct 9, 2024
18d20f1
update geth packages
Oct 9, 2024
1aa620e
remove triedb package
Oct 9, 2024
83a4e52
format: rename to libevm
Nov 12, 2024
373adba
Merge branch 'libevm' of github.com:ava-labs/coreth into use-libevm-s…
Nov 12, 2024
716d584
oops
Nov 12, 2024
ba350f9
use DBOverride to specify backends (#685)
Nov 26, 2024
0b1d1af
use upstream statedb (embedded) (#672)
Nov 26, 2024
bbbe11d
add trie prefetcher
Nov 26, 2024
df7c1f9
nit
Nov 27, 2024
0f70ae7
remove types.GenesisAccount
Nov 27, 2024
6b0a298
remove GetAssetBalance api
Nov 27, 2024
17c8f07
nit
Nov 27, 2024
f543b68
remove dump XXX comment
Nov 29, 2024
6a85a7f
statedb_test.go: remove shared tests with upstream
Nov 29, 2024
65104e6
rename file to statedb_multicoin_test.go
Nov 29, 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
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
15 changes: 5 additions & 10 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ 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/coreth/triedb"
"github.com/ava-labs/coreth/triedb/pathdb"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/hexutil"
"github.com/ava-labs/libevm/common/math"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/trie"
"github.com/ava-labs/libevm/triedb"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -219,8 +219,8 @@ func (g *Genesis) trieConfig() *triedb.Config {
return nil
}
return &triedb.Config{
PathDB: pathdb.Defaults,
IsVerkle: true,
DBOverride: pathdb.Defaults.BackendConstructor,
IsVerkle: true,
}
}

Expand Down Expand Up @@ -258,11 +258,6 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *triedb.Database) *types.Blo
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
if account.MCBalance != nil {
for coinID, value := range account.MCBalance {
statedb.AddBalanceMultiCoin(addr, coinID, value)
}
}
}
root := statedb.IntermediateRoot(false)
head.Root = root
Expand Down Expand Up @@ -299,7 +294,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
2 changes: 1 addition & 1 deletion core/genesis_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ 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/coreth/utils"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/triedb"
"github.com/stretchr/testify/require"
)

Expand Down
8 changes: 4 additions & 4 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ import (
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/precompile/contracts/warp"
"github.com/ava-labs/coreth/trie"
"github.com/ava-labs/coreth/triedb"
"github.com/ava-labs/coreth/triedb/pathdb"
"github.com/ava-labs/coreth/utils"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/ethdb"
"github.com/ava-labs/libevm/trie"
"github.com/ava-labs/libevm/triedb"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -285,7 +285,7 @@ func newDbConfig(scheme string) *triedb.Config {
if scheme == rawdb.HashScheme {
return triedb.HashDefaults
}
return &triedb.Config{PathDB: pathdb.Defaults}
return &triedb.Config{DBOverride: pathdb.Defaults.BackendConstructor}
}

func TestVerkleGenesisCommit(t *testing.T) {
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestVerkleGenesisCommit(t *testing.T) {
}

db := rawdb.NewMemoryDatabase()
triedb := triedb.NewDatabase(db, &triedb.Config{IsVerkle: true, PathDB: pathdb.Defaults})
triedb := triedb.NewDatabase(db, &triedb.Config{IsVerkle: true, DBOverride: pathdb.Defaults.BackendConstructor})
block := genesis.MustCommit(db, triedb)
if !bytes.Equal(block.Root().Bytes(), expected) {
t.Fatalf("invalid genesis state root, expected %x, got %x", expected, got)
Expand Down
Loading
Loading