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.

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