Skip to content

Commit

Permalink
Adjusted PR according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Dec 2, 2024
1 parent a691863 commit 7890af0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
53 changes: 19 additions & 34 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,12 +1844,8 @@ func (p *Parlia) distributeIncoming(val common.Address, state *state.StateDB, he
return nil
}

// FIXME Those need some balance change reason, in our current Firehose implementation we had put
// firehose.BalanceChangeReason("reward_transfaction_fee"), this would map to
// `tracing.BalanceIncreaseRewardTransactionFee` in the new tracing API, is that accurate enough here?
// I would be fine adding a new more specific reason if needed.
state.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceIncreaseRewardTransactionFee)
state.AddBalance(coinbase, balance, tracing.BalanceIncreaseRewardTransactionFee)
state.SetBalance(consensus.SystemAddress, common.U2560, tracing.BalanceDecreaseBSCDistributeReward)
state.AddBalance(coinbase, balance, tracing.BalanceIncreaseBSCDistributeReward)
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, vmConfig)
}
Expand Down Expand Up @@ -1959,19 +1955,6 @@ func (p *Parlia) applyTransaction(
receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool,
vmConfig vm.Config,
) (applyErr error) {
_, err := p.applyTransactionWithReceipt(msg, state, header, chainContext, txs, receipts, receivedTxs, usedGas, mining, vmConfig)
return err
}

func (p *Parlia) applyTransactionWithReceipt(
msg callmsg,
state *state.StateDB,
header *types.Header,
chainContext core.ChainContext,
txs *[]*types.Transaction, receipts *[]*types.Receipt,
receivedTxs *[]*types.Transaction, usedGas *uint64, mining bool,
vmConfig vm.Config,
) (receipt *types.Receipt, applyErr error) {
nonce := state.GetNonce(msg.From())
expectedTx := types.NewTransaction(nonce, *msg.To(), msg.Value(), msg.Gas(), msg.GasPrice(), msg.Data())
expectedHash := p.signer.Hash(expectedTx)
Expand All @@ -1980,15 +1963,15 @@ func (p *Parlia) applyTransactionWithReceipt(
var err error
expectedTx, err = p.signTxFn(accounts.Account{Address: msg.From()}, expectedTx, p.chainConfig.ChainID)
if err != nil {
return nil, err
return err
}
} else {
if receivedTxs == nil || len(*receivedTxs) == 0 || (*receivedTxs)[0] == nil {
return nil, errors.New("supposed to get a actual transaction, but get none")
return errors.New("supposed to get a actual transaction, but get none")
}
actualTx := (*receivedTxs)[0]
if !bytes.Equal(p.signer.Hash(actualTx).Bytes(), expectedHash.Bytes()) {
return nil, fmt.Errorf("expected tx hash %v, get %v, nonce %d, to %s, value %s, gas %d, gasPrice %s, data %s", expectedHash.String(), actualTx.Hash().String(),
return fmt.Errorf("expected tx hash %v, get %v, nonce %d, to %s, value %s, gas %d, gasPrice %s, data %s", expectedHash.String(), actualTx.Hash().String(),
expectedTx.Nonce(),
expectedTx.To().String(),
expectedTx.Value().String(),
Expand All @@ -2009,6 +1992,8 @@ func (p *Parlia) applyTransactionWithReceipt(
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(context, vm.TxContext{Origin: msg.From(), GasPrice: big.NewInt(0)}, state, p.chainConfig, vm.Config{})

// Tracing receipt will be set if there is no error and will be used to trace the transaction
var tracingReceipt *types.Receipt
if tracer := vmConfig.Tracer; tracer != nil {
if vmConfig.Tracer.OnSystemTxStart != nil {
vmConfig.Tracer.OnSystemTxStart()
Expand All @@ -2019,7 +2004,7 @@ func (p *Parlia) applyTransactionWithReceipt(

if tracer.OnTxEnd != nil {
defer func() {
tracer.OnTxEnd(receipt, applyErr)
tracer.OnTxEnd(tracingReceipt, applyErr)
}()
}
if vmConfig.Tracer.OnSystemTxEnd != nil {
Expand All @@ -2031,7 +2016,7 @@ func (p *Parlia) applyTransactionWithReceipt(

gasUsed, err := applyMessage(msg, vmenv, state, header, p.chainConfig, chainContext)
if err != nil {
return nil, err
return err
}
*txs = append(*txs, expectedTx)
var root []byte
Expand All @@ -2041,18 +2026,18 @@ func (p *Parlia) applyTransactionWithReceipt(
root = state.IntermediateRoot(p.chainConfig.IsEIP158(header.Number)).Bytes()
}
*usedGas += gasUsed
receipt = types.NewReceipt(root, false, *usedGas)
receipt.TxHash = expectedTx.Hash()
receipt.GasUsed = gasUsed
tracingReceipt = types.NewReceipt(root, false, *usedGas)
tracingReceipt.TxHash = expectedTx.Hash()
tracingReceipt.GasUsed = gasUsed

// Set the receipt logs and create a bloom for filtering
receipt.Logs = state.GetLogs(expectedTx.Hash(), header.Number.Uint64(), header.Hash())
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = header.Hash()
receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(state.TxIndex())
*receipts = append(*receipts, receipt)
return receipt, nil
tracingReceipt.Logs = state.GetLogs(expectedTx.Hash(), header.Number.Uint64(), header.Hash())
tracingReceipt.Bloom = types.CreateBloom(types.Receipts{tracingReceipt})
tracingReceipt.BlockHash = header.Hash()
tracingReceipt.BlockNumber = header.Number
tracingReceipt.TransactionIndex = uint(state.TxIndex())
*receipts = append(*receipts, tracingReceipt)
return nil
}

// GetJustifiedNumberAndHash retrieves the number and hash of the highest justified block
Expand Down
9 changes: 9 additions & 0 deletions core/tracing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ const (
// account within the same tx (captured at end of tx).
// Note it doesn't account for a self-destruct which appoints itself as recipient.
BalanceDecreaseSelfdestructBurn BalanceChangeReason = 14

// BSC specific balance changes

// BalanceDecreaseBSCDistributeReward is a balance change that decreases system address' balance and happens
// when BSC is distributing rewards to validator.
BalanceDecreaseBSCDistributeReward BalanceChangeReason = 210
// BalanceIncreaseBSCDistributeReward is a balance change that increased the block validator's balance and
// happens when BSC is distributing rewards to validator.
BalanceIncreaseBSCDistributeReward BalanceChangeReason = 211
)

// GasChangeReason is used to indicate the reason for a gas change, useful
Expand Down

0 comments on commit 7890af0

Please sign in to comment.