Skip to content

Commit

Permalink
Add unit tests for jsonrpc.Log and jsonrpc.Receipt objects (#1996)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Stefan-Ethernal committed Oct 26, 2023
1 parent 3b5ad9d commit fbe5657
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 77 deletions.
4 changes: 4 additions & 0 deletions command/secrets/output/secrets_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 2 additions & 32 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
23 changes: 2 additions & 21 deletions jsonrpc/filter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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))
}
}

Expand Down
8 changes: 7 additions & 1 deletion jsonrpc/filter_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 50 additions & 5 deletions jsonrpc/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonrpc

import (
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand All @@ -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,
Expand All @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/testsuite/block-with-txn-bodies.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"nonce": "0x1",
"gasPrice": "0xa",
"gas": "0x64",
"to": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000004",
"value": "0x3e8",
"input": "0x0102",
"v": "0x1",
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"
}
15 changes: 15 additions & 0 deletions jsonrpc/testsuite/receipt-contract-deployment.json
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions jsonrpc/testsuite/receipt-no-logs.json
Original file line number Diff line number Diff line change
@@ -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"
}
44 changes: 44 additions & 0 deletions jsonrpc/testsuite/receipt-with-logs.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 2 additions & 2 deletions jsonrpc/testsuite/transaction-eip1559.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"maxPriorityFeePerGas": "0xa",
"maxFeePerGas": "0xa",
"gas": "0x64",
"to": "0x0000000000000000000000000000000000000000",
"to": "0x0000000000000000000000000000000000000004",
"value": "0x3e8",
"input": "0x0102",
"v": "0x1",
Expand All @@ -16,4 +16,4 @@
"blockNumber": "0x1",
"transactionIndex": "0x2",
"type": "0x2"
}
}
Loading

0 comments on commit fbe5657

Please sign in to comment.