diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 35e3bc64a02d9464812f3d6d3cc904876b0b7830..92540628403e9a051e5ae2994b204b7d64456fb7 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -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 } diff --git a/core/blockchain.go b/core/blockchain.go index 5a70de3fe843930cdb1fb210975319bb745ca2dc..6ecab669b5525bcd22ab505c7ca6e50bff733f05 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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) diff --git a/core/state_processor.go b/core/state_processor.go index e6a5e9d682a74118cf7017a54108c62a8346b3d2..57faa93b72e497584769918fb1c896e4efbfdfd6 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -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 } diff --git a/core/types/block.go b/core/types/block.go index e00b5e0d2be57e80af5d433af033d905bd45485e..be31b1a60afb3e238d54eb643e1102ac1f5b8606 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -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, } } diff --git a/core/types/state_data.go b/core/types/state_data.go new file mode 100644 index 0000000000000000000000000000000000000000..bd952627acebf3fb245ca13c9520f6a596842af4 --- /dev/null +++ b/core/types/state_data.go @@ -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 +} diff --git a/core/types/transaction.go b/core/types/transaction.go index 55b3ac10361e593b60c8dca61e3475db7dbef3b3..866b715b73c443076786700d9c7b4bf7ab5edf00 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -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