Skip to content

Commit

Permalink
Move bor_fee_log from core to polygon (#12176)
Browse files Browse the repository at this point in the history
Cherry pick #10810
  • Loading branch information
yperbasis authored and AskAlexSharov committed Oct 21, 2024
1 parent 088da9b commit 8b71cee
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 208 deletions.
9 changes: 5 additions & 4 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,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 @@ -510,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 @@ -548,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 @@ -640,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 @@ -694,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
17 changes: 17 additions & 0 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -993,6 +994,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
9 changes: 9 additions & 0 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"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 @@ -1219,6 +1220,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
16 changes: 12 additions & 4 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +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/turbo/services"
"github.com/ledgerwatch/log/v3"

"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/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 @@ -643,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
}
14 changes: 14 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (

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

"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 @@ -125,6 +127,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 @@ -194,3 +200,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)
}
11 changes: 10 additions & 1 deletion consensus/ethash/ethash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ import (

"github.com/edsrzf/mmap-go"
"github.com/hashicorp/golang-lru/v2/simplelru"
"github.com/ledgerwatch/erigon/consensus/ethash/ethashcfg"

"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"
"github.com/ledgerwatch/log/v3"
)
Expand Down Expand Up @@ -573,3 +574,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
}
9 changes: 9 additions & 0 deletions consensus/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"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"
"github.com/ledgerwatch/log/v3"
Expand Down Expand Up @@ -340,6 +341,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
60 changes: 18 additions & 42 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/holiman/uint256"

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

"github.com/ledgerwatch/erigon/consensus"
Expand Down Expand Up @@ -62,23 +61,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,
ExcessBlobGas: excessBlobGas,
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,
ExcessBlobGas: excessBlobGas,
}
}

Expand Down Expand Up @@ -130,30 +133,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 8b71cee

Please sign in to comment.