Skip to content

Commit

Permalink
Move bor_fee_log from core to polygon (#10810)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Jun 20, 2024
1 parent f961b1f commit 1427994
Show file tree
Hide file tree
Showing 25 changed files with 270 additions and 233 deletions.
12 changes: 6 additions & 6 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/log/v3"
state2 "github.com/ledgerwatch/erigon-lib/state"
types2 "github.com/ledgerwatch/erigon-lib/types"

Expand All @@ -49,6 +48,7 @@ import (
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/event"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
Expand Down Expand Up @@ -511,7 +511,7 @@ func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract libcommon
return b.pendingState.GetCode(contract), nil
}

func newRevertError(result *core.ExecutionResult) *revertError {
func newRevertError(result *evmtypes.ExecutionResult) *revertError {
reason, errUnpack := abi.UnpackRevert(result.Revert())
err := errors.New("execution reverted")
if errUnpack == nil {
Expand Down Expand Up @@ -549,7 +549,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM
if blockNumber != nil && blockNumber.Cmp(b.pendingBlock.Number()) != 0 {
return nil, errBlockNumberUnsupported
}
var res *core.ExecutionResult
var res *evmtypes.ExecutionResult
if err := b.m.DB.View(context.Background(), func(tx kv.Tx) (err error) {
s := state.New(b.m.NewStateReader(tx))
res, err = b.callContract(ctx, call, b.pendingBlock, s)
Expand Down Expand Up @@ -641,7 +641,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
b.pendingState.SetTxContext(libcommon.Hash{}, libcommon.Hash{}, len(b.pendingBlock.Transactions()))

// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) (bool, *core.ExecutionResult, error) {
executable := func(gas uint64) (bool, *evmtypes.ExecutionResult, error) {
call.Gas = gas

snapshot := b.pendingState.Snapshot()
Expand Down Expand Up @@ -695,7 +695,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs

// callContract implements common code between normal and pending contract calls.
// state is modified during execution, make sure to copy it if necessary.
func (b *SimulatedBackend) callContract(_ context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.IntraBlockState) (*core.ExecutionResult, error) {
func (b *SimulatedBackend) callContract(_ context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.IntraBlockState) (*evmtypes.ExecutionResult, error) {
const baseFeeUpperLimit = 880000000
// Ensure message is initialized properly.
if call.GasPrice == nil {
Expand Down
20 changes: 18 additions & 2 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
grpcHealth "google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"

"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
Expand All @@ -39,6 +37,7 @@ import (
"github.com/ledgerwatch/erigon-lib/kv/remotedb"
"github.com/ledgerwatch/erigon-lib/kv/remotedbserver"
"github.com/ledgerwatch/erigon-lib/kv/temporal"
"github.com/ledgerwatch/erigon-lib/log/v3"
libstate "github.com/ledgerwatch/erigon-lib/state"

"github.com/ledgerwatch/erigon/cmd/rpcdaemon/cli/httpcfg"
Expand All @@ -54,6 +53,7 @@ import (
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/node"
"github.com/ledgerwatch/erigon/node/nodecfg"
Expand Down Expand Up @@ -986,6 +986,22 @@ func (e *remoteConsensusEngine) Initialize(config *chain.Config, chain consensus
e.engine.Initialize(config, chain, header, state, syscall, logger)
}

func (e *remoteConsensusEngine) GetTransferFunc() evmtypes.TransferFunc {
if err := e.validateEngineReady(); err != nil {
panic(err)
}

return e.engine.GetTransferFunc()
}

func (e *remoteConsensusEngine) GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc {
if err := e.validateEngineReady(); err != nil {
panic(err)
}

return e.engine.GetPostApplyMessageFunc()
}

func (e *remoteConsensusEngine) VerifyHeader(_ consensus.ChainHeaderReader, _ *types.Header, _ bool) error {
panic("remoteConsensusEngine.VerifyHeader not supported")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/state/exec3/trace_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/state"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (e *TraceWorker) GetLogs(txIdx int, txn types.Transaction) types.Logs {
return e.ibs.GetLogs(txn.Hash())
}

func (e *TraceWorker) ExecTxn(txNum uint64, txIndex int, txn types.Transaction) (*core.ExecutionResult, error) {
func (e *TraceWorker) ExecTxn(txNum uint64, txIndex int, txn types.Transaction) (*evmtypes.ExecutionResult, error) {
e.stateReader.SetTxNum(txNum)
txHash := txn.Hash()
e.ibs.Reset()
Expand Down
14 changes: 11 additions & 3 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ import (

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/clique"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/rpc"
)
Expand Down Expand Up @@ -1224,6 +1224,14 @@ func (c *AuRa) ExecuteSystemWithdrawals(withdrawals []*types.Withdrawal, syscall
return err
}

func (c *AuRa) GetTransferFunc() evmtypes.TransferFunc {
return consensus.Transfer
}

func (c *AuRa) GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc {
return nil
}

/*
// extracts the empty steps from the header seal. should only be called when there are 3 fields in the seal
// (i.e. header.number() >= self.empty_steps_transition).
Expand Down
19 changes: 13 additions & 6 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,29 @@ import (
"sync"
"time"

"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"

"github.com/goccy/go-json"
lru "github.com/hashicorp/golang-lru/arc/v2"

"github.com/ledgerwatch/erigon-lib/log/v3"
"github.com/ledgerwatch/erigon/turbo/services"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"
"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/crypto/cryptopool"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/services"
)

const (
Expand Down Expand Up @@ -644,3 +643,11 @@ func (c *Clique) snapshots(latest uint64, total int) ([]*Snapshot, error) {

return res, nil
}

func (c *Clique) GetTransferFunc() evmtypes.TransferFunc {
return consensus.Transfer
}

func (c *Clique) GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc {
return nil
}
17 changes: 15 additions & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/rpc"
)
Expand Down Expand Up @@ -133,6 +134,10 @@ type EngineReader interface {
CalculateRewards(config *chain.Config, header *types.Header, uncles []*types.Header, syscall SystemCall,
) ([]Reward, error)

GetTransferFunc() evmtypes.TransferFunc

GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc

// Close terminates any background threads, DB's etc maintained by the consensus engine.
Close() error
}
Expand Down Expand Up @@ -202,3 +207,11 @@ type PoW interface {
// Hashrate returns the current mining hashrate of a PoW consensus engine.
Hashrate() float64
}

// Transfer subtracts amount from sender and adds amount to recipient using the given Db
func Transfer(db evmtypes.IntraBlockState, sender, recipient libcommon.Address, amount *uint256.Int, bailout bool) {
if !bailout {
db.SubBalance(sender, amount)
}
db.AddBalance(recipient, amount)
}
13 changes: 11 additions & 2 deletions consensus/ethash/ethash.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ import (
"github.com/edsrzf/mmap-go"
"github.com/hashicorp/golang-lru/v2/simplelru"

"github.com/ledgerwatch/erigon/consensus/ethash/ethashcfg"

"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon/common/debug"
cmath "github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/ethash/ethashcfg"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/rpc"
)

Expand Down Expand Up @@ -574,3 +575,11 @@ func (ethash *Ethash) APIs(chain consensus.ChainHeaderReader) []rpc.API {
func SeedHash(block uint64) []byte {
return seedHash(block)
}

func (ethash *Ethash) GetTransferFunc() evmtypes.TransferFunc {
return consensus.Transfer
}

func (ethash *Ethash) GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc {
return nil
}
10 changes: 10 additions & 0 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/log/v3"

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/aura"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rpc"
)
Expand Down Expand Up @@ -334,6 +336,14 @@ func (s *Merge) APIs(chain consensus.ChainHeaderReader) []rpc.API {
return s.eth1Engine.APIs(chain)
}

func (s *Merge) GetTransferFunc() evmtypes.TransferFunc {
return s.eth1Engine.GetTransferFunc()
}

func (s *Merge) GetPostApplyMessageFunc() evmtypes.PostApplyMessageFunc {
return s.eth1Engine.GetPostApplyMessageFunc()
}

func (s *Merge) Close() error {
return s.eth1Engine.Close()
}
Expand Down
59 changes: 18 additions & 41 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,27 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libco
}

var transferFunc evmtypes.TransferFunc
if engine != nil && engine.Type() == chain.BorConsensus {
transferFunc = BorTransfer
var postApplyMessageFunc evmtypes.PostApplyMessageFunc
if engine != nil {
transferFunc = engine.GetTransferFunc()
postApplyMessageFunc = engine.GetPostApplyMessageFunc()
} else {
transferFunc = Transfer
transferFunc = consensus.Transfer
postApplyMessageFunc = nil
}
return evmtypes.BlockContext{
CanTransfer: CanTransfer,
Transfer: transferFunc,
GetHash: blockHashFunc,
Coinbase: beneficiary,
BlockNumber: header.Number.Uint64(),
Time: header.Time,
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: &baseFee,
GasLimit: header.GasLimit,
PrevRanDao: prevRandDao,
BlobBaseFee: blobBaseFee,
CanTransfer: CanTransfer,
Transfer: transferFunc,
GetHash: blockHashFunc,
PostApplyMessage: postApplyMessageFunc,
Coinbase: beneficiary,
BlockNumber: header.Number.Uint64(),
Time: header.Time,
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: &baseFee,
GasLimit: header.GasLimit,
PrevRanDao: prevRandDao,
BlobBaseFee: blobBaseFee,
}
}

Expand Down Expand Up @@ -135,30 +139,3 @@ func GetHashFn(ref *types.Header, getHeader func(hash libcommon.Hash, number uin
func CanTransfer(db evmtypes.IntraBlockState, addr libcommon.Address, amount *uint256.Int) bool {
return !db.GetBalance(addr).Lt(amount)
}

// Transfer subtracts amount from sender and adds amount to recipient using the given Db
func Transfer(db evmtypes.IntraBlockState, sender, recipient libcommon.Address, amount *uint256.Int, bailout bool) {
if !bailout {
db.SubBalance(sender, amount)
}
db.AddBalance(recipient, amount)
}

// BorTransfer transfer in Bor
func BorTransfer(db evmtypes.IntraBlockState, sender, recipient libcommon.Address, amount *uint256.Int, bailout bool) {
// get inputs before
input1 := db.GetBalance(sender).Clone()
input2 := db.GetBalance(recipient).Clone()

if !bailout {
db.SubBalance(sender, amount)
}
db.AddBalance(recipient, amount)

// get outputs after
output1 := db.GetBalance(sender).Clone()
output2 := db.GetBalance(recipient).Clone()

// add transfer log
AddTransferLog(db, sender, recipient, amount, input1, input2, output1, output2)
}
Loading

0 comments on commit 1427994

Please sign in to comment.