Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.H
}

// Finalize implements consensus.Engine and processes withdrawals on top.
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, tracer *tracing.Hooks) {
if !beacon.IsPoSHeader(header) {
beacon.ethone.Finalize(chain, header, state, body)
beacon.ethone.Finalize(chain, header, state, body, tracer)
return
}
// Withdrawals processing.
Expand All @@ -366,9 +366,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.

// FinalizeAndAssemble implements consensus.Engine, setting the final state and
// assembling the block.
func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, error) {
if !beacon.IsPoSHeader(header) {
return beacon.ethone.FinalizeAndAssemble(chain, header, state, body, receipts)
return beacon.ethone.FinalizeAndAssemble(chain, header, state, body, receipts, tracer)
}

shanghai := chain.Config().IsShanghai(header.Number)
Expand All @@ -383,7 +383,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
}
}
// Finalize and assemble the block.
beacon.Finalize(chain, header, state, body)
beacon.Finalize(chain, header, state, body, tracer)

// Assign the final state root to header.
header.Root = state.IntermediateRoot(true)
Expand Down
22 changes: 13 additions & 9 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
Expand Down Expand Up @@ -819,7 +820,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e

// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, tracer *tracing.Hooks) {
headerNumber := header.Number.Uint64()
if body.Withdrawals != nil || header.WithdrawalsHash != nil {
return
Expand All @@ -837,14 +838,14 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header,
start := time.Now()
cx := statefull.ChainContext{Chain: chain, Bor: c}
// check and commit span
if err := c.checkAndCommitSpan(state, header, cx); err != nil {
if err := c.checkAndCommitSpan(state, header, cx, tracer); err != nil {
log.Error("Error while committing span", "error", err)
return
}

if c.HeimdallClient != nil {
// commit states
stateSyncData, err = c.CommitStates(state, header, cx)
stateSyncData, err = c.CommitStates(state, header, cx, tracer)
if err != nil {
log.Error("Error while committing states", "error", err)
return
Expand Down Expand Up @@ -908,7 +909,7 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, error) {
headerNumber := header.Number.Uint64()
if body.Withdrawals != nil || header.WithdrawalsHash != nil {
return nil, consensus.ErrUnexpectedWithdrawals
Expand All @@ -926,14 +927,14 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ
cx := statefull.ChainContext{Chain: chain, Bor: c}

// check and commit span
if err = c.checkAndCommitSpan(state, header, cx); err != nil {
if err = c.checkAndCommitSpan(state, header, cx, tracer); err != nil {
log.Error("Error while committing span", "error", err)
return nil, err
}

if c.HeimdallClient != nil {
// commit states
stateSyncData, err = c.CommitStates(state, header, cx)
stateSyncData, err = c.CommitStates(state, header, cx, tracer)
if err != nil {
log.Error("Error while committing states", "error", err)
return nil, err
Expand Down Expand Up @@ -1108,6 +1109,7 @@ func (c *Bor) checkAndCommitSpan(
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
tracer *tracing.Hooks,
) error {
var ctx = context.Background()
headerNumber := header.Number.Uint64()
Expand All @@ -1118,7 +1120,7 @@ func (c *Bor) checkAndCommitSpan(
}

if c.needToCommitSpan(span, headerNumber) {
return c.FetchAndCommitSpan(ctx, span.ID+1, state, header, chain)
return c.FetchAndCommitSpan(ctx, span.ID+1, state, header, chain, tracer)
}

return nil
Expand Down Expand Up @@ -1149,6 +1151,7 @@ func (c *Bor) FetchAndCommitSpan(
state *state.StateDB,
header *types.Header,
chain core.ChainContext,
tracer *tracing.Hooks,
) error {
var heimdallSpan span.HeimdallSpan

Expand Down Expand Up @@ -1178,14 +1181,15 @@ func (c *Bor) FetchAndCommitSpan(
)
}

return c.spanner.CommitSpan(ctx, heimdallSpan, state, header, chain)
return c.spanner.CommitSpan(ctx, heimdallSpan, state, header, chain, tracer)
}

// CommitStates commit states
func (c *Bor) CommitStates(
state *state.StateDB,
header *types.Header,
chain statefull.ChainContext,
tracer *tracing.Hooks,
) ([]*types.StateSyncData, error) {
fetchStart := time.Now()
number := header.Number.Uint64()
Expand Down Expand Up @@ -1264,7 +1268,7 @@ func (c *Bor) CommitStates(
// we expect that this call MUST emit an event, otherwise we wouldn't make a receipt
// if the receiver address is not a contract then we'll skip the most of the execution and emitting an event as well
// https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/StateReceiver.sol#L27
gasUsed, err = c.GenesisContractsClient.CommitState(eventRecord, state, header, chain)
gasUsed, err = c.GenesisContractsClient.CommitState(eventRecord, state, header, chain, tracer)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/bor/bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestGenesisContractChange(t *testing.T) {
ParentHash: root,
Number: big.NewInt(num),
}
b.Finalize(chain, h, statedb, &types.Body{Withdrawals: nil, Transactions: nil, Uncles: nil})
b.Finalize(chain, h, statedb, &types.Body{Withdrawals: nil, Transactions: nil, Uncles: nil}, nil)

// write state to database
root, err := statedb.Commit(0, false)
Expand Down
4 changes: 3 additions & 1 deletion consensus/bor/contract/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contract

import (
"context"
"github.com/ethereum/go-ethereum/core/tracing"
"math"
"math/big"
"strings"
Expand Down Expand Up @@ -69,6 +70,7 @@ func (gc *GenesisContractsClient) CommitState(
state *state.StateDB,
header *types.Header,
chCtx statefull.ChainContext,
tracer *tracing.Hooks,
) (uint64, error) {
eventRecord := event.BuildEventRecord()

Expand All @@ -91,7 +93,7 @@ func (gc *GenesisContractsClient) CommitState(

log.Info("→ committing new state", "eventRecord", event.ID)

gasUsed, err := statefull.ApplyMessage(context.Background(), msg, state, header, gc.chainConfig, chCtx)
gasUsed, err := statefull.ApplyMessage(context.Background(), msg, state, header, gc.chainConfig, chCtx, 0, tracer)

// Logging event log with time and individual gasUsed
log.Info("→ committed new state", "eventRecord", event.String(gasUsed))
Expand Down
3 changes: 2 additions & 1 deletion consensus/bor/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bor

import (
"github.com/ethereum/go-ethereum/core/tracing"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -12,6 +13,6 @@ import (

//go:generate mockgen -destination=./genesis_contract_mock.go -package=bor . GenesisContract
type GenesisContract interface {
CommitState(event *clerk.EventRecordWithTime, state *state.StateDB, header *types.Header, chCtx statefull.ChainContext) (uint64, error)
CommitState(event *clerk.EventRecordWithTime, state *state.StateDB, header *types.Header, chCtx statefull.ChainContext, tracer *tracing.Hooks) (uint64, error)
LastStateId(state *state.StateDB, number uint64, hash common.Hash) (*big.Int, error)
}
9 changes: 5 additions & 4 deletions consensus/bor/genesis_contract_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions consensus/bor/heimdall/span/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/bor/valset"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -287,7 +288,7 @@ func (c *ChainSpanner) GetCurrentValidatorsByHash(ctx context.Context, headerHas

const method = "commitSpan"

func (c *ChainSpanner) CommitSpan(ctx context.Context, heimdallSpan HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext) error {
func (c *ChainSpanner) CommitSpan(ctx context.Context, heimdallSpan HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext, tracer *tracing.Hooks) error {
// get validators bytes
validators := make([]valset.MinimalVal, 0, len(heimdallSpan.ValidatorSet.Validators))
for _, val := range heimdallSpan.ValidatorSet.Validators {
Expand Down Expand Up @@ -335,7 +336,7 @@ func (c *ChainSpanner) CommitSpan(ctx context.Context, heimdallSpan HeimdallSpan
msg := statefull.GetSystemMessage(c.validatorContractAddress, data)

// apply message
_, err = statefull.ApplyMessage(ctx, msg, state, header, c.chainConfig, chainContext)
_, err = statefull.ApplyMessage(ctx, msg, state, header, c.chainConfig, chainContext, 0, tracer)

return err
}
3 changes: 2 additions & 1 deletion consensus/bor/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bor

import (
"context"
"github.com/ethereum/go-ethereum/core/tracing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/span"
Expand All @@ -17,5 +18,5 @@ type Spanner interface {
GetCurrentSpan(ctx context.Context, headerHash common.Hash) (*span.Span, error)
GetCurrentValidatorsByHash(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error)
GetCurrentValidatorsByBlockNrOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, blockNumber uint64) ([]*valset.Validator, error)
CommitSpan(ctx context.Context, heimdallSpan span.HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext) error
CommitSpan(ctx context.Context, heimdallSpan span.HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext, tracer *tracing.Hooks) error
}
9 changes: 5 additions & 4 deletions consensus/bor/span_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions consensus/bor/statefull/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package statefull
import (
"bytes"
"context"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/crypto"
"math"
"math/big"

Expand Down Expand Up @@ -69,15 +71,33 @@ func ApplyMessage(
header *types.Header,
chainConfig *params.ChainConfig,
chainContext core.ChainContext,
spanID int64,
tracer *tracing.Hooks,
) (uint64, error) {

tx := types.NewTx(&types.LegacyTx{
Nonce: msg.Nonce(),
GasPrice: msg.GasPrice(),
Gas: msg.Gas(),
To: msg.To(),
Value: msg.Value(),
Data: msg.Data(),
})
state.SetTxContext(tx.Hash(), 0)

initialGas := msg.Gas()

// Create a new context to be used in the EVM environment
blockContext := core.NewEVMBlockContext(header, chainContext, &header.Coinbase)

// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, state, chainConfig, vm.Config{})
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, state, chainConfig, vm.Config{Tracer: tracer})

if tracer != nil {
if tracer.OnTxStart != nil {
tracer.OnTxStart(vmenv.GetVMContext(), tx, msg.From())
}
}

// nolint : contextcheck
// Apply the transaction to the current state (included in the env)
Expand Down Expand Up @@ -112,6 +132,26 @@ func ApplyMessage(

gasUsed := initialGas - gasLeft

if tracer != nil {
blockHash := header.Hash()
cumulativeGasUsed := gasUsed

receipt := types.NewReceipt(nil, err != nil, cumulativeGasUsed)
receipt.TxHash = tx.Hash()
receipt.GasUsed = gasUsed

if msg.To() == nil {
receipt.ContractAddress = crypto.CreateAddress(vmenv.TxContext.Origin, tx.Nonce())
}

receipt.Logs = state.GetLogs(tx.Hash(), header.Number.Uint64(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = blockHash
receipt.BlockNumber = header.Number
receipt.TransactionIndex = 0
tracer.OnTxEnd(receipt, nil)
}

return gasUsed, nil
}

Expand Down
7 changes: 4 additions & 3 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/tracing"
"io"
"math/big"
"math/rand"
Expand Down Expand Up @@ -619,18 +620,18 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header

// Finalize implements consensus.Engine. There is no post-transaction
// consensus rules in clique, do nothing here.
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body) {
func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, tracer *tracing.Hooks) {
// No block rewards in PoA, so the state remains as is
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt, tracer *tracing.Hooks) (*types.Block, error) {
if len(body.Withdrawals) > 0 {
return nil, errors.New("clique does not support withdrawals")
}
// Finalize block
c.Finalize(chain, header, state, body)
c.Finalize(chain, header, state, body, tracer)

// Assign the final state root to header.
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
Expand Down
Loading
Loading