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.

114 lines
2.6 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, "0x02fb52b5d0b9ef2626c11701bb751b2720c76d59946b9a48146ac153bb6e63bf6a", 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, "0x0276114a8f666fa1ff7dbf34b4a9da577808dc501e3b2760d01fe3ef5473f5737f", 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, "0x025afb63126d3067f61f633d13e5a51da0551af3a4567a9af2db5321ed04214ff4", 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, "0x02cf390157041c3b1b59f0aaed4da464f0d0d48f1d026e46fd89c7fe1e5aed7fcf", 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, "0x020ec18eaae67fcd545998841a9c4be09ee3083e12db6ae5e5213a2ecaaa52d5cf", 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, "0x02f036223e79fac776de107f50822552cc964ee9fc4caa304613285f6976bcc940", 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, "0x029c8aef9ef24531e4cf84e78cbab1018ba1626a5a10afb6b7c356be1b5c28e92c", l2Tx.TxID.String())
  85. }
  86. func TestL2TxByteParsers(t *testing.T) {
  87. amount := new(big.Int)
  88. amount.SetString("79000000", 10)
  89. l2Tx := &L2Tx{
  90. ToIdx: 256,
  91. Amount: amount,
  92. FromIdx: 257,
  93. Fee: 201,
  94. }
  95. // Data from the compatibility test
  96. expected := "00000101000001002b16c9"
  97. encodedData, err := l2Tx.BytesDataAvailability(32)
  98. require.NoError(t, err)
  99. assert.Equal(t, expected, hex.EncodeToString(encodedData))
  100. decodedData, err := L2TxFromBytesDataAvailability(encodedData, 32)
  101. require.NoError(t, err)
  102. assert.Equal(t, l2Tx, decodedData)
  103. }