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

eip-4844: receipts to include dataGasPrice and dataGasUsed #7385

Merged
merged 2 commits into from
Apr 28, 2023
Merged
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
13 changes: 11 additions & 2 deletions cmd/rpcdaemon/commands/erigon_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"fmt"
"math/big"

"github.com/RoaringBitmap/roaring"
"github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -391,10 +392,18 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
if err != nil {
return nil, fmt.Errorf("getReceipts error: %w", err)
}
var edg *big.Int
if n := block.Number().Uint64(); n > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, block.ParentHash(), n-1); err != nil {
return nil, err
} else {
edg = parentHeader.ExcessDataGas
}
}
result := make([]map[string]interface{}, 0, len(receipts))
for _, receipt := range receipts {
txn := block.Transactions()[receipt.TransactionIndex]
result = append(result, marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true))
result = append(result, marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true, edg))
}

if chainConfig.Bor != nil {
Expand All @@ -405,7 +414,7 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
return nil, err
}
if borReceipt != nil {
result = append(result, marshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false))
result = append(result, marshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, edg))
}
}
}
Expand Down
38 changes: 33 additions & 5 deletions cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
Expand Down Expand Up @@ -646,6 +647,15 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
return nil, fmt.Errorf("getReceipts error: %w", err)
}

var edg *big.Int
if n := block.Number().Uint64(); n > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, block.ParentHash(), n-1); err != nil {
return nil, err
} else {
edg = parentHeader.ExcessDataGas
}
}

if txn == nil {
borReceipt, err := rawdb.ReadBorReceipt(tx, block.Hash(), blockNum, receipts)
if err != nil {
Expand All @@ -654,14 +664,14 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
if borReceipt == nil {
return nil, nil
}
return marshalReceipt(borReceipt, borTx, cc, block.HeaderNoCopy(), txnHash, false), nil
return marshalReceipt(borReceipt, borTx, cc, block.HeaderNoCopy(), txnHash, false, edg), nil
}

if len(receipts) <= int(txnIndex) {
return nil, fmt.Errorf("block has less receipts than expected: %d <= %d, block: %d", len(receipts), int(txnIndex), blockNum)
}

return marshalReceipt(receipts[txnIndex], block.Transactions()[txnIndex], cc, block.HeaderNoCopy(), txnHash, true), nil
return marshalReceipt(receipts[txnIndex], block.Transactions()[txnIndex], cc, block.HeaderNoCopy(), txnHash, true, edg), nil
}

// GetBlockReceipts - receipts for individual block
Expand Down Expand Up @@ -693,9 +703,17 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, number rpc.BlockNumber
return nil, fmt.Errorf("getReceipts error: %w", err)
}
result := make([]map[string]interface{}, 0, len(receipts))
var edg *big.Int
if n := block.Number().Uint64(); n > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, block.ParentHash(), n-1); err != nil {
return nil, err
} else {
edg = parentHeader.ExcessDataGas
}
}
for _, receipt := range receipts {
txn := block.Transactions()[receipt.TransactionIndex]
result = append(result, marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true))
result = append(result, marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true, edg))
}

if chainConfig.Bor != nil {
Expand All @@ -706,15 +724,15 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, number rpc.BlockNumber
return nil, err
}
if borReceipt != nil {
result = append(result, marshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false))
result = append(result, marshalReceipt(borReceipt, borTx, chainConfig, block.HeaderNoCopy(), borReceipt.TxHash, false, edg))
}
}
}

return result, nil
}

