Merge pull request #86 from maticnetwork/fix-deposit-subscribe

Fix deposit subscribe
This commit is contained in:
Jaynti Kanani 2020-08-12 15:43:04 +05:30 committed by GitHub
commit 05243eb7c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 35 deletions

View file

@ -655,6 +655,8 @@ func (c *Bor) Prepare(chain consensus.ChainReader, header *types.Header) error {
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
stateSyncData := []*types.StateData{}
var err error
headerNumber := header.Number.Uint64()
if headerNumber%c.config.Sprint == 0 {
cx := chainContext{Chain: chain, Bor: c}
@ -666,7 +668,7 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
if !c.WithoutHeimdall {
// commit statees
_, err := c.CommitStates(state, header, cx)
stateSyncData, err = c.CommitStates(state, header, cx)
if err != nil {
log.Error("Error while committing states", "error", err)
return
@ -677,6 +679,8 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
bc := chain.(*core.BlockChain)
bc.SetStateSync(stateSyncData)
}
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
@ -707,10 +711,11 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Hea
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
// Assemble block
block := types.NewBlock(header, txs, nil, receipts)
block.SetStateSync(stateSyncData)
bc := chain.(*core.BlockChain)
bc.SetStateSync(stateSyncData)
// return the final block for sealing
return block, nil
}

View file

@ -180,6 +180,8 @@ type BlockChain struct {
txLookupCache *lru.Cache // Cache for the most recent transaction lookup data.
futureBlocks *lru.Cache // future blocks are blocks added for later processing
stateSyncData []*types.StateData
quit chan struct{} // blockchain quit channel
wg sync.WaitGroup // chain processing wait group for shutting down
running int32 // 0 if chain is running, 1 when stopped
@ -333,6 +335,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
return bc, nil
}
// SetStateSync set sync data in state_data
func (bc *BlockChain) SetStateSync(stateData []*types.StateData) {
bc.stateSyncData = stateData
}
// GetVMConfig returns the block chain VM config.
func (bc *BlockChain) GetVMConfig() *vm.Config {
return &bc.vmConfig
@ -1541,10 +1548,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// event here.
if emitHeadEvent {
bc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
}
syncData := block.StateSyncData()
for _, data := range syncData {
bc.stateSyncFeed.Send(StateSyncEvent{StateData: data})
for _, data := range bc.stateSyncData {
bc.stateSyncFeed.Send(StateSyncEvent{StateData: data})
}
}
} else {
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
@ -1797,6 +1803,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// Process block using the parent state as reference point
substart := time.Now()
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
for _, data := range bc.stateSyncData {
bc.stateSyncFeed.Send(StateSyncEvent{StateData: data})
}
if err != nil {
bc.reportBlock(block, receipts, err)
atomic.StoreUint32(&followupInterrupt, 1)

View file

@ -77,7 +77,6 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
}
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
return receipts, allLogs, *usedGas, nil
}

View file

@ -156,10 +156,10 @@ type Body struct {
// Block represents an entire block in the Ethereum blockchain.
type Block struct {
header *Header
uncles []*Header
transactions Transactions
stateSyncData []*StateData
header *Header
uncles []*Header
transactions Transactions
// caches
hash atomic.Value
size atomic.Value
@ -266,11 +266,6 @@ func CopyHeader(h *Header) *Header {
return &cpy
}
// SetStateSync set sync data in block
func (b *Block) SetStateSync(stateData []*StateData) {
b.stateSyncData = stateData
}
// DecodeRLP decodes the Ethereum
func (b *Block) DecodeRLP(s *rlp.Stream) error {
var eb extblock
@ -316,12 +311,11 @@ func (b *Block) Transaction(hash common.Hash) *Transaction {
return nil
}
func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
func (b *Block) Time() uint64 { return b.header.Time }
func (b *Block) StateSyncData() []*StateData { return b.stateSyncData }
func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
func (b *Block) Time() uint64 { return b.header.Time }
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
@ -378,10 +372,9 @@ func (b *Block) WithSeal(header *Header) *Block {
cpy := *header
return &Block{
header: &cpy,
transactions: b.transactions,
uncles: b.uncles,
stateSyncData: b.stateSyncData,
header: &cpy,
transactions: b.transactions,
uncles: b.uncles,
}
}

11
core/types/state_data.go Normal file
View file

@ -0,0 +1,11 @@
package types
import "github.com/maticnetwork/bor/common"
// StateData represents state received from Ethereum Blockchain
type StateData struct {
Did uint64
Contract common.Address
Data string
TxHash common.Hash
}

View file

@ -60,14 +60,6 @@ type txdata struct {
Hash *common.Hash `json:"hash" rlp:"-"`
}
// State represents state received from Ethereum Blockchain
type StateData struct {
Did uint64
Contract common.Address
Data string
TxHash common.Hash
}
type txdataMarshaling struct {
AccountNonce hexutil.Uint64
Price *hexutil.Big