Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the balance change reason withdrawal #50

Open
wants to merge 4 commits into
base: firehose-v2
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/firehose"
"github.com/ledgerwatch/log/v3"
"golang.org/x/crypto/sha3"

"github.com/ledgerwatch/erigon/firehose"

"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/consensus"
Expand Down
4 changes: 2 additions & 2 deletions consensus/serenity/serenity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon/firehose"

"github.com/ledgerwatch/erigon/consensus"
Expand Down Expand Up @@ -138,8 +139,7 @@ func (s *Serenity) Finalize(config *chain.Config, header *types.Header, state *s
}
for _, w := range withdrawals {
amountInWei := new(uint256.Int).Mul(uint256.NewInt(w.Amount), uint256.NewInt(params.GWei))
// CS TODO: confirm the addBalance reason
state.AddBalance(w.Address, amountInWei, false, firehoseContext, firehose.IgnoredBalanceChangeReason)
state.AddBalance(w.Address, amountInWei, false, firehoseContext, firehose.BalanceChangeReason("withdrawal"))
}
return txs, r, nil
}
Expand Down
108 changes: 60 additions & 48 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func ExecuteBlockEphemerallyForBSC(
if !vmConfig.ReadOnly {
if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs, firehoseContext); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand All @@ -128,7 +128,7 @@ func ExecuteBlockEphemerallyForBSC(
if isPoSA {
if isSystemTx, err := posa.IsSystemTransaction(tx, block.Header()); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
} else if isSystemTx {
Expand All @@ -147,8 +147,7 @@ func ExecuteBlockEphemerallyForBSC(
tracer, err := getTracer(i, tx.Hash())
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not obtain tracer: %w", err)
}
Expand All @@ -167,14 +166,10 @@ func ExecuteBlockEphemerallyForBSC(
if err != nil {
if !vmConfig.StatelessExec {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not apply tx %d from block %d [%v]: %w", i, block.NumberU64(), tx.Hash().Hex(), err)
}
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
}
rejectedTxs = append(rejectedTxs, &RejectedTx{i, err.Error()})
} else {
if firehoseContext.Enabled() {
Expand Down Expand Up @@ -213,7 +208,7 @@ func ExecuteBlockEphemerallyForBSC(
outTxs, outReceipts, err := engine.Finalize(chainConfig, header, ibs, block.Transactions(), block.Uncles(), receipts, block.Withdrawals(), epochReader, chainReader, syscall, firehoseContext)
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand All @@ -234,38 +229,41 @@ func ExecuteBlockEphemerallyForBSC(

if chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts {
if !vmConfig.StatelessExec && receiptSha != block.ReceiptHash() {
err := fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
return nil, err
}
}
if !vmConfig.StatelessExec && newBlock.GasUsed() != header.GasUsed {
err := fmt.Errorf("gas used by execution: %d, in header: %d, in new Block: %v", *usedGas, header.GasUsed, newBlock.GasUsed())
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("gas used by execution: %d, in header: %d, in new Block: %v", *usedGas, header.GasUsed, newBlock.GasUsed())
return nil, err
}

var bloom types.Bloom
if !vmConfig.NoReceipts {
bloom = newBlock.Bloom()
if !vmConfig.StatelessExec && bloom != header.Bloom {
err := fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
return nil, err
}
}

if err := ibs.CommitBlock(chainConfig.Rules(header.Number.Uint64(), header.Time), stateWriter); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("committing block %d failed: %w", header.Number.Uint64(), err)
} else if err := stateWriter.WriteChangeSets(); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("writing changesets for block %d failed: %w", header.Number.Uint64(), err)
}
Expand Down Expand Up @@ -349,7 +347,7 @@ func ExecuteBlockEphemerally(
if !vmConfig.ReadOnly {
if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs, firehoseContext); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand All @@ -371,8 +369,7 @@ func ExecuteBlockEphemerally(
tracer, err := getTracer(i, tx.Hash())
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not obtain tracer: %w", err)
}
Expand All @@ -391,14 +388,10 @@ func ExecuteBlockEphemerally(
if err != nil {
if !vmConfig.StatelessExec {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not apply tx %d from block %d [%v]: %w", i, block.NumberU64(), tx.Hash().Hex(), err)
}
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
}
rejectedTxs = append(rejectedTxs, &RejectedTx{i, err.Error()})
} else {
if firehoseContext.Enabled() {
Expand All @@ -413,27 +406,30 @@ func ExecuteBlockEphemerally(

receiptSha := types.DeriveSha(receipts)
if !vmConfig.StatelessExec && chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts && receiptSha != block.ReceiptHash() {
err := fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
return nil, err
}

if !vmConfig.StatelessExec && *usedGas != header.GasUsed {
err := fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
return nil, err
}

var bloom types.Bloom
if !vmConfig.NoReceipts {
bloom = types.CreateBloom(receipts)
if !vmConfig.StatelessExec && bloom != header.Bloom {
err := fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
return nil, err
}
}

Expand All @@ -449,11 +445,20 @@ func ExecuteBlockEphemerally(
firehose.SyncContext().FinalizeBlock(block)
}

withdrawals := block.Withdrawals()
if len(withdrawals) > 0 && !chainConfig.IsShanghai(block.Time()) {
err := fmt.Errorf("withdrawals before shanghai")
if firehoseContext.Enabled() {
firehoseContext.CancelBlock(block, err)
}
return nil, err
}

txs := block.Transactions()
newBlock, _, _, err = FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, block.Withdrawals(), epochReader, chainReader, false, firehoseContext)
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand Down Expand Up @@ -539,7 +544,7 @@ func ExecuteBlockEphemerallyBor(
if !vmConfig.ReadOnly {
if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs, firehoseContext); err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand All @@ -561,8 +566,7 @@ func ExecuteBlockEphemerallyBor(
tracer, err := getTracer(i, tx.Hash())
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not obtain tracer: %w", err)
}
Expand All @@ -581,14 +585,10 @@ func ExecuteBlockEphemerallyBor(
if err != nil {
if !vmConfig.StatelessExec {
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("could not apply tx %d from block %d [%v]: %w", i, block.NumberU64(), tx.Hash().Hex(), err)
}
if firehoseContext.Enabled() {
firehoseContext.RecordFailedTransaction(err)
}
rejectedTxs = append(rejectedTxs, &RejectedTx{i, err.Error()})
} else {
if firehoseContext.Enabled() {
Expand All @@ -603,27 +603,30 @@ func ExecuteBlockEphemerallyBor(

receiptSha := types.DeriveSha(receipts)
if !vmConfig.StatelessExec && chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts && receiptSha != block.ReceiptHash() {
err := fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("mismatched receipt headers for block %d (%s != %s)", block.NumberU64(), receiptSha.Hex(), block.ReceiptHash().Hex())
return nil, err
}

if !vmConfig.StatelessExec && *usedGas != header.GasUsed {
err := fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("gas used by execution: %d, in header: %d", *usedGas, header.GasUsed)
return nil, err
}

var bloom types.Bloom
if !vmConfig.NoReceipts {
bloom = types.CreateBloom(receipts)
if !vmConfig.StatelessExec && bloom != header.Bloom {
err := fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
return nil, err
}
}

Expand All @@ -639,11 +642,20 @@ func ExecuteBlockEphemerallyBor(
firehose.SyncContext().FinalizeBlock(block)
}

withdrawals := block.Withdrawals()
if len(withdrawals) > 0 && !chainConfig.IsShanghai(block.Time()) {
err := fmt.Errorf("withdrawals before shanghai")
if firehoseContext.Enabled() {
firehoseContext.CancelBlock(block, err)
}
return nil, err
}

txs := block.Transactions()
newBlock, _, _, err = FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, block.Withdrawals(), epochReader, chainReader, false, firehoseContext)
if err != nil {
if firehoseContext.Enabled() {
firehoseContext.ExitBlock()
firehoseContext.CancelBlock(block, err)
}
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,11 @@ func (tx *AccessListTx) Sender(signer Signer) (libcommon.Address, error) {
tx.from.Store(addr)
return addr, nil
}

func (tx *AccessListTx) BlobGas() uint64 { return 0 }
func (tx *AccessListTx) BlobGasFeeCap() *big.Int { return nil }
func (tx *AccessListTx) BlobHashes() []libcommon.Hash { return nil }

func (tx *AccessListTx) EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice.ToBig())
}
15 changes: 15 additions & 0 deletions core/types/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,18 @@ func NewEIP1559Transaction(chainID uint256.Int, nonce uint64, to libcommon.Addre
FeeCap: gasFeeCap,
}
}

func (tx *DynamicFeeTransaction) BlobGas() uint64 { return 0 }
func (tx *DynamicFeeTransaction) BlobGasFeeCap() *big.Int { return nil }
func (tx *DynamicFeeTransaction) BlobHashes() []libcommon.Hash { return nil }

func (tx *DynamicFeeTransaction) EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {
return dst.Set(tx.GetFeeCap().ToBig())
}
tip := dst.Sub(tx.GetFeeCap().ToBig(), baseFee)
if tip.Cmp(tx.GetFeeCap().ToBig()) > 0 {
tip.Set(tx.GetFeeCap().ToBig())
}
return tip.Add(tip, baseFee)
}
8 changes: 8 additions & 0 deletions core/types/legacy_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,11 @@ func (tx *LegacyTx) Sender(signer Signer) (libcommon.Address, error) {
tx.from.Store(addr)
return addr, nil
}

func (tx *LegacyTx) BlobGas() uint64 { return 0 }
func (tx *LegacyTx) BlobGasFeeCap() *big.Int { return nil }
func (tx *LegacyTx) BlobHashes() []libcommon.Hash { return nil }

func (tx *LegacyTx) EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice.ToBig())
}
11 changes: 11 additions & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ type Transaction interface {
GetSender() (libcommon.Address, bool)
SetSender(libcommon.Address)
IsContractDeploy() bool

// effectiveGasPrice computes the gas price paid by the transaction, given
// the inclusion block baseFee.
//
// Unlike other TxData methods, the returned *big.Int should be an independent
// copy of the computed value, i.e. callers are allowed to mutate the result.
// Method implementations can use 'dst' to store the result.
EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int
BlobGas() uint64
BlobGasFeeCap() *big.Int
BlobHashes() []libcommon.Hash
}

// TransactionMisc is collection of miscelaneous fields for transaction that is supposed to be embedded into concrete
Expand Down
Loading