mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 11:41:19 +00:00
f9f54f012d
* Add revive and phoenix * store enode address to file, then read it from tester * store enode address to file, then read it from tester * rebase master * fix miss-type * dbg p2p-sub-protocol, add self-destruct test case * re-create blockFetcher * exit syncer loop and start new one * rebase to master * use core.GenerateChain * root miss-match * introduce reduceComplexity flag * fix transfer to 0 account * cleanup * test-case for intermediate cache * clean * clean * clean * fix handler panic Co-authored-by: Alexey Akhunov <akhounov@gmail.com> Co-authored-by: alex.sharov <alex.sharov@lazada.com>
219 lines
7.6 KiB
Go
219 lines
7.6 KiB
Go
// Copyright 2015 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus"
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
|
)
|
|
|
|
// BlockValidator is responsible for validating block headers, uncles and
|
|
// processed state.
|
|
//
|
|
// BlockValidator implements Validator.
|
|
type BlockValidator struct {
|
|
config *params.ChainConfig // Chain configuration options
|
|
bc *BlockChain // Canonical block chain
|
|
engine consensus.Engine // Consensus engine used for validating
|
|
dblks map[uint64]bool // Block numbers to run diagnostics on
|
|
}
|
|
|
|
// NewBlockValidator returns a new block validator which is safe for re-use
|
|
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine) *BlockValidator {
|
|
validator := &BlockValidator{
|
|
config: config,
|
|
engine: engine,
|
|
bc: blockchain,
|
|
dblks: make(map[uint64]bool),
|
|
}
|
|
files, err := ioutil.ReadDir("./")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, f := range files {
|
|
if !f.IsDir() && strings.HasPrefix(f.Name(), "root_") && strings.HasSuffix(f.Name(), ".txt") {
|
|
blockNumber, err := strconv.ParseUint(f.Name()[len("root_"):len(f.Name())-len(".txt")], 10, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if _, ok := validator.dblks[blockNumber]; !ok {
|
|
validator.dblks[blockNumber] = true
|
|
}
|
|
}
|
|
if !f.IsDir() && strings.HasPrefix(f.Name(), "right_") && strings.HasSuffix(f.Name(), ".txt") {
|
|
blockNumber, err := strconv.ParseUint(f.Name()[len("right_"):len(f.Name())-len(".txt")], 10, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
validator.dblks[blockNumber] = false
|
|
}
|
|
}
|
|
for blockNumber, ok := range validator.dblks {
|
|
if ok {
|
|
log.Info("Block validator will watch", "block", blockNumber)
|
|
}
|
|
}
|
|
return validator
|
|
}
|
|
|
|
// ValidateBody validates the given block's uncles and verifies the block
|
|
// header's transaction and uncle roots. The headers are assumed to be already
|
|
// validated at this point.
|
|
func (v *BlockValidator) ValidateBody(ctx context.Context, block *types.Block) error {
|
|
// Check whether the block's known, and if not, that it's linkable
|
|
//if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
|
|
// return ErrKnownBlock
|
|
//}
|
|
// Check whether the block is linkable
|
|
_, noHistory := params.GetNoHistoryByBlock(ctx, block.Number())
|
|
if !noHistory && v.bc.GetBlockByHash(block.ParentHash()) == nil {
|
|
return consensus.ErrUnknownAncestor
|
|
}
|
|
// Header validity is known at this point, check the uncles and transactions
|
|
header := block.Header()
|
|
if err := v.engine.VerifyUncles(v.bc, block); err != nil {
|
|
return err
|
|
}
|
|
if hash := types.CalcUncleHash(block.Uncles()); hash != header.UncleHash {
|
|
return fmt.Errorf("uncle root hash mismatch: have %x, want %x", hash, header.UncleHash)
|
|
}
|
|
if hash := types.DeriveSha(block.Transactions()); hash != header.TxHash {
|
|
return fmt.Errorf("transaction root hash mismatch: have %x, want %x", hash, header.TxHash)
|
|
}
|
|
if noHistory {
|
|
return nil
|
|
}
|
|
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
|
|
if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) {
|
|
return consensus.ErrUnknownAncestor
|
|
}
|
|
return consensus.ErrPrunedAncestor
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateState validates the various changes that happen after a state
|
|
// transition, such as amount of used gas, the receipt roots and the state root
|
|
// itself. ValidateState returns a database batch if the validation was a success
|
|
// otherwise nil and an error is returned.
|
|
func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *state.IntraBlockState, tds *state.TrieDbState, receipts types.Receipts, usedGas uint64) error {
|
|
header := block.Header()
|
|
var errorBuf strings.Builder
|
|
if block.GasUsed() != usedGas {
|
|
fmt.Fprintf(&errorBuf, "invalid gas used (remote: %d local: %d)", block.GasUsed(), usedGas)
|
|
}
|
|
// Validate the received block's bloom with the one derived from the generated receipts.
|
|
// For valid blocks this should always validate to true.
|
|
rbloom := types.CreateBloom(receipts)
|
|
if rbloom != header.Bloom {
|
|
if errorBuf.Len() > 0 {
|
|
errorBuf.WriteString("; ")
|
|
}
|
|
fmt.Fprintf(&errorBuf, "invalid bloom (remote: %x local: %x)", header.Bloom, rbloom)
|
|
}
|
|
|
|
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
|
|
receiptSha := types.DeriveSha(receipts)
|
|
if receiptSha != header.ReceiptHash {
|
|
if errorBuf.Len() > 0 {
|
|
errorBuf.WriteString("; ")
|
|
}
|
|
for _, r := range receipts {
|
|
for _, l := range r.Logs {
|
|
fmt.Printf("receipts: %s %x\n", l.Data, l.Data)
|
|
}
|
|
|
|
}
|
|
fmt.Fprintf(&errorBuf, "invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha)
|
|
}
|
|
// Validate the state root against the received state root and throw
|
|
// an error if they don't match.
|
|
if root := tds.LastRoot(); header.Root != root {
|
|
filename := fmt.Sprintf("root_%d.txt", block.NumberU64())
|
|
log.Warn("Generating deep snapshot of the wront tries...", "file", filename)
|
|
f, err := os.Create(filename)
|
|
if err == nil {
|
|
defer f.Close()
|
|
tds.PrintTrie(f)
|
|
}
|
|
if errorBuf.Len() > 0 {
|
|
errorBuf.WriteString("; ")
|
|
}
|
|
fmt.Fprintf(&errorBuf, "invalid merkle root (remote: %x local: %x)", header.Root, root)
|
|
} else if has, ok := v.dblks[block.NumberU64()]; ok && has {
|
|
filename := fmt.Sprintf("right_%d.txt", block.NumberU64())
|
|
log.Warn("Generating deep snapshot of right tries...", "file", filename)
|
|
f, err := os.Create(filename)
|
|
if err == nil {
|
|
defer f.Close()
|
|
tds.PrintTrie(f)
|
|
}
|
|
}
|
|
if errorBuf.Len() > 0 {
|
|
return errors.New(errorBuf.String())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CalcGasLimit computes the gas limit of the next block after parent. It aims
|
|
// to keep the baseline gas above the provided floor, and increase it towards the
|
|
// ceil if the blocks are full. If the ceil is exceeded, it will always decrease
|
|
// the gas allowance.
|
|
func CalcGasLimit(parent *types.Block, gasFloor, gasCeil uint64) uint64 {
|
|
// contrib = (parentGasUsed * 3 / 2) / 1024
|
|
contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor
|
|
|
|
// decay = parentGasLimit / 1024 -1
|
|
decay := parent.GasLimit()/params.GasLimitBoundDivisor - 1
|
|
|
|
/*
|
|
strategy: gasLimit of block-to-mine is set based on parent's
|
|
gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we
|
|
increase it, otherwise lower it (or leave it unchanged if it's right
|
|
at that usage) the amount increased/decreased depends on how far away
|
|
from parentGasLimit * (2/3) parentGasUsed is.
|
|
*/
|
|
limit := parent.GasLimit() - decay + contrib
|
|
if limit < params.MinGasLimit {
|
|
limit = params.MinGasLimit
|
|
}
|
|
// If we're outside our allowed gas range, we try to hone towards them
|
|
if limit < gasFloor {
|
|
limit = parent.GasLimit() + decay
|
|
if limit > gasFloor {
|
|
limit = gasFloor
|
|
}
|
|
} else if limit > gasCeil {
|
|
limit = parent.GasLimit() - decay
|
|
if limit < gasCeil {
|
|
limit = gasCeil
|
|
}
|
|
}
|
|
return limit
|
|
}
|