add genesis block to test; handle block 0 in StateDiffAt

This commit is contained in:
Ian Norden 2020-05-07 18:39:56 -05:00
parent d07dc803d2
commit 8c9ac32245
3 changed files with 92 additions and 10 deletions

View File

@ -41,6 +41,7 @@ var (
miningReward = int64(2000000000000000000)
minerAddress = common.HexToAddress("0x0")
minerLeafKey = testhelpers.AddressToLeafKey(minerAddress)
nullHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
balanceChange10000 = int64(10000)
balanceChange1000 = int64(1000)
@ -72,6 +73,16 @@ var (
common.Hex2Bytes("305787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace"),
newStorageValue,
})
bankAccountAtBlock0, _ = rlp.EncodeToBytes(state.Account{
Nonce: nonce0,
Balance: big.NewInt(testhelpers.TestBankFunds.Int64()),
CodeHash: common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").Bytes(),
Root: common.HexToHash(originalContractRoot),
})
bankAccountAtBlock0LeafNode, _ = rlp.EncodeToBytes([]interface{}{
common.Hex2Bytes("2000bf49f440a1cd0527e4d06e2765654c0f56452257516d793a9b8d604dcfdf2a"),
bankAccountAtBlock0,
})
account1AtBlock1, _ = rlp.EncodeToBytes(state.Account{
Nonce: nonce0,
Balance: big.NewInt(balanceChange10000),
@ -302,7 +313,31 @@ func TestBuilder1(t *testing.T) {
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock0",
//10000 transferred from testBankAddress to account1Addr
arguments{
oldStateRoot: nullHash,
newStateRoot: block0.Root(),
blockNumber: block0.Number(),
blockHash: block0.Hash(),
},
&statediff.StateDiff{
BlockNumber: block0.Number(),
BlockHash: block0.Hash(),
CreatedAccounts: []statediff.AccountDiff{
{
Path: []byte{},
NodeType: statediff.Leaf,
LeafKey: testhelpers.BankLeafKey,
NodeValue: bankAccountAtBlock0LeafNode,
Storage: []statediff.StorageDiff{},
},
},
DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock1",
//10000 transferred from testBankAddress to account1Addr
@ -462,6 +497,9 @@ func TestBuilder1(t *testing.T) {
}
for _, test := range tests {
if test.name != "testBlock0" {
continue
}
arguments := test.startingArguments
diff, err := builder.BuildStateDiff(arguments.oldStateRoot, arguments.newStateRoot, arguments.blockNumber, arguments.blockHash)
if err != nil {
@ -518,7 +556,31 @@ func TestBuilderWithIntermediateNodes(t *testing.T) {
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock0",
//10000 transferred from testBankAddress to account1Addr
arguments{
oldStateRoot: nullHash,
newStateRoot: block0.Root(),
blockNumber: block0.Number(),
blockHash: block0.Hash(),
},
&statediff.StateDiff{
BlockNumber: block0.Number(),
BlockHash: block0.Hash(),
CreatedAccounts: []statediff.AccountDiff{
{
Path: []byte{},
NodeType: statediff.Leaf,
LeafKey: testhelpers.BankLeafKey,
NodeValue: bankAccountAtBlock0LeafNode,
Storage: []statediff.StorageDiff{},
},
},
DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock1",
//10000 transferred from testBankAddress to account1Addr
@ -758,6 +820,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock0",
//10000 transferred from testBankAddress to account1Addr
arguments{
oldStateRoot: nullHash,
newStateRoot: block0.Root(),
blockNumber: block0.Number(),
blockHash: block0.Hash(),
},
&statediff.StateDiff{
BlockNumber: block0.Number(),
BlockHash: block0.Hash(),
CreatedAccounts: emptyAccountDiffEventualMap,
DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: emptyAccountDiffIncrementalMap,
},
},
{
"testBlock1",
//10000 transferred from testBankAddress to account1Addr

View File

@ -136,7 +136,7 @@ func (sds *Service) Loop(chainEventCh chan core.ChainEvent) {
log.Error(fmt.Sprintf("Parent block is nil, skipping this block (%d)", currentBlock.Number()))
continue
}
payload, err := sds.processStateDiff(currentBlock, parentBlock)
payload, err := sds.processStateDiff(currentBlock, parentBlock.Root())
if err != nil {
log.Error(fmt.Sprintf("Error building statediff for block %d; error: ", currentBlock.Number()) + err.Error())
continue
@ -155,8 +155,8 @@ func (sds *Service) Loop(chainEventCh chan core.ChainEvent) {
}
// processStateDiff method builds the state diff payload from the current and parent block before sending it to listening subscriptions
func (sds *Service) processStateDiff(currentBlock, parentBlock *types.Block) (*Payload, error) {
stateDiff, err := sds.Builder.BuildStateDiff(parentBlock.Root(), currentBlock.Root(), currentBlock.Number(), currentBlock.Hash())
func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot common.Hash) (*Payload, error) {
stateDiff, err := sds.Builder.BuildStateDiff(parentRoot, currentBlock.Root(), currentBlock.Number(), currentBlock.Hash())
if err != nil {
return nil, err
}
@ -280,7 +280,10 @@ func (sds *Service) close() {
// This operation cannot be performed back past the point of db pruning; it requires an archival node
func (sds *Service) StateDiffAt(blockNumber uint64) (*Payload, error) {
currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber)
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
log.Info(fmt.Sprintf("sending state diff at %d", blockNumber))
return sds.processStateDiff(currentBlock, parentBlock)
if blockNumber == 0 {
return sds.processStateDiff(currentBlock, common.Hash{})
}
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
return sds.processStateDiff(currentBlock, parentBlock.Root())
}

View File

@ -55,9 +55,9 @@ var (
testRoot1 = common.HexToHash("0x03")
testRoot2 = common.HexToHash("0x04")
testRoot3 = common.HexToHash("0x04")
header1 = types.Header{ParentHash: parentHash1, Root: testRoot1}
header2 = types.Header{ParentHash: parentHash2, Root: testRoot2}
header3 = types.Header{ParentHash: common.HexToHash("parent hash"), Root: testRoot3}
header1 = types.Header{ParentHash: parentHash1, Root: testRoot1, Number: big.NewInt(1)}
header2 = types.Header{ParentHash: parentHash2, Root: testRoot2, Number: big.NewInt(2)}
header3 = types.Header{ParentHash: common.HexToHash("parent hash"), Root: testRoot3, Number: big.NewInt(3)}
testBlock1 = types.NewBlock(&header1, nil, nil, nil)
testBlock2 = types.NewBlock(&header2, nil, nil, nil)