update api_test.go

This commit is contained in:
Ian Norden 2020-05-14 16:14:40 -05:00
parent ea9a8feb81
commit e7b78dae41

View File

@ -79,6 +79,12 @@ var (
bankAccount, bankAccount,
}) })
mockTotalDifficulty = big.NewInt(1337) mockTotalDifficulty = big.NewInt(1337)
params = statediff.Params{
IntermediateStateNodes: false,
IncludeTD: true,
IncludeBlock: true,
IncludeReceipts: true,
}
) )
func TestAPI(t *testing.T) { func TestAPI(t *testing.T) {
@ -102,7 +108,7 @@ func testSubscriptionAPI(t *testing.T) {
expectedStateDiff := statediff.StateDiff{ expectedStateDiff := statediff.StateDiff{
BlockNumber: block1.Number(), BlockNumber: block1.Number(),
BlockHash: block1.Hash(), BlockHash: block1.Hash(),
CreatedNodes: []statediff.StateNode{ Nodes: []statediff.StateNode{
{ {
Path: []byte{'\x05'}, Path: []byte{'\x05'},
NodeType: statediff.Leaf, NodeType: statediff.Leaf,
@ -125,42 +131,29 @@ func testSubscriptionAPI(t *testing.T) {
StorageDiffs: emptyStorage, StorageDiffs: emptyStorage,
}, },
}, },
DeletedNodes: []statediff.StateNode{ // This leaf appears to be deleted since it is turned into a branch node
{ // It would instead show up in the UpdateAccounts as new branch node IF intermediate node diffing was turned on (as it is in the test below)
Path: []byte{},
NodeType: statediff.Removed,
LeafKey: testhelpers.BankLeafKey,
NodeValue: []byte{},
StorageDiffs: emptyStorage,
},
},
UpdatedNodes: emptyAccounts,
} }
expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff) expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff)
blockChan := make(chan *types.Block) blockChan := make(chan *types.Block)
parentBlockChain := make(chan *types.Block) parentBlockChain := make(chan *types.Block)
serviceQuitChan := make(chan bool) serviceQuitChan := make(chan bool)
config := statediff.Config{
IntermediateNodes: false,
}
mockBlockChain := &BlockChain{} mockBlockChain := &BlockChain{}
mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt}) mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt})
mockBlockChain.SetTdByHash(block1Hash, mockTotalDifficulty) mockBlockChain.SetTdByHash(block1Hash, mockTotalDifficulty)
mockService := MockStateDiffService{ mockService := MockStateDiffService{
Mutex: sync.Mutex{}, Mutex: sync.Mutex{},
Builder: statediff.NewBuilder(chain, config), Builder: statediff.NewBuilder(chain.StateCache()),
BlockChan: blockChan, BlockChan: blockChan,
BlockChain: mockBlockChain, BlockChain: mockBlockChain,
ParentBlockChan: parentBlockChain, ParentBlockChan: parentBlockChain,
QuitChan: serviceQuitChan, QuitChan: serviceQuitChan,
Subscriptions: make(map[rpc.ID]statediff.Subscription), Subscriptions: make(map[common.Hash]map[rpc.ID]statediff.Subscription),
streamBlock: true, SubscriptionTypes: make(map[common.Hash]statediff.Params),
} }
mockService.Start(nil) mockService.Start(nil)
id := rpc.NewID() id := rpc.NewID()
payloadChan := make(chan statediff.Payload) payloadChan := make(chan statediff.Payload)
quitChan := make(chan bool) quitChan := make(chan bool)
mockService.Subscribe(id, payloadChan, quitChan) mockService.Subscribe(id, payloadChan, quitChan, params)
blockChan <- block1 blockChan <- block1
parentBlockChain <- block0 parentBlockChain <- block0
@ -201,7 +194,7 @@ func testHTTPAPI(t *testing.T) {
expectedStateDiff := statediff.StateDiff{ expectedStateDiff := statediff.StateDiff{
BlockNumber: block1.Number(), BlockNumber: block1.Number(),
BlockHash: block1.Hash(), BlockHash: block1.Hash(),
CreatedNodes: []statediff.StateNode{ Nodes: []statediff.StateNode{
{ {
Path: []byte{'\x05'}, Path: []byte{'\x05'},
NodeType: statediff.Leaf, NodeType: statediff.Leaf,
@ -224,33 +217,19 @@ func testHTTPAPI(t *testing.T) {
StorageDiffs: emptyStorage, StorageDiffs: emptyStorage,
}, },
}, },
DeletedNodes: []statediff.StateNode{ // This leaf appears to be deleted since it is turned into a branch node
{ // It would instead show up in the UpdateAccounts as new branch node IF intermediate node diffing was turned on (as it is in the test below)
Path: []byte{},
NodeType: statediff.Removed,
LeafKey: testhelpers.BankLeafKey,
NodeValue: []byte{},
StorageDiffs: emptyStorage,
},
},
UpdatedNodes: emptyAccounts,
} }
expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff) expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff)
config := statediff.Config{
IntermediateNodes: false,
}
mockBlockChain := &BlockChain{} mockBlockChain := &BlockChain{}
mockBlockChain.SetBlocksForHashes(blockMap) mockBlockChain.SetBlocksForHashes(blockMap)
mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64()) mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64())
mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt}) mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt})
mockBlockChain.SetTdByHash(block1Hash, big.NewInt(1337)) mockBlockChain.SetTdByHash(block1Hash, big.NewInt(1337))
mockService := MockStateDiffService{ mockService := MockStateDiffService{
Mutex: sync.Mutex{}, Mutex: sync.Mutex{},
Builder: statediff.NewBuilder(chain, config), Builder: statediff.NewBuilder(chain.StateCache()),
BlockChain: mockBlockChain, BlockChain: mockBlockChain,
streamBlock: true,
} }
payload, err := mockService.StateDiffAt(block1.Number().Uint64()) payload, err := mockService.StateDiffAt(block1.Number().Uint64(), params)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }