add state sync to core.blockchain

This commit is contained in:
ptsayli@gmail.com 2020-08-12 11:32:03 +05:30
parent 38ebecb2fa
commit ae38eea1f3
5 changed files with 42 additions and 57 deletions

View file

@ -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 // 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.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil) header.UncleHash = types.CalcUncleHash(nil)
header.SetStateSync(stateSyncData) r := chain.(*core.BlockChain)
r.SetStateSync(stateSyncData)
} }
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, // FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block. // 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) { 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() headerNumber := header.Number.Uint64()
if headerNumber%c.config.Sprint == 0 { if headerNumber%c.config.Sprint == 0 {
cx := chainContext{Chain: chain, Bor: c} cx := chainContext{Chain: chain, Bor: c}
@ -699,7 +699,7 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Hea
if !c.WithoutHeimdall { if !c.WithoutHeimdall {
// commit statees // commit statees
stateSyncData, err = c.CommitStates(state, header, cx) _, err = c.CommitStates(state, header, cx)
if err != nil { if err != nil {
log.Error("Error while committing states", "error", err) log.Error("Error while committing states", "error", err)
return nil, 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 // 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.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil) header.UncleHash = types.CalcUncleHash(nil)
header.SetStateSync(stateSyncData)
// Assemble block // Assemble block
block := types.NewBlock(header, txs, nil, receipts) block := types.NewBlock(header, txs, nil, receipts)

View file

@ -180,6 +180,8 @@ type BlockChain struct {
txLookupCache *lru.Cache // Cache for the most recent transaction lookup data. txLookupCache *lru.Cache // Cache for the most recent transaction lookup data.
futureBlocks *lru.Cache // future blocks are blocks added for later processing futureBlocks *lru.Cache // future blocks are blocks added for later processing
stateSyncData []*types.StateData
quit chan struct{} // blockchain quit channel quit chan struct{} // blockchain quit channel
wg sync.WaitGroup // chain processing wait group for shutting down wg sync.WaitGroup // chain processing wait group for shutting down
running int32 // 0 if chain is running, 1 when stopped 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 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. // GetVMConfig returns the block chain VM config.
func (bc *BlockChain) GetVMConfig() *vm.Config { func (bc *BlockChain) GetVMConfig() *vm.Config {
return &bc.vmConfig 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 // Process block using the parent state as reference point
substart := time.Now() substart := time.Now()
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig) receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
syncData := block.StateSyncData() for _, data := range bc.stateSyncData {
for _, data := range syncData {
bc.stateSyncFeed.Send(StateSyncEvent{StateData: data}) bc.stateSyncFeed.Send(StateSyncEvent{StateData: data})
} }
if err != nil { if err != nil {

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) // Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles()) p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
block.SetStateSync(header.StateData())
return receipts, allLogs, *usedGas, nil return receipts, allLogs, *usedGas, nil
} }

View file

@ -66,6 +66,14 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) 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 //go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
// Header represents a block header in the Ethereum blockchain. // Header represents a block header in the Ethereum blockchain.
@ -85,7 +93,6 @@ type Header struct {
Extra []byte `json:"extraData" gencodec:"required"` Extra []byte `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash"` MixDigest common.Hash `json:"mixHash"`
Nonce BlockNonce `json:"nonce"` Nonce BlockNonce `json:"nonce"`
stateSyncData []*StateData
} }
// field type overrides for gencodec // 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) 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 // 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 // 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 // that the unbounded fields are stuffed with junk data to add processing
@ -175,7 +167,6 @@ type Block struct {
header *Header header *Header
uncles []*Header uncles []*Header
transactions Transactions transactions Transactions
stateSyncData []*StateData
// caches // caches
hash atomic.Value hash atomic.Value
@ -333,7 +324,6 @@ func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
func (b *Block) GasUsed() uint64 { return b.header.GasUsed } 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) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
func (b *Block) Time() uint64 { return b.header.Time } func (b *Block) Time() uint64 { return b.header.Time }
func (b *Block) StateSyncData() []*StateData { return b.stateSyncData }
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() } func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest } func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }

View file

@ -60,14 +60,6 @@ type txdata struct {
Hash *common.Hash `json:"hash" rlp:"-"` 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 { type txdataMarshaling struct {
AccountNonce hexutil.Uint64 AccountNonce hexutil.Uint64
Price *hexutil.Big Price *hexutil.Big