forked from cerc-io/plugeth
core: moved TD calculation from proc to chain
This commit is contained in:
parent
fa729a0c55
commit
97b0c4b697
@ -152,25 +152,25 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
|
|||||||
// Process block will attempt to process the given block's transactions and applies them
|
// Process block will attempt to process the given block's transactions and applies them
|
||||||
// on top of the block's parent state (given it exists) and will return wether it was
|
// on top of the block's parent state (given it exists) and will return wether it was
|
||||||
// successful or not.
|
// successful or not.
|
||||||
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, logs state.Logs, err error) {
|
func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, err error) {
|
||||||
// Processing a blocks may never happen simultaneously
|
// Processing a blocks may never happen simultaneously
|
||||||
sm.mutex.Lock()
|
sm.mutex.Lock()
|
||||||
defer sm.mutex.Unlock()
|
defer sm.mutex.Unlock()
|
||||||
|
|
||||||
header := block.Header()
|
header := block.Header()
|
||||||
if sm.bc.HasBlock(header.Hash()) {
|
if sm.bc.HasBlock(header.Hash()) {
|
||||||
return nil, nil, &KnownBlockError{header.Number, header.Hash()}
|
return nil, &KnownBlockError{header.Number, header.Hash()}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sm.bc.HasBlock(header.ParentHash) {
|
if !sm.bc.HasBlock(header.ParentHash) {
|
||||||
return nil, nil, ParentError(header.ParentHash)
|
return nil, ParentError(header.ParentHash)
|
||||||
}
|
}
|
||||||
parent := sm.bc.GetBlock(header.ParentHash)
|
parent := sm.bc.GetBlock(header.ParentHash)
|
||||||
|
|
||||||
return sm.processWithParent(block, parent)
|
return sm.processWithParent(block, parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, logs state.Logs, err error) {
|
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, err error) {
|
||||||
sm.lastAttemptedBlock = block
|
sm.lastAttemptedBlock = block
|
||||||
|
|
||||||
// Create a new state based on the parent's root (e.g., create copy)
|
// Create a new state based on the parent's root (e.g., create copy)
|
||||||
@ -183,7 +183,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
|
|||||||
|
|
||||||
// There can be at most two uncles
|
// There can be at most two uncles
|
||||||
if len(block.Uncles()) > 2 {
|
if len(block.Uncles()) > 2 {
|
||||||
return nil, nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
|
return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
|
||||||
}
|
}
|
||||||
|
|
||||||
receipts, err := sm.TransitionState(state, parent, block, false)
|
receipts, err := sm.TransitionState(state, parent, block, false)
|
||||||
@ -232,7 +232,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the td for this block
|
// Calculate the td for this block
|
||||||
td = CalculateTD(block, parent)
|
//td = CalculateTD(block, parent)
|
||||||
// Sync the current block's state to the database
|
// Sync the current block's state to the database
|
||||||
state.Sync()
|
state.Sync()
|
||||||
|
|
||||||
@ -244,7 +244,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
|
|||||||
putTx(sm.extraDb, tx, block, uint64(i))
|
putTx(sm.extraDb, tx, block, uint64(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
return td, state.Logs(), nil
|
return state.Logs(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validates the current block. Returns an error if the block was invalid,
|
// Validates the current block. Returns an error if the block was invalid,
|
||||||
|
@ -93,12 +93,12 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Dat
|
|||||||
blocks := make(types.Blocks, max)
|
blocks := make(types.Blocks, max)
|
||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
block := makeBlock(bman, parent, i, db, seed)
|
block := makeBlock(bman, parent, i, db, seed)
|
||||||
td, _, err := bman.processWithParent(block, parent)
|
_, err := bman.processWithParent(block, parent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("process with parent failed", err)
|
fmt.Println("process with parent failed", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
block.Td = td
|
block.Td = CalculateTD(block, parent)
|
||||||
blocks[i] = block
|
blocks[i] = block
|
||||||
parent = block
|
parent = block
|
||||||
}
|
}
|
||||||
|
@ -461,7 +461,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
|
|||||||
}
|
}
|
||||||
// Call in to the block processor and check for errors. It's likely that if one block fails
|
// Call in to the block processor and check for errors. It's likely that if one block fails
|
||||||
// all others will fail too (unless a known block is returned).
|
// all others will fail too (unless a known block is returned).
|
||||||
td, logs, err := self.processor.Process(block)
|
logs, err := self.processor.Process(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsKnownBlockErr(err) {
|
if IsKnownBlockErr(err) {
|
||||||
continue
|
continue
|
||||||
@ -492,7 +492,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
block.Td = td
|
|
||||||
|
block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash())))
|
||||||
|
|
||||||
self.mu.Lock()
|
self.mu.Lock()
|
||||||
{
|
{
|
||||||
@ -502,14 +503,14 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
|
|||||||
self.write(block)
|
self.write(block)
|
||||||
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
|
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
|
||||||
// At this point it's possible that a different chain (fork) becomes the new canonical chain.
|
// At this point it's possible that a different chain (fork) becomes the new canonical chain.
|
||||||
if td.Cmp(self.td) > 0 {
|
if block.Td.Cmp(self.td) > 0 {
|
||||||
//if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, common.Big1)) < 0 {
|
//if block.Header().Number.Cmp(new(big.Int).Add(cblock.Header().Number, common.Big1)) < 0 {
|
||||||
if block.Number().Cmp(cblock.Number()) <= 0 {
|
if block.Number().Cmp(cblock.Number()) <= 0 {
|
||||||
chash := cblock.Hash()
|
chash := cblock.Hash()
|
||||||
hash := block.Hash()
|
hash := block.Hash()
|
||||||
|
|
||||||
if glog.V(logger.Info) {
|
if glog.V(logger.Info) {
|
||||||
glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], td, cblock.Header().Number, chash[:4], self.td)
|
glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], block.Td, cblock.Header().Number, chash[:4], self.td)
|
||||||
}
|
}
|
||||||
// during split we merge two different chains and create the new canonical chain
|
// during split we merge two different chains and create the new canonical chain
|
||||||
self.merge(self.getBlockByNumber(block.NumberU64()), block)
|
self.merge(self.getBlockByNumber(block.NumberU64()), block)
|
||||||
@ -518,7 +519,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
|
|||||||
queueEvent.splitCount++
|
queueEvent.splitCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
self.setTotalDifficulty(td)
|
self.setTotalDifficulty(block.Td)
|
||||||
self.insert(block)
|
self.insert(block)
|
||||||
|
|
||||||
jsonlogger.LogJson(&logger.EthChainNewHead{
|
jsonlogger.LogJson(&logger.EthChainNewHead{
|
||||||
|
@ -52,6 +52,7 @@ func GenesisBlock(db common.Database) *types.Block {
|
|||||||
}
|
}
|
||||||
statedb.Sync()
|
statedb.Sync()
|
||||||
genesis.Header().Root = statedb.Root()
|
genesis.Header().Root = statedb.Root()
|
||||||
|
genesis.Td = params.GenesisDifficulty
|
||||||
|
|
||||||
return genesis
|
return genesis
|
||||||
}
|
}
|
||||||
|
@ -347,22 +347,20 @@ func (self *Block) Copy() *Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Block) String() string {
|
func (self *Block) String() string {
|
||||||
return fmt.Sprintf(`BLOCK(%x): Size: %v TD: %v {
|
return fmt.Sprintf(`Block(#%v): Size: %v TD: %v {
|
||||||
NoNonce: %x
|
MinerHash: %x
|
||||||
Header:
|
|
||||||
[
|
|
||||||
%v
|
%v
|
||||||
]
|
|
||||||
Transactions:
|
Transactions:
|
||||||
%v
|
%v
|
||||||
Uncles:
|
Uncles:
|
||||||
%v
|
%v
|
||||||
}
|
}
|
||||||
`, self.header.Hash(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles)
|
`, self.Number(), self.Size(), self.Td, self.header.HashNoNonce(), self.header, self.transactions, self.uncles)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Header) String() string {
|
func (self *Header) String() string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`Header(%x):
|
||||||
|
[
|
||||||
ParentHash: %x
|
ParentHash: %x
|
||||||
UncleHash: %x
|
UncleHash: %x
|
||||||
Coinbase: %x
|
Coinbase: %x
|
||||||
@ -377,8 +375,8 @@ func (self *Header) String() string {
|
|||||||
Time: %v
|
Time: %v
|
||||||
Extra: %s
|
Extra: %s
|
||||||
MixDigest: %x
|
MixDigest: %x
|
||||||
Nonce: %x`,
|
Nonce: %x
|
||||||
self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.MixDigest, self.Nonce)
|
]`, self.Hash(), self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.MixDigest, self.Nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Blocks []*Block
|
type Blocks []*Block
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BlockProcessor interface {
|
type BlockProcessor interface {
|
||||||
Process(*Block) (*big.Int, state.Logs, error)
|
Process(*Block) (state.Logs, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const bloomLength = 256
|
const bloomLength = 256
|
||||||
|
Loading…
Reference in New Issue
Block a user