Skip to content

Commit ec6ee6e

Browse files
authored
Merge pull request #132 from vulcanize/v1.10.10-statediff-0.0.27
v1.10.10-statediff
2 parents a66e1fe + 401caff commit ec6ee6e

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

.github/workflows/on-master.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Docker Build and publish to Github
33
on:
44
push:
55
branches:
6+
- v1.10.10-statediff
67
- v1.10.9-statediff
78
- v1.10.8-statediff
89
- v1.10.7-statediff

statediff/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type blockChain interface {
6464
GetBlockByHash(hash common.Hash) *types.Block
6565
GetBlockByNumber(number uint64) *types.Block
6666
GetReceiptsByHash(hash common.Hash) types.Receipts
67-
GetTdByHash(hash common.Hash) *big.Int
67+
GetTd(hash common.Hash, number uint64) *big.Int
6868
UnlockTrie(root common.Hash)
6969
StateCache() state.Database
7070
}
@@ -442,7 +442,7 @@ func (sds *Service) newPayload(stateObject []byte, block *types.Block, params Pa
442442
payload.BlockRlp = blockBuff.Bytes()
443443
}
444444
if params.IncludeTD {
445-
payload.TotalDifficulty = sds.BlockChain.GetTdByHash(block.Hash())
445+
payload.TotalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64())
446446
}
447447
if params.IncludeReceipts {
448448
receiptBuff := new(bytes.Buffer)
@@ -658,7 +658,7 @@ func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, p
658658
var err error
659659
var tx *ind.BlockTx
660660
if params.IncludeTD {
661-
totalDifficulty = sds.BlockChain.GetTdByHash(block.Hash())
661+
totalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64())
662662
}
663663
if params.IncludeReceipts {
664664
receipts = sds.BlockChain.GetReceiptsByHash(block.Hash())

statediff/testhelpers/mocks/blockchain.go

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,41 @@ type BlockChain struct {
3838
ChainEvents []core.ChainEvent
3939
Receipts map[common.Hash]types.Receipts
4040
TDByHash map[common.Hash]*big.Int
41+
TDByNum map[uint64]*big.Int
4142
}
4243

4344
// SetBlocksForHashes mock method
44-
func (blockChain *BlockChain) SetBlocksForHashes(blocks map[common.Hash]*types.Block) {
45-
if blockChain.blocksToReturnByHash == nil {
46-
blockChain.blocksToReturnByHash = make(map[common.Hash]*types.Block)
45+
func (bc *BlockChain) SetBlocksForHashes(blocks map[common.Hash]*types.Block) {
46+
if bc.blocksToReturnByHash == nil {
47+
bc.blocksToReturnByHash = make(map[common.Hash]*types.Block)
4748
}
48-
blockChain.blocksToReturnByHash = blocks
49+
bc.blocksToReturnByHash = blocks
4950
}
5051

5152
// GetBlockByHash mock method
52-
func (blockChain *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
53-
blockChain.HashesLookedUp = append(blockChain.HashesLookedUp, hash)
53+
func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
54+
bc.HashesLookedUp = append(bc.HashesLookedUp, hash)
5455

5556
var block *types.Block
56-
if len(blockChain.blocksToReturnByHash) > 0 {
57-
block = blockChain.blocksToReturnByHash[hash]
57+
if len(bc.blocksToReturnByHash) > 0 {
58+
block = bc.blocksToReturnByHash[hash]
5859
}
5960

6061
return block
6162
}
6263

6364
// SetChainEvents mock method
64-
func (blockChain *BlockChain) SetChainEvents(chainEvents []core.ChainEvent) {
65-
blockChain.ChainEvents = chainEvents
65+
func (bc *BlockChain) SetChainEvents(chainEvents []core.ChainEvent) {
66+
bc.ChainEvents = chainEvents
6667
}
6768

6869
// SubscribeChainEvent mock method
69-
func (blockChain *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
70+
func (bc *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
7071
subErr := errors.New("subscription error")
7172

7273
var eventCounter int
7374
subscription := event.NewSubscription(func(quit <-chan struct{}) error {
74-
for _, chainEvent := range blockChain.ChainEvents {
75+
for _, chainEvent := range bc.ChainEvents {
7576
if eventCounter > 1 {
7677
time.Sleep(250 * time.Millisecond)
7778
return subErr
@@ -90,45 +91,57 @@ func (blockChain *BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) eve
9091
}
9192

9293
// SetReceiptsForHash test method
93-
func (blockChain *BlockChain) SetReceiptsForHash(hash common.Hash, receipts types.Receipts) {
94-
if blockChain.Receipts == nil {
95-
blockChain.Receipts = make(map[common.Hash]types.Receipts)
94+
func (bc *BlockChain) SetReceiptsForHash(hash common.Hash, receipts types.Receipts) {
95+
if bc.Receipts == nil {
96+
bc.Receipts = make(map[common.Hash]types.Receipts)
9697
}
97-
blockChain.Receipts[hash] = receipts
98+
bc.Receipts[hash] = receipts
9899
}
99100

100101
// GetReceiptsByHash mock method
101-
func (blockChain *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
102-
return blockChain.Receipts[hash]
102+
func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
103+
return bc.Receipts[hash]
103104
}
104105

105106
// SetBlockForNumber test method
106-
func (blockChain *BlockChain) SetBlockForNumber(block *types.Block, number uint64) {
107-
if blockChain.blocksToReturnByNumber == nil {
108-
blockChain.blocksToReturnByNumber = make(map[uint64]*types.Block)
107+
func (bc *BlockChain) SetBlockForNumber(block *types.Block, number uint64) {
108+
if bc.blocksToReturnByNumber == nil {
109+
bc.blocksToReturnByNumber = make(map[uint64]*types.Block)
109110
}
110-
blockChain.blocksToReturnByNumber[number] = block
111+
bc.blocksToReturnByNumber[number] = block
111112
}
112113

113114
// GetBlockByNumber mock method
114-
func (blockChain *BlockChain) GetBlockByNumber(number uint64) *types.Block {
115-
return blockChain.blocksToReturnByNumber[number]
115+
func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
116+
return bc.blocksToReturnByNumber[number]
116117
}
117118

118-
// GetTdByHash mock method
119-
func (blockChain *BlockChain) GetTdByHash(hash common.Hash) *big.Int {
120-
return blockChain.TDByHash[hash]
119+
// GetTd mock method
120+
func (bc *BlockChain) GetTd(hash common.Hash, blockNum uint64) *big.Int {
121+
if td, ok := bc.TDByHash[hash]; ok {
122+
return td
123+
}
124+
125+
if td, ok := bc.TDByNum[blockNum]; ok {
126+
return td
127+
}
128+
return nil
121129
}
122130

123-
func (blockChain *BlockChain) SetTdByHash(hash common.Hash, td *big.Int) {
124-
if blockChain.TDByHash == nil {
125-
blockChain.TDByHash = make(map[common.Hash]*big.Int)
131+
func (bc *BlockChain) SetTd(hash common.Hash, blockNum uint64, td *big.Int) {
132+
if bc.TDByHash == nil {
133+
bc.TDByHash = make(map[common.Hash]*big.Int)
134+
}
135+
bc.TDByHash[hash] = td
136+
137+
if bc.TDByNum == nil {
138+
bc.TDByNum = make(map[uint64]*big.Int)
126139
}
127-
blockChain.TDByHash[hash] = td
140+
bc.TDByNum[blockNum] = td
128141
}
129142

130-
func (blockChain *BlockChain) UnlockTrie(root common.Hash) {}
143+
func (bc *BlockChain) UnlockTrie(root common.Hash) {}
131144

132-
func (BlockChain *BlockChain) StateCache() state.Database {
145+
func (bc *BlockChain) StateCache() state.Database {
133146
return nil
134147
}

statediff/testhelpers/mocks/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (sds *MockStateDiffService) newPayload(stateObject []byte, block *types.Blo
166166
payload.BlockRlp = blockBuff.Bytes()
167167
}
168168
if params.IncludeTD {
169-
payload.TotalDifficulty = sds.BlockChain.GetTdByHash(block.Hash())
169+
payload.TotalDifficulty = sds.BlockChain.GetTd(block.Hash(), block.NumberU64())
170170
}
171171
if params.IncludeReceipts {
172172
receiptBuff := new(bytes.Buffer)

statediff/testhelpers/mocks/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func testSubscriptionAPI(t *testing.T) {
134134
serviceQuitChan := make(chan bool)
135135
mockBlockChain := &BlockChain{}
136136
mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt})
137-
mockBlockChain.SetTdByHash(block1.Hash(), mockTotalDifficulty)
137+
mockBlockChain.SetTd(block1.Hash(), block1.NumberU64(), mockTotalDifficulty)
138138
mockService := MockStateDiffService{
139139
Mutex: sync.Mutex{},
140140
Builder: statediff.NewBuilder(chain.StateCache()),
@@ -221,7 +221,7 @@ func testHTTPAPI(t *testing.T) {
221221
})
222222
mockBlockChain.SetBlockForNumber(block1, block1.Number().Uint64())
223223
mockBlockChain.SetReceiptsForHash(block1.Hash(), types.Receipts{mockReceipt})
224-
mockBlockChain.SetTdByHash(block1.Hash(), big.NewInt(1337))
224+
mockBlockChain.SetTd(block1.Hash(), block1.NumberU64(), big.NewInt(1337))
225225
mockService := MockStateDiffService{
226226
Mutex: sync.Mutex{},
227227
Builder: statediff.NewBuilder(chain.StateCache()),

0 commit comments

Comments
 (0)