Compare commits
93 Commits
versa_perf
...
versa_inte
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
facbe6a886 | ||
|
|
84da730eed | ||
|
|
85a6b17ab0 | ||
|
|
8db789b2c0 | ||
|
|
36044b05f3 | ||
|
|
d5e38ad81e | ||
|
|
d5a438a06d | ||
|
|
b585984e32 | ||
|
|
a1d4b06707 | ||
|
|
ea76d5f359 | ||
|
|
b8fa4761b8 | ||
|
|
3ad7c4e63b | ||
|
|
74ab9ab248 | ||
|
|
9c239db9d9 | ||
|
|
d3854b41ec | ||
|
|
30b371daf2 | ||
|
|
4764fdb47f | ||
|
|
fa7a43a2df | ||
|
|
1a40f79ade | ||
|
|
3ad4700907 | ||
|
|
f724941948 | ||
|
|
ef3f9dfaa7 | ||
|
|
aa9d5182c1 | ||
|
|
a0daf107f9 | ||
|
|
55cdcfaff3 | ||
|
|
7d5e6ce2d7 | ||
|
|
1cbdf0a4c9 | ||
|
|
997d7b5bba | ||
|
|
90aa2e137c | ||
|
|
e940e28f6c | ||
|
|
23c60a38a4 | ||
|
|
f67f494346 | ||
|
|
84767d1957 | ||
|
|
bf808915f0 | ||
|
|
b80d78701a | ||
|
|
3c4b452ee2 | ||
|
|
8197064e91 | ||
|
|
90de1d7ff8 | ||
|
|
77dea68afd | ||
|
|
9d20658f8d | ||
|
|
dc510033c0 | ||
|
|
7bce07abdf | ||
|
|
2e4ec39e52 | ||
|
|
0a901183b9 | ||
|
|
2e38d0662c | ||
|
|
ac2d1a8ada | ||
|
|
c6d8d6355d | ||
|
|
7462078caa | ||
|
|
cf04af0cc1 | ||
|
|
dec27d1b52 | ||
|
|
bbef39738d | ||
|
|
6dfcbc6519 | ||
|
|
42b72feed1 | ||
|
|
8d7ee3aeb6 | ||
|
|
a3f8c707e6 | ||
|
|
bd9c2ae465 | ||
|
|
023b092e95 | ||
|
|
0941eb6ccf | ||
|
|
b73a98cd65 | ||
|
|
26d7f62caa | ||
|
|
be0a305251 | ||
|
|
bfe07c5a83 | ||
|
|
412c5ee791 | ||
|
|
3a70b35ea2 | ||
|
|
f284358f8f | ||
|
|
5b4e00c905 | ||
|
|
c572535693 | ||
|
|
324e4a4af7 | ||
|
|
58dabc990e | ||
|
|
2f0f91dbf8 | ||
|
|
657eced3b7 | ||
|
|
f69ff95e77 | ||
|
|
04e5c1732c | ||
|
|
ec2384aca4 | ||
|
|
83287cec5c | ||
|
|
d162a63438 | ||
|
|
b00582cb8a | ||
|
|
d8c3726154 | ||
|
|
e8309adedc | ||
|
|
ed7aa3e3b9 | ||
|
|
6188aaec48 | ||
|
|
60df2fa712 | ||
|
|
bb167a9af6 | ||
|
|
66bd59013f | ||
|
|
6b7c807e86 | ||
|
|
3a34c737dc | ||
|
|
4635a457c1 | ||
|
|
e1a2b8e8d6 | ||
|
|
6cf5169ce1 | ||
|
|
7f3aac35e4 | ||
|
|
435410f6f3 | ||
|
|
d2e5c03965 | ||
|
|
41ab497c22 |
@@ -343,7 +343,7 @@ func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block
|
||||
continue
|
||||
}
|
||||
// If we're above the chain head, state availability is a must
|
||||
if !chain.HasBlockAndState(block.Hash(), block.Number().Int64()) {
|
||||
if !chain.HasBlockAndState(block.Hash(), block.NumberU64()) {
|
||||
return blocks[i:]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/consensus"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/trie"
|
||||
)
|
||||
@@ -41,12 +40,6 @@ func EnableRemoteVerifyManager(remoteValidator *remoteVerifyManager) BlockValida
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
validateBloomTimer = metrics.NewRegisteredTimer("validate/bloom/time", nil)
|
||||
validateReceiptTimer = metrics.NewRegisteredTimer("validate/receipt/time", nil)
|
||||
validateRootTimer = metrics.NewRegisteredTimer("validate/root/time", nil)
|
||||
)
|
||||
|
||||
// BlockValidator is responsible for validating block headers, uncles and
|
||||
// processed state.
|
||||
//
|
||||
@@ -103,7 +96,7 @@ func ValidateListsInBody(block *types.Block) error {
|
||||
// validated at this point.
|
||||
func (v *BlockValidator) ValidateBody(block *types.Block) error {
|
||||
// Check whether the block is already imported.
|
||||
if v.bc.HasBlockAndState(block.Hash(), block.Number().Int64()) {
|
||||
if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
|
||||
return ErrKnownBlock
|
||||
}
|
||||
if v.bc.isCachedBadBlock(block) {
|
||||
@@ -149,7 +142,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
if !v.bc.HasBlockAndState(block.ParentHash(), block.Number().Int64()-1) {
|
||||
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
|
||||
if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
|
||||
return consensus.ErrUnknownAncestor
|
||||
}
|
||||
@@ -191,10 +184,6 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
|
||||
// For valid blocks this should always validate to true.
|
||||
validateFuns := []func() error{
|
||||
func() error {
|
||||
defer func(start time.Time) {
|
||||
validateBloomTimer.UpdateSince(start)
|
||||
}(time.Now())
|
||||
|
||||
rbloom := types.CreateBloom(receipts)
|
||||
if rbloom != header.Bloom {
|
||||
return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
|
||||
@@ -202,9 +191,6 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
|
||||
return nil
|
||||
},
|
||||
func() error {
|
||||
defer func(start time.Time) {
|
||||
validateReceiptTimer.UpdateSince(start)
|
||||
}(time.Now())
|
||||
receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil))
|
||||
if receiptSha != header.ReceiptHash {
|
||||
return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
|
||||
@@ -223,9 +209,6 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
|
||||
})
|
||||
} else {
|
||||
validateFuns = append(validateFuns, func() error {
|
||||
defer func(start time.Time) {
|
||||
validateRootTimer.UpdateSince(start)
|
||||
}(time.Now())
|
||||
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
|
||||
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
|
||||
}
|
||||
|
||||
@@ -92,13 +92,10 @@ var (
|
||||
|
||||
triedbCommitTimer = metrics.NewRegisteredTimer("chain/triedb/commits", nil)
|
||||
|
||||
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
|
||||
blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil)
|
||||
blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil)
|
||||
blockWriteTimer = metrics.NewRegisteredTimer("chain/write", nil)
|
||||
blockValidationTotalTimer = metrics.NewRegisteredTimer("chain/total/validation", nil)
|
||||
blockExecutionTotalTimer = metrics.NewRegisteredTimer("chain/total/execution", nil)
|
||||
blockWriteTotalTimer = metrics.NewRegisteredTimer("chain/total/write", nil)
|
||||
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
|
||||
blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil)
|
||||
blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil)
|
||||
blockWriteTimer = metrics.NewRegisteredTimer("chain/write", nil)
|
||||
|
||||
blockReorgMeter = metrics.NewRegisteredMeter("chain/reorg/executes", nil)
|
||||
blockReorgAddMeter = metrics.NewRegisteredMeter("chain/reorg/add", nil)
|
||||
@@ -346,15 +343,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
|
||||
// Open trie database with provided config
|
||||
triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig())
|
||||
|
||||
if triedb.Scheme() == rawdb.VersionScheme {
|
||||
vdb := triedb.VersaDB()
|
||||
ver, root := vdb.LatestStoreDiskVersionInfo()
|
||||
if ver == -1 {
|
||||
rawdb.WriteCanonicalHash(db, common.Hash{}, 0)
|
||||
}
|
||||
log.Info("version db latest version info", "version", ver, "root", root.String())
|
||||
}
|
||||
|
||||
// Setup the genesis block, commit the provided genesis specification
|
||||
// to database if the genesis block is not present yet, or load the
|
||||
// stored one from database.
|
||||
@@ -437,138 +425,78 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
|
||||
// Make sure the state associated with the block is available, or log out
|
||||
// if there is no available state, waiting for state sync.
|
||||
head := bc.CurrentBlock()
|
||||
if bc.triedb.Scheme() != rawdb.VersionScheme {
|
||||
if !bc.HasState(head.Number.Int64(), head.Root) {
|
||||
if head.Number.Uint64() == 0 {
|
||||
// The genesis state is missing, which is only possible in the path-based
|
||||
// scheme. This situation occurs when the initial state sync is not finished
|
||||
// yet, or the chain head is rewound below the pivot point. In both scenarios,
|
||||
// there is no possible recovery approach except for rerunning a snap sync.
|
||||
// Do nothing here until the state syncer picks it up.
|
||||
log.Info("Genesis state is missing, wait state sync")
|
||||
} else {
|
||||
// Head state is missing, before the state recovery, find out the
|
||||
// disk layer point of snapshot(if it's enabled). Make sure the
|
||||
// rewound point is lower than disk layer.
|
||||
var diskRoot common.Hash
|
||||
if bc.cacheConfig.SnapshotLimit > 0 {
|
||||
diskRoot = rawdb.ReadSnapshotRoot(bc.db)
|
||||
if !bc.HasState(head.Root) {
|
||||
if head.Number.Uint64() == 0 {
|
||||
// The genesis state is missing, which is only possible in the path-based
|
||||
// scheme. This situation occurs when the initial state sync is not finished
|
||||
// yet, or the chain head is rewound below the pivot point. In both scenarios,
|
||||
// there is no possible recovery approach except for rerunning a snap sync.
|
||||
// Do nothing here until the state syncer picks it up.
|
||||
log.Info("Genesis state is missing, wait state sync")
|
||||
} else {
|
||||
// Head state is missing, before the state recovery, find out the
|
||||
// disk layer point of snapshot(if it's enabled). Make sure the
|
||||
// rewound point is lower than disk layer.
|
||||
var diskRoot common.Hash
|
||||
if bc.cacheConfig.SnapshotLimit > 0 {
|
||||
diskRoot = rawdb.ReadSnapshotRoot(bc.db)
|
||||
}
|
||||
if bc.triedb.Scheme() == rawdb.PathScheme && !bc.NoTries() {
|
||||
recoverable, _ := bc.triedb.Recoverable(diskRoot)
|
||||
if !bc.HasState(diskRoot) && !recoverable {
|
||||
diskRoot = bc.triedb.Head()
|
||||
}
|
||||
if bc.triedb.Scheme() == rawdb.PathScheme && !bc.NoTries() {
|
||||
recoverable, _ := bc.triedb.Recoverable(diskRoot)
|
||||
if !bc.HasState(0, diskRoot) && !recoverable {
|
||||
diskRoot = bc.triedb.Head()
|
||||
}
|
||||
}
|
||||
if diskRoot != (common.Hash{}) {
|
||||
log.Warn("Head state missing, repairing", "number", head.Number, "hash", head.Hash(), "diskRoot", diskRoot)
|
||||
}
|
||||
if diskRoot != (common.Hash{}) {
|
||||
log.Warn("Head state missing, repairing", "number", head.Number, "hash", head.Hash(), "diskRoot", diskRoot)
|
||||
|
||||
snapDisk, err := bc.setHeadBeyondRoot(head.Number.Uint64(), 0, diskRoot, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Chain rewound, persist old snapshot number to indicate recovery procedure
|
||||
if snapDisk != 0 {
|
||||
rawdb.WriteSnapshotRecoveryNumber(bc.db, snapDisk)
|
||||
}
|
||||
} else {
|
||||
log.Warn("Head state missing, repairing", "number", head.Number, "hash", head.Hash())
|
||||
if _, err := bc.setHeadBeyondRoot(head.Number.Uint64(), 0, common.Hash{}, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
snapDisk, err := bc.setHeadBeyondRoot(head.Number.Uint64(), 0, diskRoot, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Warn("versa db no recovery, rewind in load state")
|
||||
}
|
||||
// Ensure that a previous crash in SetHead doesn't leave extra ancients
|
||||
if bc.triedb.Scheme() != rawdb.VersionScheme {
|
||||
if frozen, err := bc.db.BlockStore().ItemAmountInAncient(); err == nil && frozen > 0 {
|
||||
frozen, err = bc.db.BlockStore().Ancients()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
needRewind bool
|
||||
low uint64
|
||||
)
|
||||
// The head full block may be rolled back to a very low height due to
|
||||
// blockchain repair. If the head full block is even lower than the ancient
|
||||
// chain, truncate the ancient store.
|
||||
fullBlock := bc.CurrentBlock()
|
||||
if fullBlock != nil && fullBlock.Hash() != bc.genesisBlock.Hash() && fullBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
low = fullBlock.Number.Uint64()
|
||||
}
|
||||
// In snap sync, it may happen that ancient data has been written to the
|
||||
// ancient store, but the LastFastBlock has not been updated, truncate the
|
||||
// extra data here.
|
||||
snapBlock := bc.CurrentSnapBlock()
|
||||
if snapBlock != nil && snapBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
if snapBlock.Number.Uint64() < low || low == 0 {
|
||||
low = snapBlock.Number.Uint64()
|
||||
// Chain rewound, persist old snapshot number to indicate recovery procedure
|
||||
if snapDisk != 0 {
|
||||
rawdb.WriteSnapshotRecoveryNumber(bc.db, snapDisk)
|
||||
}
|
||||
}
|
||||
if needRewind {
|
||||
log.Error("Truncating ancient chain", "from", bc.CurrentHeader().Number.Uint64(), "to", low)
|
||||
if err := bc.SetHead(low); err != nil {
|
||||
} else {
|
||||
log.Warn("Head state missing, repairing", "number", head.Number, "hash", head.Hash())
|
||||
if _, err := bc.setHeadBeyondRoot(head.Number.Uint64(), 0, common.Hash{}, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ensure that a previous crash in SetHead doesn't leave extra ancients
|
||||
if bc.triedb.Scheme() != rawdb.VersionScheme {
|
||||
if frozen, err := bc.db.BlockStore().ItemAmountInAncient(); err == nil && frozen > 0 {
|
||||
frozen, err = bc.db.BlockStore().Ancients()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
needRewind bool
|
||||
low uint64
|
||||
)
|
||||
// The head full block may be rolled back to a very low height due to
|
||||
// blockchain repair. If the head full block is even lower than the ancient
|
||||
// chain, truncate the ancient store.
|
||||
fullBlock := bc.CurrentBlock()
|
||||
if fullBlock != nil && fullBlock.Hash() != bc.genesisBlock.Hash() && fullBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
low = fullBlock.Number.Uint64()
|
||||
}
|
||||
// In snap sync, it may happen that ancient data has been written to the
|
||||
// ancient store, but the LastFastBlock has not been updated, truncate the
|
||||
// extra data here.
|
||||
snapBlock := bc.CurrentSnapBlock()
|
||||
if snapBlock != nil && snapBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
if snapBlock.Number.Uint64() < low || low == 0 {
|
||||
low = snapBlock.Number.Uint64()
|
||||
}
|
||||
}
|
||||
if needRewind {
|
||||
log.Error("Truncating ancient chain", "from", bc.CurrentHeader().Number.Uint64(), "to", low)
|
||||
if err := bc.SetHead(low); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//TODO:: need consider the offline and inline prune block
|
||||
frozen, err := bc.db.BlockStore().Ancients()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := bc.db.BlockStore().ItemAmountInAncient()
|
||||
if frozen, err := bc.db.BlockStore().ItemAmountInAncient(); err == nil && frozen > 0 {
|
||||
frozen, err = bc.db.BlockStore().Ancients()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
needRewind bool
|
||||
low uint64
|
||||
)
|
||||
// The head full block may be rolled back to a very low height due to
|
||||
// blockchain repair. If the head full block is even lower than the ancient
|
||||
// chain, truncate the ancient store.
|
||||
fullBlock := bc.CurrentBlock()
|
||||
log.Info("version mode rewind ancient store", "target", fullBlock.Number.Uint64(), "old head", frozen, "items", items, "offset", bc.db.BlockStore().AncientOffSet())
|
||||
if frozen >= fullBlock.Number.Uint64() {
|
||||
if _, err = bc.db.BlockStore().TruncateTail(fullBlock.Number.Uint64()); err != nil {
|
||||
if fullBlock != nil && fullBlock.Hash() != bc.genesisBlock.Hash() && fullBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
low = fullBlock.Number.Uint64()
|
||||
}
|
||||
// In snap sync, it may happen that ancient data has been written to the
|
||||
// ancient store, but the LastFastBlock has not been updated, truncate the
|
||||
// extra data here.
|
||||
snapBlock := bc.CurrentSnapBlock()
|
||||
if snapBlock != nil && snapBlock.Number.Uint64() < frozen-1 {
|
||||
needRewind = true
|
||||
if snapBlock.Number.Uint64() < low || low == 0 {
|
||||
low = snapBlock.Number.Uint64()
|
||||
}
|
||||
}
|
||||
if needRewind {
|
||||
log.Error("Truncating ancient chain", "from", bc.CurrentHeader().Number.Uint64(), "to", low)
|
||||
if err := bc.SetHead(low); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -775,61 +703,20 @@ func (bc *BlockChain) getFinalizedNumber(header *types.Header) uint64 {
|
||||
// loadLastState loads the last known chain state from the database. This method
|
||||
// assumes that the chain manager mutex is held.
|
||||
func (bc *BlockChain) loadLastState() error {
|
||||
// TODO:: before versa db support recovery, only rewind
|
||||
var headBlock *types.Block
|
||||
if bc.triedb.Scheme() == rawdb.VersionScheme {
|
||||
head := rawdb.ReadHeadBlockHash(bc.db)
|
||||
headBlock = bc.GetBlockByHash(head)
|
||||
|
||||
versa := bc.triedb.VersaDB()
|
||||
archiveVersion, archiveRoot := versa.LatestStoreDiskVersionInfo()
|
||||
// first start
|
||||
if archiveVersion == -1 {
|
||||
archiveVersion = 0
|
||||
archiveRoot = bc.genesisBlock.Root()
|
||||
}
|
||||
|
||||
if int64(headBlock.NumberU64()) < archiveVersion {
|
||||
log.Crit("versa db disk version large than header block", "head number", headBlock.NumberU64(), "versa archive number", archiveVersion)
|
||||
}
|
||||
log.Info("begin rewind versa db head", "target_number", archiveVersion, "target_root", archiveRoot.String(), "head_number", headBlock.NumberU64(), "head_root", headBlock.Root().String())
|
||||
for {
|
||||
if int64(headBlock.NumberU64()) == archiveVersion && archiveRoot.Cmp(headBlock.Root()) == 0 {
|
||||
rawdb.WriteCanonicalHash(bc.db, headBlock.Hash(), headBlock.NumberU64())
|
||||
rawdb.WriteHeadHeaderHash(bc.db, headBlock.Hash())
|
||||
rawdb.WriteHeadBlockHash(bc.db, headBlock.Hash())
|
||||
rawdb.WriteHeadFastBlockHash(bc.db, headBlock.Hash())
|
||||
log.Info("reset versa db head block", "number", headBlock.NumberU64(), "hash", headBlock.Hash())
|
||||
break
|
||||
} else if int64(headBlock.NumberU64()) == archiveVersion {
|
||||
log.Crit("rewinding meet same number", "target_number", archiveVersion, "target_root", archiveRoot.String(), "head_number", headBlock.NumberU64(), "head_root", headBlock.Root().String())
|
||||
} else if archiveRoot.Cmp(headBlock.Root()) == 0 {
|
||||
log.Info("rewinding meet same root", "target_number", archiveVersion, "target_root", archiveRoot.String(), "head_number", headBlock.NumberU64(), "head_root", headBlock.Root().String())
|
||||
}
|
||||
|
||||
log.Info("rewinding", "target_number", archiveVersion, "target_root", archiveRoot.String(), "head_number", headBlock.NumberU64(), "head_root", headBlock.Root().String())
|
||||
headBlock = rawdb.ReadBlock(bc.db, headBlock.ParentHash(), headBlock.NumberU64()-1)
|
||||
if headBlock == nil {
|
||||
panic("versa db rewind head is nil")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Restore the last known head block
|
||||
head := rawdb.ReadHeadBlockHash(bc.db)
|
||||
if head == (common.Hash{}) {
|
||||
// Corrupt or empty database, init from scratch
|
||||
log.Warn("Empty database, resetting chain")
|
||||
return bc.Reset()
|
||||
}
|
||||
// Make sure the entire head block is available
|
||||
headBlock = bc.GetBlockByHash(head)
|
||||
if headBlock == nil {
|
||||
// Corrupt or empty database, init from scratch
|
||||
log.Warn("Head block missing, resetting chain", "hash", head)
|
||||
return bc.Reset()
|
||||
}
|
||||
// Restore the last known head block
|
||||
head := rawdb.ReadHeadBlockHash(bc.db)
|
||||
if head == (common.Hash{}) {
|
||||
// Corrupt or empty database, init from scratch
|
||||
log.Warn("Empty database, resetting chain")
|
||||
return bc.Reset()
|
||||
}
|
||||
// Make sure the entire head block is available
|
||||
headBlock := bc.GetBlockByHash(head)
|
||||
if headBlock == nil {
|
||||
// Corrupt or empty database, init from scratch
|
||||
log.Warn("Head block missing, resetting chain", "hash", head)
|
||||
return bc.Reset()
|
||||
}
|
||||
log.Info("load state head block", "number", headBlock.NumberU64())
|
||||
|
||||
// Everything seems to be fine, set as the head block
|
||||
bc.currentBlock.Store(headBlock.Header())
|
||||
@@ -998,7 +885,7 @@ func (bc *BlockChain) rewindHashHead(head *types.Header, root common.Hash) (*typ
|
||||
}
|
||||
// If the associated state is not reachable, continue searching
|
||||
// backwards until an available state is found.
|
||||
if !bc.HasState(head.Number.Int64(), head.Root) {
|
||||
if !bc.HasState(head.Root) {
|
||||
// If the chain is gapped in the middle, return the genesis
|
||||
// block as the new chain head.
|
||||
parent := bc.GetHeader(head.ParentHash, head.Number.Uint64()-1)
|
||||
@@ -1038,7 +925,7 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ
|
||||
|
||||
// noState represents if the target state requested for search
|
||||
// is unavailable and impossible to be recovered.
|
||||
noState = !bc.HasState(head.Number.Int64(), root) && !bc.stateRecoverable(root)
|
||||
noState = !bc.HasState(root) && !bc.stateRecoverable(root)
|
||||
|
||||
start = time.Now() // Timestamp the rewinding is restarted
|
||||
logged = time.Now() // Timestamp last progress log was printed
|
||||
@@ -1059,13 +946,13 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ
|
||||
// If the root threshold hasn't been crossed but the available
|
||||
// state is reached, quickly determine if the target state is
|
||||
// possible to be reached or not.
|
||||
if !beyondRoot && noState && bc.HasState(head.Number.Int64(), head.Root) {
|
||||
if !beyondRoot && noState && bc.HasState(head.Root) {
|
||||
beyondRoot = true
|
||||
log.Info("Disable the search for unattainable state", "root", root)
|
||||
}
|
||||
// Check if the associated state is available or recoverable if
|
||||
// the requested root has already been crossed.
|
||||
if beyondRoot && (bc.HasState(head.Number.Int64(), head.Root) || bc.stateRecoverable(head.Root)) {
|
||||
if beyondRoot && (bc.HasState(head.Root) || bc.stateRecoverable(head.Root)) {
|
||||
break
|
||||
}
|
||||
// If pivot block is reached, return the genesis block as the
|
||||
@@ -1092,7 +979,7 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ
|
||||
}
|
||||
}
|
||||
// Recover if the target state if it's not available yet.
|
||||
if !bc.HasState(head.Number.Int64(), head.Root) {
|
||||
if !bc.HasState(head.Root) {
|
||||
if err := bc.triedb.Recover(head.Root); err != nil {
|
||||
log.Crit("Failed to rollback state", "err", err)
|
||||
}
|
||||
@@ -1187,7 +1074,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
|
||||
// the pivot point. In this scenario, there is no possible recovery
|
||||
// approach except for rerunning a snap sync. Do nothing here until the
|
||||
// state syncer picks it up.
|
||||
if !bc.HasState(newHeadBlock.Number.Int64(), newHeadBlock.Root) {
|
||||
if !bc.HasState(newHeadBlock.Root) {
|
||||
if newHeadBlock.Number.Uint64() != 0 {
|
||||
log.Crit("Chain is stateless at a non-genesis block")
|
||||
}
|
||||
@@ -1299,7 +1186,7 @@ func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !bc.NoTries() && !bc.HasState(0, root) {
|
||||
if !bc.NoTries() && !bc.HasState(root) {
|
||||
return fmt.Errorf("non existent state [%x..]", root[:4])
|
||||
}
|
||||
// If all checks out, manually set the head block.
|
||||
@@ -2356,7 +2243,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
||||
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
|
||||
}
|
||||
|
||||
bc.stateCache.SetVersion(int64(block.NumberU64()) - 1)
|
||||
if bc.stateCache.Scheme() != rawdb.VersionScheme {
|
||||
if block.NumberU64() == 2000001 {
|
||||
log.Crit("exit.... path mode, 200w blocks")
|
||||
}
|
||||
}
|
||||
|
||||
bc.stateCache.SetVersion(int64(block.NumberU64()))
|
||||
statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, bc.snaps)
|
||||
if err != nil {
|
||||
bc.stateCache.Release()
|
||||
@@ -2368,18 +2261,18 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
||||
//statedb.StartPrefetcher("chain")
|
||||
interruptCh := make(chan struct{})
|
||||
// For diff sync, it may fallback to full sync, so we still do prefetch
|
||||
if len(block.Transactions()) >= prefetchTxNumber {
|
||||
// do Prefetch in a separate goroutine to avoid blocking the critical path
|
||||
|
||||
//1.do state prefetch for snapshot cache
|
||||
throwaway := statedb.CopyDoPrefetch()
|
||||
go bc.prefetcher.Prefetch(block, throwaway, &bc.vmConfig, interruptCh)
|
||||
|
||||
// // 2.do trie prefetch for MPT trie node cache
|
||||
// // it is for the big state trie tree, prefetch based on transaction's From/To address.
|
||||
// // trie prefetcher is thread safe now, ok to prefetch in a separate routine
|
||||
// go throwaway.TriePrefetchInAdvance(block, signer)
|
||||
}
|
||||
//if len(block.Transactions()) >= prefetchTxNumber {
|
||||
// // do Prefetch in a separate goroutine to avoid blocking the critical path
|
||||
//
|
||||
// // 1.do state prefetch for snapshot cache
|
||||
// throwaway := statedb.CopyDoPrefetch()
|
||||
// go bc.prefetcher.Prefetch(block, throwaway, &bc.vmConfig, interruptCh)
|
||||
//
|
||||
// // 2.do trie prefetch for MPT trie node cache
|
||||
// // it is for the big state trie tree, prefetch based on transaction's From/To address.
|
||||
// // trie prefetcher is thread safe now, ok to prefetch in a separate routine
|
||||
// go throwaway.TriePrefetchInAdvance(block, signer)
|
||||
//}
|
||||
|
||||
// Process block using the parent state as reference point
|
||||
if bc.pipeCommit {
|
||||
@@ -2395,8 +2288,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
||||
statedb.StopPrefetcher()
|
||||
return it.index, err
|
||||
}
|
||||
blockExecutionTotalTimer.UpdateSince(pstart)
|
||||
|
||||
ptime := time.Since(pstart)
|
||||
|
||||
// Validate the state using the default validator
|
||||
@@ -2407,8 +2298,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
||||
statedb.StopPrefetcher()
|
||||
return it.index, err
|
||||
}
|
||||
blockValidationTotalTimer.UpdateSince(vstart)
|
||||
|
||||
vtime := time.Since(vstart)
|
||||
proctime := time.Since(start) // processing + validation
|
||||
|
||||
@@ -2444,7 +2333,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
|
||||
return it.index, err
|
||||
}
|
||||
bc.stateCache.Release()
|
||||
blockWriteTotalTimer.UpdateSince(wstart)
|
||||
|
||||
bc.cacheReceipts(block.Hash(), receipts, block)
|
||||
|
||||
@@ -2624,7 +2512,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
|
||||
numbers []uint64
|
||||
)
|
||||
parent := it.previous()
|
||||
for parent != nil && !bc.HasState(parent.Number.Int64(), parent.Root) {
|
||||
for parent != nil && !bc.HasState(parent.Root) {
|
||||
if bc.stateRecoverable(parent.Root) {
|
||||
if err := bc.triedb.Recover(parent.Root); err != nil {
|
||||
return 0, err
|
||||
@@ -2691,7 +2579,7 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error)
|
||||
numbers []uint64
|
||||
parent = block
|
||||
)
|
||||
for parent != nil && !bc.HasState(parent.Number().Int64(), parent.Root()) {
|
||||
for parent != nil && !bc.HasState(parent.Root()) {
|
||||
if bc.stateRecoverable(parent.Root()) {
|
||||
if err := bc.triedb.Recover(parent.Root()); err != nil {
|
||||
return common.Hash{}, err
|
||||
@@ -2962,7 +2850,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) {
|
||||
defer bc.chainmu.Unlock()
|
||||
|
||||
// Re-execute the reorged chain in case the head state is missing.
|
||||
if !bc.HasState(head.Number().Int64(), head.Root()) {
|
||||
if !bc.HasState(head.Root()) {
|
||||
if latestValidHash, err := bc.recoverAncestors(head); err != nil {
|
||||
return latestValidHash, err
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
@@ -339,7 +338,7 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
|
||||
}
|
||||
|
||||
// HasState checks if state trie is fully present in the database or not.
|
||||
func (bc *BlockChain) HasState(number int64, hash common.Hash) bool {
|
||||
func (bc *BlockChain) HasState(hash common.Hash) bool {
|
||||
if bc.NoTries() {
|
||||
return bc.snaps != nil && bc.snaps.Snapshot(hash) != nil
|
||||
}
|
||||
@@ -349,24 +348,18 @@ func (bc *BlockChain) HasState(number int64, hash common.Hash) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return bc.stateCache.HasState(number, hash)
|
||||
return bc.stateCache.HasState(hash)
|
||||
}
|
||||
|
||||
// HasBlockAndState checks if a block and associated state trie is fully present
|
||||
// in the database or not, caching it if present.
|
||||
func (bc *BlockChain) HasBlockAndState(hash common.Hash, number int64) bool {
|
||||
func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool {
|
||||
// Check first that the block itself is known
|
||||
var root common.Hash
|
||||
if number < 0 {
|
||||
root = types.EmptyRootHash
|
||||
} else {
|
||||
block := bc.GetBlock(hash, uint64(number))
|
||||
if block == nil {
|
||||
return false
|
||||
}
|
||||
root = block.Root()
|
||||
block := bc.GetBlock(hash, number)
|
||||
if block == nil {
|
||||
return false
|
||||
}
|
||||
return bc.HasState(number, root)
|
||||
return bc.HasState(block.Root())
|
||||
}
|
||||
|
||||
// stateRecoverable checks if the specified state is recoverable.
|
||||
@@ -397,19 +390,13 @@ func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) ([]byte, error) {
|
||||
|
||||
// State returns a new mutable state based on the current HEAD block.
|
||||
func (bc *BlockChain) State() (*state.StateDB, error) {
|
||||
return bc.StateAt(bc.CurrentBlock().Number.Int64(), bc.CurrentBlock().Root)
|
||||
return bc.StateAt(bc.CurrentBlock().Root)
|
||||
}
|
||||
|
||||
// StateAt returns a new mutable state based on a particular point in time.
|
||||
func (bc *BlockChain) StateAt(number int64, root common.Hash) (*state.StateDB, error) {
|
||||
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
|
||||
// new state db with no need commit mode
|
||||
has := bc.HasState(number, root)
|
||||
if !has {
|
||||
return nil, fmt.Errorf(fmt.Sprintf("do not has state, verison: %d, root: %s", number, root.String()))
|
||||
}
|
||||
sdb := state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false)
|
||||
sdb.SetVersion(number)
|
||||
stateDb, err := state.New(root, sdb, bc.snaps)
|
||||
stateDb, err := state.New(root, state.NewDatabaseWithNodeDB(bc.db, bc.triedb, false), bc.snaps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Databa
|
||||
triedbConfig.NoTries = false
|
||||
}
|
||||
cachingdb := state.NewDatabaseWithNodeDB(db, triedb, true)
|
||||
cachingdb.SetVersion(-1)
|
||||
cachingdb.SetVersion(0)
|
||||
defer cachingdb.Release()
|
||||
statedb, err := state.New(types.EmptyRootHash, cachingdb, nil)
|
||||
if err != nil {
|
||||
@@ -274,8 +274,6 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
|
||||
log.Info("genesis block hash", "hash", block.Hash())
|
||||
return genesis.Config, block.Hash(), nil
|
||||
}
|
||||
log.Info("init genesis", "stored root", stored.String())
|
||||
|
||||
// The genesis block is present(perhaps in ancient database) while the
|
||||
// state database is not initialized yet. It can happen that the node
|
||||
// is initialized with an external ancient store. Commit genesis state
|
||||
|
||||
@@ -36,7 +36,7 @@ type cachingVersaDB struct {
|
||||
mode versa.StateMode
|
||||
hasState atomic.Bool
|
||||
|
||||
//debug *DebugVersionState
|
||||
debug *DebugVersionState
|
||||
}
|
||||
|
||||
// NewVersaDatabase should be call by NewDatabaseWithNodeDB
|
||||
@@ -60,16 +60,15 @@ func (cv *cachingVersaDB) Copy() Database {
|
||||
cp.triedb = cv.triedb
|
||||
cp.versionDB = cv.versionDB
|
||||
cp.codeDB = cv.codeDB
|
||||
cp.version = cv.version
|
||||
cp.mode = versa.S_RW // it is important
|
||||
|
||||
// TODO:: maybe add lock for cv.root
|
||||
if cv.hasState.Load() {
|
||||
_, err := cp.OpenTrie(cv.root)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open trie in copy caching versa db, error: %s", err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open trie in copy caching versa db, error: %s", err.Error()))
|
||||
}
|
||||
return cp
|
||||
}
|
||||
}
|
||||
@@ -91,18 +90,18 @@ func (cv *cachingVersaDB) CopyTrie(tr Trie) Trie {
|
||||
}
|
||||
tree, err := cv.OpenTrie(vtr.root)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open trie in copy versa trie, error: %s", err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open trie in copy versa trie, error: %s", err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return tree
|
||||
} else {
|
||||
tree, err := cv.OpenStorageTrie(vtr.stateRoot, vtr.address, vtr.root, nil)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open storage trie in copy versa trie, error: %s", err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open storage trie in copy versa trie, error: %s", err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return tree
|
||||
@@ -110,8 +109,8 @@ func (cv *cachingVersaDB) CopyTrie(tr Trie) Trie {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cv *cachingVersaDB) HasState(version int64, root common.Hash) bool {
|
||||
return cv.versionDB.HasState(version, root)
|
||||
func (cv *cachingVersaDB) HasState(root common.Hash) bool {
|
||||
return cv.versionDB.HasState(root)
|
||||
}
|
||||
|
||||
func (cv *cachingVersaDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
@@ -123,20 +122,20 @@ func (cv *cachingVersaDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
// TODO:: if root tree, versa db should ignore check version, temp use -1
|
||||
state, err := cv.versionDB.OpenState(cv.version, root, cv.mode)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open state, root:%s, error: %s", root.String(), err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open state, root:%s, error: %s", root.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnOpenState(state)
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnOpenState(state)
|
||||
}
|
||||
|
||||
handler, err := cv.versionDB.OpenTree(state, cv.version, common.Hash{}, root)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open account trie, root:%s, error: %s", root.String(), err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open account trie, root:%s, error: %s", root.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -146,7 +145,7 @@ func (cv *cachingVersaDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
accountTree: true,
|
||||
root: root,
|
||||
mode: cv.mode,
|
||||
//debug: cv.debug,
|
||||
debug: cv.debug,
|
||||
}
|
||||
|
||||
cv.state = state
|
||||
@@ -154,9 +153,9 @@ func (cv *cachingVersaDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
cv.accTree = tree
|
||||
cv.root = root
|
||||
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnOpenTree(handler, common.Hash{}, common.Address{})
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnOpenTree(handler, common.Hash{}, common.Address{})
|
||||
}
|
||||
|
||||
return tree, nil
|
||||
}
|
||||
@@ -181,16 +180,16 @@ func (cv *cachingVersaDB) openStorageTreeWithVersion(version int64, stateRoot co
|
||||
owner := crypto.Keccak256Hash(address.Bytes())
|
||||
handler, err := cv.versionDB.OpenTree(cv.state, version, owner, root)
|
||||
if err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to open storage trie, version: %d,stateRoot:%s, address:%s, root: %s, error: %s",
|
||||
// version, stateRoot.String(), address.String(), root.String(), err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to open storage trie, version: %d,stateRoot:%s, address:%s, root: %s, error: %s",
|
||||
version, stateRoot.String(), address.String(), root.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnOpenTree(handler, owner, address)
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnOpenTree(handler, owner, address)
|
||||
}
|
||||
tree := &VersaTree{
|
||||
db: cv.versionDB,
|
||||
handler: handler,
|
||||
@@ -199,7 +198,7 @@ func (cv *cachingVersaDB) openStorageTreeWithVersion(version int64, stateRoot co
|
||||
stateRoot: root,
|
||||
address: address,
|
||||
mode: cv.mode,
|
||||
//debug: cv.debug,
|
||||
debug: cv.debug,
|
||||
}
|
||||
return tree, nil
|
||||
}
|
||||
@@ -207,17 +206,17 @@ func (cv *cachingVersaDB) openStorageTreeWithVersion(version int64, stateRoot co
|
||||
// Flush unique to versa
|
||||
func (cv *cachingVersaDB) Flush() error {
|
||||
err := cv.versionDB.Flush(cv.state)
|
||||
//if err != nil && cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to flush state, version: %d, root:%s, mode:%d, error: %s",
|
||||
// cv.accTree.version, cv.accTree.root.String(), cv.accTree.mode, err.Error()))
|
||||
//}
|
||||
if err != nil && cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to flush state, version: %d, root:%s, mode:%d, error: %s",
|
||||
cv.accTree.version, cv.accTree.root.String(), cv.accTree.mode, err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cv *cachingVersaDB) SetVersion(version int64) {
|
||||
cv.version = version
|
||||
//cv.debug = NewDebugVersionState(cv.codeDB, cv.versionDB)
|
||||
//cv.debug.Version = version
|
||||
cv.version = version - 1
|
||||
cv.debug = NewDebugVersionState(cv.codeDB, cv.versionDB)
|
||||
cv.debug.Version = version
|
||||
}
|
||||
|
||||
func (cv *cachingVersaDB) GetVersion() int64 {
|
||||
@@ -228,21 +227,21 @@ func (cv *cachingVersaDB) GetVersion() int64 {
|
||||
func (cv *cachingVersaDB) Release() error {
|
||||
//log.Info("close state", "state info", cv.versionDB.ParseStateHandler(cv.state))
|
||||
if cv.state != versa.ErrStateHandler {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnCloseState(cv.state)
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnCloseState(cv.state)
|
||||
}
|
||||
if err := cv.versionDB.CloseState(cv.state); err != nil {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnError(fmt.Errorf("failed to close state in release, version: %d, root:%s, mode:%d, error: %s",
|
||||
// cv.accTree.version, cv.accTree.root.String(), cv.accTree.mode, err.Error()))
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnError(fmt.Errorf("failed to close state in release, version: %d, root:%s, mode:%d, error: %s",
|
||||
cv.accTree.version, cv.accTree.root.String(), cv.accTree.mode, err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
cv.hasState.Store(false)
|
||||
cv.accTree = nil
|
||||
cv.state = versa.ErrStateHandler
|
||||
cv.root = common.Hash{}
|
||||
//cv.debug = nil
|
||||
cv.debug = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -271,9 +270,9 @@ func (cv *cachingVersaDB) Scheme() string {
|
||||
}
|
||||
|
||||
func (cv *cachingVersaDB) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) {
|
||||
//if cv.debug != nil {
|
||||
// cv.debug.OnGetCode(addr, codeHash)
|
||||
//}
|
||||
if cv.debug != nil {
|
||||
cv.debug.OnGetCode(addr, codeHash)
|
||||
}
|
||||
code, _ := cv.codeCache.Get(codeHash)
|
||||
if len(code) > 0 {
|
||||
return code, nil
|
||||
@@ -327,7 +326,7 @@ type VersaTree struct {
|
||||
handler versa.TreeHandler
|
||||
version int64
|
||||
accountTree bool
|
||||
//debug *DebugVersionState
|
||||
debug *DebugVersionState
|
||||
|
||||
// TODO:: debugging, used for logging
|
||||
stateRoot common.Hash
|
||||
@@ -353,23 +352,23 @@ func (vt *VersaTree) getAccountWithVersion(address common.Address) (int64, *type
|
||||
vt.CheckAccountTree()
|
||||
ver, res, err := vt.db.Get(vt.handler, address.Bytes())
|
||||
if res == nil || err != nil {
|
||||
//if err != nil && vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to get account, root: %s, address: %s, error: %s",
|
||||
// vt.root.String(), address.String(), err.Error()))
|
||||
//}
|
||||
if err != nil && vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to get account, root: %s, address: %s, error: %s",
|
||||
vt.root.String(), address.String(), err.Error()))
|
||||
}
|
||||
return ver, nil, err
|
||||
}
|
||||
ret := new(types.StateAccount)
|
||||
err = rlp.DecodeBytes(res, ret)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to rlp decode account, root: %s, address: %s, error: %s",
|
||||
// vt.root.String(), address.String(), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to rlp decode account, root: %s, address: %s, error: %s",
|
||||
vt.root.String(), address.String(), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnGetAccount(address, ret)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnGetAccount(address, ret)
|
||||
//}
|
||||
return ver, ret, err
|
||||
}
|
||||
|
||||
@@ -380,22 +379,22 @@ func (vt *VersaTree) GetStorage(address common.Address, key []byte) ([]byte, err
|
||||
vt.CheckStorageTree()
|
||||
_, enc, err := vt.db.Get(vt.handler, key)
|
||||
if err != nil || len(enc) == 0 {
|
||||
//if err != nil && vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to get storage, root: %s, stateRoot: %s, address:%s, key: %s, error: %s",
|
||||
// vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
//}
|
||||
if err != nil && vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to get storage, root: %s, stateRoot: %s, address:%s, key: %s, error: %s",
|
||||
vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
_, content, _, err := rlp.Split(enc)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to rlp decode storage, root: %s, stateRoot: %s, address: %s, key: %s,error: %s",
|
||||
// vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to rlp decode storage, root: %s, stateRoot: %s, address: %s, key: %s,error: %s",
|
||||
vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnGetStorage(vt.handler, address, key, content)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnGetStorage(vt.handler, address, key, content)
|
||||
//}
|
||||
return content, err
|
||||
}
|
||||
|
||||
@@ -403,22 +402,18 @@ func (vt *VersaTree) UpdateAccount(address common.Address, account *types.StateA
|
||||
vt.CheckAccountTree()
|
||||
data, err := rlp.EncodeToBytes(account)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to update account, root: %s, address: %s, account: %s, error: %s",
|
||||
// vt.root.String(), address.String(), account.String(), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to update account, root: %s, address: %s, account: %s, error: %s",
|
||||
vt.root.String(), address.String(), account.String(), err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnUpdateAccount(address, account)
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnUpdateAccount(address, account)
|
||||
}
|
||||
return vt.db.Put(vt.handler, address.Bytes(), data)
|
||||
}
|
||||
|
||||
func (vt *VersaTree) WriteBatch(values map[string][]byte) error {
|
||||
return vt.db.WriteBatch(vt.handler, values)
|
||||
}
|
||||
|
||||
func (vt *VersaTree) UpdateStorage(address common.Address, key, value []byte) error {
|
||||
if vt.address.Cmp(address) != 0 {
|
||||
panic(fmt.Sprintf("address mismatch in get storage, expect: %s, actul: %s", vt.address.String(), address.String()))
|
||||
@@ -427,14 +422,14 @@ func (vt *VersaTree) UpdateStorage(address common.Address, key, value []byte) er
|
||||
v, _ := rlp.EncodeToBytes(value)
|
||||
err := vt.db.Put(vt.handler, key, v)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to update storage, root: %s, stateRoot: %s, address: %s, key: %s, val: %s, error: %s",
|
||||
// vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), common.Bytes2Hex(value), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to update storage, root: %s, stateRoot: %s, address: %s, key: %s, val: %s, error: %s",
|
||||
vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), common.Bytes2Hex(value), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnUpdateStorage(vt.handler, address, key, value)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnUpdateStorage(vt.handler, address, key, value)
|
||||
//}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -442,14 +437,14 @@ func (vt *VersaTree) DeleteAccount(address common.Address) error {
|
||||
vt.CheckAccountTree()
|
||||
err := vt.db.Delete(vt.handler, address.Bytes())
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to delete account, root: %s, address: %s, error: %s",
|
||||
// vt.root.String(), address.String(), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to delete account, root: %s, address: %s, error: %s",
|
||||
vt.root.String(), address.String(), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnDeleteAccount(address)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnDeleteAccount(address)
|
||||
//}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -457,14 +452,14 @@ func (vt *VersaTree) DeleteStorage(address common.Address, key []byte) error {
|
||||
vt.CheckStorageTree()
|
||||
err := vt.db.Delete(vt.handler, key)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to delete storage, root: %s, stateRoot: %s, address: %s, key: %s, error: %s",
|
||||
// vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to delete storage, root: %s, stateRoot: %s, address: %s, key: %s, error: %s",
|
||||
vt.root.String(), vt.stateRoot.String(), address.String(), common.Bytes2Hex(key), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnDeleteStorage(vt.handler, address, key)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnDeleteStorage(vt.handler, address, key)
|
||||
//}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -475,29 +470,29 @@ func (vt *VersaTree) UpdateContractCode(address common.Address, codeHash common.
|
||||
func (vt *VersaTree) Hash() common.Hash {
|
||||
hash, err := vt.db.CalcRootHash(vt.handler)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to calc root, root: %s, stateRoot%s, error:%s",
|
||||
// vt.root.String(), vt.stateRoot.String(), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to calc root, root: %s, stateRoot%s, error:%s",
|
||||
vt.root.String(), vt.stateRoot.String(), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnCalcHash(vt.address, hash)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnCalcHash(vt.address, hash)
|
||||
//}
|
||||
return hash
|
||||
}
|
||||
|
||||
func (vt *VersaTree) Commit(_ bool) (common.Hash, *trienode.NodeSet, error) {
|
||||
hash, err := vt.db.Commit(vt.handler)
|
||||
if err != nil {
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnError(fmt.Errorf("failed to commit versa tree, root: %s, stateRoot: %s, error: %s",
|
||||
// vt.root.String(), vt.stateRoot.String(), err.Error()))
|
||||
//}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnError(fmt.Errorf("failed to commit versa tree, root: %s, stateRoot: %s, error: %s",
|
||||
vt.root.String(), vt.stateRoot.String(), err.Error()))
|
||||
}
|
||||
}
|
||||
if vt.debug != nil {
|
||||
vt.debug.OnCalcHash(vt.address, hash)
|
||||
vt.debug.OnCommitTree(vt.address, vt.handler)
|
||||
}
|
||||
//if vt.debug != nil {
|
||||
// vt.debug.OnCalcHash(vt.address, hash)
|
||||
// vt.debug.OnCommitTree(vt.address, vt.handler)
|
||||
//}
|
||||
return hash, nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ type Database interface {
|
||||
Copy() Database
|
||||
|
||||
// HasState returns the state data whether in the triedb.
|
||||
HasState(version int64, root common.Hash) bool
|
||||
HasState(root common.Hash) bool
|
||||
|
||||
// HasTreeExpired used for caching versa db, whether the state where the opened tree resides has been closed
|
||||
HasTreeExpired(tr Trie) bool
|
||||
@@ -143,8 +143,6 @@ type Trie interface {
|
||||
// to be moved to the stateWriter interface when the latter is ready.
|
||||
UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error
|
||||
|
||||
WriteBatch(values map[string][]byte) error
|
||||
|
||||
// Hash returns the root hash of the trie. It does not write to the database and
|
||||
// can be used even if the trie doesn't have one.
|
||||
Hash() common.Hash
|
||||
@@ -229,7 +227,7 @@ type cachingDB struct {
|
||||
triedb *triedb.Database
|
||||
noTries bool
|
||||
|
||||
//debug *DebugHashState
|
||||
debug *DebugHashState
|
||||
}
|
||||
|
||||
// OpenTrie opens the main account trie at a specific root hash.
|
||||
@@ -242,23 +240,22 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
}
|
||||
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
|
||||
if err != nil {
|
||||
//if db.debug != nil {
|
||||
// db.debug.OnError(fmt.Errorf("failed to open tree, root: %s, error: %s", root.String(), err.Error()))
|
||||
//}
|
||||
if db.debug != nil {
|
||||
db.debug.OnError(fmt.Errorf("failed to open tree, root: %s, error: %s", root.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
//ht := &HashTrie{
|
||||
// trie: tr,
|
||||
// root: root,
|
||||
// address: common.Address{},
|
||||
// owner: common.Hash{},
|
||||
// debug: db.debug,
|
||||
//}
|
||||
//if db.debug != nil {
|
||||
// db.debug.OnOpenTree(root, common.Hash{}, common.Address{})
|
||||
//}
|
||||
//return ht, nil
|
||||
return tr, nil
|
||||
ht := &HashTrie{
|
||||
trie: tr,
|
||||
root: root,
|
||||
address: common.Address{},
|
||||
owner: common.Hash{},
|
||||
debug: db.debug,
|
||||
}
|
||||
if db.debug != nil {
|
||||
db.debug.OnOpenTree(root, common.Hash{}, common.Address{})
|
||||
}
|
||||
return ht, nil
|
||||
}
|
||||
|
||||
// OpenStorageTrie opens the storage trie of an account.
|
||||
@@ -276,25 +273,24 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre
|
||||
owner := crypto.Keccak256Hash(address.Bytes())
|
||||
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, owner, root), db.triedb)
|
||||
if err != nil {
|
||||
//if db.debug != nil {
|
||||
// db.debug.OnError(fmt.Errorf("failed to storage open tree, stateRoot: %s, address: %s, root: %s, error: %s",
|
||||
// stateRoot.String(), address.String(), root.String(), err.Error()))
|
||||
//}
|
||||
if db.debug != nil {
|
||||
db.debug.OnError(fmt.Errorf("failed to storage open tree, stateRoot: %s, address: %s, root: %s, error: %s",
|
||||
stateRoot.String(), address.String(), root.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
//ht := &HashTrie{
|
||||
// trie: tr,
|
||||
// root: stateRoot,
|
||||
// statRoot: root,
|
||||
// address: address,
|
||||
// owner: owner,
|
||||
// debug: db.debug,
|
||||
//}
|
||||
//if db.debug != nil {
|
||||
// db.debug.OnOpenTree(root, owner, address)
|
||||
//}
|
||||
//return ht, nil
|
||||
return tr, nil
|
||||
ht := &HashTrie{
|
||||
trie: tr,
|
||||
root: stateRoot,
|
||||
statRoot: root,
|
||||
address: address,
|
||||
owner: owner,
|
||||
debug: db.debug,
|
||||
}
|
||||
if db.debug != nil {
|
||||
db.debug.OnOpenTree(root, owner, address)
|
||||
}
|
||||
return ht, nil
|
||||
}
|
||||
|
||||
func (db *cachingDB) NoTries() bool {
|
||||
@@ -311,8 +307,8 @@ func (db *cachingDB) CopyTrie(t Trie) Trie {
|
||||
return t.Copy()
|
||||
case *trie.EmptyTrie:
|
||||
return t.Copy()
|
||||
//case *HashTrie:
|
||||
// return db.CopyTrie(t.trie)
|
||||
case *HashTrie:
|
||||
return db.CopyTrie(t.trie)
|
||||
default:
|
||||
panic(fmt.Errorf("unknown trie type %T", t))
|
||||
}
|
||||
@@ -320,9 +316,9 @@ func (db *cachingDB) CopyTrie(t Trie) Trie {
|
||||
|
||||
// ContractCode retrieves a particular contract's code.
|
||||
func (db *cachingDB) ContractCode(address common.Address, codeHash common.Hash) ([]byte, error) {
|
||||
//if db.debug != nil {
|
||||
// db.debug.OnGetCode(address, codeHash)
|
||||
//}
|
||||
if db.debug != nil {
|
||||
db.debug.OnGetCode(address, codeHash)
|
||||
}
|
||||
code, _ := db.codeCache.Get(codeHash)
|
||||
if len(code) > 0 {
|
||||
return code, nil
|
||||
@@ -385,26 +381,25 @@ func (db *cachingDB) Flush() error {
|
||||
}
|
||||
|
||||
func (db *cachingDB) Release() error {
|
||||
//db.debug.flush()
|
||||
//db.debug = nil
|
||||
db.debug.flush()
|
||||
db.debug = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *cachingDB) SetVersion(version int64) {
|
||||
//db.debug = NewDebugHashState(db.disk)
|
||||
//db.debug.Version = version
|
||||
db.debug = NewDebugHashState(db.disk)
|
||||
db.debug.Version = version
|
||||
}
|
||||
|
||||
func (db *cachingDB) GetVersion() int64 {
|
||||
//return db.debug.Version
|
||||
return 0
|
||||
return db.debug.Version
|
||||
}
|
||||
|
||||
func (db *cachingDB) Copy() Database {
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *cachingDB) HasState(_ int64, root common.Hash) bool {
|
||||
func (db *cachingDB) HasState(root common.Hash) bool {
|
||||
_, err := db.OpenTrie(root)
|
||||
return err == nil
|
||||
}
|
||||
@@ -413,137 +408,137 @@ func (db *cachingDB) HasTreeExpired(_ Trie) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
//type HashTrie struct {
|
||||
// trie Trie
|
||||
// root common.Hash
|
||||
// statRoot common.Hash
|
||||
// address common.Address
|
||||
// owner common.Hash
|
||||
//
|
||||
// debug *DebugHashState
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) GetKey(key []byte) []byte {
|
||||
// return ht.trie.GetKey(key)
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
|
||||
// acc, err := ht.trie.GetAccount(address)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to get account, address: %s, error: %s", address.String(), err.Error()))
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnGetAccount(address, acc)
|
||||
// }
|
||||
// return acc, nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
|
||||
// val, err := ht.trie.GetStorage(addr, key)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to get storage, address: %s, error: %s", addr.String(), err.Error()))
|
||||
// }
|
||||
// return val, err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnGetStorage(addr, key, val)
|
||||
// }
|
||||
// return val, err
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) UpdateAccount(address common.Address, account *types.StateAccount) error {
|
||||
// err := ht.trie.UpdateAccount(address, account)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to update account, address: %s, account: %s, error: %s",
|
||||
// address.String(), account.String(), err.Error()))
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnUpdateAccount(address, account)
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) UpdateStorage(addr common.Address, key, value []byte) error {
|
||||
// err := ht.trie.UpdateStorage(addr, key, value)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, val: %s, error: %s",
|
||||
// addr.String(), common.Bytes2Hex(key), common.Bytes2Hex(value), err.Error()))
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnUpdateStorage(addr, key, value)
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) DeleteAccount(address common.Address) error {
|
||||
// err := ht.trie.DeleteAccount(address)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to delete account, address: %s, error: %s", address.String(), err.Error()))
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnDeleteAccount(address)
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) DeleteStorage(addr common.Address, key []byte) error {
|
||||
// err := ht.trie.DeleteStorage(addr, key)
|
||||
// if err != nil {
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, error: %s",
|
||||
// addr.String(), common.Bytes2Hex(key), err.Error()))
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnDeleteStorage(addr, key)
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error {
|
||||
// return ht.trie.UpdateContractCode(address, codeHash, code)
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) Hash() common.Hash {
|
||||
// root := ht.trie.Hash()
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnCalcHash(ht.address, root)
|
||||
// }
|
||||
// return root
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
|
||||
// hash, set, err := ht.trie.Commit(collectLeaf)
|
||||
// if err != nil {
|
||||
// ht.debug.OnError(fmt.Errorf("failed to commit tree, address: %s, error: %s",
|
||||
// ht.address.String(), err.Error()))
|
||||
// return hash, set, err
|
||||
// }
|
||||
// if ht.debug != nil {
|
||||
// ht.debug.OnCalcHash(ht.address, hash)
|
||||
// ht.debug.OnCommitTree(ht.address, hash)
|
||||
// }
|
||||
// return hash, set, nil
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) NodeIterator(startKey []byte) (trie.NodeIterator, error) {
|
||||
// return ht.trie.NodeIterator(startKey)
|
||||
//}
|
||||
//
|
||||
//func (ht *HashTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
|
||||
// return ht.trie.Prove(key, proofDb)
|
||||
//}
|
||||
type HashTrie struct {
|
||||
trie Trie
|
||||
root common.Hash
|
||||
statRoot common.Hash
|
||||
address common.Address
|
||||
owner common.Hash
|
||||
|
||||
debug *DebugHashState
|
||||
}
|
||||
|
||||
func (ht *HashTrie) GetKey(key []byte) []byte {
|
||||
return ht.trie.GetKey(key)
|
||||
}
|
||||
|
||||
func (ht *HashTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
|
||||
acc, err := ht.trie.GetAccount(address)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to get account, address: %s, error: %s", address.String(), err.Error()))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnGetAccount(address, acc)
|
||||
}
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
|
||||
val, err := ht.trie.GetStorage(addr, key)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to get storage, address: %s, error: %s", addr.String(), err.Error()))
|
||||
}
|
||||
return val, err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnGetStorage(addr, key, val)
|
||||
}
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (ht *HashTrie) UpdateAccount(address common.Address, account *types.StateAccount) error {
|
||||
err := ht.trie.UpdateAccount(address, account)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to update account, address: %s, account: %s, error: %s",
|
||||
address.String(), account.String(), err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnUpdateAccount(address, account)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) UpdateStorage(addr common.Address, key, value []byte) error {
|
||||
err := ht.trie.UpdateStorage(addr, key, value)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, val: %s, error: %s",
|
||||
addr.String(), common.Bytes2Hex(key), common.Bytes2Hex(value), err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnUpdateStorage(addr, key, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) DeleteAccount(address common.Address) error {
|
||||
err := ht.trie.DeleteAccount(address)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to delete account, address: %s, error: %s", address.String(), err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnDeleteAccount(address)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) DeleteStorage(addr common.Address, key []byte) error {
|
||||
err := ht.trie.DeleteStorage(addr, key)
|
||||
if err != nil {
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, error: %s",
|
||||
addr.String(), common.Bytes2Hex(key), err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnDeleteStorage(addr, key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error {
|
||||
return ht.trie.UpdateContractCode(address, codeHash, code)
|
||||
}
|
||||
|
||||
func (ht *HashTrie) Hash() common.Hash {
|
||||
root := ht.trie.Hash()
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnCalcHash(ht.address, root)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func (ht *HashTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
|
||||
hash, set, err := ht.trie.Commit(collectLeaf)
|
||||
if err != nil {
|
||||
ht.debug.OnError(fmt.Errorf("failed to commit tree, address: %s, error: %s",
|
||||
ht.address.String(), err.Error()))
|
||||
return hash, set, err
|
||||
}
|
||||
if ht.debug != nil {
|
||||
ht.debug.OnCalcHash(ht.address, hash)
|
||||
ht.debug.OnCommitTree(ht.address, hash)
|
||||
}
|
||||
return hash, set, nil
|
||||
}
|
||||
|
||||
func (ht *HashTrie) NodeIterator(startKey []byte) (trie.NodeIterator, error) {
|
||||
return ht.trie.NodeIterator(startKey)
|
||||
}
|
||||
|
||||
func (ht *HashTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
|
||||
return ht.trie.Prove(key, proofDb)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,4 @@ var (
|
||||
slotDeletionCount = metrics.NewRegisteredMeter("state/delete/storage/slot", nil)
|
||||
slotDeletionSize = metrics.NewRegisteredMeter("state/delete/storage/size", nil)
|
||||
slotDeletionSkip = metrics.NewRegisteredGauge("state/delete/storage/skip", nil)
|
||||
|
||||
accountIntermediateRootTimer = metrics.NewRegisteredTimer("state/account/intermediate/root/time", nil)
|
||||
storageIntermediateRootTimer = metrics.NewRegisteredTimer("state/storage/intermediate/root/time", nil)
|
||||
)
|
||||
|
||||
@@ -235,14 +235,6 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
|
||||
return common.Hash{}
|
||||
}
|
||||
// If no live objects are available, attempt to use snapshots
|
||||
|
||||
defer func(start time.Time) {
|
||||
stateDBGetTimer.UpdateSince(start)
|
||||
stateDBGetQPS.Mark(1)
|
||||
stateDBGetStorageTimer.UpdateSince(start)
|
||||
stateDBGetStorageQPS.Mark(1)
|
||||
}(time.Now())
|
||||
|
||||
var (
|
||||
enc []byte
|
||||
err error
|
||||
@@ -398,10 +390,6 @@ func (s *stateObject) updateTrie() (Trie, error) {
|
||||
s.db.setError(err)
|
||||
return nil, err
|
||||
}
|
||||
if len(s.pendingStorage) == 0 {
|
||||
return s.trie, nil
|
||||
}
|
||||
|
||||
// Insert all the pending storage updates into the trie
|
||||
usedStorage := make([][]byte, 0, len(s.pendingStorage))
|
||||
dirtyStorage := make(map[common.Hash][]byte)
|
||||
@@ -417,14 +405,11 @@ func (s *stateObject) updateTrie() (Trie, error) {
|
||||
}
|
||||
dirtyStorage[key] = v
|
||||
}
|
||||
|
||||
//storages := make(map[string][]byte)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for key, value := range dirtyStorage {
|
||||
//TODO:: add version schema check
|
||||
if len(value) == 0 {
|
||||
if err := tr.DeleteStorage(s.address, key[:]); err != nil {
|
||||
s.db.setError(err)
|
||||
@@ -436,16 +421,6 @@ func (s *stateObject) updateTrie() (Trie, error) {
|
||||
}
|
||||
s.db.StorageUpdated += 1
|
||||
}
|
||||
//if len(value) == 0 {
|
||||
// storages[string(key[:])] = nil
|
||||
//} else {
|
||||
// v, _ := rlp.EncodeToBytes(value)
|
||||
// storages[string(key[:])] = v
|
||||
//}
|
||||
//if err := tr.WriteBatch(storages); err != nil {
|
||||
// s.db.setError(err)
|
||||
//}
|
||||
|
||||
// Cache the items for preloading
|
||||
usedStorage = append(usedStorage, common.CopyBytes(key[:]))
|
||||
}
|
||||
@@ -532,11 +507,6 @@ func (s *stateObject) updateRoot() {
|
||||
// The returned set can be nil if nothing to commit. This function assumes all
|
||||
// storage mutations have already been flushed into trie by updateRoot.
|
||||
func (s *stateObject) commit() (*trienode.NodeSet, error) {
|
||||
if s.IsContractAccount() && s.trie == nil && s.db.db.GetVersion() != 0 {
|
||||
panic(fmt.Sprintf("not open contract account, owner: %s, r_version: %d, c_version: %d, root: %s",
|
||||
s.address.String(), s.db.db.GetVersion(), s.version, s.Root().String()))
|
||||
}
|
||||
|
||||
// Short circuit if trie is not even loaded, don't bother with committing anything
|
||||
if s.trie == nil {
|
||||
s.origin = s.data.Copy()
|
||||
|
||||
@@ -54,16 +54,6 @@ type revision struct {
|
||||
journalIndex int
|
||||
}
|
||||
|
||||
var (
|
||||
stateDBGetTimer = metrics.NewRegisteredTimer("statedb/get/time", nil)
|
||||
stateDBGetQPS = metrics.NewRegisteredMeter("statedb/get/qps", nil)
|
||||
|
||||
stateDBGetAccountTimer = metrics.NewRegisteredTimer("statedb/account/get/time", nil)
|
||||
stateDBGetAccountQPS = metrics.NewRegisteredMeter("statedb/account/get/qps", nil)
|
||||
stateDBGetStorageTimer = metrics.NewRegisteredTimer("statedb/storage/get/time", nil)
|
||||
stateDBGetStorageQPS = metrics.NewRegisteredMeter("statedb/storage/get/qps", nil)
|
||||
)
|
||||
|
||||
// StateDB structs within the ethereum protocol are used to store anything
|
||||
// within the merkle trie. StateDBs take care of caching and storing
|
||||
// nested states. It's the general query interface to retrieve:
|
||||
@@ -692,7 +682,6 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
|
||||
if err := s.trie.UpdateAccount(addr, &obj.data); err != nil {
|
||||
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
|
||||
}
|
||||
|
||||
if obj.dirtyCode {
|
||||
s.trie.UpdateContractCode(obj.Address(), common.BytesToHash(obj.CodeHash()), obj.code)
|
||||
}
|
||||
@@ -720,7 +709,6 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
|
||||
}
|
||||
// Delete the account from the trie
|
||||
addr := obj.Address()
|
||||
|
||||
if err := s.trie.DeleteAccount(addr); err != nil {
|
||||
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
|
||||
}
|
||||
@@ -745,14 +733,6 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
|
||||
if obj := s.stateObjects[addr]; obj != nil {
|
||||
return obj
|
||||
}
|
||||
|
||||
defer func(start time.Time) {
|
||||
stateDBGetTimer.UpdateSince(start)
|
||||
stateDBGetQPS.Mark(1)
|
||||
stateDBGetAccountTimer.UpdateSince(start)
|
||||
stateDBGetAccountQPS.Mark(1)
|
||||
}(time.Now())
|
||||
|
||||
// If no live objects are available, attempt to use snapshots
|
||||
var data *types.StateAccount
|
||||
if s.snap != nil {
|
||||
@@ -1197,10 +1177,6 @@ func (s *StateDB) populateSnapStorage(obj *stateObject) bool {
|
||||
}
|
||||
|
||||
func (s *StateDB) AccountsIntermediateRoot() {
|
||||
defer func(start time.Time) {
|
||||
storageIntermediateRootTimer.UpdateSince(start)
|
||||
}(time.Now())
|
||||
|
||||
tasks := make(chan func())
|
||||
finishCh := make(chan struct{})
|
||||
defer close(finishCh)
|
||||
@@ -1228,6 +1204,7 @@ func (s *StateDB) AccountsIntermediateRoot() {
|
||||
wg.Add(1)
|
||||
tasks <- func() {
|
||||
obj.updateRoot()
|
||||
|
||||
// Cache the data until commit. Note, this update mechanism is not symmetric
|
||||
// to the deletion, because whereas it is enough to track account updates
|
||||
// at commit time, deletions need tracking at transaction boundary level to
|
||||
@@ -1244,9 +1221,6 @@ func (s *StateDB) AccountsIntermediateRoot() {
|
||||
}
|
||||
|
||||
func (s *StateDB) StateIntermediateRoot() common.Hash {
|
||||
defer func(start time.Time) {
|
||||
accountIntermediateRootTimer.UpdateSince(start)
|
||||
}(time.Now())
|
||||
// If there was a trie prefetcher operating, it gets aborted and irrevocably
|
||||
// modified after we start retrieving tries. Remove it from the statedb after
|
||||
// this round of use.
|
||||
@@ -1680,9 +1654,9 @@ func (s *StateDB) Commit(block uint64, failPostCommitFunc func(), postCommitFunc
|
||||
if err := s.db.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := s.db.Release(); err != nil {
|
||||
// return err
|
||||
//}
|
||||
if err := s.db.Release(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.originalRoot = root
|
||||
if metrics.EnabledExpensive {
|
||||
s.TrieDBCommits += time.Since(start)
|
||||
@@ -1739,18 +1713,18 @@ func (s *StateDB) Commit(block uint64, failPostCommitFunc func(), postCommitFunc
|
||||
// Write any contract code associated with the state object
|
||||
if obj.code != nil && obj.dirtyCode {
|
||||
rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
|
||||
//switch d := s.db.(type) {
|
||||
//case *cachingVersaDB:
|
||||
// if d.debug != nil {
|
||||
// d.debug.OnUpdateCode(obj.address, common.BytesToHash(obj.CodeHash()))
|
||||
// }
|
||||
//case *cachingDB:
|
||||
// if d.debug != nil {
|
||||
// d.debug.OnUpdateCode(obj.address, common.BytesToHash(obj.CodeHash()))
|
||||
// }
|
||||
//default:
|
||||
// panic("caching db type error")
|
||||
//}
|
||||
switch d := s.db.(type) {
|
||||
case *cachingVersaDB:
|
||||
if d.debug != nil {
|
||||
d.debug.OnUpdateCode(obj.address, common.BytesToHash(obj.CodeHash()))
|
||||
}
|
||||
case *cachingDB:
|
||||
if d.debug != nil {
|
||||
d.debug.OnUpdateCode(obj.address, common.BytesToHash(obj.CodeHash()))
|
||||
}
|
||||
default:
|
||||
panic("caching db type error")
|
||||
}
|
||||
obj.dirtyCode = false
|
||||
if s.snap != nil {
|
||||
panic("snapshot is not nil")
|
||||
|
||||
@@ -49,6 +49,7 @@ func NewStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine conse
|
||||
// the transaction messages using the statedb, but any changes are discarded. The
|
||||
// only goal is to pre-cache transaction signatures and state trie nodes.
|
||||
func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg *vm.Config, interruptCh <-chan struct{}) {
|
||||
panic("prefetcher not support")
|
||||
var (
|
||||
header = block.Header()
|
||||
signer = types.MakeSigner(p.config, header.Number, header.Time)
|
||||
|
||||
@@ -366,9 +366,9 @@ func (p *BlobPool) Init(gasTip uint64, head *types.Header, reserve txpool.Addres
|
||||
// Initialize the state with head block, or fallback to empty one in
|
||||
// case the head state is not available (might occur when node is not
|
||||
// fully synced).
|
||||
state, err := p.chain.StateAt(head.Number.Int64(), head.Root)
|
||||
state, err := p.chain.StateAt(head.Root)
|
||||
if err != nil {
|
||||
state, err = p.chain.StateAt(-1, types.EmptyRootHash)
|
||||
state, err = p.chain.StateAt(types.EmptyRootHash)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -793,7 +793,7 @@ func (p *BlobPool) Reset(oldHead, newHead *types.Header) {
|
||||
resettimeHist.Update(time.Since(start).Nanoseconds())
|
||||
}(time.Now())
|
||||
|
||||
statedb, err := p.chain.StateAt(newHead.Number.Int64(), newHead.Root)
|
||||
statedb, err := p.chain.StateAt(newHead.Root)
|
||||
if err != nil {
|
||||
log.Error("Failed to reset blobpool state", "err", err)
|
||||
return
|
||||
|
||||
@@ -40,5 +40,5 @@ type BlockChain interface {
|
||||
GetBlock(hash common.Hash, number uint64) *types.Block
|
||||
|
||||
// StateAt returns a state database for a given root hash (generally the head).
|
||||
StateAt(number int64, root common.Hash) (*state.StateDB, error)
|
||||
StateAt(root common.Hash) (*state.StateDB, error)
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ type BlockChain interface {
|
||||
GetBlock(hash common.Hash, number uint64) *types.Block
|
||||
|
||||
// StateAt returns a state database for a given root hash (generally the head).
|
||||
StateAt(number int64, root common.Hash) (*state.StateDB, error)
|
||||
StateAt(root common.Hash) (*state.StateDB, error)
|
||||
}
|
||||
|
||||
// Config are the configuration parameters of the transaction pool.
|
||||
@@ -311,9 +311,9 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A
|
||||
// Initialize the state with head block, or fallback to empty one in
|
||||
// case the head state is not available (might occur when node is not
|
||||
// fully synced).
|
||||
statedb, err := pool.chain.StateAt(head.Number.Int64(), head.Root)
|
||||
statedb, err := pool.chain.StateAt(head.Root)
|
||||
if err != nil {
|
||||
statedb, err = pool.chain.StateAt(-1, types.EmptyRootHash)
|
||||
statedb, err = pool.chain.StateAt(types.EmptyRootHash)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1492,7 +1492,7 @@ func (pool *LegacyPool) reset(oldHead, newHead *types.Header) {
|
||||
if newHead == nil {
|
||||
newHead = pool.chain.CurrentBlock() // Special case during testing
|
||||
}
|
||||
statedb, err := pool.chain.StateAt(newHead.Number.Int64(), newHead.Root)
|
||||
statedb, err := pool.chain.StateAt(newHead.Root)
|
||||
if err != nil {
|
||||
log.Error("Failed to reset txpool state", "err", err)
|
||||
return
|
||||
|
||||
@@ -204,7 +204,7 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B
|
||||
if header == nil {
|
||||
return nil, nil, errors.New("header not found")
|
||||
}
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Number.Int64(), header.Root)
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN
|
||||
if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
|
||||
return nil, nil, errors.New("hash is not currently canonical")
|
||||
}
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Number.Int64(), header.Root)
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
|
||||
if header == nil {
|
||||
return state.Dump{}, fmt.Errorf("block #%d not found", blockNr)
|
||||
}
|
||||
stateDb, err := api.eth.BlockChain().StateAt(header.Number.Int64(), header.Root)
|
||||
stateDb, err := api.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return state.Dump{}, err
|
||||
}
|
||||
@@ -167,7 +167,7 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex
|
||||
if header == nil {
|
||||
return state.Dump{}, fmt.Errorf("block #%d not found", number)
|
||||
}
|
||||
stateDb, err = api.eth.BlockChain().StateAt(header.Number.Int64(), header.Root)
|
||||
stateDb, err = api.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return state.Dump{}, err
|
||||
}
|
||||
@@ -177,7 +177,7 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex
|
||||
if block == nil {
|
||||
return state.Dump{}, fmt.Errorf("block %s not found", hash.Hex())
|
||||
}
|
||||
stateDb, err = api.eth.BlockChain().StateAt(block.Number().Int64(), block.Root())
|
||||
stateDb, err = api.eth.BlockChain().StateAt(block.Root())
|
||||
if err != nil {
|
||||
return state.Dump{}, err
|
||||
}
|
||||
|
||||
@@ -220,13 +220,13 @@ func newHandler(config *handlerConfig) (*handler, error) {
|
||||
}
|
||||
h.snapSync.Store(true)
|
||||
log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete")
|
||||
} else if !h.chain.NoTries() && !h.chain.HasState(fullBlock.Number.Int64(), fullBlock.Root) {
|
||||
} else if !h.chain.NoTries() && !h.chain.HasState(fullBlock.Root) {
|
||||
h.snapSync.Store(true)
|
||||
log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing")
|
||||
}
|
||||
} else {
|
||||
head := h.chain.CurrentBlock()
|
||||
if head.Number.Uint64() > 0 && h.chain.HasState(head.Number.Int64(), head.Root) {
|
||||
if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) {
|
||||
// Print warning log if database is not empty to run snap sync.
|
||||
log.Warn("Switch sync mode from snap sync to full sync", "reason", "snap sync complete")
|
||||
} else {
|
||||
|
||||
@@ -55,7 +55,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
|
||||
// The state is available in live database, create a reference
|
||||
// on top to prevent garbage collection and return a release
|
||||
// function to deref it.
|
||||
if statedb, err = eth.blockchain.StateAt(block.Number().Int64(), block.Root()); err == nil {
|
||||
if statedb, err = eth.blockchain.StateAt(block.Root()); err == nil {
|
||||
eth.blockchain.TrieDB().Reference(block.Root(), common.Hash{})
|
||||
return statedb, func() {
|
||||
eth.blockchain.TrieDB().Dereference(block.Root())
|
||||
@@ -185,7 +185,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec u
|
||||
|
||||
func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), error) {
|
||||
// Check if the requested state is available in the live chain.
|
||||
statedb, err := eth.blockchain.StateAt(block.Number().Int64(), block.Root())
|
||||
statedb, err := eth.blockchain.StateAt(block.Root())
|
||||
if err == nil {
|
||||
return statedb, noopReleaser, nil
|
||||
}
|
||||
|
||||
50
eth/sync.go
50
eth/sync.go
@@ -17,7 +17,6 @@
|
||||
package eth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
@@ -192,40 +191,33 @@ func peerToSyncOp(mode downloader.SyncMode, p *eth.Peer) *chainSyncOp {
|
||||
}
|
||||
|
||||
func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, *big.Int) {
|
||||
// If we're in snap sync mode, return that directly
|
||||
if cs.handler.snapSync.Load() {
|
||||
block := cs.handler.chain.CurrentSnapBlock()
|
||||
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
|
||||
return downloader.SnapSync, td
|
||||
}
|
||||
// We are probably in full sync, but we might have rewound to before the
|
||||
// snap sync pivot, check if we should re-enable snap sync.
|
||||
head := cs.handler.chain.CurrentBlock()
|
||||
if cs.handler.chain.TrieDB().Scheme() != rawdb.VersionScheme {
|
||||
// If we're in snap sync mode, return that directly
|
||||
if cs.handler.snapSync.Load() {
|
||||
block := cs.handler.chain.CurrentSnapBlock()
|
||||
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
|
||||
return downloader.SnapSync, td
|
||||
}
|
||||
// We are probably in full sync, but we might have rewound to before the
|
||||
// snap sync pivot, check if we should re-enable snap sync.
|
||||
head = cs.handler.chain.CurrentBlock()
|
||||
if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
|
||||
if head.Number.Uint64() < *pivot {
|
||||
if rawdb.ReadAncientType(cs.handler.database) == rawdb.PruneFreezerType {
|
||||
log.Crit("Current rewound to before the fast sync pivot, can't enable pruneancient mode", "current block number", head.Number.Uint64(), "pivot", *pivot)
|
||||
}
|
||||
block := cs.handler.chain.CurrentSnapBlock()
|
||||
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
|
||||
return downloader.SnapSync, td
|
||||
if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
|
||||
if head.Number.Uint64() < *pivot {
|
||||
if rawdb.ReadAncientType(cs.handler.database) == rawdb.PruneFreezerType {
|
||||
log.Crit("Current rewound to before the fast sync pivot, can't enable pruneancient mode", "current block number", head.Number.Uint64(), "pivot", *pivot)
|
||||
}
|
||||
}
|
||||
// We are in a full sync, but the associated head state is missing. To complete
|
||||
// the head state, forcefully rerun the snap sync. Note it doesn't mean the
|
||||
// persistent state is corrupted, just mismatch with the head block.
|
||||
if !cs.handler.chain.NoTries() && !cs.handler.chain.HasState(head.Number.Int64(), head.Root) {
|
||||
block := cs.handler.chain.CurrentSnapBlock()
|
||||
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
|
||||
log.Info("Reenabled snap sync as chain is stateless")
|
||||
return downloader.SnapSync, td
|
||||
}
|
||||
} else {
|
||||
if !cs.handler.chain.HasState(head.Number.Int64(), head.Root) {
|
||||
panic(fmt.Sprintf("version db not support snap sync, version: %d, root: %s", head.Number.Int64(), head.Root))
|
||||
}
|
||||
}
|
||||
// We are in a full sync, but the associated head state is missing. To complete
|
||||
// the head state, forcefully rerun the snap sync. Note it doesn't mean the
|
||||
// persistent state is corrupted, just mismatch with the head block.
|
||||
if !cs.handler.chain.NoTries() && !cs.handler.chain.HasState(head.Root) {
|
||||
block := cs.handler.chain.CurrentSnapBlock()
|
||||
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
|
||||
log.Info("Reenabled snap sync as chain is stateless")
|
||||
return downloader.SnapSync, td
|
||||
}
|
||||
// Nope, we're really full syncing
|
||||
td := cs.handler.chain.GetTd(head.Hash(), head.Number.Uint64())
|
||||
|
||||
3
go.mod
3
go.mod
@@ -42,7 +42,7 @@ require (
|
||||
github.com/gorilla/websocket v1.5.1
|
||||
github.com/graph-gophers/graphql-go v1.3.0
|
||||
github.com/hashicorp/go-bexpr v0.1.10
|
||||
github.com/hashicorp/golang-lru v1.0.2
|
||||
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
|
||||
github.com/holiman/billy v0.0.0-20240216141850-2abb0c79d3c4
|
||||
github.com/holiman/bloomfilter/v2 v2.0.3
|
||||
github.com/holiman/uint256 v1.3.0
|
||||
@@ -252,6 +252,7 @@ require (
|
||||
github.com/spf13/afero v1.10.0 // indirect
|
||||
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
|
||||
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e // indirect
|
||||
github.com/tidwall/btree v1.7.0 // indirect
|
||||
github.com/tidwall/gjson v1.10.2 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@@ -598,8 +598,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
|
||||
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
|
||||
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
|
||||
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
@@ -1152,6 +1152,8 @@ github.com/tendermint/iavl v0.12.0 h1:xcaFAr+ycqCj7WN1RzL2EfcBioRDOHcU1oWcg83K02
|
||||
github.com/tendermint/iavl v0.12.0/go.mod h1:EoKMMv++tDOL5qKKVnoIqtVPshRrEPeJ0WsgDOLAauM=
|
||||
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e h1:cR8/SYRgyQCt5cNCMniB/ZScMkhI9nk8U5C7SbISXjo=
|
||||
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e/go.mod h1:Tu4lItkATkonrYuvtVjG0/rhy15qrNGNTjPdaphtZ/8=
|
||||
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
|
||||
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
|
||||
github.com/tidwall/gjson v1.10.2 h1:APbLGOM0rrEkd8WBw9C24nllro4ajFuJu0Sc9hRz8Bo=
|
||||
github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
|
||||
@@ -1367,11 +1367,11 @@ func (s *BlockChainAPI) needToReplay(ctx context.Context, block *types.Block, ac
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("block not found for block number (%d): %v", block.NumberU64()-1, err)
|
||||
}
|
||||
parentState, err := s.b.Chain().StateAt(parent.Number().Int64(), parent.Root())
|
||||
parentState, err := s.b.Chain().StateAt(parent.Root())
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("statedb not found for block number (%d): %v", block.NumberU64()-1, err)
|
||||
}
|
||||
currentState, err := s.b.Chain().StateAt(parent.Number().Int64(), block.Root())
|
||||
currentState, err := s.b.Chain().StateAt(block.Root())
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("statedb not found for block number (%d): %v", block.NumberU64(), err)
|
||||
}
|
||||
@@ -1397,7 +1397,7 @@ func (s *BlockChainAPI) replay(ctx context.Context, block *types.Block, accounts
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("block not found for block number (%d): %v", block.NumberU64()-1, err)
|
||||
}
|
||||
statedb, err := s.b.Chain().StateAt(parent.Number().Int64(), parent.Root())
|
||||
statedb, err := s.b.Chain().StateAt(parent.Root())
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("state not found for block number (%d): %v", block.NumberU64()-1, err)
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ func (miner *Miner) Pending() (*types.Block, *state.StateDB) {
|
||||
if block == nil {
|
||||
return nil, nil
|
||||
}
|
||||
stateDb, err := miner.worker.chain.StateAt(block.Number.Int64(), block.Root)
|
||||
stateDb, err := miner.worker.chain.StateAt(block.Root)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -690,7 +690,7 @@ func (w *worker) makeEnv(parent *types.Header, header *types.Header, coinbase co
|
||||
prevEnv *environment) (*environment, error) {
|
||||
// Retrieve the parent state to execute on top and start a prefetcher for
|
||||
// the miner to speed block sealing up a bit
|
||||
state, err := w.chain.StateAt(parent.Number.Int64(), parent.Root)
|
||||
state, err := w.chain.StateAt(parent.Root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -31,11 +31,6 @@ func NewEmptyTrie() *EmptyTrie {
|
||||
return &EmptyTrie{}
|
||||
}
|
||||
|
||||
func (t *EmptyTrie) WriteBatch(values map[string][]byte) error {
|
||||
panic("EmptyTrie not support WriteBatch")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *EmptyTrie) GetKey(shaKey []byte) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -73,11 +73,6 @@ func NewStateTrie(id *ID, db database.Database) (*StateTrie, error) {
|
||||
return &StateTrie{trie: *trie, db: db}, nil
|
||||
}
|
||||
|
||||
func (t *StateTrie) WriteBatch(values map[string][]byte) error {
|
||||
panic("StateTrie not support WriteBatch")
|
||||
return nil
|
||||
}
|
||||
|
||||
// MustGet returns the value for key stored in the trie.
|
||||
// The value bytes must not be modified by the caller.
|
||||
//
|
||||
|
||||
@@ -69,11 +69,6 @@ func NewVerkleTrie(root common.Hash, db database.Database, cache *utils.PointCac
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *VerkleTrie) WriteBatch(values map[string][]byte) error {
|
||||
panic("VerkleTrie not support WriteBatch")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetKey returns the sha3 preimage of a hashed key that was previously used
|
||||
// to store a value.
|
||||
func (t *VerkleTrie) GetKey(key []byte) []byte {
|
||||
|
||||
@@ -22,22 +22,18 @@ type VersionDB struct {
|
||||
|
||||
func New(config *Config) *VersionDB {
|
||||
var (
|
||||
//cfg *versa.VersaDBConfig
|
||||
cfg *versa.VersaDBConfig
|
||||
path = "./node/version_db" // TODO:: debug code
|
||||
)
|
||||
|
||||
//if config != nil {
|
||||
// path = config.Path
|
||||
// cfg = &versa.VersaDBConfig{
|
||||
// FlushInterval: 2000,
|
||||
// MaxStatesInMem: 128,
|
||||
// MemLowWaterMark: 10,
|
||||
// MemHighWaterMark: 20,
|
||||
// MemEvictInternal: 200,
|
||||
// }
|
||||
// _ = cfg
|
||||
//}
|
||||
db, err := versa.NewVersaDB(path, &versa.VersaDBConfig{})
|
||||
if config != nil {
|
||||
path = config.Path
|
||||
cfg = &versa.VersaDBConfig{
|
||||
FlushInterval: config.FlushInterval,
|
||||
MaxStatesInMem: config.MaxStatesInMem,
|
||||
}
|
||||
}
|
||||
db, err := versa.NewVersaDB(path, cfg)
|
||||
if err != nil {
|
||||
log.Crit("failed to new version db", "error", err)
|
||||
}
|
||||
@@ -53,8 +49,7 @@ func (v *VersionDB) Scheme() string {
|
||||
}
|
||||
|
||||
func (v *VersionDB) Initialized(genesisRoot common.Hash) bool {
|
||||
version, _ := v.db.LatestStoreDiskVersionInfo()
|
||||
return version >= 0
|
||||
return v.db.HasState(genesisRoot)
|
||||
}
|
||||
|
||||
func (v *VersionDB) Size() (common.StorageSize, common.StorageSize, common.StorageSize) {
|
||||
|
||||
Reference in New Issue
Block a user