Skip to content

Commit

Permalink
add unit testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Jan 20, 2022
1 parent 03ade41 commit 8913b09
Show file tree
Hide file tree
Showing 28 changed files with 324 additions and 169 deletions.
16 changes: 12 additions & 4 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
if chainConfig.IsByzantium(vmContext.BlockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
stateRoot, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
if err != nil {
return nil, nil, err
}
root = stateRoot.Bytes()
}

// Create a new receipt for the transaction, storing the intermediate root and
Expand Down Expand Up @@ -197,7 +201,11 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,

txIndex++
}
statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))

_, err := statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
if err != nil {
return nil, nil, err
}
// Add mining reward?
if miningReward > 0 {
// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
Expand All @@ -223,7 +231,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
statedb.AddBalance(pre.Env.Coinbase, minerReward)
}
// Commit block
root, _, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber))
root, _, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber), nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not commit state: %v", err)
return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
Expand Down Expand Up @@ -252,7 +260,7 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB
}
}
// Commit and re-open to start with a clean state.
root, _, _ := statedb.Commit(false)
root, _, _ := statedb.Commit(false, nil)
statedb, _ = state.New(root, sdb, nil)
return statedb
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ func runCmd(ctx *cli.Context) error {
output, leftOverGas, stats, err := timedExec(bench, execFunc)

if ctx.GlobalBool(DumpFlag.Name) {
statedb.Commit(true)
statedb.IntermediateRoot(true)
statedb.Commit(true, nil)
_, err := statedb.IntermediateRoot(true)
if err != nil {
return err
}
fmt.Println(string(statedb.Dump(false, false, true)))
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ func stateTestCmd(ctx *cli.Context) error {
_, state, err := test.Run(st, cfg, false)
// print state root for evmlab tracing
if ctx.GlobalBool(MachineFlag.Name) && state != nil {
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", state.IntermediateRoot(false))
root, err := state.IntermediateRoot(false)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", root)
}
if err != nil {
// Test failed, mark as so and dump any state to aid debugging
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var (
}
PipeCommitFlag = cli.BoolFlag{
Name: "pipecommit",
Usage: "Enable pipeline commit feature",
Usage: "Enable MPT pipeline commit, it will improve syncing performance. It is an experimental feature",
}
RangeLimitFlag = cli.BoolFlag{
Name: "rangelimit",
Expand Down
8 changes: 6 additions & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction, uncles []*types.Header,
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
return
}
Expand All @@ -560,7 +560,11 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB,
txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error) {
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
var err error
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
if err != nil {
return nil, nil, err
}
header.UncleHash = types.CalcUncleHash(nil)

// Assemble and return the final block for sealing
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
receipts *[]*types.Receipt, _ *[]*types.Transaction, _ *uint64) (err error) {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header, uncles)
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.Root, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
return
}

Expand Down
10 changes: 7 additions & 3 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
rootHash = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
rootHash, err = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
wg.Done()
}()
go func() {
Expand All @@ -779,7 +779,7 @@ func (p *Parlia) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
wg.Wait()
blk.SetRoot(rootHash)
// Assemble and return the final block for sealing
return blk, receipts, nil
return blk, receipts, err
}

// Authorize injects a private key into the consensus engine to mint new blocks
Expand Down Expand Up @@ -1211,7 +1211,11 @@ func (p *Parlia) applyTransaction(
if p.chainConfig.IsByzantium(header.Number) {
state.Finalise(true)
} else {
root = state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number)).Bytes()
stateRoot, err := state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number))
if err != nil {
return err
}
root = stateRoot.Bytes()
}
*usedGas += gasUsed
receipt := types.NewReceipt(root, false, *usedGas)
Expand Down
6 changes: 3 additions & 3 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
return ErrKnownBlock
}
if v.bc.isCachedBadBlock(block) {
return errStateRootVerificationFailed
return ErrKnownBadBlock
}
// Header validity is known at this point, check the uncles and transactions
header := block.Header()
Expand Down Expand Up @@ -138,8 +138,8 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
}
if !skipHeavyVerify {
validateFuns = append(validateFuns, func() error {
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x)", header.Root, root)
if root, err := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root || err != nil {
return fmt.Errorf("invalid merkle root (remote: %x local: %x), err %v", header.Root, root, err)
} else {
return nil
}
Expand Down
14 changes: 8 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
}
go bc.untrustedDiffLayerPruneLoop()
if bc.pipeCommit {
// check current block and rewind invalid one
go bc.rewindInvalidHeaderBlockLoop()
}
return bc, nil
Expand Down Expand Up @@ -595,8 +596,10 @@ func (bc *BlockChain) tryRewindBadBlocks() {
block := bc.CurrentBlock()
snaps := bc.snaps
// Verified and Result is false
if snaps != nil && snaps.Snapshot(block.Root()).Verified() && !snaps.Snapshot(block.Root()).WaitVerified() {
if snaps != nil && snaps.Snapshot(block.Root()) != nil &&
snaps.Snapshot(block.Root()).Verified() && !snaps.Snapshot(block.Root()).WaitVerified() {
// Rewind by one block
log.Warn("current block verified failed, rewind to its parent", "height", block.NumberU64(), "hash", block.Hash())
bc.futureBlocks.Remove(block.Hash())
bc.badBlockCache.Add(block.Hash(), time.Now())
bc.reportBlock(block, nil, errStateRootVerificationFailed)
Expand Down Expand Up @@ -1086,8 +1089,8 @@ func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool {

// HasState checks if state trie is fully present in the database or not.
func (bc *BlockChain) HasState(hash common.Hash) bool {
if bc.snaps != nil {
// If parent snap is pending on verified, treat it as state exist
if bc.pipeCommit && bc.snaps != nil {
// If parent snap is pending on verification, treat it as state exist
if s := bc.snaps.Snapshot(hash); s != nil && !s.Verified() {
return true
}
Expand Down Expand Up @@ -1774,7 +1777,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}

// Commit all cached state changes into underlying memory database.
_, diffLayer, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()), tryCommitTrieDB)
_, diffLayer, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()), bc.tryRewindBadBlocks, tryCommitTrieDB)
if err != nil {
return NonStatTy, err
}
Expand Down Expand Up @@ -2138,8 +2141,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// Validate the state using the default validator
substart = time.Now()
if !statedb.IsLightProcessed() {
// Do the state root verification asynchronously
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas, true); err != nil {
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas, bc.pipeCommit); err != nil {
log.Error("validate state failed", "error", err)
bc.reportBlock(block, receipts, err)
return it.index, err
Expand Down
Loading

0 comments on commit 8913b09

Please sign in to comment.