From 94d1ab517f2b060c7829545753cde017b4678949 Mon Sep 17 00:00:00 2001 From: Rafael Sampaio <5679073+r4f4ss@users.noreply.github.com> Date: Tue, 24 Sep 2024 01:06:02 -0300 Subject: [PATCH 1/3] implementation of eth_getBlockReceipts and eth_getBlockTransactionCountByHash --- portalnetwork/ethapi/api.go | 98 +++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/portalnetwork/ethapi/api.go b/portalnetwork/ethapi/api.go index 6b9945790069..585d382eeeac 100644 --- a/portalnetwork/ethapi/api.go +++ b/portalnetwork/ethapi/api.go @@ -1,6 +1,8 @@ package ethapi import ( + "errors" + "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -10,8 +12,53 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/portalnetwork/history" + "github.com/ethereum/go-ethereum/rpc" ) +var errParameterNotImplemented = errors.New("parameter not implemented") + +// marshalReceipt marshals a transaction receipt into a JSON object. +func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber uint64, signer types.Signer, tx *types.Transaction, txIndex int) map[string]interface{} { + from, _ := types.Sender(signer, tx) + + fields := map[string]interface{}{ + "blockHash": blockHash, + "blockNumber": hexutil.Uint64(blockNumber), + "transactionHash": tx.Hash(), + "transactionIndex": hexutil.Uint64(txIndex), + "from": from, + "to": tx.To(), + "gasUsed": hexutil.Uint64(receipt.GasUsed), + "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), + "contractAddress": nil, + "logs": receipt.Logs, + "logsBloom": receipt.Bloom, + "type": hexutil.Uint(tx.Type()), + "effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice), + } + + // Assign receipt status or post state. + if len(receipt.PostState) > 0 { + fields["root"] = hexutil.Bytes(receipt.PostState) + } else { + fields["status"] = hexutil.Uint(receipt.Status) + } + if receipt.Logs == nil { + fields["logs"] = []*types.Log{} + } + + if tx.Type() == types.BlobTxType { + fields["blobGasUsed"] = hexutil.Uint64(receipt.BlobGasUsed) + fields["blobGasPrice"] = (*hexutil.Big)(receipt.BlobGasPrice) + } + + // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation + if receipt.ContractAddress != (common.Address{}) { + fields["contractAddress"] = receipt.ContractAddress + } + return fields +} + type API struct { History *history.HistoryNetwork ChainID *big.Int @@ -38,3 +85,54 @@ func (p *API) GetBlockByHash(hash *common.Hash, fullTransactions bool) (map[stri //static configuration of Config, currently only mainnet implemented return ethapi.RPCMarshalBlock(block, true, fullTransactions, params.MainnetChainConfig), nil } + +func (p *API) GetBlockReceipts(blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) { + hash, isHhash := blockNrOrHash.Hash() + if !isHhash { + return nil, errParameterNotImplemented + } + + blockReceipts, err := p.History.GetReceipts(hash.Bytes()) + if err != nil { + log.Error(err.Error()) + return nil, err + } + + blockBody, err := p.History.GetBlockBody(hash.Bytes()) + if err != nil { + log.Error(err.Error()) + return nil, err + } + + blockHeader, err := p.History.GetBlockHeader(hash.Bytes()) + if err != nil { + log.Error(err.Error()) + return nil, err + } + + txs := blockBody.Transactions + if len(txs) != len(blockReceipts) { + return nil, fmt.Errorf("receipts length mismatch: %d vs %d", len(txs), len(blockReceipts)) + } + + // Derive the sender. + signer := types.MakeSigner(params.MainnetChainConfig, blockHeader.Number, blockHeader.Time) + + result := make([]map[string]interface{}, len(blockReceipts)) + for i, receipt := range blockReceipts { + result[i] = marshalReceipt(receipt, blockHeader.Hash(), blockHeader.Number.Uint64(), signer, txs[i], i) + } + + return result, nil +} + +func (p *API) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint { + blockBody, err := p.History.GetBlockBody(hash.Bytes()) + if err != nil { + log.Error(err.Error()) + return nil + } + + n := hexutil.Uint(len(blockBody.Transactions)) + return &n +} From d1f8a53b19f1e43bf6bd74af0b0c8142d3fa3d9d Mon Sep 17 00:00:00 2001 From: Rafael Sampaio <5679073+r4f4ss@users.noreply.github.com> Date: Tue, 24 Sep 2024 01:23:37 -0300 Subject: [PATCH 2/3] exchange of cp to mv in make file --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c206c9818c7d..11e53d4ca4b9 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ GORUN = go run shisui: go build ./cmd/shisui/main.go mkdir -p $(GOBIN) - cp main $(GOBIN)/shisui + mv main $(GOBIN)/shisui @echo "Done building." @echo "Run \"$(GOBIN)/shisui\" to launch shisui." From f2d525092ee2f43a388629902b5a75edfc16a1b4 Mon Sep 17 00:00:00 2001 From: Rafael Sampaio <5679073+r4f4ss@users.noreply.github.com> Date: Tue, 24 Sep 2024 01:32:12 -0300 Subject: [PATCH 3/3] removal of code from other PR (my mistake) --- portalnetwork/ethapi/api.go | 98 ------------------------------------- 1 file changed, 98 deletions(-) diff --git a/portalnetwork/ethapi/api.go b/portalnetwork/ethapi/api.go index 585d382eeeac..6b9945790069 100644 --- a/portalnetwork/ethapi/api.go +++ b/portalnetwork/ethapi/api.go @@ -1,8 +1,6 @@ package ethapi import ( - "errors" - "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -12,53 +10,8 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/portalnetwork/history" - "github.com/ethereum/go-ethereum/rpc" ) -var errParameterNotImplemented = errors.New("parameter not implemented") - -// marshalReceipt marshals a transaction receipt into a JSON object. -func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber uint64, signer types.Signer, tx *types.Transaction, txIndex int) map[string]interface{} { - from, _ := types.Sender(signer, tx) - - fields := map[string]interface{}{ - "blockHash": blockHash, - "blockNumber": hexutil.Uint64(blockNumber), - "transactionHash": tx.Hash(), - "transactionIndex": hexutil.Uint64(txIndex), - "from": from, - "to": tx.To(), - "gasUsed": hexutil.Uint64(receipt.GasUsed), - "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), - "contractAddress": nil, - "logs": receipt.Logs, - "logsBloom": receipt.Bloom, - "type": hexutil.Uint(tx.Type()), - "effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice), - } - - // Assign receipt status or post state. - if len(receipt.PostState) > 0 { - fields["root"] = hexutil.Bytes(receipt.PostState) - } else { - fields["status"] = hexutil.Uint(receipt.Status) - } - if receipt.Logs == nil { - fields["logs"] = []*types.Log{} - } - - if tx.Type() == types.BlobTxType { - fields["blobGasUsed"] = hexutil.Uint64(receipt.BlobGasUsed) - fields["blobGasPrice"] = (*hexutil.Big)(receipt.BlobGasPrice) - } - - // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation - if receipt.ContractAddress != (common.Address{}) { - fields["contractAddress"] = receipt.ContractAddress - } - return fields -} - type API struct { History *history.HistoryNetwork ChainID *big.Int @@ -85,54 +38,3 @@ func (p *API) GetBlockByHash(hash *common.Hash, fullTransactions bool) (map[stri //static configuration of Config, currently only mainnet implemented return ethapi.RPCMarshalBlock(block, true, fullTransactions, params.MainnetChainConfig), nil } - -func (p *API) GetBlockReceipts(blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) { - hash, isHhash := blockNrOrHash.Hash() - if !isHhash { - return nil, errParameterNotImplemented - } - - blockReceipts, err := p.History.GetReceipts(hash.Bytes()) - if err != nil { - log.Error(err.Error()) - return nil, err - } - - blockBody, err := p.History.GetBlockBody(hash.Bytes()) - if err != nil { - log.Error(err.Error()) - return nil, err - } - - blockHeader, err := p.History.GetBlockHeader(hash.Bytes()) - if err != nil { - log.Error(err.Error()) - return nil, err - } - - txs := blockBody.Transactions - if len(txs) != len(blockReceipts) { - return nil, fmt.Errorf("receipts length mismatch: %d vs %d", len(txs), len(blockReceipts)) - } - - // Derive the sender. - signer := types.MakeSigner(params.MainnetChainConfig, blockHeader.Number, blockHeader.Time) - - result := make([]map[string]interface{}, len(blockReceipts)) - for i, receipt := range blockReceipts { - result[i] = marshalReceipt(receipt, blockHeader.Hash(), blockHeader.Number.Uint64(), signer, txs[i], i) - } - - return result, nil -} - -func (p *API) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint { - blockBody, err := p.History.GetBlockBody(hash.Bytes()) - if err != nil { - log.Error(err.Error()) - return nil - } - - n := hexutil.Uint(len(blockBody.Transactions)) - return &n -}