cleanup chain maker

This commit is contained in:
Ian Norden 2020-05-14 22:46:21 -05:00
parent c4529a1139
commit 859e18aea4
3 changed files with 37 additions and 49 deletions

View File

@ -267,13 +267,13 @@ var (
) )
func TestBuilder(t *testing.T) { func TestBuilder(t *testing.T) {
BlockHashes, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(3, testhelpers.Genesis)
contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr) contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr)
defer chain.Stop() defer chain.Stop()
block0 = blockMap[BlockHashes[3]] block0 = testhelpers.Genesis
block1 = blockMap[BlockHashes[2]] block1 = blocks[0]
block2 = blockMap[BlockHashes[1]] block2 = blocks[1]
block3 = blockMap[BlockHashes[0]] block3 = blocks[2]
params := statediff.Params{} params := statediff.Params{}
builder = statediff.NewBuilder(chain.StateCache()) builder = statediff.NewBuilder(chain.StateCache())
@ -492,13 +492,13 @@ func TestBuilder(t *testing.T) {
} }
func TestBuilderWithIntermediateNodes(t *testing.T) { func TestBuilderWithIntermediateNodes(t *testing.T) {
BlockHashes, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(3, testhelpers.Genesis)
contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr) contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr)
defer chain.Stop() defer chain.Stop()
block0 = blockMap[BlockHashes[3]] block0 = testhelpers.Genesis
block1 = blockMap[BlockHashes[2]] block1 = blocks[0]
block2 = blockMap[BlockHashes[1]] block2 = blocks[1]
block3 = blockMap[BlockHashes[0]] block3 = blocks[2]
params := statediff.Params{ params := statediff.Params{
IntermediateStateNodes: true, IntermediateStateNodes: true,
IntermediateStorageNodes: true, IntermediateStorageNodes: true,
@ -743,13 +743,13 @@ func TestBuilderWithIntermediateNodes(t *testing.T) {
} }
func TestBuilderWithWatchedAddressList(t *testing.T) { func TestBuilderWithWatchedAddressList(t *testing.T) {
BlockHashes, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(3, testhelpers.Genesis)
contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr) contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr)
defer chain.Stop() defer chain.Stop()
block0 = blockMap[BlockHashes[3]] block0 = testhelpers.Genesis
block1 = blockMap[BlockHashes[2]] block1 = blocks[0]
block2 = blockMap[BlockHashes[1]] block2 = blocks[1]
block3 = blockMap[BlockHashes[0]] block3 = blocks[2]
params := statediff.Params{ params := statediff.Params{
WatchedAddresses: []common.Address{testhelpers.Account1Addr, testhelpers.ContractAddr}, WatchedAddresses: []common.Address{testhelpers.Account1Addr, testhelpers.ContractAddr},
} }
@ -912,13 +912,13 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
} }
func TestBuilderWithWatchedAddressAndStorageKeyList(t *testing.T) { func TestBuilderWithWatchedAddressAndStorageKeyList(t *testing.T) {
BlockHashes, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(3, testhelpers.Genesis)
contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr) contractLeafKey = testhelpers.AddressToLeafKey(testhelpers.ContractAddr)
defer chain.Stop() defer chain.Stop()
block0 = blockMap[BlockHashes[3]] block0 = testhelpers.Genesis
block1 = blockMap[BlockHashes[2]] block1 = blocks[0]
block2 = blockMap[BlockHashes[1]] block2 = blocks[1]
block3 = blockMap[BlockHashes[0]] block3 = blocks[2]
params := statediff.Params{ params := statediff.Params{
WatchedAddresses: []common.Address{testhelpers.Account1Addr, testhelpers.ContractAddr}, WatchedAddresses: []common.Address{testhelpers.Account1Addr, testhelpers.ContractAddr},
WatchedStorageSlots: []common.Hash{originalStorageKey}, WatchedStorageSlots: []common.Hash{originalStorageKey},

View File

@ -29,22 +29,11 @@ import (
) )
// MakeChain creates a chain of n blocks starting at and including parent. // MakeChain creates a chain of n blocks starting at and including parent.
// the returned hash chain is ordered head->parent. In addition, every 3rd block // the returned hash chain is ordered head->parent.
// contains a transaction and every 5th an uncle to allow testing correct block func MakeChain(n int, parent *types.Block) ([]*types.Block, *core.BlockChain) {
// reassembly.
func MakeChain(n int, parent *types.Block) ([]common.Hash, map[common.Hash]*types.Block, *core.BlockChain) {
blocks, _ := core.GenerateChain(params.TestChainConfig, parent, ethash.NewFaker(), Testdb, n, testChainGen) blocks, _ := core.GenerateChain(params.TestChainConfig, parent, ethash.NewFaker(), Testdb, n, testChainGen)
chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil) chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil)
return blocks, chain
hashes := make([]common.Hash, n+1)
hashes[len(hashes)-1] = parent.Hash()
blockm := make(map[common.Hash]*types.Block, n+1)
blockm[parent.Hash()] = parent
for i, b := range blocks {
hashes[len(hashes)-i-2] = b.Hash()
blockm[b.Hash()] = b
}
return hashes, blockm, chain
} }
func testChainGen(i int, block *core.BlockGen) { func testChainGen(i int, block *core.BlockGen) {

View File

@ -81,12 +81,10 @@ func TestAPI(t *testing.T) {
} }
func testSubscriptionAPI(t *testing.T) { func testSubscriptionAPI(t *testing.T) {
_, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(1, testhelpers.Genesis)
defer chain.Stop() defer chain.Stop()
block0Hash := common.HexToHash("0xd1721cfd0b29c36fd7a68f25c128e86413fb666a6e1d68e89b875bd299262661") block0 = testhelpers.Genesis
block1Hash := common.HexToHash("0xbbe88de60ba33a3f18c0caa37d827bfb70252e19e40a07cd34041696c35ecb1a") block1 = blocks[0]
block0 = blockMap[block0Hash]
block1 = blockMap[block1Hash]
expectedBlockRlp, _ := rlp.EncodeToBytes(block1) expectedBlockRlp, _ := rlp.EncodeToBytes(block1)
mockReceipt := &types.Receipt{ mockReceipt := &types.Receipt{
BlockNumber: block1.Number(), BlockNumber: block1.Number(),
@ -125,8 +123,8 @@ func testSubscriptionAPI(t *testing.T) {
parentBlockChain := make(chan *types.Block) parentBlockChain := make(chan *types.Block)
serviceQuitChan := make(chan bool) serviceQuitChan := make(chan bool)
mockBlockChain := &BlockChain{} mockBlockChain := &BlockChain{}
mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt}) mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt})
mockBlockChain.SetTdByHash(block1Hash, mockTotalDifficulty) mockBlockChain.SetTdByHash(block1.Hash(), mockTotalDifficulty)
mockService := MockStateDiffService{ mockService := MockStateDiffService{
Mutex: sync.Mutex{}, Mutex: sync.Mutex{},
Builder: statediff.NewBuilder(chain.StateCache()), Builder: statediff.NewBuilder(chain.StateCache()),
@ -167,12 +165,10 @@ func testSubscriptionAPI(t *testing.T) {
} }
func testHTTPAPI(t *testing.T) { func testHTTPAPI(t *testing.T) {
_, blockMap, chain := testhelpers.MakeChain(3, testhelpers.Genesis) blocks, chain := testhelpers.MakeChain(1, testhelpers.Genesis)
defer chain.Stop() defer chain.Stop()
block0Hash := common.HexToHash("0xd1721cfd0b29c36fd7a68f25c128e86413fb666a6e1d68e89b875bd299262661") block0 = testhelpers.Genesis
block1Hash := common.HexToHash("0xbbe88de60ba33a3f18c0caa37d827bfb70252e19e40a07cd34041696c35ecb1a") block1 = blocks[0]
block0 = blockMap[block0Hash]
block1 = blockMap[block1Hash]
expectedBlockRlp, _ := rlp.EncodeToBytes(block1) expectedBlockRlp, _ := rlp.EncodeToBytes(block1)
mockReceipt := &types.Receipt{ mockReceipt := &types.Receipt{
BlockNumber: block1.Number(), BlockNumber: block1.Number(),
@ -208,10 +204,13 @@ func testHTTPAPI(t *testing.T) {
} }
expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff) expectedStateDiffBytes, _ := rlp.EncodeToBytes(expectedStateDiff)
mockBlockChain := &BlockChain{} mockBlockChain := &BlockChain{}
mockBlockChain.SetBlocksForHashes(blockMap) mockBlockChain.SetBlocksForHashes(map[common.Hash]*types.Block{
block0.Hash(): block0,
block1.Hash(): block1,
})
mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64()) mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64())
mockBlockChain.SetReceiptsForHash(block1Hash, types.Receipts{mockReceipt}) mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt})
mockBlockChain.SetTdByHash(block1Hash, big.NewInt(1337)) mockBlockChain.SetTdByHash(block1.Hash(), big.NewInt(1337))
mockService := MockStateDiffService{ mockService := MockStateDiffService{
Mutex: sync.Mutex{}, Mutex: sync.Mutex{},
Builder: statediff.NewBuilder(chain.StateCache()), Builder: statediff.NewBuilder(chain.StateCache()),