diff --git a/jsonrpc/helper_test.go b/jsonrpc/helper_test.go index 76ac4aae39..6b0b93717f 100644 --- a/jsonrpc/helper_test.go +++ b/jsonrpc/helper_test.go @@ -12,16 +12,29 @@ 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 @@ -68,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-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 index 23369c7d82..3c4231e9e7 100644 --- a/jsonrpc/testsuite/receipt-contract-deployment.json +++ b/jsonrpc/testsuite/receipt-contract-deployment.json @@ -10,6 +10,6 @@ "blockNumber": "0x14", "gasUsed": "0x6590", "contractAddress": "0x0000000000000000000000000000000000000003", - "from": "0x0000000000000000000000000000000000000000", + "from": "0x0000000000000000000000000000000000000001", "to": null } diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index b1f9d0f862..77bbcf6b64 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -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) { @@ -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) { @@ -306,9 +304,8 @@ func Test_toReceipt(t *testing.T) { res, err := json.Marshal(r) 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("no logs", func(t *testing.T) { @@ -317,16 +314,17 @@ func Test_toReceipt(t *testing.T) { tx.From = types.StringToAddress("1") tx.To = &recipient - header := createTestHeader(15) + 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) + 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)) @@ -338,7 +336,7 @@ func Test_toReceipt(t *testing.T) { tx.From = types.StringToAddress("1") tx.To = &recipient - header := createTestHeader(30) + header := createTestHeader(30, nil) logs := createTestLogs(2, recipient) originReceipt := createTestReceipt(logs, cumulativeGasUsed, gasUsed, tx.Hash) txIdx := uint64(1) @@ -346,3 +344,36 @@ func Test_toReceipt(t *testing.T) { 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) +}