Skip to content

Commit 6306002

Browse files
committed
generate diff layer by replaying block
1 parent 66dd9ea commit 6306002

File tree

3 files changed

+252
-0
lines changed

3 files changed

+252
-0
lines changed

core/blockchain.go

+60
Original file line numberDiff line numberDiff line change
@@ -3105,6 +3105,66 @@ func (bc *BlockChain) GetTrustedDiffLayer(blockHash common.Hash) *types.DiffLaye
31053105
return diff
31063106
}
31073107

3108+
// GenerateDiffLayer generates DiffLayer of a specified block by replaying the block's transactions.
3109+
// If the block is an empty block, no DiffLayer will be generated.
3110+
// The generated DiffLayer whose Receipts are empty, whose DiffAccounts' storage root is empty.
3111+
func (bc *BlockChain) GenerateDiffLayer(blockHash common.Hash) (*types.DiffLayer, error) {
3112+
if bc.snaps == nil {
3113+
return nil, fmt.Errorf("snapshot disabled, can't generate difflayer")
3114+
}
3115+
3116+
block := bc.GetBlockByHash(blockHash)
3117+
if block == nil {
3118+
return nil, fmt.Errorf("block not found, block number: %d, blockhash: %v", block.NumberU64(), blockHash)
3119+
}
3120+
3121+
parent := bc.GetBlockByHash(block.ParentHash())
3122+
if parent == nil {
3123+
return nil, fmt.Errorf("block not found, block number: %d, blockhash: %v", block.NumberU64()-1, block.ParentHash())
3124+
}
3125+
statedb, err := bc.StateAt(parent.Root())
3126+
if err != nil {
3127+
return nil, fmt.Errorf("state not found for block number (%d): %v", parent.NumberU64(), err)
3128+
}
3129+
3130+
// Empty block, no DiffLayer would be generated.
3131+
if block.Header().TxHash == types.EmptyRootHash {
3132+
return nil, nil
3133+
}
3134+
3135+
// Replay transactions.
3136+
signer := types.MakeSigner(bc.Config(), block.Number())
3137+
for _, tx := range block.Transactions() {
3138+
msg, _ := tx.AsMessage(signer)
3139+
txContext := NewEVMTxContext(msg)
3140+
context := NewEVMBlockContext(block.Header(), bc, nil)
3141+
vmenv := vm.NewEVM(context, txContext, statedb, bc.Config(), vm.Config{})
3142+
3143+
if posa, ok := bc.Engine().(consensus.PoSA); ok {
3144+
if isSystem, _ := posa.IsSystemTransaction(tx, block.Header()); isSystem {
3145+
balance := statedb.GetBalance(consensus.SystemAddress)
3146+
if balance.Cmp(common.Big0) > 0 {
3147+
statedb.SetBalance(consensus.SystemAddress, big.NewInt(0))
3148+
statedb.AddBalance(block.Header().Coinbase, balance)
3149+
}
3150+
}
3151+
}
3152+
3153+
if _, err := ApplyMessage(vmenv, msg, new(GasPool).AddGas(tx.Gas())); err != nil {
3154+
return nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
3155+
}
3156+
statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number()))
3157+
}
3158+
3159+
diffLayer := statedb.GenerateDiffLayer()
3160+
if diffLayer != nil {
3161+
diffLayer.BlockHash = blockHash
3162+
diffLayer.Number = block.NumberU64()
3163+
}
3164+
3165+
return diffLayer, nil
3166+
}
3167+
31083168
func GetTrustedDiffHash(d *types.DiffLayer) (common.Hash, error) {
31093169
diff := &types.ExtDiffLayer{
31103170
BlockHash: d.BlockHash,

core/blockchain_diff_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"testing"
2828
"time"
2929

30+
"github.com/ethereum/go-ethereum/consensus/clique"
31+
3032
"golang.org/x/crypto/sha3"
3133

3234
"github.com/ethereum/go-ethereum/common"
@@ -644,3 +646,118 @@ func TestGetRootByDiffHash(t *testing.T) {
644646
testGetRootByDiffHash(t, chain1, chain2, 24, types.StatusBlockNewer)
645647
testGetRootByDiffHash(t, chain1, chain2, 35, types.StatusBlockTooNew)
646648
}
649+
650+
func newBlockChainWithCliqueEngine(blocks int) *BlockChain {
651+
signer := types.HomesteadSigner{}
652+
db := rawdb.NewMemoryDatabase()
653+
engine := clique.New(params.AllCliqueProtocolChanges.Clique, db)
654+
genspec := &Genesis{
655+
//Config: params.TestChainConfig,
656+
ExtraData: make([]byte, 32+common.AddressLength+65),
657+
Alloc: GenesisAlloc{testAddr: {Balance: big.NewInt(100000000000000000)}},
658+
}
659+
copy(genspec.ExtraData[32:], testAddr[:])
660+
genesis := genspec.MustCommit(db)
661+
662+
chain, _ := NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil)
663+
generator := func(i int, block *BlockGen) {
664+
// The chain maker doesn't have access to a chain, so the difficulty will be
665+
// lets unset (nil). Set it here to the correct value.
666+
// block.SetCoinbase(testAddr)
667+
block.SetDifficulty(big.NewInt(2))
668+
669+
for idx, testBlock := range testBlocks {
670+
// Specific block setting, the index in this generator has 1 diff from specified blockNr.
671+
if i+1 == testBlock.blockNr {
672+
for _, testTransaction := range testBlock.txs {
673+
var transaction *types.Transaction
674+
if testTransaction.to == nil {
675+
transaction = types.NewContractCreation(block.TxNonce(testAddr),
676+
testTransaction.value, uint64(commonGas), nil, testTransaction.data)
677+
} else {
678+
transaction = types.NewTransaction(block.TxNonce(testAddr), *testTransaction.to,
679+
testTransaction.value, uint64(commonGas), nil, testTransaction.data)
680+
}
681+
tx, err := types.SignTx(transaction, signer, testKey)
682+
if err != nil {
683+
panic(err)
684+
}
685+
block.AddTxWithChain(chain, tx)
686+
}
687+
break
688+
}
689+
690+
// Default block setting.
691+
if idx == len(testBlocks)-1 {
692+
// We want to simulate an empty middle block, having the same state as the
693+
// first one. The last is needs a state change again to force a reorg.
694+
for _, testTransaction := range testBlocks[0].txs {
695+
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), *testTransaction.to,
696+
testTransaction.value, uint64(commonGas), nil, testTransaction.data), signer, testKey)
697+
if err != nil {
698+
panic(err)
699+
}
700+
block.AddTxWithChain(chain, tx)
701+
}
702+
}
703+
}
704+
705+
}
706+
bs, _ := GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, blocks, generator)
707+
for i, block := range bs {
708+
header := block.Header()
709+
if i > 0 {
710+
header.ParentHash = bs[i-1].Hash()
711+
}
712+
header.Extra = make([]byte, 32+65)
713+
header.Difficulty = big.NewInt(2)
714+
715+
sig, _ := crypto.Sign(clique.SealHash(header).Bytes(), testKey)
716+
copy(header.Extra[len(header.Extra)-65:], sig)
717+
bs[i] = block.WithSeal(header)
718+
}
719+
720+
if _, err := chain.InsertChain(bs); err != nil {
721+
panic(err)
722+
}
723+
724+
return chain
725+
}
726+
727+
func TestGenerateDiffLayer(t *testing.T) {
728+
blockNum := 32
729+
chain := newBlockChainWithCliqueEngine(blockNum)
730+
defer chain.Stop()
731+
732+
for blockNr := 1; blockNr <= blockNum; blockNr++ {
733+
block := chain.GetBlockByNumber(uint64(blockNr))
734+
if block == nil {
735+
t.Fatal("block should not be nil")
736+
}
737+
738+
expDiffLayer := chain.GetTrustedDiffLayer(block.Hash())
739+
if expDiffLayer == nil {
740+
// Skip empty block.
741+
if blockNr == 15 {
742+
continue
743+
}
744+
t.Fatalf("unexpected nil diff layer, block number: %v, block hash: %v", blockNr, block.Hash())
745+
}
746+
expDiffHash, err := GetTrustedDiffHash(expDiffLayer)
747+
if err != nil {
748+
t.Fatalf("compute diff hash failed: %v", err)
749+
}
750+
751+
diffLayer, err := chain.GenerateDiffLayer(block.Hash())
752+
if err != nil || diffLayer == nil {
753+
t.Fatalf("generate diff layer failed: %v", err)
754+
}
755+
diffHash, err := GetTrustedDiffHash(diffLayer)
756+
if err != nil {
757+
t.Fatalf("compute diff hash failed: %v", err)
758+
}
759+
if expDiffHash != diffHash {
760+
t.Fatalf("generated wrong diff layer for block: %d, expected hash: %v, real hash: %v", blockNr, expDiffHash, diffHash)
761+
}
762+
}
763+
}

