erigon-pulse/cmd/devnet/requests/request_generator_test.go
Leonard Chinonso f76736e14a
Implemented mining of contract transactions on the devnet tool (#6102)
- Added a new method and type for contract transactions.
- Added functions to emit fallback events from contract transactions.
- Added GetLogs request generator
- Added tests for GetLogs request generator
2022-11-22 20:28:53 +07:00

187 lines
5.6 KiB
Go

package requests
import (
"testing"
"github.com/ledgerwatch/erigon/cmd/devnet/models"
"github.com/ledgerwatch/erigon/common"
"github.com/stretchr/testify/require"
)
func MockRequestGenerator(reqId int) *RequestGenerator {
return &RequestGenerator{
reqID: reqId,
client: nil,
}
}
func TestRequestGenerator_GetAdminNodeInfo(t *testing.T) {
testCases := []struct {
reqId int
expected string
}{
{1, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":1}`},
{2, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":2}`},
{3, `{"jsonrpc":"2.0","method":"admin_nodeInfo","id":3}`},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.GetAdminNodeInfo()
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetBalance(t *testing.T) {
testCases := []struct {
reqId int
address common.Address
blockNum models.BlockNumber
expected string
}{
{
1,
common.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
models.Latest,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x67b1d87101671b127f5f8714789c7192f7ad340e","latest"],"id":1}`,
},
{
2,
common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
models.Earliest,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x71562b71999873db5b286df957af199ec94617f7","earliest"],"id":2}`,
},
{
3,
common.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
models.Pending,
`{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x1b5fd2fed153fa7fac43300273c70c068bfa406a","pending"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.GetBalance(testCase.address, testCase.blockNum)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetLogs(t *testing.T) {
testCases := []struct {
reqId int
fromBlock uint64
toBlock uint64
address common.Address
expected string
}{
{
1,
1843,
1848,
common.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x733","toBlock":"0x738","address":"0x67b1d87101671b127f5f8714789c7192f7ad340e"}],"id":1}`,
},
{
2,
12,
12,
common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0xc","toBlock":"0xc","address":"0x71562b71999873db5b286df957af199ec94617f7"}],"id":2}`,
},
{
3,
0,
123456789,
common.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
`{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x0","toBlock":"0x75bcd15","address":"0x1b5fd2fed153fa7fac43300273c70c068bfa406a"}],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.GetLogs(testCase.fromBlock, testCase.toBlock, testCase.address)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_GetTransactionCount(t *testing.T) {
testCases := []struct {
reqId int
address common.Address
blockNum models.BlockNumber
expected string
}{
{
1,
common.HexToAddress("0x67b1d87101671b127f5f8714789c7192f7ad340e"),
models.Latest,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x67b1d87101671b127f5f8714789c7192f7ad340e","latest"],"id":1}`,
},
{
2,
common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7"),
models.Earliest,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x71562b71999873db5b286df957af199ec94617f7","earliest"],"id":2}`,
},
{
3,
common.HexToAddress("0x1b5fd2fed153fa7fac43300273c70c068bfa406a"),
models.Pending,
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x1b5fd2fed153fa7fac43300273c70c068bfa406a","pending"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.GetTransactionCount(testCase.address, testCase.blockNum)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_SendRawTransaction(t *testing.T) {
testCases := []struct {
reqId int
signedTx []byte
expected string
}{
{
1,
common.HexToHash("0x1cd73c7adf5b31f3cf94c67b9e251e699559d91c27664463fb5978b97f8b2d1b").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x1cd73c7adf5b31f3cf94c67b9e251e699559d91c27664463fb5978b97f8b2d1b"],"id":1}`,
},
{
2,
common.HexToHash("0x1cfe7ce95a1694d8969365cb472ce4a0d3eed812c540fd7708bbe6941e34c4de").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x1cfe7ce95a1694d8969365cb472ce4a0d3eed812c540fd7708bbe6941e34c4de"],"id":2}`,
},
{
3,
common.HexToHash("0x6f9e34c00812a80fa87df26208bbe69411e36d6a9f00b35444ef4181f6c483ca").Bytes(),
`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x6f9e34c00812a80fa87df26208bbe69411e36d6a9f00b35444ef4181f6c483ca"],"id":3}`,
},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.SendRawTransaction(testCase.signedTx)
require.EqualValues(t, testCase.expected, got)
}
}
func TestRequestGenerator_TxpoolContent(t *testing.T) {
testCases := []struct {
reqId int
expected string
}{
{1, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":1}`},
{2, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":2}`},
{3, `{"jsonrpc":"2.0","method":"txpool_content","params":[],"id":3}`},
}
for _, testCase := range testCases {
reqGen := MockRequestGenerator(testCase.reqId)
got := reqGen.TxpoolContent()
require.EqualValues(t, testCase.expected, got)
}
}