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.

128 lines
3.6 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/tendermint/tendermint/account"
  5. . "github.com/tendermint/tendermint/common"
  6. _ "github.com/tendermint/tendermint/config/tendermint_test"
  7. )
  8. func TestSendTxSignable(t *testing.T) {
  9. sendTx := &SendTx{
  10. Inputs: []*TxInput{
  11. &TxInput{
  12. Address: []byte("input1"),
  13. Amount: 12345,
  14. Sequence: 67890,
  15. },
  16. &TxInput{
  17. Address: []byte("input2"),
  18. Amount: 111,
  19. Sequence: 222,
  20. },
  21. },
  22. Outputs: []*TxOutput{
  23. &TxOutput{
  24. Address: []byte("output1"),
  25. Amount: 333,
  26. },
  27. &TxOutput{
  28. Address: []byte("output2"),
  29. Amount: 444,
  30. },
  31. },
  32. }
  33. signBytes := account.SignBytes(sendTx)
  34. signStr := string(signBytes)
  35. expected := Fmt(`{"network":"%X","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  36. config.GetString("network"))
  37. if signStr != expected {
  38. t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  39. }
  40. }
  41. func TestCallTxSignable(t *testing.T) {
  42. callTx := &CallTx{
  43. Input: &TxInput{
  44. Address: []byte("input1"),
  45. Amount: 12345,
  46. Sequence: 67890,
  47. },
  48. Address: []byte("contract1"),
  49. GasLimit: 111,
  50. Fee: 222,
  51. Data: []byte("data1"),
  52. }
  53. signBytes := account.SignBytes(callTx)
  54. signStr := string(signBytes)
  55. expected := Fmt(`{"network":"%X","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
  56. config.GetString("network"))
  57. if signStr != expected {
  58. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  59. }
  60. }
  61. func TestBondTxSignable(t *testing.T) {
  62. privAccount := account.GenPrivAccountFromKey([64]byte{})
  63. bondTx := &BondTx{
  64. PubKey: privAccount.PubKey.(account.PubKeyEd25519),
  65. Inputs: []*TxInput{
  66. &TxInput{
  67. Address: []byte("input1"),
  68. Amount: 12345,
  69. Sequence: 67890,
  70. },
  71. &TxInput{
  72. Address: []byte("input2"),
  73. Amount: 111,
  74. Sequence: 222,
  75. },
  76. },
  77. UnbondTo: []*TxOutput{
  78. &TxOutput{
  79. Address: []byte("output1"),
  80. Amount: 333,
  81. },
  82. &TxOutput{
  83. Address: []byte("output2"),
  84. Amount: 444,
  85. },
  86. },
  87. }
  88. signBytes := account.SignBytes(bondTx)
  89. signStr := string(signBytes)
  90. expected := Fmt(`{"network":"%X","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  91. config.GetString("network"))
  92. if signStr != expected {
  93. t.Errorf("Got unexpected sign string for BondTx")
  94. }
  95. }
  96. func TestUnbondTxSignable(t *testing.T) {
  97. unbondTx := &UnbondTx{
  98. Address: []byte("address1"),
  99. Height: 111,
  100. }
  101. signBytes := account.SignBytes(unbondTx)
  102. signStr := string(signBytes)
  103. expected := Fmt(`{"network":"%X","tx":[18,{"address":"6164647265737331","height":111}]}`,
  104. config.GetString("network"))
  105. if signStr != expected {
  106. t.Errorf("Got unexpected sign string for UnbondTx")
  107. }
  108. }
  109. func TestRebondTxSignable(t *testing.T) {
  110. rebondTx := &RebondTx{
  111. Address: []byte("address1"),
  112. Height: 111,
  113. }
  114. signBytes := account.SignBytes(rebondTx)
  115. signStr := string(signBytes)
  116. expected := Fmt(`{"network":"%X","tx":[19,{"address":"6164647265737331","height":111}]}`,
  117. config.GetString("network"))
  118. if signStr != expected {
  119. t.Errorf("Got unexpected sign string for RebondTx")
  120. }
  121. }