Skip to content

Commit

Permalink
feat: support customized tx body
Browse files Browse the repository at this point in the history
Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>
  • Loading branch information
GeekArthur committed Jul 29, 2024
1 parent 3b6a8e4 commit 71e84ea
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,7 @@ func (ec *SDKClient) GetNativeTransferGasLimit(ctx context.Context, toAddress st
func (ec *SDKClient) GetL1DataFee(ctx context.Context, ethTxBytes []byte) (*big.Int, error) {
return nil, errors.New("GetL1DataFee not implemented")
}

func (ec *SDKClient) GetCustomizedBlockBody(raw json.RawMessage, body *RPCBlock) error {
return errors.New("GetCustomizedBlockBody not implemented")
}
3 changes: 3 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ type RosettaConfig struct {

// CustomizedTraceConfig is the blockchain customized trace config
CustomizedTraceConfig interface{}

// SupportCustomizedBlockBody indicates if the blockchain supports customized block body
SupportCustomizedBlockBody bool
}

type Token struct {
Expand Down
18 changes: 18 additions & 0 deletions mocks/services/client.go

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

30 changes: 26 additions & 4 deletions services/block_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,28 @@ func (s *BlockAPIService) PopulateTransaction(
traceList = append(traceList, traceMap)
}

var gasLimit uint64
if tx.Receipt != nil && tx.Receipt.GasUsed != nil {
gasLimit = tx.Receipt.GasUsed.Uint64()
} else {
gasLimit = tx.Transaction.Gas()
}

var gasPrice *big.Int
if tx.Receipt != nil && tx.Receipt.GasPrice != nil {
gasPrice = tx.Receipt.GasPrice
} else {
gasPrice = tx.Transaction.GasPrice()
}

populatedTransaction := &RosettaTypes.Transaction{
TransactionIdentifier: &RosettaTypes.TransactionIdentifier{
Hash: tx.TxHash.String(),
},
Operations: ops,
Metadata: map[string]interface{}{
"gas_limit": hexutil.EncodeUint64(tx.Transaction.Gas()),
"gas_price": hexutil.EncodeBig(tx.Transaction.GasPrice()),
"gas_limit": hexutil.EncodeUint64(gasLimit),
"gas_price": hexutil.EncodeBig(gasPrice),
"receipt": receiptMap,
"trace": traceList,
},
Expand Down Expand Up @@ -217,9 +231,17 @@ func (s *BlockAPIService) GetBlock(
if err := json.Unmarshal(raw, &head); err != nil {
return nil, nil, nil, err
}
if err := json.Unmarshal(raw, &body); err != nil {
return nil, nil, nil, err
if s.config.RosettaCfg.SupportCustomizedBlockBody {
err = s.client.GetCustomizedBlockBody(raw, &body)
if err != nil {
return nil, nil, nil, err
}
} else {
if err := json.Unmarshal(raw, &body); err != nil {
return nil, nil, nil, err
}
}

// Note: We need a full node to return a complete RPCBlock,
// otherwise, only body.Hash is populated. body.Transactions is empty.
// TODO(xiaying): log warn if len(body.Hash) > 1 && len(body.txs) == 0
Expand Down
3 changes: 3 additions & 0 deletions services/construction/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Client interface {
// SkipTxReceiptParsing determines if the tx receipt parsing can be skipped for specific contract address
SkipTxReceiptParsing(contractAddress string) bool

// GetCustomizedBlockBody returns the customized block body
GetCustomizedBlockBody(raw json.RawMessage, body *evmClient.RPCBlock) error

// TraceBlockByHash returns all traces for each transaction in the block
// by calling geth debug_traceBlockByHash JSON RPC.
// The output is map which key is transaction hash, and the value is list of
Expand Down

0 comments on commit 71e84ea

Please sign in to comment.