Skip to content

Commit

Permalink
Merge pull request #132 from vulcanize/v1.10.10-statediff-0.0.27
Browse files Browse the repository at this point in the history
 v1.10.10-statediff
  • Loading branch information
arijitAD authored Oct 18, 2021
2 parents a66e1fe + 401caff commit ec6ee6e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 39 deletions.
1 change: 1 addition & 0 deletions .github/workflows/on-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions statediff/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
79 changes: 46 additions & 33 deletions statediff/testhelpers/mocks/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion statediff/testhelpers/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions statediff/testhelpers/mocks/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit ec6ee6e

Please sign in to comment.