Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Test toBlock with full transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 25, 2023
1 parent e73e2e9 commit b0d70b4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 18 deletions.
23 changes: 18 additions & 5 deletions jsonrpc/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
58 changes: 58 additions & 0 deletions jsonrpc/testsuite/block-with-txn-full.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion jsonrpc/testsuite/receipt-contract-deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"blockNumber": "0x14",
"gasUsed": "0x6590",
"contractAddress": "0x0000000000000000000000000000000000000003",
"from": "0x0000000000000000000000000000000000000000",
"from": "0x0000000000000000000000000000000000000001",
"to": null
}
55 changes: 43 additions & 12 deletions jsonrpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -338,11 +336,44 @@ 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)
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)
}

0 comments on commit b0d70b4

Please sign in to comment.