func marshalReceipt(receipt *types.Receipt, txn types.Transaction, chainConfig *chain.Config, header *types.Header, txnHash common.Hash, signed bool) map[string]interface{} {
func marshalReceipt(receipt *types.Receipt, txn types.Transaction, chainConfig *chain.Config, header *types.Header, txnHash common.Hash, signed bool, excessDataGas *big.Int) map[string]interface{} {
var chainId *big.Int
switch t := txn.(type) {
case *types.LegacyTx:
Expand Down Expand Up @@ -764,6 +782,16 @@ func marshalReceipt(receipt *types.Receipt, txn types.Transaction, chainConfig *
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}
// Set derived blob related fields
numBlobs := len(txn.GetDataHashes())
if numBlobs > 0 {
if excessDataGas == nil {
log.Warn("excess data gas not set when trying to marshal blob tx")
} else {
fields["dataGasPrice"] = misc.GetDataGasPrice(excessDataGas)
fields["dataGasUsed"] = misc.GetDataGasUsed(numBlobs)
}
}
return fields
}

Expand Down
10 changes: 9 additions & 1 deletion cmd/rpcdaemon/commands/graphql_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ func (api *GraphQLAPIImpl) GetBlockDetails(ctx context.Context, blockNumber rpc.
if err != nil {
return nil, fmt.Errorf("getReceipts error: %w", err)
}
var edg *big.Int
if n := block.Number().Uint64(); n > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, block.ParentHash(), n-1); err != nil {
return nil, err
} else {
edg = parentHeader.ExcessDataGas
}
}
result := make([]map[string]interface{}, 0, len(receipts))
for _, receipt := range receipts {
txn := block.Transactions()[receipt.TransactionIndex]

transaction := marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true)
transaction := marshalReceipt(receipt, txn, chainConfig, block.HeaderNoCopy(), txn.Hash(), true, edg)
transaction["nonce"] = txn.GetNonce()
transaction["value"] = txn.GetValue()
transaction["data"] = txn.GetData()
Expand Down
21 changes: 19 additions & 2 deletions cmd/rpcdaemon/commands/otterscan_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func (api *OtterscanAPIImpl) searchTransactionsBeforeV3(tx kv.TemporalTx, ctx co
receipts := make([]map[string]interface{}, 0, pageSize)
resultCount := uint16(0)

var excessDataGas *big.Int
for txNumsIter.HasNext() {
txNum, blockNum, txIndex, isFinalTxn, blockNumChanged, err := txNumsIter.Next()
if err != nil {
Expand All @@ -306,6 +307,13 @@ func (api *OtterscanAPIImpl) searchTransactionsBeforeV3(tx kv.TemporalTx, ctx co
}
blockHash = header.Hash()
exec.changeBlock(header)
if blockNum > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, header.ParentHash, blockNum-1); err != nil {
return nil, err
} else {
excessDataGas = parentHeader.ExcessDataGas
}
}
}

//fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, maxTxNumInBlock=%d,mixTxNumInBlock=%d\n", txNum, blockNum, txIndex, maxTxNumInBlock, minTxNumInBlock)
Expand All @@ -327,7 +335,7 @@ func (api *OtterscanAPIImpl) searchTransactionsBeforeV3(tx kv.TemporalTx, ctx co
TransactionIndex: uint(txIndex),
BlockNumber: header.Number, BlockHash: blockHash, Logs: rawLogs,
}
mReceipt := marshalReceipt(receipt, txn, chainConfig, header, txn.Hash(), true)
mReceipt := marshalReceipt(receipt, txn, chainConfig, header, txn.Hash(), true, excessDataGas)
mReceipt["timestamp"] = header.Time
receipts = append(receipts, mReceipt)

Expand Down Expand Up @@ -577,10 +585,19 @@ func (api *OtterscanAPIImpl) GetBlockTransactions(ctx context.Context, number rp
if err != nil {
return nil, fmt.Errorf("getReceipts error: %v", err)
}

var edg *big.Int
if n := b.Number().Uint64(); n > 0 {
if parentHeader, err := api._blockReader.Header(ctx, tx, b.ParentHash(), n-1); err != nil {
return nil, err
} else {
edg = parentHeader.ExcessDataGas
}
}
result := make([]map[string]interface{}, 0, len(receipts))
for _, receipt := range receipts {
txn := b.Transactions()[receipt.TransactionIndex]
marshalledRcpt := marshalReceipt(receipt, txn, chainConfig, b.HeaderNoCopy(), txn.Hash(), true)
marshalledRcpt := marshalReceipt(receipt, txn, chainConfig, b.HeaderNoCopy(), txn.Hash(), true, edg)
marshalledRcpt["logs"] = nil
marshalledRcpt["logsBloom"] = nil
result = append(result, marshalledRcpt)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/otterscan_search_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (api *OtterscanAPIImpl) traceBlock(dbtx kv.Tx, ctx context.Context, blockNu

if tracer.Found {
rpcTx := newRPCTransaction(tx, block.Hash(), blockNum, uint64(idx), block.BaseFee())
mReceipt := marshalReceipt(blockReceipts[idx], tx, chainConfig, block.HeaderNoCopy(), tx.Hash(), true)
mReceipt := marshalReceipt(blockReceipts[idx], tx, chainConfig, block.HeaderNoCopy(), tx.Hash(), true, excessDataGas)
mReceipt["timestamp"] = block.Time()
rpcTxs = append(rpcTxs, rpcTx)
receipts = append(receipts, mReceipt)
Expand Down
6 changes: 3 additions & 3 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, header *types.Header, tx types.Transaction, usedGas *uint64, evm vm.VMInterface, cfg vm.Config) (*types.Receipt, []byte, error) {
func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, header *types.Header, tx types.Transaction, usedGas *uint64, evm vm.VMInterface, cfg vm.Config, excessDataGas *big.Int) (*types.Receipt, []byte, error) {
rules := evm.ChainRules()
msg, err := tx.AsMessage(*types.MakeSigner(config, header.Number.Uint64()), header.BaseFee, rules)
if err != nil {
Expand All @@ -45,7 +45,7 @@ func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *G
if msg.FeeCap().IsZero() && engine != nil {
// Only zero-gas transactions may be service ones
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
return SysCallContract(contract, data, *config, ibs, header, engine, true /* constCall */, nil /*excessDataGas*/)
return SysCallContract(contract, data, *config, ibs, header, engine, true /* constCall */, excessDataGas)
}
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
}
Expand Down Expand Up @@ -109,5 +109,5 @@ func ApplyTransaction(config *chain.Config, blockHashFunc func(n uint64) libcomm
blockContext := NewEVMBlockContext(header, blockHashFunc, engine, author, excessDataGas)
vmenv := vm.NewEVM(blockContext, evmtypes.TxContext{}, ibs, config, cfg)

return applyTransaction(config, engine, gp, ibs, stateWriter, header, tx, usedGas, vmenv, cfg)
return applyTransaction(config, engine, gp, ibs, stateWriter, header, tx, usedGas, vmenv, cfg, excessDataGas)
}