core/state/statedb.go

+75
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,81 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, *types.DiffLayer
14011401
return root, diffLayer, nil
14021402
}
14031403

1404+
// GenerateDiffLayer generates block's DiffLayer after executing the block's txs.
1405+
// Attention, the DiffLayer returned include no Receipts, whose accounts' storage root
1406+
// is empty, whose BlockHash and Number field is empty, should further process by caller.
1407+
func (s *StateDB) GenerateDiffLayer() *types.DiffLayer {
1408+
if s.snap == nil {
1409+
return nil
1410+
}
1411+
1412+
for addr := range s.stateObjectsPending {
1413+
if obj := s.stateObjects[addr]; !obj.deleted {
1414+
// The snapshot storage map for the object
1415+
var storage map[string][]byte
1416+
obj.finalise(false)
1417+
for key, value := range obj.pendingStorage {
1418+
// Skip noop changes, persist actual changes
1419+
if value == obj.originStorage[key] {
1420+
continue
1421+
}
1422+
obj.originStorage[key] = value
1423+
1424+
var v []byte
1425+
if (value != common.Hash{}) {
1426+
// Encoding []byte cannot fail, ok to ignore the error.
1427+
v, _ = rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
1428+
}
1429+
1430+
obj.db.snapMux.Lock()
1431+
if storage == nil {
1432+
// Retrieve the old storage map, if available, create a new one otherwise
1433+
if storage = obj.db.snapStorage[obj.address]; storage == nil {
1434+
storage = make(map[string][]byte)
1435+
obj.db.snapStorage[obj.address] = storage
1436+
}
1437+
}
1438+
storage[string(key[:])] = v // v will be nil if value is 0x00
1439+
obj.db.snapMux.Unlock()
1440+
}
1441+
1442+
if !obj.deleted {
1443+
s.snapMux.Lock()
1444+
// The storage root hasn't been intermediate, pass empty storage root here.
1445+
s.snapAccounts[obj.address] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, common.Hash{}, obj.data.CodeHash)
1446+
s.snapMux.Unlock()
1447+
}
1448+
}
1449+
}
1450+
1451+
var diffLayer = &types.DiffLayer{}
1452+
for addr := range s.stateObjectsDirty {
1453+
if obj := s.stateObjects[addr]; !obj.deleted {
1454+
if obj.code != nil && obj.dirtyCode {
1455+
diffLayer.Codes = append(diffLayer.Codes, types.DiffCode{
1456+
Hash: common.BytesToHash(obj.CodeHash()),
1457+
Code: obj.code,
1458+
})
1459+
}
1460+
}
1461+
}
1462+
1463+
diffLayer.Destructs, diffLayer.Accounts, diffLayer.Storages = s.SnapToDiffLayer()
1464+
sort.SliceStable(diffLayer.Codes, func(i, j int) bool {
1465+
return diffLayer.Codes[i].Hash.Hex() < diffLayer.Codes[j].Hash.Hex()
1466+
})
1467+
sort.SliceStable(diffLayer.Destructs, func(i, j int) bool {
1468+
return diffLayer.Destructs[i].Hex() < (diffLayer.Destructs[j].Hex())
1469+
})
1470+
sort.SliceStable(diffLayer.Accounts, func(i, j int) bool {
1471+
return diffLayer.Accounts[i].Account.Hex() < diffLayer.Accounts[j].Account.Hex()
1472+
})
1473+
sort.SliceStable(diffLayer.Storages, func(i, j int) bool {
1474+
return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex()
1475+
})
1476+
return diffLayer
1477+
}
1478+
14041479
func (s *StateDB) DiffLayerToSnap(diffLayer *types.DiffLayer) (map[common.Address]struct{}, map[common.Address][]byte, map[common.Address]map[string][]byte, error) {
14051480
snapDestructs := make(map[common.Address]struct{})
14061481
snapAccounts := make(map[common.Address][]byte)

0 commit comments

Comments
 (0)