github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/jsonrpc/types/types_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/0xPolygon/supernets2-node/hex"
     9  	"github.com/0xPolygon/supernets2-node/state"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestArgHashUnmarshalFromShortString(t *testing.T) {
    16  	type testCase struct {
    17  		name           string
    18  		input          string
    19  		expectedResult string
    20  		expectedError  error
    21  	}
    22  	testCases := []testCase{
    23  		{
    24  			name:           "valid hex value starting with 0x",
    25  			input:          "0x1",
    26  			expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000001",
    27  			expectedError:  nil,
    28  		},
    29  		{
    30  			name:           "valid hex value starting without 0x",
    31  			input:          "1",
    32  			expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000001",
    33  			expectedError:  nil,
    34  		},
    35  		{
    36  			name:           "valid full hash value",
    37  			input:          "0x05b21ee5f65c28a0af8e71290fc33625a1279a8b3d6357ce3ca60f22dbf59e63",
    38  			expectedResult: "0x05b21ee5f65c28a0af8e71290fc33625a1279a8b3d6357ce3ca60f22dbf59e63",
    39  			expectedError:  nil,
    40  		},
    41  		{
    42  			name:           "invalid hex value starting with 0x",
    43  			input:          "0xG",
    44  			expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000000",
    45  			expectedError:  fmt.Errorf("invalid hash, it needs to be a hexadecimal value"),
    46  		},
    47  		{
    48  			name:           "invalid hex value starting without 0x",
    49  			input:          "G",
    50  			expectedResult: "0x0000000000000000000000000000000000000000000000000000000000000000",
    51  			expectedError:  fmt.Errorf("invalid hash, it needs to be a hexadecimal value"),
    52  		},
    53  	}
    54  
    55  	for _, testCase := range testCases {
    56  		t.Run(testCase.name, func(t *testing.T) {
    57  			arg := ArgHash{}
    58  			err := arg.UnmarshalText([]byte(testCase.input))
    59  			require.Equal(t, testCase.expectedError, err)
    60  			assert.Equal(t, testCase.expectedResult, arg.Hash().String())
    61  		})
    62  	}
    63  }
    64  
    65  func TestArgAddressUnmarshalFromShortString(t *testing.T) {
    66  	type testCase struct {
    67  		name           string
    68  		input          string
    69  		expectedResult string
    70  		expectedError  error
    71  	}
    72  	testCases := []testCase{
    73  		{
    74  			name:           "valid hex value starting with 0x",
    75  			input:          "0x1",
    76  			expectedResult: "0x0000000000000000000000000000000000000001",
    77  			expectedError:  nil,
    78  		},
    79  		{
    80  			name:           "valid hex value starting without 0x",
    81  			input:          "1",
    82  			expectedResult: "0x0000000000000000000000000000000000000001",
    83  			expectedError:  nil,
    84  		},
    85  		{
    86  			name:           "valid full address value",
    87  			input:          "0x748964F22eFd023eB78A246A7AC2506e84CC4545",
    88  			expectedResult: "0x748964F22eFd023eB78A246A7AC2506e84CC4545",
    89  			expectedError:  nil,
    90  		},
    91  		{
    92  			name:           "invalid hex value starting with 0x",
    93  			input:          "0xG",
    94  			expectedResult: "0x0000000000000000000000000000000000000000",
    95  			expectedError:  fmt.Errorf("invalid address, it needs to be a hexadecimal value"),
    96  		},
    97  		{
    98  			name:           "invalid hex value starting without 0x",
    99  			input:          "G",
   100  			expectedResult: "0x0000000000000000000000000000000000000000",
   101  			expectedError:  fmt.Errorf("invalid address, it needs to be a hexadecimal value"),
   102  		},
   103  	}
   104  
   105  	for _, testCase := range testCases {
   106  		t.Run(testCase.name, func(t *testing.T) {
   107  			arg := ArgAddress{}
   108  			err := arg.UnmarshalText([]byte(testCase.input))
   109  			require.Equal(t, testCase.expectedError, err)
   110  			assert.Equal(t, testCase.expectedResult, arg.Address().String())
   111  		})
   112  	}
   113  }
   114  
   115  func TestBatchUnmarshal(t *testing.T) {
   116  	// json: `{"number":"0x1","coinbase":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","stateRoot":"0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72","globalExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000001","localExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000002","accInputHash":"0x0000000000000000000000000000000000000000000000000000000000000003","timestamp":"0x64133495","sendSequencesTxHash":"0x0000000000000000000000000000000000000000000000000000000000000004","verifyBatchTxHash":"0x0000000000000000000000000000000000000000000000000000000000000005","transactions":[{"nonce":"0x8","gasPrice":"0x3b9aca00","gas":"0x5208","to":"0xb48ca794d49eec406a5dd2c547717e37b5952a83","value":"0xde0b6b3a7640000","input":"0x","v":"0x7f5","r":"0x27d94abdecca8324d23221cec81f0a3398d7eee2dc831f698fe12447695897d5","s":"0x1b9f1d7cabbb69d309f9e6ffe10b3e205ad86af1058f4dbacdd06a8db03a5669","hash":"0xd0433908a0b56ec6d90758abfe5ae11185e13bedb3d70e8ab7c0d7e3f0e395b5","from":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","blockHash":"0x7e8efeb2b5bb9aaef68a9b2f5b6c0a14900745380a68f72f9c15f978546109cc","blockNumber":"0x1","transactionIndex":"0x0","chainId":"0x3e9","type":"0x0"}]}`
   117  	type testCase struct {
   118  		name     string
   119  		json     string
   120  		expected Batch
   121  	}
   122  	testCases := []testCase{
   123  		{
   124  			name: "with out txs",
   125  			json: `{"number":"0x1","coinbase":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","stateRoot":"0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72","globalExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000001","localExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000002","accInputHash":"0x0000000000000000000000000000000000000000000000000000000000000003","timestamp":"0x64133495","sendSequencesTxHash":"0x0000000000000000000000000000000000000000000000000000000000000004","verifyBatchTxHash":"0x0000000000000000000000000000000000000000000000000000000000000005"}`,
   126  			expected: Batch{
   127  				Number:              1,
   128  				Coinbase:            common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
   129  				StateRoot:           common.HexToHash("0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72"),
   130  				GlobalExitRoot:      common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
   131  				LocalExitRoot:       common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002"),
   132  				AccInputHash:        common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000003"),
   133  				Timestamp:           ArgUint64(1678980245),
   134  				SendSequencesTxHash: state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000004"),
   135  				VerifyBatchTxHash:   state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000005"),
   136  			},
   137  		},
   138  		{
   139  			name: "with txs",
   140  			json: `{"number":"0x1","coinbase":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","stateRoot":"0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72","globalExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000001","localExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000002","accInputHash":"0x0000000000000000000000000000000000000000000000000000000000000003","timestamp":"0x64133495","sendSequencesTxHash":"0x0000000000000000000000000000000000000000000000000000000000000004","verifyBatchTxHash":"0x0000000000000000000000000000000000000000000000000000000000000005","transactions":[{"nonce":"0x8","gasPrice":"0x3b9aca00","gas":"0x5208","to":"0xb48ca794d49eec406a5dd2c547717e37b5952a83","value":"0xde0b6b3a7640000","input":"0x","v":"0x7f5","r":"0x27d94abdecca8324d23221cec81f0a3398d7eee2dc831f698fe12447695897d5","s":"0x1b9f1d7cabbb69d309f9e6ffe10b3e205ad86af1058f4dbacdd06a8db03a5669","hash":"0xd0433908a0b56ec6d90758abfe5ae11185e13bedb3d70e8ab7c0d7e3f0e395b5","from":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","blockHash":"0x7e8efeb2b5bb9aaef68a9b2f5b6c0a14900745380a68f72f9c15f978546109cc","blockNumber":"0x1","transactionIndex":"0x0","chainId":"0x3e9","type":"0x0"}]}`,
   141  			expected: Batch{
   142  				Number:              1,
   143  				Coinbase:            common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
   144  				StateRoot:           common.HexToHash("0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72"),
   145  				GlobalExitRoot:      common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
   146  				LocalExitRoot:       common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002"),
   147  				AccInputHash:        common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000003"),
   148  				Timestamp:           ArgUint64(hex.DecodeUint64("0x64133495")),
   149  				SendSequencesTxHash: state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000004"),
   150  				VerifyBatchTxHash:   state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000005"),
   151  				Transactions: []TransactionOrHash{
   152  					{
   153  						Tx: &Transaction{
   154  							Nonce:       ArgUint64(hex.DecodeUint64("0x8")),
   155  							GasPrice:    ArgBig(*hex.DecodeBig("0x3b9aca00")),
   156  							Gas:         ArgUint64(hex.DecodeUint64("0x5208")),
   157  							To:          state.HexToAddressPtr("0xb48ca794d49eec406a5dd2c547717e37b5952a83"),
   158  							Value:       ArgBig(*hex.DecodeBig("0xde0b6b3a7640000")),
   159  							Input:       ArgBytes{},
   160  							V:           ArgBig(*hex.DecodeBig("0x7f5")),
   161  							R:           ArgBig(*hex.DecodeBig("0x27d94abdecca8324d23221cec81f0a3398d7eee2dc831f698fe12447695897d5")),
   162  							S:           ArgBig(*hex.DecodeBig("0x1b9f1d7cabbb69d309f9e6ffe10b3e205ad86af1058f4dbacdd06a8db03a5669")),
   163  							Hash:        common.HexToHash("0xd0433908a0b56ec6d90758abfe5ae11185e13bedb3d70e8ab7c0d7e3f0e395b5"),
   164  							From:        common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
   165  							BlockHash:   state.HexToHashPtr("0x7e8efeb2b5bb9aaef68a9b2f5b6c0a14900745380a68f72f9c15f978546109cc"),
   166  							BlockNumber: ArgUint64Ptr(ArgUint64(hex.DecodeUint64("0x1"))),
   167  							TxIndex:     ArgUint64Ptr(ArgUint64(hex.DecodeUint64("0x0"))),
   168  							ChainID:     ArgBig(*hex.DecodeBig("0x3e9")),
   169  							Type:        ArgUint64(hex.DecodeUint64("0x0")),
   170  						},
   171  					},
   172  				},
   173  			},
   174  		},
   175  		{
   176  			name: "with tx hashes",
   177  			json: `{"number":"0x1","coinbase":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","stateRoot":"0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72","globalExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000001","localExitRoot":"0x0000000000000000000000000000000000000000000000000000000000000002","accInputHash":"0x0000000000000000000000000000000000000000000000000000000000000003","timestamp":"0x64133495","sendSequencesTxHash":"0x0000000000000000000000000000000000000000000000000000000000000004","verifyBatchTxHash":"0x0000000000000000000000000000000000000000000000000000000000000005","transactions":["0x0000000000000000000000000000000000000000000000000000000000000001","0x0000000000000000000000000000000000000000000000000000000000000002"]}`,
   178  			expected: Batch{
   179  				Number:              1,
   180  				Coinbase:            common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"),
   181  				StateRoot:           common.HexToHash("0x49e7b7eb6bb34b07a1063cd2c7a9cac88845c1867e8a026d69fc00b862c2ca72"),
   182  				GlobalExitRoot:      common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
   183  				LocalExitRoot:       common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002"),
   184  				AccInputHash:        common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000003"),
   185  				Timestamp:           ArgUint64(1678980245),
   186  				SendSequencesTxHash: state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000004"),
   187  				VerifyBatchTxHash:   state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000005"),
   188  				Transactions: []TransactionOrHash{
   189  					{Hash: state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000001")},
   190  					{Hash: state.HexToHashPtr("0x0000000000000000000000000000000000000000000000000000000000000002")},
   191  				},
   192  			},
   193  		},
   194  	}
   195  
   196  	for _, testCase := range testCases {
   197  		b := []byte(testCase.json)
   198  		var result *Batch
   199  		err := json.Unmarshal(b, &result)
   200  		require.NoError(t, err)
   201  
   202  		assert.Equal(t, testCase.expected.Number, result.Number)
   203  		assert.Equal(t, testCase.expected.Coinbase, result.Coinbase)
   204  		assert.Equal(t, testCase.expected.StateRoot, result.StateRoot)
   205  		assert.Equal(t, testCase.expected.GlobalExitRoot, result.GlobalExitRoot)
   206  		assert.Equal(t, testCase.expected.LocalExitRoot, result.LocalExitRoot)
   207  		assert.Equal(t, testCase.expected.AccInputHash, result.AccInputHash)
   208  		assert.Equal(t, testCase.expected.Timestamp, result.Timestamp)
   209  		assert.Equal(t, testCase.expected.SendSequencesTxHash, result.SendSequencesTxHash)
   210  		assert.Equal(t, testCase.expected.VerifyBatchTxHash, result.VerifyBatchTxHash)
   211  		assert.Equal(t, len(testCase.expected.Transactions), len(result.Transactions))
   212  
   213  		for i := 0; i < len(testCase.expected.Transactions); i++ {
   214  			txOrHashExpected := testCase.expected.Transactions[i]
   215  			txOrHashResult := result.Transactions[i]
   216  
   217  			if txOrHashExpected.Hash != nil {
   218  				assert.Equal(t, txOrHashExpected.Hash.String(), txOrHashResult.Hash.String())
   219  			} else {
   220  				assert.Equal(t, txOrHashExpected.Tx.Nonce, txOrHashResult.Tx.Nonce)
   221  				assert.Equal(t, txOrHashExpected.Tx.GasPrice, txOrHashResult.Tx.GasPrice)
   222  				assert.Equal(t, txOrHashExpected.Tx.Gas, txOrHashResult.Tx.Gas)
   223  				assert.Equal(t, txOrHashExpected.Tx.To, txOrHashResult.Tx.To)
   224  				assert.Equal(t, txOrHashExpected.Tx.Value, txOrHashResult.Tx.Value)
   225  				assert.Equal(t, txOrHashExpected.Tx.Input, txOrHashResult.Tx.Input)
   226  				assert.Equal(t, txOrHashExpected.Tx.V, txOrHashResult.Tx.V)
   227  				assert.Equal(t, txOrHashExpected.Tx.R, txOrHashResult.Tx.R)
   228  				assert.Equal(t, txOrHashExpected.Tx.S, txOrHashResult.Tx.S)
   229  				assert.Equal(t, txOrHashExpected.Tx.Hash, txOrHashResult.Tx.Hash)
   230  				assert.Equal(t, txOrHashExpected.Tx.From, txOrHashResult.Tx.From)
   231  				assert.Equal(t, txOrHashExpected.Tx.BlockHash, txOrHashResult.Tx.BlockHash)
   232  				assert.Equal(t, txOrHashExpected.Tx.BlockNumber, txOrHashResult.Tx.BlockNumber)
   233  				assert.Equal(t, txOrHashExpected.Tx.TxIndex, txOrHashResult.Tx.TxIndex)
   234  				assert.Equal(t, txOrHashExpected.Tx.ChainID, txOrHashResult.Tx.ChainID)
   235  				assert.Equal(t, txOrHashExpected.Tx.Type, txOrHashResult.Tx.Type)
   236  			}
   237  		}
   238  	}
   239  }