From fbe5657584398adc912871cece6f1e1de4027ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= <93934272+Stefan-Ethernal@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:12:10 +0200 Subject: [PATCH] Add unit tests for `jsonrpc.Log` and `jsonrpc.Receipt` objects (#1996) * Added new line for test suite data * Test_appendLogsToFilters add some transactions to test block * Use non-zero recipient address in test transactions * Introduce toReceipt, toLogs and toLog and UTs * Fix unit test and lint * Better readability * Add TestOverrideAccount_ToType * Test toBlock with full transactions * Fix linter issues --- command/secrets/output/secrets_output.go | 4 + jsonrpc/eth_endpoint.go | 34 +----- jsonrpc/eth_endpoint_test.go | 27 +++++ jsonrpc/filter_manager.go | 23 +--- jsonrpc/filter_manager_test.go | 8 +- jsonrpc/helper_test.go | 55 ++++++++- jsonrpc/testsuite/block-with-txn-bodies.json | 2 +- jsonrpc/testsuite/block-with-txn-full.json | 58 ++++++++++ .../receipt-contract-deployment.json | 15 +++ jsonrpc/testsuite/receipt-no-logs.json | 15 +++ jsonrpc/testsuite/receipt-with-logs.json | 44 ++++++++ jsonrpc/testsuite/transaction-eip1559.json | 4 +- jsonrpc/testsuite/transaction-pending.json | 4 +- jsonrpc/testsuite/transaction-sealed.json | 4 +- jsonrpc/types.go | 41 +++++++ jsonrpc/types_test.go | 104 ++++++++++++++++-- 16 files changed, 365 insertions(+), 77 deletions(-) create mode 100644 jsonrpc/testsuite/block-with-txn-full.json create mode 100644 jsonrpc/testsuite/receipt-contract-deployment.json create mode 100644 jsonrpc/testsuite/receipt-no-logs.json create mode 100644 jsonrpc/testsuite/receipt-with-logs.json diff --git a/command/secrets/output/secrets_output.go b/command/secrets/output/secrets_output.go index f223b876b4..02df556c51 100644 --- a/command/secrets/output/secrets_output.go +++ b/command/secrets/output/secrets_output.go @@ -5,6 +5,10 @@ import ( "github.com/spf13/cobra" ) +const ( + outputFlagDesc = "output the %s from the provided secrets manager" +) + func GetCommand() *cobra.Command { secretsOutputCmd := &cobra.Command{ Use: "output", diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index 455d5d94ba..e564950822 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -339,39 +339,9 @@ func (e *Eth) GetTransactionReceipt(hash types.Hash) (interface{}, error) { } raw := receipts[txIndex] + logs := toLogs(raw.Logs, uint64(logIndex), uint64(txIndex), block.Header, hash) - logs := make([]*Log, len(raw.Logs)) - for i, elem := range raw.Logs { - logs[i] = &Log{ - Address: elem.Address, - Topics: elem.Topics, - Data: argBytes(elem.Data), - BlockHash: block.Hash(), - BlockNumber: argUint64(block.Number()), - TxHash: hash, - TxIndex: argUint64(txIndex), - LogIndex: argUint64(logIndex + i), - Removed: false, - } - } - - res := &receipt{ - Root: raw.Root, - CumulativeGasUsed: argUint64(raw.CumulativeGasUsed), - LogsBloom: raw.LogsBloom, - Status: argUint64(*raw.Status), - TxHash: hash, - TxIndex: argUint64(txIndex), - BlockHash: block.Hash(), - BlockNumber: argUint64(block.Number()), - GasUsed: argUint64(raw.GasUsed), - ContractAddress: raw.ContractAddress, - FromAddr: txn.From, - ToAddr: txn.To, - Logs: logs, - } - - return res, nil + return toReceipt(raw, txn, uint64(txIndex), block.Header, logs), nil } // GetStorageAt returns the contract storage at the index position diff --git a/jsonrpc/eth_endpoint_test.go b/jsonrpc/eth_endpoint_test.go index 65ebc743cc..ed61a4184a 100644 --- a/jsonrpc/eth_endpoint_test.go +++ b/jsonrpc/eth_endpoint_test.go @@ -8,6 +8,7 @@ import ( "github.com/0xPolygon/polygon-edge/types" "github.com/hashicorp/go-hclog" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestEth_DecodeTxn(t *testing.T) { @@ -364,3 +365,29 @@ func TestEth_HeaderResolveBlock(t *testing.T) { } } } + +func TestOverrideAccount_ToType(t *testing.T) { + t.Parallel() + + nonce := uint64(10) + code := []byte("SC code") + balance := uint64(10000) + state := map[types.Hash]types.Hash{types.StringToHash("1"): types.StringToHash("2")} + stateDiff := map[types.Hash]types.Hash{types.StringToHash("3"): types.StringToHash("4")} + + overrideAcc := &overrideAccount{ + Nonce: toArgUint64Ptr(nonce), + Code: toArgBytesPtr(code), + Balance: toArgUint64Ptr(balance), + State: &state, + StateDiff: &stateDiff, + } + + convertedAcc := overrideAcc.ToType() + require.NotNil(t, convertedAcc) + require.Equal(t, nonce, *convertedAcc.Nonce) + require.Equal(t, code, convertedAcc.Code) + require.Equal(t, new(big.Int).SetUint64(balance), convertedAcc.Balance) + require.Equal(t, state, convertedAcc.State) + require.Equal(t, stateDiff, convertedAcc.StateDiff) +} diff --git a/jsonrpc/filter_manager.go b/jsonrpc/filter_manager.go index 8bf8ff4bf3..086565be5f 100644 --- a/jsonrpc/filter_manager.go +++ b/jsonrpc/filter_manager.go @@ -495,16 +495,7 @@ func (f *FilterManager) getLogsFromBlock(query *LogQuery, block *types.Block) ([ for idx, receipt := range receipts { for _, log := range receipt.Logs { if query.Match(log) { - logs = append(logs, &Log{ - Address: log.Address, - Topics: log.Topics, - Data: log.Data, - BlockNumber: argUint64(block.Header.Number), - BlockHash: block.Header.Hash, - TxHash: block.Transactions[idx].Hash, - TxIndex: argUint64(idx), - LogIndex: argUint64(logIdx), - }) + logs = append(logs, toLog(log, logIdx, uint64(idx), block.Header, block.Transactions[idx].Hash)) } logIdx++ @@ -817,17 +808,7 @@ func (f *FilterManager) appendLogsToFilters(header *block) error { for _, log := range receipt.Logs { for _, f := range logFilters { if f.query.Match(log) { - f.appendLog(&Log{ - Address: log.Address, - Topics: log.Topics, - Data: argBytes(log.Data), - BlockNumber: header.Number, - BlockHash: header.Hash, - TxHash: receipt.TxHash, - TxIndex: argUint64(indx), - Removed: false, - LogIndex: argUint64(logIndex), - }) + f.appendLog(toLog(log, logIndex, uint64(indx), block.Header, receipt.TxHash)) } } diff --git a/jsonrpc/filter_manager_test.go b/jsonrpc/filter_manager_test.go index 0f127ff781..98dee97f8c 100644 --- a/jsonrpc/filter_manager_test.go +++ b/jsonrpc/filter_manager_test.go @@ -869,7 +869,13 @@ func Test_appendLogsToFilters(t *testing.T) { defer f.Close() }) - b := toBlock(&types.Block{Header: block.Header}, false) + txs := []*types.Transaction{ + createTestTransaction(types.StringToHash("tx1")), + createTestTransaction(types.StringToHash("tx2")), + createTestTransaction(types.StringToHash("tx3")), + } + + b := toBlock(&types.Block{Header: block.Header, Transactions: txs}, false) err := f.appendLogsToFilters(b) require.NoError(t, err) diff --git a/jsonrpc/helper_test.go b/jsonrpc/helper_test.go index 1f2d4f64de..6b0b93717f 100644 --- a/jsonrpc/helper_test.go +++ b/jsonrpc/helper_test.go @@ -1,6 +1,7 @@ package jsonrpc import ( + "encoding/hex" "errors" "fmt" "math/big" @@ -11,21 +12,65 @@ import ( ) func createTestTransaction(hash types.Hash) *types.Transaction { + recipient := types.StringToAddress("2") + return &types.Transaction{ - Hash: hash, + Hash: hash, + From: types.StringToAddress("1"), + To: &recipient, + GasPrice: big.NewInt(400), + Value: big.NewInt(100), + V: big.NewInt(1), + R: big.NewInt(2), + S: big.NewInt(3), } } -func createTestHeader(height uint64) *types.Header { +func createTestHeader(height uint64, setterFn func(h *types.Header)) *types.Header { h := &types.Header{ Number: height, } + if setterFn != nil { + setterFn(h) + } + h.ComputeHash() return h } +func createTestReceipt(logs []*types.Log, cumulativeGasUsed, gasUsed uint64, txHash types.Hash) *types.Receipt { + success := types.ReceiptSuccess + + return &types.Receipt{ + Root: types.ZeroHash, + CumulativeGasUsed: cumulativeGasUsed, + Status: &success, + LogsBloom: types.CreateBloom(nil), + Logs: logs, + GasUsed: gasUsed, + TxHash: txHash, + TransactionType: types.DynamicFeeTx, + } +} + +func createTestLogs(logsCount int, address types.Address) []*types.Log { + logs := make([]*types.Log, 0, logsCount) + for i := 0; i < logsCount; i++ { + logs = append(logs, &types.Log{ + Address: address, + Topics: []types.Hash{ + types.StringToHash("100"), + types.StringToHash("ABCD"), + }, + Data: types.StringToBytes(hex.EncodeToString([]byte("Lorem Ipsum Dolor"))), + }) + } + + return logs +} + func wrapHeaderWithTestBlock(h *types.Header) *types.Block { return &types.Block{ Header: h, @@ -36,13 +81,13 @@ var ( testTxHash1 = types.BytesToHash([]byte{1}) testTx1 = createTestTransaction(testTxHash1) - testGenesisHeader = createTestHeader(0) + testGenesisHeader = createTestHeader(0, nil) testGenesisBlock = wrapHeaderWithTestBlock(testGenesisHeader) - testLatestHeader = createTestHeader(100) + testLatestHeader = createTestHeader(100, nil) testLatestBlock = wrapHeaderWithTestBlock(testLatestHeader) - testHeader10 = createTestHeader(10) + testHeader10 = createTestHeader(10, nil) testBlock10 = wrapHeaderWithTestBlock(testHeader10) testHash11 = types.BytesToHash([]byte{11}) diff --git a/jsonrpc/testsuite/block-with-txn-bodies.json b/jsonrpc/testsuite/block-with-txn-bodies.json index 8bec03c534..4e03f04667 100644 --- a/jsonrpc/testsuite/block-with-txn-bodies.json +++ b/jsonrpc/testsuite/block-with-txn-bodies.json @@ -22,7 +22,7 @@ "nonce": "0x1", "gasPrice": "0xa", "gas": "0x64", - "to": "0x0000000000000000000000000000000000000000", + "to": "0x0000000000000000000000000000000000000004", "value": "0x3e8", "input": "0x0102", "v": "0x1", diff --git a/jsonrpc/testsuite/block-with-txn-full.json b/jsonrpc/testsuite/block-with-txn-full.json new file mode 100644 index 0000000000..6f2a00489b --- /dev/null +++ b/jsonrpc/testsuite/block-with-txn-full.json @@ -0,0 +1,58 @@ +{ + "parentHash": "0x00000000000000000000000000000000000000000000506172656e7448617368", + "sha3Uncles": "0x0000000000000000000000000000000000000000000000000000000000000000", + "miner": "0x", + "stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "receiptsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "difficulty": "0x0", + "totalDifficulty": "0x0", + "size": "0x224", + "number": "0x14", + "gasLimit": "0x0", + "gasUsed": "0x0", + "timestamp": "0x0", + "extraData": "0x", + "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0x0000000000000000", + "hash": "0x7935c790bdff1ec20912f28c9f7722eed41837c478a1f9ce0dc49f5d4a2d7c88", + "transactions": [ + { + "nonce": "0x0", + "gasPrice": "0x190", + "gas": "0x0", + "to": "0x0000000000000000000000000000000000000002", + "value": "0x64", + "input": "0x", + "v": "0x1", + "r": "0x2", + "s": "0x3", + "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "from": "0x0000000000000000000000000000000000000001", + "blockHash": "0x7935c790bdff1ec20912f28c9f7722eed41837c478a1f9ce0dc49f5d4a2d7c88", + "blockNumber": "0x14", + "transactionIndex": "0x0", + "type": "0x0" + }, + { + "nonce": "0x0", + "gasPrice": "0x190", + "gas": "0x0", + "to": "0x0000000000000000000000000000000000000002", + "value": "0x64", + "input": "0x", + "v": "0x1", + "r": "0x2", + "s": "0x3", + "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "from": "0x0000000000000000000000000000000000000001", + "blockHash": "0x7935c790bdff1ec20912f28c9f7722eed41837c478a1f9ce0dc49f5d4a2d7c88", + "blockNumber": "0x14", + "transactionIndex": "0x1", + "type": "0x0" + } + ], + "uncles": [], + "baseFeePerGas": "0xc8" +} \ No newline at end of file diff --git a/jsonrpc/testsuite/receipt-contract-deployment.json b/jsonrpc/testsuite/receipt-contract-deployment.json new file mode 100644 index 0000000000..3c4231e9e7 --- /dev/null +++ b/jsonrpc/testsuite/receipt-contract-deployment.json @@ -0,0 +1,15 @@ +{ + "root": "0x0000000000000000000000000000000000000000000000000000000000000000", + "cumulativeGasUsed": "0x6d60", + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "logs": null, + "status": "0x1", + "transactionHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionIndex": "0x0", + "blockHash": "0x9a4931c84f077e3b77984b216ea409186811ad681f66a4ab2ca6be53fae9da82", + "blockNumber": "0x14", + "gasUsed": "0x6590", + "contractAddress": "0x0000000000000000000000000000000000000003", + "from": "0x0000000000000000000000000000000000000001", + "to": null +} diff --git a/jsonrpc/testsuite/receipt-no-logs.json b/jsonrpc/testsuite/receipt-no-logs.json new file mode 100644 index 0000000000..8968be786b --- /dev/null +++ b/jsonrpc/testsuite/receipt-no-logs.json @@ -0,0 +1,15 @@ +{ + "root": "0x0000000000000000000000000000000000000000000000000000000000000000", + "cumulativeGasUsed": "0x6d60", + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "logs": null, + "status": "0x1", + "transactionHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionIndex": "0x0", + "blockHash": "0xc6434852d5086633921b6bb2d71c412dc9dc4f6c6c6d8b279903e1fbaba52f57", + "blockNumber": "0xf", + "gasUsed": "0x6590", + "contractAddress": null, + "from": "0x0000000000000000000000000000000000000001", + "to": "0x0000000000000000000000000000000000000002" +} diff --git a/jsonrpc/testsuite/receipt-with-logs.json b/jsonrpc/testsuite/receipt-with-logs.json new file mode 100644 index 0000000000..294868bddd --- /dev/null +++ b/jsonrpc/testsuite/receipt-with-logs.json @@ -0,0 +1,44 @@ +{ + "root": "0x0000000000000000000000000000000000000000000000000000000000000000", + "cumulativeGasUsed": "0x6d60", + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "logs": [ + { + "address": "0x0000000000000000000000000000000000000002", + "topics": [ + "0x0000000000000000000000000000000000000000000000000000000000000100", + "0x000000000000000000000000000000000000000000000000000000000000abcd" + ], + "data": "0x4c6f72656d20497073756d20446f6c6f72", + "blockNumber": "0x1e", + "transactionHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionIndex": "0x1", + "blockHash": "0x6644e9031437757ffed25e1776c4a1c55ad953ff9875252b84746ae771c2c688", + "logIndex": "0x0", + "removed": false + }, + { + "address": "0x0000000000000000000000000000000000000002", + "topics": [ + "0x0000000000000000000000000000000000000000000000000000000000000100", + "0x000000000000000000000000000000000000000000000000000000000000abcd" + ], + "data": "0x4c6f72656d20497073756d20446f6c6f72", + "blockNumber": "0x1e", + "transactionHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionIndex": "0x1", + "blockHash": "0x6644e9031437757ffed25e1776c4a1c55ad953ff9875252b84746ae771c2c688", + "logIndex": "0x1", + "removed": false + } + ], + "status": "0x1", + "transactionHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "transactionIndex": "0x1", + "blockHash": "0x6644e9031437757ffed25e1776c4a1c55ad953ff9875252b84746ae771c2c688", + "blockNumber": "0x1e", + "gasUsed": "0x6590", + "contractAddress": null, + "from": "0x0000000000000000000000000000000000000001", + "to": "0x0000000000000000000000000000000000000002" +} diff --git a/jsonrpc/testsuite/transaction-eip1559.json b/jsonrpc/testsuite/transaction-eip1559.json index d64209c8f3..6c7068461b 100644 --- a/jsonrpc/testsuite/transaction-eip1559.json +++ b/jsonrpc/testsuite/transaction-eip1559.json @@ -4,7 +4,7 @@ "maxPriorityFeePerGas": "0xa", "maxFeePerGas": "0xa", "gas": "0x64", - "to": "0x0000000000000000000000000000000000000000", + "to": "0x0000000000000000000000000000000000000004", "value": "0x3e8", "input": "0x0102", "v": "0x1", @@ -16,4 +16,4 @@ "blockNumber": "0x1", "transactionIndex": "0x2", "type": "0x2" -} \ No newline at end of file +} diff --git a/jsonrpc/testsuite/transaction-pending.json b/jsonrpc/testsuite/transaction-pending.json index f7e0f6de3b..22763e4cc6 100644 --- a/jsonrpc/testsuite/transaction-pending.json +++ b/jsonrpc/testsuite/transaction-pending.json @@ -2,7 +2,7 @@ "nonce": "0x1", "gasPrice": "0xa", "gas": "0x64", - "to": "0x0000000000000000000000000000000000000000", + "to": "0x0000000000000000000000000000000000000004", "value": "0x3e8", "input": "0x0102", "v": "0x1", @@ -14,4 +14,4 @@ "blockNumber": null, "transactionIndex": null, "type": "0x0" -} \ No newline at end of file +} diff --git a/jsonrpc/testsuite/transaction-sealed.json b/jsonrpc/testsuite/transaction-sealed.json index c54be34d8c..dcbc9909d6 100644 --- a/jsonrpc/testsuite/transaction-sealed.json +++ b/jsonrpc/testsuite/transaction-sealed.json @@ -2,7 +2,7 @@ "nonce": "0x1", "gasPrice": "0xa", "gas": "0x64", - "to": "0x0000000000000000000000000000000000000000", + "to": "0x0000000000000000000000000000000000000004", "value": "0x3e8", "input": "0x0102", "v": "0x1", @@ -14,4 +14,4 @@ "blockNumber": "0x1", "transactionIndex": "0x2", "type": "0x0" -} \ No newline at end of file +} diff --git a/jsonrpc/types.go b/jsonrpc/types.go index eab7ee9929..3bc717ea3a 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -208,6 +208,25 @@ type receipt struct { ToAddr *types.Address `json:"to"` } +func toReceipt(src *types.Receipt, tx *types.Transaction, + txIndex uint64, header *types.Header, logs []*Log) *receipt { + return &receipt{ + Root: src.Root, + CumulativeGasUsed: argUint64(src.CumulativeGasUsed), + LogsBloom: src.LogsBloom, + Status: argUint64(*src.Status), + TxHash: tx.Hash, + TxIndex: argUint64(txIndex), + BlockHash: header.Hash, + BlockNumber: argUint64(header.Number), + GasUsed: argUint64(src.GasUsed), + ContractAddress: src.ContractAddress, + FromAddr: tx.From, + ToAddr: tx.To, + Logs: logs, + } +} + type Log struct { Address types.Address `json:"address"` Topics []types.Hash `json:"topics"` @@ -220,6 +239,28 @@ type Log struct { Removed bool `json:"removed"` } +func toLogs(srcLogs []*types.Log, baseIdx, txIdx uint64, header *types.Header, txHash types.Hash) []*Log { + logs := make([]*Log, len(srcLogs)) + for i, srcLog := range srcLogs { + logs[i] = toLog(srcLog, baseIdx+uint64(i), txIdx, header, txHash) + } + + return logs +} + +func toLog(src *types.Log, logIdx, txIdx uint64, header *types.Header, txHash types.Hash) *Log { + return &Log{ + Address: src.Address, + Topics: src.Topics, + Data: argBytes(src.Data), + BlockNumber: argUint64(header.Number), + BlockHash: header.Hash, + TxHash: txHash, + TxIndex: argUint64(txIdx), + LogIndex: argUint64(logIdx), + } +} + type argBig big.Int func argBigPtr(b *big.Int) *argBig { diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index aac6899692..77bbcf6b64 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -103,7 +103,7 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) { v, _ := hex.DecodeHex(hexWithLeading0) r, _ := hex.DecodeHex(hexWithLeading0) s, _ := hex.DecodeHex(hexWithLeading0) - txn := types.Transaction{ + txn := &types.Transaction{ Nonce: 0, GasPrice: big.NewInt(0), Gas: 0, @@ -117,7 +117,7 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) { From: types.Address{}, } - jsonTx := toTransaction(&txn, nil, nil, nil) + jsonTx := toTransaction(txn, nil, nil, nil) jsonV, _ := jsonTx.V.MarshalText() jsonR, _ := jsonTx.R.MarshalText() @@ -134,7 +134,7 @@ func TestToTransaction_EIP1559(t *testing.T) { v, _ := hex.DecodeHex(hexWithLeading0) r, _ := hex.DecodeHex(hexWithLeading0) s, _ := hex.DecodeHex(hexWithLeading0) - txn := types.Transaction{ + txn := &types.Transaction{ Nonce: 0, GasPrice: nil, GasTipCap: big.NewInt(10), @@ -150,7 +150,7 @@ func TestToTransaction_EIP1559(t *testing.T) { From: types.Address{}, } - jsonTx := toTransaction(&txn, nil, nil, nil) + jsonTx := toTransaction(txn, nil, nil, nil) jsonV, _ := jsonTx.V.MarshalText() jsonR, _ := jsonTx.R.MarshalText() @@ -202,9 +202,8 @@ func TestBlock_Encoding(t *testing.T) { res, err := json.Marshal(b) require.NoError(t, err) - data, err := testsuite.ReadFile(name) - require.NoError(t, err) - require.JSONEq(t, string(data), string(res)) + expectedData := loadTestData(t, name) + require.JSONEq(t, expectedData, string(res)) } t.Run("empty block", func(t *testing.T) { @@ -235,7 +234,7 @@ func TestBlock_Encoding(t *testing.T) { } func mockTxn() *transaction { - to := types.Address{} + to := types.StringToAddress("0x4") tt := &transaction{ Nonce: 1, @@ -263,9 +262,8 @@ func TestTransaction_Encoding(t *testing.T) { res, err := json.Marshal(tt) require.NoError(t, err) - data, err := testsuite.ReadFile(name) - require.NoError(t, err) - require.JSONEq(t, string(data), string(res)) + expectedData := loadTestData(t, name) + require.JSONEq(t, expectedData, string(res)) } t.Run("sealed", func(t *testing.T) { @@ -295,3 +293,87 @@ func TestTransaction_Encoding(t *testing.T) { testTransaction("testsuite/transaction-eip1559.json", tt) }) } + +func Test_toReceipt(t *testing.T) { + const ( + cumulativeGasUsed = 28000 + gasUsed = 26000 + ) + + testReceipt := func(name string, r *receipt) { + res, err := json.Marshal(r) + require.NoError(t, err) + + expectedData := loadTestData(t, name) + require.JSONEq(t, expectedData, string(res)) + } + + t.Run("no logs", func(t *testing.T) { + tx := createTestTransaction(types.StringToHash("tx1")) + recipient := types.StringToAddress("2") + tx.From = types.StringToAddress("1") + tx.To = &recipient + + header := createTestHeader(15, nil) + rec := createTestReceipt(nil, cumulativeGasUsed, gasUsed, tx.Hash) + testReceipt("testsuite/receipt-no-logs.json", toReceipt(rec, tx, 0, header, nil)) + }) + + t.Run("with contract address", func(t *testing.T) { + tx := createTestTransaction(types.StringToHash("tx1")) + tx.To = nil + + contractAddr := types.StringToAddress("3") + header := createTestHeader(20, nil) + rec := createTestReceipt(nil, cumulativeGasUsed, gasUsed, tx.Hash) + rec.ContractAddress = &contractAddr + testReceipt("testsuite/receipt-contract-deployment.json", toReceipt(rec, tx, 0, header, nil)) + }) + + t.Run("with logs", func(t *testing.T) { + tx := createTestTransaction(types.StringToHash("tx1")) + recipient := types.StringToAddress("2") + tx.From = types.StringToAddress("1") + tx.To = &recipient + + header := createTestHeader(30, nil) + logs := createTestLogs(2, recipient) + originReceipt := createTestReceipt(logs, cumulativeGasUsed, gasUsed, tx.Hash) + txIdx := uint64(1) + receipt := toReceipt(originReceipt, tx, txIdx, header, toLogs(logs, 0, txIdx, header, tx.Hash)) + testReceipt("testsuite/receipt-with-logs.json", receipt) + }) +} + +func Test_toBlock(t *testing.T) { + h := createTestHeader(20, func(h *types.Header) { + h.BaseFee = 200 + h.ParentHash = types.BytesToHash([]byte("ParentHash")) + }) + + block := &types.Block{ + Header: h, + Transactions: []*types.Transaction{ + createTestTransaction(types.StringToHash("tx1")), + createTestTransaction(types.StringToHash("tx2")), + }, + } + + b := toBlock(block, true) + require.NotNil(t, b) + + res, err := json.Marshal(b) + require.NoError(t, err) + + expectedJSON := loadTestData(t, "testsuite/block-with-txn-full.json") + require.JSONEq(t, expectedJSON, string(res)) +} + +func loadTestData(t *testing.T, name string) string { + t.Helper() + + data, err := testsuite.ReadFile(name) + require.NoError(t, err) + + return string(data) +}