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.

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