Skip to content

Commit

Permalink
Add unit tests for jsonrpc transaction encoding (0xPolygon#923)
Browse files Browse the repository at this point in the history
* Add unit tests for jsonrpc transaction encoding

* Remove TODO

* Formatting fix

* Fix linter

Co-authored-by: Stefan Negovanović <stefan@ethernal.tech>
  • Loading branch information
ferranbt and Stefan-Ethernal authored Nov 20, 2022
1 parent 5250168 commit f80b615
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
39 changes: 39 additions & 0 deletions jsonrpc/testsuite/block-with-txn-bodies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"parentHash": "0x0100000000000000000000000000000000000000000000000000000000000000",
"sha3Uncles": "0x0200000000000000000000000000000000000000000000000000000000000000",
"miner": "0x0100000000000000000000000000000000000000",
"stateRoot": "0x0400000000000000000000000000000000000000000000000000000000000000",
"transactionsRoot": "0x0500000000000000000000000000000000000000000000000000000000000000",
"receiptsRoot": "0x0600000000000000000000000000000000000000000000000000000000000000",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0xa",
"totalDifficulty": "0x0",
"size": "0x0",
"number": "0xb",
"gasLimit": "0xc",
"gasUsed": "0xd",
"timestamp": "0xe",
"extraData": "0x616263646566",
"mixHash": "0x0700000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0a00000000000000",
"hash": "0x0800000000000000000000000000000000000000000000000000000000000000",
"transactions": [
{
"nonce": "0x1",
"gasPrice": "0xa",
"gas": "0x64",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x3e8",
"input": "0x0102",
"v": "0x1",
"r": "0x2",
"s": "0x3",
"hash": "0x0200000000000000000000000000000000000000000000000000000000000000",
"from": "0x0300000000000000000000000000000000000000",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": "0x1",
"transactionIndex": "0x2"
}
],
"uncles": null
}
16 changes: 16 additions & 0 deletions jsonrpc/testsuite/transaction-pending.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"nonce": "0x1",
"gasPrice": "0xa",
"gas": "0x64",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x3e8",
"input": "0x0102",
"v": "0x1",
"r": "0x2",
"s": "0x3",
"hash": "0x0200000000000000000000000000000000000000000000000000000000000000",
"from": "0x0300000000000000000000000000000000000000",
"blockHash": null,
"blockNumber": null,
"transactionIndex": null
}
16 changes: 16 additions & 0 deletions jsonrpc/testsuite/transaction-sealed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"nonce": "0x1",
"gasPrice": "0xa",
"gas": "0x64",
"to": "0x0000000000000000000000000000000000000000",
"value": "0x3e8",
"input": "0x0102",
"v": "0x1",
"r": "0x2",
"s": "0x3",
"hash": "0x0200000000000000000000000000000000000000000000000000000000000000",
"from": "0x0300000000000000000000000000000000000000",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": "0x1",
"transactionIndex": "0x2"
}
60 changes: 56 additions & 4 deletions jsonrpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,18 @@ func TestBlock_Encoding(t *testing.T) {
testBlock("testsuite/block-empty.json")
})

t.Run("block with transactions", func(t *testing.T) {
t.Run("block with transaction hashes", func(t *testing.T) {
b.Transactions = []transactionOrHash{
transactionHash{0x8},
}
testBlock("testsuite/block-with-txn-hashes.json")
})

t.Run("block with transactions", func(t *testing.T) {
// TODO: do the same tests for the transaction object and enable this test
t.Skip("TODO")
t.Run("block with transaction bodies", func(t *testing.T) {
b.Transactions = []transactionOrHash{
mockTxn(),
}
testBlock("testsuite/block-with-txn-bodies.json")
})
}

Expand All @@ -198,3 +200,53 @@ func removeWhiteSpace(d []byte) []byte {

return []byte(s)
}

func mockTxn() *transaction {
to := types.Address{}

tt := &transaction{
Nonce: 1,
GasPrice: argBig(*big.NewInt(10)),
Gas: 100,
To: &to,
Value: argBig(*big.NewInt(1000)),
Input: []byte{0x1, 0x2},
V: argBig(*big.NewInt(1)),
R: argBig(*big.NewInt(2)),
S: argBig(*big.NewInt(3)),
Hash: types.Hash{0x2},
From: types.Address{0x3},
BlockHash: &types.ZeroHash,
BlockNumber: argUintPtr(1),
TxIndex: argUintPtr(2),
}

return tt
}

func TestTransaction_Encoding(t *testing.T) {
tt := mockTxn()

testTransaction := func(name string) {
res, err := json.Marshal(tt)
require.NoError(t, err)

data, err := testsuite.ReadFile(name)
require.NoError(t, err)

data = removeWhiteSpace(data)
require.Equal(t, res, data)
}

t.Run("sealed", func(t *testing.T) {
testTransaction("testsuite/transaction-sealed.json")
})

t.Run("pending", func(t *testing.T) {
tt.BlockHash = nil
tt.BlockNumber = nil
tt.TxIndex = nil

testTransaction("testsuite/transaction-pending.json")
})
}
3 changes: 2 additions & 1 deletion server/system_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ func (s *systemService) Export(req *proto.ExportRequest, stream proto.System_Exp

const (
defaultMaxGRPCPayloadSize uint64 = 512 * 1024 // 4MB
maxHeaderInfoSize int = 3 * 8 //Number of header fields * bytes per field (From, To, Latest all them uint64)
// number of header fields * bytes per field (From, To, Latest all them uint64)
maxHeaderInfoSize int = 3 * 8
)

type blockStreamWriter struct {
Expand Down

0 comments on commit f80b615

Please sign in to comment.