You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.3 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestNewL2Tx(t *testing.T) {
  10. l2Tx := &L2Tx{
  11. FromIdx: 87654,
  12. ToIdx: 300,
  13. TokenID: 5,
  14. Amount: big.NewInt(4),
  15. Nonce: 144,
  16. }
  17. l2Tx, err := NewL2Tx(l2Tx)
  18. assert.NoError(t, err)
  19. assert.Equal(t, "0x022669acda59b827d20ef5354a3eebd1dffb3972b0a6bf89d18bfd2efa0ab9f41e",
  20. l2Tx.TxID.String())
  21. l2Tx = &L2Tx{
  22. FromIdx: 87654,
  23. ToIdx: 300,
  24. TokenID: 5,
  25. Amount: big.NewInt(4),
  26. Nonce: 1,
  27. }
  28. l2Tx, err = NewL2Tx(l2Tx)
  29. assert.NoError(t, err)
  30. assert.Equal(t, "0x029e7499a830f8f5eb17c07da48cf91415710f1bcbe0169d363ff91e81faf92fc2",
  31. l2Tx.TxID.String())
  32. l2Tx = &L2Tx{
  33. FromIdx: 87654,
  34. ToIdx: 300,
  35. TokenID: 5,
  36. Amount: big.NewInt(4),
  37. Fee: 126,
  38. Nonce: 3,
  39. }
  40. l2Tx, err = NewL2Tx(l2Tx)
  41. assert.NoError(t, err)
  42. assert.Equal(t, "0x0255c70ed20e1b8935232e1b9c5884dbcc88a6e1a3454d24f2d77252eb2bb0b64e",
  43. l2Tx.TxID.String())
  44. l2Tx = &L2Tx{
  45. FromIdx: 87654,
  46. ToIdx: 300,
  47. TokenID: 5,
  48. Amount: big.NewInt(4),
  49. Nonce: 1003,
  50. Fee: 144,
  51. }
  52. l2Tx, err = NewL2Tx(l2Tx)
  53. assert.NoError(t, err)
  54. assert.Equal(t, "0x0206b372f967061d1148bbcff679de38120e075141a80a07326d0f514c2efc6ca9",
  55. l2Tx.TxID.String())
  56. l2Tx = &L2Tx{
  57. FromIdx: 1,
  58. ToIdx: 1,
  59. TokenID: 1,
  60. Amount: big.NewInt(1),
  61. Nonce: 1,
  62. Fee: 1,
  63. }
  64. l2Tx, err = NewL2Tx(l2Tx)
  65. assert.NoError(t, err)
  66. assert.Equal(t, "0x0236f7ea5bccf78ba60baf56c058d235a844f9b09259fd0efa4f5f72a7d4a26618",
  67. l2Tx.TxID.String())
  68. l2Tx = &L2Tx{
  69. FromIdx: 999,
  70. ToIdx: 999,
  71. TokenID: 999,
  72. Amount: big.NewInt(999),
  73. Nonce: 999,
  74. Fee: 255,
  75. }
  76. l2Tx, err = NewL2Tx(l2Tx)
  77. assert.NoError(t, err)
  78. assert.Equal(t, "0x02ac122f5b709ce190129fecbbe35bfd30c70e6433dbd85a8eb743d110906a1dc1",
  79. l2Tx.TxID.String())
  80. l2Tx = &L2Tx{
  81. FromIdx: 4444,
  82. ToIdx: 300,
  83. TokenID: 0,
  84. Amount: big.NewInt(3400000000),
  85. Nonce: 2,
  86. Fee: 25,
  87. }
  88. l2Tx, err = NewL2Tx(l2Tx)
  89. assert.NoError(t, err)
  90. assert.Equal(t, "0x02c674951a81881b7bc50db3b9e5efd97ac88550c7426ac548720e5057cfba515a",
  91. l2Tx.TxID.String())
  92. }
  93. func TestL2TxByteParsers(t *testing.T) {
  94. // test vectors values generated from javascript implementation
  95. amount, ok := new(big.Int).SetString("343597383670000000000000000000000000000000", 10)
  96. require.True(t, ok)
  97. l2Tx := &L2Tx{
  98. ToIdx: (1 << 16) - 1,
  99. FromIdx: (1 << 16) - 1,
  100. Amount: amount,
  101. Fee: (1 << 8) - 1,
  102. }
  103. expected := "ffffffffffffffffffff"
  104. encodedData, err := l2Tx.BytesDataAvailability(16)
  105. require.NoError(t, err)
  106. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  107. decodedData, err := L2TxFromBytesDataAvailability(encodedData, 16)
  108. require.NoError(t, err)
  109. assert.Equal(t, l2Tx, decodedData)
  110. l2Tx = &L2Tx{
  111. ToIdx: (1 << 32) - 1,
  112. FromIdx: (1 << 32) - 1,
  113. Amount: amount,
  114. Fee: (1 << 8) - 1,
  115. }
  116. expected = "ffffffffffffffffffffffffffff"
  117. encodedData, err = l2Tx.BytesDataAvailability(32)
  118. require.NoError(t, err)
  119. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  120. decodedData, err = L2TxFromBytesDataAvailability(encodedData, 32)
  121. require.NoError(t, err)
  122. assert.Equal(t, l2Tx, decodedData)
  123. l2Tx = &L2Tx{
  124. ToIdx: 0,
  125. FromIdx: 0,
  126. Amount: big.NewInt(0),
  127. Fee: 0,
  128. }
  129. expected = "0000000000000000000000000000"
  130. encodedData, err = l2Tx.BytesDataAvailability(32)
  131. require.NoError(t, err)
  132. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  133. decodedData, err = L2TxFromBytesDataAvailability(encodedData, 32)
  134. require.NoError(t, err)
  135. assert.Equal(t, l2Tx, decodedData)
  136. l2Tx = &L2Tx{
  137. ToIdx: 0,
  138. FromIdx: 1061,
  139. Amount: big.NewInt(420000000000),
  140. Fee: 127,
  141. }
  142. expected = "000004250000000010fa56ea007f"
  143. encodedData, err = l2Tx.BytesDataAvailability(32)
  144. require.NoError(t, err)
  145. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  146. decodedData, err = L2TxFromBytesDataAvailability(encodedData, 32)
  147. require.NoError(t, err)
  148. assert.Equal(t, l2Tx, decodedData)
  149. l2Tx = &L2Tx{
  150. ToIdx: 256,
  151. FromIdx: 257,
  152. Amount: big.NewInt(79000000),
  153. Fee: 201,
  154. }
  155. expected = "00000101000001000004b571c0c9"
  156. encodedData, err = l2Tx.BytesDataAvailability(32)
  157. require.NoError(t, err)
  158. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  159. decodedData, err = L2TxFromBytesDataAvailability(encodedData, 32)
  160. require.NoError(t, err)
  161. assert.Equal(t, l2Tx, decodedData)
  162. }