Skip to content

Commit

Permalink
Merge pull request #46 from buddh0/merge_develop
Browse files Browse the repository at this point in the history
Merge develop
  • Loading branch information
buddh0 authored Oct 25, 2024
2 parents 3618e04 + 08daa36 commit 0e2acdf
Show file tree
Hide file tree
Showing 39 changed files with 990 additions and 824 deletions.
3 changes: 1 addition & 2 deletions accounts/abi/bind/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
)

var (
Expand Down Expand Up @@ -101,7 +100,7 @@ type ContractTransactor interface {
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

// SendTransactionConditional injects the conditional transaction into the pending pool for execution after verification.
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error
SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error
}

// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
Expand Down
3 changes: 1 addition & 2 deletions accounts/abi/bind/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -76,7 +75,7 @@ func (mt *mockTransactor) SendTransaction(ctx context.Context, tx *types.Transac
return nil
}

func (mt *mockTransactor) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts ethapi.TransactionOpts) error {
func (mt *mockTransactor) SendTransactionConditional(ctx context.Context, tx *types.Transaction, opts types.TransactionOpts) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ var (
utils.DirectBroadcastFlag,
utils.DisableSnapProtocolFlag,
utils.EnableTrustProtocolFlag,
utils.PipeCommitFlag,
utils.RangeLimitFlag,
utils.USBFlag,
utils.SmartCardDaemonPathFlag,
Expand All @@ -93,6 +92,7 @@ var (
utils.TxPoolGlobalSlotsFlag,
utils.TxPoolAccountQueueFlag,
utils.TxPoolGlobalQueueFlag,
utils.TxPoolOverflowPoolSlotsFlag,
utils.TxPoolLifetimeFlag,
utils.TxPoolReannounceTimeFlag,
utils.BlobPoolDataDirFlag,
Expand Down
38 changes: 20 additions & 18 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ var (
Usage: "Enable trust protocol",
Category: flags.FastNodeCategory,
}
PipeCommitFlag = &cli.BoolFlag{
Name: "pipecommit",
Usage: "Enable MPT pipeline commit, it will improve syncing performance. It is an experimental feature(default is false)",
Category: flags.DeprecatedCategory,
}
RangeLimitFlag = &cli.BoolFlag{
Name: "rangelimit",
Usage: "Enable 5000 blocks limit for range query",
Expand Down Expand Up @@ -512,6 +507,12 @@ var (
Value: ethconfig.Defaults.TxPool.GlobalQueue,
Category: flags.TxPoolCategory,
}
TxPoolOverflowPoolSlotsFlag = &cli.Uint64Flag{
Name: "txpool.overflowpoolslots",
Usage: "Maximum number of transaction slots in overflow pool",
Value: ethconfig.Defaults.TxPool.OverflowPoolSlots,
Category: flags.TxPoolCategory,
}
TxPoolLifetimeFlag = &cli.DurationFlag{
Name: "txpool.lifetime",
Usage: "Maximum amount of time non-executable transaction are queued",
Expand Down Expand Up @@ -1840,6 +1841,9 @@ func setTxPool(ctx *cli.Context, cfg *legacypool.Config) {
if ctx.IsSet(TxPoolGlobalQueueFlag.Name) {
cfg.GlobalQueue = ctx.Uint64(TxPoolGlobalQueueFlag.Name)
}
if ctx.IsSet(TxPoolOverflowPoolSlotsFlag.Name) {
cfg.OverflowPoolSlots = ctx.Uint64(TxPoolOverflowPoolSlotsFlag.Name)
}
if ctx.IsSet(TxPoolLifetimeFlag.Name) {
cfg.Lifetime = ctx.Duration(TxPoolLifetimeFlag.Name)
}
Expand Down Expand Up @@ -2025,9 +2029,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.IsSet(EnableTrustProtocolFlag.Name) {
cfg.EnableTrustProtocol = ctx.IsSet(EnableTrustProtocolFlag.Name)
}
if ctx.IsSet(PipeCommitFlag.Name) {
log.Warn("The --pipecommit flag is deprecated and could be removed in the future!")
}
if ctx.IsSet(RangeLimitFlag.Name) {
cfg.RangeLimit = ctx.Bool(RangeLimitFlag.Name)
}
Expand Down Expand Up @@ -2374,16 +2375,17 @@ func EnableNodeInfo(poolConfig *legacypool.Config, nodeInfo *p2p.NodeInfo) Setup
return func() {
// register node info into metrics
metrics.NewRegisteredLabel("node-info", nil).Mark(map[string]interface{}{
"Enode": nodeInfo.Enode,
"ENR": nodeInfo.ENR,
"ID": nodeInfo.ID,
"PriceLimit": poolConfig.PriceLimit,
"PriceBump": poolConfig.PriceBump,
"AccountSlots": poolConfig.AccountSlots,
"GlobalSlots": poolConfig.GlobalSlots,
"AccountQueue": poolConfig.AccountQueue,
"GlobalQueue": poolConfig.GlobalQueue,
"Lifetime": poolConfig.Lifetime,
"Enode": nodeInfo.Enode,
"ENR": nodeInfo.ENR,
"ID": nodeInfo.ID,
"PriceLimit": poolConfig.PriceLimit,
"PriceBump": poolConfig.PriceBump,
"AccountSlots": poolConfig.AccountSlots,
"GlobalSlots": poolConfig.GlobalSlots,
"AccountQueue": poolConfig.AccountQueue,
"GlobalQueue": poolConfig.GlobalQueue,
"OverflowPoolSlots": poolConfig.OverflowPoolSlots,
"Lifetime": poolConfig.Lifetime,
})
}
}
Expand Down
17 changes: 10 additions & 7 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,27 +1807,30 @@ func (p *Parlia) getCurrentValidators(blockHash common.Hash, blockNum *big.Int)
func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, header *types.Header, chain core.ChainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt, receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool) error {
coinbase := header.Coinbase
balance := state.GetBalance(consensus.SystemAddress)
if balance.Cmp(common.U2560) <= 0 {
return nil
}
state.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
state.AddBalance(coinbase, balance, tracing.BalanceChangeUnspecified)

doDistributeSysReward := !p.chainConfig.IsKepler(header.Number, header.Time) &&
state.GetBalance(common.HexToAddress(systemcontracts.SystemRewardContract)).Cmp(maxSystemBalance) < 0
if doDistributeSysReward {
balance := state.GetBalance(consensus.SystemAddress)
rewards := new(uint256.Int)
rewards = rewards.Rsh(balance, systemRewardPercent)
if rewards.Cmp(common.U2560) > 0 {
state.SetBalance(consensus.SystemAddress, balance.Sub(balance, rewards), tracing.BalanceChangeUnspecified)
state.AddBalance(coinbase, rewards, tracing.BalanceChangeUnspecified)
err := p.distributeToSystem(rewards.ToBig(), state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
if err != nil {
return err
}
log.Trace("distribute to system reward pool", "block hash", header.Hash(), "amount", rewards)
balance = balance.Sub(balance, rewards)
}
}

balance := state.GetBalance(consensus.SystemAddress)
if balance.Cmp(common.U2560) <= 0 {
return nil
}
state.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceChangeUnspecified)
state.AddBalance(coinbase, balance, tracing.BalanceChangeUnspecified)
log.Trace("distribute to validator contract", "block hash", header.Hash(), "amount", balance)
return p.distributeToValidator(balance.ToBig(), val, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
}
Expand Down
30 changes: 6 additions & 24 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ package core
import (
"errors"
"fmt"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)

const badBlockCacheExpire = 30 * time.Second

type BlockValidatorOption func(*BlockValidator) *BlockValidator

func EnableRemoteVerifyManager(remoteValidator *remoteVerifyManager) BlockValidatorOption {
Expand Down Expand Up @@ -74,9 +70,6 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) {
return ErrKnownBlock
}
if v.bc.isCachedBadBlock(block) {
return ErrKnownBadBlock
}
// Header validity is known at this point. Here we verify that uncles, transactions
// and withdrawals given in the block body match the header.
header := block.Header()
Expand Down Expand Up @@ -192,23 +185,12 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
return nil
},
}
if statedb.IsPipeCommit() {
validateFuns = append(validateFuns, func() error {
if err := statedb.WaitPipeVerification(); err != nil {
return err
}
statedb.CorrectAccountsRoot(common.Hash{})
statedb.Finalise(v.config.IsEIP158(header.Number))
return nil
})
} else {
validateFuns = append(validateFuns, func() error {
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
}
return nil
})
}
validateFuns = append(validateFuns, func() error {
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
}
return nil
})
validateRes := make(chan error, len(validateFuns))
for _, f := range validateFuns {
tmpFunc := f
Expand Down
78 changes: 5 additions & 73 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ var (

blockRecvTimeDiffGauge = metrics.NewRegisteredGauge("chain/block/recvtimediff", nil)

errStateRootVerificationFailed = errors.New("state root verification failed")
errInsertionInterrupted = errors.New("insertion is interrupted")
errChainStopped = errors.New("blockchain is stopped")
errInvalidOldChain = errors.New("invalid old chain")
errInvalidNewChain = errors.New("invalid new chain")
errInsertionInterrupted = errors.New("insertion is interrupted")
errChainStopped = errors.New("blockchain is stopped")
errInvalidOldChain = errors.New("invalid old chain")
errInvalidNewChain = errors.New("invalid new chain")
)

const (
Expand All @@ -117,7 +116,6 @@ const (
receiptsCacheLimit = 10000
sidecarsCacheLimit = 1024
txLookupCacheLimit = 1024
maxBadBlockLimit = 16
maxFutureBlocks = 256
maxTimeFutureBlocks = 30
TriesInMemory = 128
Expand All @@ -127,8 +125,6 @@ const (
diffLayerFreezerRecheckInterval = 3 * time.Second
maxDiffForkDist = 11 // Maximum allowed backward distance from the chain head

rewindBadBlockInterval = 1 * time.Second

// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
//
// Changelog:
Expand Down Expand Up @@ -298,8 +294,6 @@ type BlockChain struct {

// future blocks are blocks added for later processing
futureBlocks *lru.Cache[common.Hash, *types.Block]
// Cache for the blocks that failed to pass MPT root verification
badBlockCache *lru.Cache[common.Hash, time.Time]

// trusted diff layers
diffLayerCache *exlru.Cache // Cache for the diffLayers
Expand All @@ -320,7 +314,6 @@ type BlockChain struct {
processor Processor // Block transaction processor interface
forker *ForkChoice
vmConfig vm.Config
pipeCommit bool

// monitor
doubleSignMonitor *monitor.DoubleSignMonitor
Expand Down Expand Up @@ -383,7 +376,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
blockCache: lru.NewCache[common.Hash, *types.Block](blockCacheLimit),
txLookupCache: lru.NewCache[common.Hash, txLookup](txLookupCacheLimit),
futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks),
badBlockCache: lru.NewCache[common.Hash, time.Time](maxBadBlockLimit),
diffLayerCache: diffLayerCache,
diffLayerChanCache: diffLayerChanCache,
engine: engine,
Expand Down Expand Up @@ -565,11 +557,6 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
bc.wg.Add(1)
go bc.trustedDiffLayerLoop()
}
if bc.pipeCommit {
// check current block and rewind invalid one
bc.wg.Add(1)
go bc.rewindInvalidHeaderBlockLoop()
}

if bc.doubleSignMonitor != nil {
bc.wg.Add(1)
Expand Down Expand Up @@ -824,26 +811,6 @@ func (bc *BlockChain) SetHeadWithTimestamp(timestamp uint64) error {
return nil
}

func (bc *BlockChain) tryRewindBadBlocks() {
if !bc.chainmu.TryLock() {
return
}
defer bc.chainmu.Unlock()
block := bc.CurrentBlock()
snaps := bc.snaps
// Verified and Result is false
if snaps != nil && snaps.Snapshot(block.Root) != nil &&
snaps.Snapshot(block.Root).Verified() && !snaps.Snapshot(block.Root).WaitAndGetVerifyRes() {
// Rewind by one block
log.Warn("current block verified failed, rewind to its parent", "height", block.Number.Uint64(), "hash", block.Hash())
bc.futureBlocks.Remove(block.Hash())
bc.badBlockCache.Add(block.Hash(), time.Now())
bc.diffLayerCache.Remove(block.Hash())
bc.reportBlock(bc.GetBlockByHash(block.Hash()), nil, errStateRootVerificationFailed)
bc.setHeadBeyondRoot(block.Number.Uint64()-1, 0, common.Hash{}, false)
}
}

// rewindHashHead implements the logic of rewindHead in the context of hash scheme.
func (bc *BlockChain) rewindHashHead(head *types.Header, root common.Hash) (*types.Header, uint64) {
var (
Expand Down Expand Up @@ -1904,7 +1871,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
return nil
}
// Commit all cached state changes into underlying memory database.
_, diffLayer, err := state.Commit(block.NumberU64(), bc.tryRewindBadBlocks, tryCommitTrieDB)
_, diffLayer, err := state.Commit(block.NumberU64(), tryCommitTrieDB)
if err != nil {
return err
}
Expand Down Expand Up @@ -2383,9 +2350,6 @@ type blockProcessingResult struct {
// processBlock executes and validates the given block. If there was no error
// it writes the block and associated state to database.
func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, start time.Time, setHead bool, interruptCh chan struct{}) (_ *blockProcessingResult, blockEndErr error) {
if bc.pipeCommit {
statedb.EnablePipeCommit()
}
statedb.SetExpectedStateRoot(block.Root())

if bc.logger != nil && bc.logger.OnBlockStart != nil {
Expand Down Expand Up @@ -2944,22 +2908,6 @@ func (bc *BlockChain) updateFutureBlocks() {
}
}

func (bc *BlockChain) rewindInvalidHeaderBlockLoop() {
recheck := time.NewTicker(rewindBadBlockInterval)
defer func() {
recheck.Stop()
bc.wg.Done()
}()
for {
select {
case <-recheck.C:
bc.tryRewindBadBlocks()
case <-bc.quit:
return
}
}
}

func (bc *BlockChain) trustedDiffLayerLoop() {
recheck := time.NewTicker(diffLayerFreezerRecheckInterval)
defer func() {
Expand Down Expand Up @@ -3097,17 +3045,6 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool {
return false
}

func (bc *BlockChain) isCachedBadBlock(block *types.Block) bool {
if timeAt, exist := bc.badBlockCache.Get(block.Hash()); exist {
if time.Since(timeAt) >= badBlockCacheExpire {
bc.badBlockCache.Remove(block.Hash())
return false
}
return true
}
return false
}

// reportBlock logs a bad block error.
// bad block need not save receipts & sidecars.
func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) {
Expand Down Expand Up @@ -3169,11 +3106,6 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header) (int, error) {

func (bc *BlockChain) TriesInMemory() uint64 { return bc.triesInMemory }

func EnablePipelineCommit(bc *BlockChain) (*BlockChain, error) {
bc.pipeCommit = false
return bc, nil
}

func EnablePersistDiff(limit uint64) BlockChainOption {
return func(chain *BlockChain) (*BlockChain, error) {
chain.diffLayerFreezerBlockLimit = limit
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestFreezeDiffLayer(t *testing.T) {
// Wait for the buffer to be zero.
}
// Minus one empty block.
if fullBackend.chain.diffQueue.Size() > blockNum-1 && fullBackend.chain.diffQueue.Size() < blockNum-2 {
if fullBackend.chain.diffQueue.Size() != blockNum-1 {
t.Errorf("size of diff queue is wrong, expected: %d, get: %d", blockNum-1, fullBackend.chain.diffQueue.Size())
}

Expand Down
Loading

0 comments on commit 0e2acdf

Please sign in to comment.