From ae38eea1f340ab0ab4962fbc1a769ca4e4966af7 Mon Sep 17 00:00:00 2001 From: "ptsayli@gmail.com" <ptsayli@gmail.com> Date: Wed, 12 Aug 2020 11:32:03 +0530 Subject: [PATCH] add state sync to core.blockchain --- consensus/bor/bor.go | 7 ++-- core/blockchain.go | 11 ++++-- core/state_processor.go | 1 - core/types/block.go | 72 +++++++++++++++++---------------------- core/types/transaction.go | 8 ----- 5 files changed, 42 insertions(+), 57 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 2a81a2ad3..f112eefdd 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -679,13 +679,13 @@ 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) - header.SetStateSync(stateSyncData) + r := chain.(*core.BlockChain) + r.SetStateSync(stateSyncData) } // FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, // nor block rewards given, and returns the final block. func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { - stateSyncData := []*types.StateData{} headerNumber := header.Number.Uint64() if headerNumber%c.config.Sprint == 0 { cx := chainContext{Chain: chain, Bor: c} @@ -699,7 +699,7 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Hea if !c.WithoutHeimdall { // commit statees - stateSyncData, err = c.CommitStates(state, header, cx) + _, err = c.CommitStates(state, header, cx) if err != nil { log.Error("Error while committing states", "error", err) return nil, err @@ -710,7 +710,6 @@ 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) - header.SetStateSync(stateSyncData) // Assemble block block := types.NewBlock(header, txs, nil, receipts) diff --git a/core/blockchain.go b/core/blockchain.go index 952dea7dc..1a0cfb6c2 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 block +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 @@ -1793,9 +1800,7 @@ 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) - syncData := block.StateSyncData() - - for _, data := range syncData { + for _, data := range bc.stateSyncData { bc.stateSyncFeed.Send(StateSyncEvent{StateData: data}) } if err != nil { diff --git a/core/state_processor.go b/core/state_processor.go index ec98e3ee4..57faa93b7 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()) - block.SetStateSync(header.StateData()) return receipts, allLogs, *usedGas, nil } diff --git a/core/types/block.go b/core/types/block.go index c2ebaedd9..6baafa225 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -66,26 +66,33 @@ func (n *BlockNonce) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) } +// StateData represents state received from Ethereum Blockchain +type StateData struct { + Did uint64 + Contract common.Address + Data string + TxHash common.Hash +} + //go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go // Header represents a block header in the Ethereum blockchain. type Header struct { - ParentHash common.Hash `json:"parentHash" gencodec:"required"` - UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` - Coinbase common.Address `json:"miner" gencodec:"required"` - Root common.Hash `json:"stateRoot" gencodec:"required"` - TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` - ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` - Bloom Bloom `json:"logsBloom" gencodec:"required"` - Difficulty *big.Int `json:"difficulty" gencodec:"required"` - Number *big.Int `json:"number" gencodec:"required"` - GasLimit uint64 `json:"gasLimit" gencodec:"required"` - GasUsed uint64 `json:"gasUsed" gencodec:"required"` - Time uint64 `json:"timestamp" gencodec:"required"` - Extra []byte `json:"extraData" gencodec:"required"` - MixDigest common.Hash `json:"mixHash"` - Nonce BlockNonce `json:"nonce"` - stateSyncData []*StateData + ParentHash common.Hash `json:"parentHash" gencodec:"required"` + UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` + Coinbase common.Address `json:"miner" gencodec:"required"` + Root common.Hash `json:"stateRoot" gencodec:"required"` + TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` + ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` + Bloom Bloom `json:"logsBloom" gencodec:"required"` + Difficulty *big.Int `json:"difficulty" gencodec:"required"` + Number *big.Int `json:"number" gencodec:"required"` + GasLimit uint64 `json:"gasLimit" gencodec:"required"` + GasUsed uint64 `json:"gasUsed" gencodec:"required"` + Time uint64 `json:"timestamp" gencodec:"required"` + Extra []byte `json:"extraData" gencodec:"required"` + MixDigest common.Hash `json:"mixHash"` + Nonce BlockNonce `json:"nonce"` } // field type overrides for gencodec @@ -113,21 +120,6 @@ func (h *Header) Size() common.StorageSize { return headerSize + common.StorageSize(len(h.Extra)+(h.Difficulty.BitLen()+h.Number.BitLen())/8) } -// SetStateSync set sync data in block -func (h *Header) SetStateSync(stateData []*StateData) { - h.stateSyncData = stateData -} - -// SetStateSync set sync data in block -func (h *Block) SetStateSync(stateData []*StateData) { - h.stateSyncData = stateData -} - -// StateSyncData set sync data in block -func (h *Header) StateData() []*StateData { - return h.stateSyncData -} - // SanityCheck checks a few basic things -- these checks are way beyond what // any 'sane' production values should hold, and can mainly be used to prevent // that the unbounded fields are stuffed with junk data to add processing @@ -172,10 +164,9 @@ 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 @@ -328,12 +319,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 } diff --git a/core/types/transaction.go b/core/types/transaction.go index 55b3ac103..866b715b7 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 -- GitLab