From 13fde78c62e29bc16ef540f9b233097c657c0606 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Mon, 18 Oct 2021 18:51:30 +0530 Subject: [PATCH 1/2] Add new major branch to github action. --- .github/workflows/on-master.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/on-master.yaml b/.github/workflows/on-master.yaml index 62da01757d38..6fd65f4981ee 100644 --- a/.github/workflows/on-master.yaml +++ b/.github/workflows/on-master.yaml @@ -3,6 +3,7 @@ name: Docker Build and publish to Github on: push: branches: + - v1.10.10-statediff - v1.10.9-statediff - v1.10.8-statediff - v1.10.7-statediff From 401caff29f8e9915993f84593a74290e4999a0fa Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Mon, 18 Oct 2021 19:06:22 +0530 Subject: [PATCH 2/2] Use `GetTd` instead of `GetTdByHash` https://github.com/ethereum/go-ethereum/commit/62891378274a58c84b9fa35f3c9b99155644b5eb --- statediff/service.go | 6 +- statediff/testhelpers/mocks/blockchain.go | 79 ++++++++++++--------- statediff/testhelpers/mocks/service.go | 2 +- statediff/testhelpers/mocks/service_test.go | 4 +- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/statediff/service.go b/statediff/service.go index edf1ca8f4155..6656168f3d39 100644 --- a/statediff/service.go +++ b/statediff/service.go @@ -64,7 +64,7 @@ type blockChain interface { GetBlockByHash(hash common.Hash) *types.Block GetBlockByNumber(number uint64) *types.Block GetReceiptsByHash(hash common.Hash) types.Receipts - GetTdByHash(hash common.Hash) *big.Int + GetTd(hash common.Hash, number uint64) *big.Int UnlockTrie(root common.Hash) StateCache() state.Database } @@ -442,7 +442,7 @@ func (sds *Service) newPayload(stateObject []byte, block *types.Block, params Pa payload.BlockRlp = blockBuff.Bytes() } if params.IncludeTD { - payload.TotalDifficulty = sds.BlockChain.GetTdByHash(block.Hash()) + payload.TotalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64()) } if params.IncludeReceipts { receiptBuff := new(bytes.Buffer) @@ -658,7 +658,7 @@ func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, p var err error var tx *ind.BlockTx if params.IncludeTD { - totalDifficulty = sds.BlockChain.GetTdByHash(block.Hash()) + totalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64()) } if params.IncludeReceipts { receipts = sds.BlockChain.GetReceiptsByHash(block.Hash()) diff --git a/statediff/testhelpers/mocks/blockchain.go b/statediff/testhelpers/mocks/blockchain.go index b0111e64c915..b4b1f36942a4 100644 --- a/statediff/testhelpers/mocks/blockchain.go +++ b/statediff/testhelpers/mocks/blockchain.go @@ -38,40 +38,41 @@ type BlockChain struct { ChainEvents []core.ChainEvent Receipts map[common.Hash]types.Receipts TDByHash map[common.Hash]*big.Int + TDByNum map[uint64]*big.Int } // SetBlocksForHashes mock method -func (blockChain *BlockChain) SetBlocksForHashes(blocks map[common.Hash]*types.Block) { - if blockChain.blocksToReturnByHash == nil { - blockChain.blocksToReturnByHash = make(map[common.Hash]*types.Block) +func (bc *BlockChain) SetBlocksForHashes(blocks map[common.Hash]*types.Block) { + if bc.blocksToReturnByHash == nil { + bc.blocksToReturnByHash = make(map[common.Hash]*types.Block) } - blockChain.blocksToReturnByHash = blocks + bc.blocksToReturnByHash = blocks } // GetBlockByHash mock method -func (blockChain *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { - blockChain.HashesLookedUp = append(blockChain.HashesLookedUp, hash) +func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { + bc.HashesLookedUp = append(bc.HashesLookedUp, hash) var block *types.Block - if len(blockChain.blocksToReturnByHash) > 0 { - block = blockChain.blocksToReturnByHash[hash] + if len(bc.blocksToReturnByHash) > 0 { + block = bc.blocksToReturnByHash[hash] } return block } // SetChainEvents mock method -func (blockChain *BlockChain) SetChainEvents(chainEvents []core.ChainEvent) { - blockChain.ChainEvents = chainEvents +func (bc *BlockChain) SetChainEvents(chainEvents []core.ChainEvent) { + bc.ChainEvents = chainEvents } // SubscribeChainEvent mock method -func (blockChain *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { +func (bc *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { subErr := errors.New("subscription error") var eventCounter int subscription := event.NewSubscription(func(quit <-chan struct{}) error { - for _, chainEvent := range blockChain.ChainEvents { + for _, chainEvent := range bc.ChainEvents { if eventCounter > 1 { time.Sleep(250 * time.Millisecond) return subErr @@ -90,45 +91,57 @@ func (blockChain *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) eve } // SetReceiptsForHash test method -func (blockChain *BlockChain) SetReceiptsForHash(hash common.Hash, receipts types.Receipts) { - if blockChain.Receipts == nil { - blockChain.Receipts = make(map[common.Hash]types.Receipts) +func (bc *BlockChain) SetReceiptsForHash(hash common.Hash, receipts types.Receipts) { + if bc.Receipts == nil { + bc.Receipts = make(map[common.Hash]types.Receipts) } - blockChain.Receipts[hash] = receipts + bc.Receipts[hash] = receipts } // GetReceiptsByHash mock method -func (blockChain *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { - return blockChain.Receipts[hash] +func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { + return bc.Receipts[hash] } // SetBlockForNumber test method -func (blockChain *BlockChain) SetBlockForNumber(block *types.Block, number uint64) { - if blockChain.blocksToReturnByNumber == nil { - blockChain.blocksToReturnByNumber = make(map[uint64]*types.Block) +func (bc *BlockChain) SetBlockForNumber(block *types.Block, number uint64) { + if bc.blocksToReturnByNumber == nil { + bc.blocksToReturnByNumber = make(map[uint64]*types.Block) } - blockChain.blocksToReturnByNumber[number] = block + bc.blocksToReturnByNumber[number] = block } // GetBlockByNumber mock method -func (blockChain *BlockChain) GetBlockByNumber(number uint64) *types.Block { - return blockChain.blocksToReturnByNumber[number] +func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { + return bc.blocksToReturnByNumber[number] } -// GetTdByHash mock method -func (blockChain *BlockChain) GetTdByHash(hash common.Hash) *big.Int { - return blockChain.TDByHash[hash] +// GetTd mock method +func (bc *BlockChain) GetTd(hash common.Hash, blockNum uint64) *big.Int { + if td, ok := bc.TDByHash[hash]; ok { + return td + } + + if td, ok := bc.TDByNum[blockNum]; ok { + return td + } + return nil } -func (blockChain *BlockChain) SetTdByHash(hash common.Hash, td *big.Int) { - if blockChain.TDByHash == nil { - blockChain.TDByHash = make(map[common.Hash]*big.Int) +func (bc *BlockChain) SetTd(hash common.Hash, blockNum uint64, td *big.Int) { + if bc.TDByHash == nil { + bc.TDByHash = make(map[common.Hash]*big.Int) + } + bc.TDByHash[hash] = td + + if bc.TDByNum == nil { + bc.TDByNum = make(map[uint64]*big.Int) } - blockChain.TDByHash[hash] = td + bc.TDByNum[blockNum] = td } -func (blockChain *BlockChain) UnlockTrie(root common.Hash) {} +func (bc *BlockChain) UnlockTrie(root common.Hash) {} -func (BlockChain *BlockChain) StateCache() state.Database { +func (bc *BlockChain) StateCache() state.Database { return nil } diff --git a/statediff/testhelpers/mocks/service.go b/statediff/testhelpers/mocks/service.go index e4195eb10ee3..f10017df43f3 100644 --- a/statediff/testhelpers/mocks/service.go +++ b/statediff/testhelpers/mocks/service.go @@ -166,7 +166,7 @@ func (sds *MockStateDiffService) newPayload(stateObject []byte, block *types.Blo payload.BlockRlp = blockBuff.Bytes() } if params.IncludeTD { - payload.TotalDifficulty = sds.BlockChain.GetTdByHash(block.Hash()) + payload.TotalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64()) } if params.IncludeReceipts { receiptBuff := new(bytes.Buffer) diff --git a/statediff/testhelpers/mocks/service_test.go b/statediff/testhelpers/mocks/service_test.go index 3543036e3920..8c1fd49cfc11 100644 --- a/statediff/testhelpers/mocks/service_test.go +++ b/statediff/testhelpers/mocks/service_test.go @@ -134,7 +134,7 @@ func testSubscriptionAPI(t *testing.T) { serviceQuitChan := make(chan bool) mockBlockChain := &BlockChain{} mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt}) - mockBlockChain.SetTdByHash(block1.Hash(), mockTotalDifficulty) + mockBlockChain.SetTd(block1.Hash(), block1.NumberU64(), mockTotalDifficulty) mockService := MockStateDiffService{ Mutex: sync.Mutex{}, Builder: statediff.NewBuilder(chain.StateCache()), @@ -221,7 +221,7 @@ func testHTTPAPI(t *testing.T) { }) mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64()) mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt}) - mockBlockChain.SetTdByHash(block1.Hash(), big.NewInt(1337)) + mockBlockChain.SetTd(block1.Hash(), block1.NumberU64(), big.NewInt(1337)) mockService := MockStateDiffService{ Mutex: sync.Mutex{}, Builder: statediff.NewBuilder(chain.StateCache()),