diff --git a/jsonrpc/eth_blockchain_test.go b/jsonrpc/eth_blockchain_test.go index fd4141b13d..7989a16553 100644 --- a/jsonrpc/eth_blockchain_test.go +++ b/jsonrpc/eth_blockchain_test.go @@ -191,8 +191,7 @@ func TestEth_Block_GetTransactionByBlockNumberAndIndex(t *testing.T) { transaction := toTransaction( block.Transactions[testIndex], - argUintPtr(block.Number()), - argHashPtr(block.Hash()), + block.Header, &testIndex, ) @@ -217,8 +216,7 @@ func TestEth_Block_GetTransactionByBlockHashAndIndex(t *testing.T) { transaction := toTransaction( block.Transactions[testIndex], - argUintPtr(block.Number()), - argHashPtr(block.Hash()), + block.Header, &testIndex, ) diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index c2b7e66f09..c7d61ed560 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -361,12 +361,9 @@ func (e *Eth) GetTransactionByHash(hash types.Hash) (interface{}, error) { // Find the transaction within the block if txn, idx := types.FindTxByHash(block.Transactions, hash); txn != nil { - txn.SetGasPrice(txn.GetGasPrice(block.Header.BaseFee)) - return toTransaction( txn, - argUintPtr(block.Number()), - argHashPtr(block.Hash()), + block.Header, &idx, ) } diff --git a/jsonrpc/helper.go b/jsonrpc/helper.go index 1e3feff457..12a142ea44 100644 --- a/jsonrpc/helper.go +++ b/jsonrpc/helper.go @@ -56,8 +56,7 @@ func GetTransactionByBlockAndIndex(block *types.Block, index argUint64) (interfa return toTransaction( block.Transactions[index], - argUintPtr(block.Number()), - argHashPtr(block.Hash()), + block.Header, &idx, ), nil } diff --git a/jsonrpc/txpool_endpoint.go b/jsonrpc/txpool_endpoint.go index c2ecc6aac1..a32e227eba 100644 --- a/jsonrpc/txpool_endpoint.go +++ b/jsonrpc/txpool_endpoint.go @@ -51,7 +51,7 @@ func (t *TxPool) ContentFrom(addr types.Address) (interface{}, error) { convertTxMap := func(txs []*types.Transaction) map[uint64]*transaction { result := make(map[uint64]*transaction, len(txs)) for _, tx := range txs { - result[tx.Nonce()] = toTransaction(tx, nil, &types.ZeroHash, nil) + result[tx.Nonce()] = toTransaction(tx, nil, nil) } return result @@ -76,7 +76,7 @@ func (t *TxPool) Content() (interface{}, error) { result[addr] = make(map[uint64]*transaction, len(txs)) for _, tx := range txs { - result[addr][tx.Nonce()] = toTransaction(tx, nil, &types.ZeroHash, nil) + result[addr][tx.Nonce()] = toTransaction(tx, nil, nil) } } diff --git a/jsonrpc/txpool_endpoint_test.go b/jsonrpc/txpool_endpoint_test.go index 430f38f93e..4e10383a25 100644 --- a/jsonrpc/txpool_endpoint_test.go +++ b/jsonrpc/txpool_endpoint_test.go @@ -8,6 +8,7 @@ import ( "github.com/0xPolygon/polygon-edge/types" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestContentEndpoint(t *testing.T) { @@ -38,7 +39,8 @@ func TestContentEndpoint(t *testing.T) { mockStore.pending[address1] = []*types.Transaction{testTx1, testTx2} txPoolEndpoint := &TxPool{mockStore} - result, _ := txPoolEndpoint.Content() + result, err := txPoolEndpoint.Content() + require.NoError(t, err) response := result.(ContentResponse) assert.Equal(t, 1, len(response.Pending)) @@ -63,12 +65,12 @@ func TestContentEndpoint(t *testing.T) { assert.Equal(t, (argUint64)(types.DynamicFeeTxType), txData.Type) assert.Equal(t, testTx2.Gas(), uint64(txData.Gas)) assert.Nil(t, testTx2.GasPrice()) - assert.Equal(t, *(testTx2.GasFeeCap()), big.Int(*txData.GasFeeCap)) - assert.Equal(t, *(testTx2.GasTipCap()), big.Int(*txData.GasTipCap)) + assert.Equal(t, *testTx2.GasFeeCap(), big.Int(*txData.GasFeeCap)) + assert.Equal(t, *testTx2.GasTipCap(), big.Int(*txData.GasTipCap)) assert.Equal(t, testTx2.To(), txData.To) assert.Equal(t, testTx2.From(), txData.From) - assert.Equal(t, *(testTx2.ChainID()), big.Int(*txData.ChainID)) - assert.Equal(t, *(testTx2.Value()), big.Int(txData.Value)) + assert.Equal(t, *testTx2.ChainID(), big.Int(*txData.ChainID)) + assert.Equal(t, *testTx2.Value(), big.Int(txData.Value)) assert.Equal(t, testTx2.Input(), []byte(txData.Input)) assert.Equal(t, (*argUint64)(nil), txData.BlockNumber) assert.Equal(t, (*argUint64)(nil), txData.TxIndex) @@ -112,12 +114,12 @@ func TestContentEndpoint(t *testing.T) { assert.Equal(t, (argUint64)(types.DynamicFeeTxType), txData.Type) assert.Equal(t, testTx2.Gas(), uint64(txData.Gas)) assert.Nil(t, testTx2.GasPrice()) - assert.Equal(t, *(testTx2.GasFeeCap()), big.Int(*txData.GasFeeCap)) - assert.Equal(t, *(testTx2.GasTipCap()), big.Int(*txData.GasTipCap)) + assert.Equal(t, *testTx2.GasFeeCap(), big.Int(*txData.GasFeeCap)) + assert.Equal(t, *testTx2.GasTipCap(), big.Int(*txData.GasTipCap)) assert.Equal(t, testTx2.To(), txData.To) assert.Equal(t, testTx2.From(), txData.From) - assert.Equal(t, *(testTx2.ChainID()), big.Int(*txData.ChainID)) - assert.Equal(t, *(testTx2.Value()), big.Int(txData.Value)) + assert.Equal(t, *testTx2.ChainID(), big.Int(*txData.ChainID)) + assert.Equal(t, *testTx2.Value(), big.Int(txData.Value)) assert.Equal(t, testTx2.Input(), []byte(txData.Input)) assert.Equal(t, (*argUint64)(nil), txData.BlockNumber) assert.Equal(t, (*argUint64)(nil), txData.TxIndex) @@ -204,12 +206,12 @@ func TestContentFrom(t *testing.T) { assert.Equal(t, (argUint64)(types.DynamicFeeTxType), txData.Type) assert.Equal(t, testTx2.Gas(), uint64(txData.Gas)) assert.Nil(t, testTx2.GasPrice()) - assert.Equal(t, *(testTx2.GasFeeCap()), big.Int(*txData.GasFeeCap)) - assert.Equal(t, *(testTx2.GasTipCap()), big.Int(*txData.GasTipCap)) + assert.Equal(t, *testTx2.GasFeeCap(), big.Int(*txData.GasFeeCap)) + assert.Equal(t, *testTx2.GasTipCap(), big.Int(*txData.GasTipCap)) assert.Equal(t, testTx2.To(), txData.To) assert.Equal(t, testTx2.From(), txData.From) - assert.Equal(t, *(testTx2.ChainID()), big.Int(*txData.ChainID)) - assert.Equal(t, *(testTx2.Value()), big.Int(txData.Value)) + assert.Equal(t, *testTx2.ChainID(), big.Int(*txData.ChainID)) + assert.Equal(t, *testTx2.Value(), big.Int(txData.Value)) assert.Equal(t, testTx2.Input(), []byte(txData.Input)) assert.Equal(t, (*argUint64)(nil), txData.BlockNumber) assert.Equal(t, (*argUint64)(nil), txData.TxIndex) @@ -237,12 +239,12 @@ func TestContentFrom(t *testing.T) { assert.Equal(t, (argUint64)(types.DynamicFeeTxType), txData.Type) assert.Equal(t, testTx2.Gas(), uint64(txData.Gas)) assert.Nil(t, testTx2.GasPrice()) - assert.Equal(t, *(testTx2.GasFeeCap()), big.Int(*txData.GasFeeCap)) - assert.Equal(t, *(testTx2.GasTipCap()), big.Int(*txData.GasTipCap)) + assert.Equal(t, *testTx2.GasFeeCap(), big.Int(*txData.GasFeeCap)) + assert.Equal(t, *testTx2.GasTipCap(), big.Int(*txData.GasTipCap)) assert.Equal(t, testTx2.To(), txData.To) assert.Equal(t, testTx2.From(), txData.From) - assert.Equal(t, *(testTx2.ChainID()), big.Int(*txData.ChainID)) - assert.Equal(t, *(testTx2.Value()), big.Int(txData.Value)) + assert.Equal(t, *testTx2.ChainID(), big.Int(*txData.ChainID)) + assert.Equal(t, *testTx2.Value(), big.Int(txData.Value)) assert.Equal(t, testTx2.Input(), []byte(txData.Input)) assert.Equal(t, (*argUint64)(nil), txData.BlockNumber) assert.Equal(t, (*argUint64)(nil), txData.TxIndex) diff --git a/jsonrpc/types.go b/jsonrpc/types.go index 23100a6a51..2a6264a290 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -59,45 +59,52 @@ func (h transactionHash) MarshalText() ([]byte, error) { } func toPendingTransaction(t *types.Transaction) *transaction { - return toTransaction(t, nil, nil, nil) + return toTransaction(t, nil, nil) } +// toTransaction converts types.Transaction struct to JSON RPC transaction format func toTransaction( t *types.Transaction, - blockNumber *argUint64, - blockHash *types.Hash, + header *types.Header, txIndex *int, ) *transaction { v, r, s := t.RawSignatureValues() res := &transaction{ - Nonce: argUint64(t.Nonce()), - Gas: argUint64(t.Gas()), - To: t.To(), - Value: argBig(*t.Value()), - Input: t.Input(), - V: argBig(*v), - R: argBig(*r), - S: argBig(*s), - Hash: t.Hash(), - From: t.From(), - Type: argUint64(t.Type()), - BlockNumber: blockNumber, - BlockHash: blockHash, - } - - if t.GasPrice() != nil && t.Type() != types.DynamicFeeTxType { - gasPrice := argBig(*(t.GasPrice())) - res.GasPrice = &gasPrice - } + Nonce: argUint64(t.Nonce()), + Gas: argUint64(t.Gas()), + To: t.To(), + Value: argBig(*t.Value()), + Input: t.Input(), + V: argBig(*v), + R: argBig(*r), + S: argBig(*s), + Hash: t.Hash(), + From: t.From(), + Type: argUint64(t.Type()), + } + + if header != nil { + // transaction is already mined + res.BlockNumber = argUintPtr(header.Number) + res.BlockHash = &header.Hash + res.GasPrice = argBigPtr(t.GetGasPrice(header.BaseFee)) + } else if t.GasPrice() != nil { + // transaction is pending (within the tx pool) + res.GasPrice = argBigPtr(t.GasPrice()) + } + + if t.Type() == types.DynamicFeeTxType { + if t.GasTipCap() != nil { + res.GasTipCap = argBigPtr(t.GasTipCap()) + } - if t.GasTipCap() != nil && t.Type() == types.DynamicFeeTxType { - gasTipCap := argBig(*(t.GasTipCap())) - res.GasTipCap = &gasTipCap - } + if t.GasFeeCap() != nil { + res.GasFeeCap = argBigPtr(t.GasFeeCap()) + } - if t.GasFeeCap() != nil && t.Type() == types.DynamicFeeTxType { - gasFeeCap := argBig(*(t.GasFeeCap())) - res.GasFeeCap = &gasFeeCap + if res.GasPrice == nil { + res.GasPrice = res.GasFeeCap + } } if t.ChainID() != nil { @@ -195,13 +202,11 @@ func toBlock(b *types.Block, fullTx bool) *block { for idx, txn := range b.Transactions { if fullTx { - txn.SetGasPrice(txn.GetGasPrice(b.Header.BaseFee)) res.Transactions = append( res.Transactions, toTransaction( txn, - argUintPtr(b.Number()), - argHashPtr(b.Hash()), + b.Header, &idx, ), ) diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index 286b4cbf3c..6f1a7212d8 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -115,7 +115,7 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) { types.WithFrom(types.Address{}), )) - jsonTx := toTransaction(txn, nil, nil, nil) + jsonTx := toTransaction(txn, nil, nil) jsonV, _ := jsonTx.V.MarshalText() jsonR, _ := jsonTx.R.MarshalText() @@ -143,7 +143,7 @@ func TestToTransaction_EIP1559(t *testing.T) { types.WithFrom(types.Address{}), )) - jsonTx := toTransaction(txn, nil, nil, nil) + jsonTx := toTransaction(txn, nil, nil) jsonV, _ := jsonTx.V.MarshalText() jsonR, _ := jsonTx.R.MarshalText()