diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-22 02:44:22 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-22 02:44:22 +0800 |
commit | 0467a6ceec4973b00c344d2a724f7fb01a6b0aee (patch) | |
tree | 66e010270bdf25fa0058c89fb31f6df8cf7f7829 /core/block_processor.go | |
parent | dba15d9c3609bcddfc7a4f0fe8f01c48a8bbfbc8 (diff) | |
parent | 5b0ee8ec304663898073b7a4c659e1def23716df (diff) | |
download | go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar.gz go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar.bz2 go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar.lz go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar.xz go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.tar.zst go-tangerine-0467a6ceec4973b00c344d2a724f7fb01a6b0aee.zip |
Merge pull request #1889 from karalabe/fast-sync-rebase
eth/63 fast synchronization algorithm
Diffstat (limited to 'core/block_processor.go')
-rw-r--r-- | core/block_processor.go | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/core/block_processor.go b/core/block_processor.go index 7032c077c..e7b2f63e5 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -128,7 +128,7 @@ func (self *BlockProcessor) ApplyTransaction(gp *GasPool, statedb *state.StateDB } logs := statedb.GetLogs(tx.Hash()) - receipt.SetLogs(logs) + receipt.Logs = logs receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) glog.V(logger.Debug).Infoln(receipt) @@ -212,14 +212,16 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs vm.Logs, receipts ty defer sm.mutex.Unlock() if sm.bc.HasBlock(block.Hash()) { - return nil, nil, &KnownBlockError{block.Number(), block.Hash()} + if _, err := state.New(block.Root(), sm.chainDb); err == nil { + return nil, nil, &KnownBlockError{block.Number(), block.Hash()} + } } - - if !sm.bc.HasBlock(block.ParentHash()) { - return nil, nil, ParentError(block.ParentHash()) + if parent := sm.bc.GetBlock(block.ParentHash()); parent != nil { + if _, err := state.New(parent.Root(), sm.chainDb); err == nil { + return sm.processWithParent(block, parent) + } } - parent := sm.bc.GetBlock(block.ParentHash()) - return sm.processWithParent(block, parent) + return nil, nil, ParentError(block.ParentHash()) } func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs vm.Logs, receipts types.Receipts, err error) { @@ -381,18 +383,40 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs vm.Logs, err error) receipts := GetBlockReceipts(sm.chainDb, block.Hash()) // coalesce logs for _, receipt := range receipts { - logs = append(logs, receipt.Logs()...) + logs = append(logs, receipt.Logs...) } return logs, nil } +// ValidateHeader verifies the validity of a header, relying on the database and +// POW behind the block processor. +func (sm *BlockProcessor) ValidateHeader(header *types.Header, checkPow, uncle bool) error { + // Short circuit if the header's already known or its parent missing + if sm.bc.HasHeader(header.Hash()) { + return nil + } + if parent := sm.bc.GetHeader(header.ParentHash); parent == nil { + return ParentError(header.ParentHash) + } else { + return ValidateHeader(sm.Pow, header, parent, checkPow, uncle) + } +} + +// ValidateHeaderWithParent verifies the validity of a header, relying on the database and +// POW behind the block processor. +func (sm *BlockProcessor) ValidateHeaderWithParent(header, parent *types.Header, checkPow, uncle bool) error { + if sm.bc.HasHeader(header.Hash()) { + return nil + } + return ValidateHeader(sm.Pow, header, parent, checkPow, uncle) +} + // See YP section 4.3.4. "Block Header Validity" // Validates a header. Returns an error if the header is invalid. func ValidateHeader(pow pow.PoW, header *types.Header, parent *types.Header, checkPow, uncle bool) error { if big.NewInt(int64(len(header.Extra))).Cmp(params.MaximumExtraDataSize) == 1 { return fmt.Errorf("Header extra data too long (%d)", len(header.Extra)) } - if uncle { if header.Time.Cmp(common.MaxBig) == 1 { return BlockTSTooBigErr @@ -429,7 +453,7 @@ func ValidateHeader(pow pow.PoW, header *types.Header, parent *types.Header, che if checkPow { // Verify the nonce of the header. Return an error if it's not valid if !pow.Verify(types.NewBlockWithHeader(header)) { - return ValidationError("Header's nonce is invalid (= %x)", header.Nonce) + return &BlockNonceErr{Hash: header.Hash(), Number: header.Number, Nonce: header.Nonce.Uint64()} } } return nil |