op-geth/internal/ethapi/api_test.go
2023-02-22 09:49:45 -06:00

97 lines
2.2 KiB
Go

package ethapi
import (
"encoding/json"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
)
func TestUnmarshalRpcDepositTx(t *testing.T) {
tests := []struct {
name string
modifier func(tx *RPCTransaction)
valid bool
}{
{
name: "Unmodified",
modifier: func(tx *RPCTransaction) {},
valid: true,
},
{
name: "Zero Values",
modifier: func(tx *RPCTransaction) {
tx.V = (*hexutil.Big)(common.Big0)
tx.R = (*hexutil.Big)(common.Big0)
tx.S = (*hexutil.Big)(common.Big0)
tx.GasPrice = (*hexutil.Big)(common.Big0)
},
valid: true,
},
{
name: "Nil Values",
modifier: func(tx *RPCTransaction) {
tx.V = nil
tx.R = nil
tx.S = nil
tx.GasPrice = nil
},
valid: true,
},
{
name: "Non-Zero GasPrice",
modifier: func(tx *RPCTransaction) {
tx.GasPrice = (*hexutil.Big)(big.NewInt(43))
},
valid: false,
},
{
name: "Non-Zero V",
modifier: func(tx *RPCTransaction) {
tx.V = (*hexutil.Big)(big.NewInt(43))
},
valid: false,
},
{
name: "Non-Zero R",
modifier: func(tx *RPCTransaction) {
tx.R = (*hexutil.Big)(big.NewInt(43))
},
valid: false,
},
{
name: "Non-Zero S",
modifier: func(tx *RPCTransaction) {
tx.S = (*hexutil.Big)(big.NewInt(43))
},
valid: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tx := types.NewTx(&types.DepositTx{
SourceHash: common.HexToHash("0x1234"),
IsSystemTransaction: true,
Mint: big.NewInt(34),
})
rpcTx := newRPCTransaction(tx, common.Hash{}, uint64(12), uint64(1), big.NewInt(0), &params.ChainConfig{})
test.modifier(rpcTx)
json, err := json.Marshal(rpcTx)
require.NoError(t, err, "marshalling failed: %w", err)
println(string(json))
parsed := &types.Transaction{}
err = parsed.UnmarshalJSON(json)
if test.valid {
require.NoError(t, err, "unmarshal failed: %w", err)
} else {
require.Error(t, err, "unmarshal should have failed but did not")
}
})
}
}