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.

137 lines
3.8 KiB

  1. package types
  2. import (
  3. "testing"
  4. acm "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 := acm.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 := acm.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. privKeyBytes := make([]byte, 64)
  67. var privKeyArray [64]byte
  68. copy(privKeyArray[:], privKeyBytes)
  69. privAccount := acm.GenPrivAccountFromPrivKeyBytes(&privKeyArray)
  70. bondTx := &BondTx{
  71. PubKey: privAccount.PubKey.(acm.PubKeyEd25519),
  72. Inputs: []*TxInput{
  73. &TxInput{
  74. Address: []byte("input1"),
  75. Amount: 12345,
  76. Sequence: 67890,
  77. },
  78. &TxInput{
  79. Address: []byte("input2"),
  80. Amount: 111,
  81. Sequence: 222,
  82. },
  83. },
  84. UnbondTo: []*TxOutput{
  85. &TxOutput{
  86. Address: []byte("output1"),
  87. Amount: 333,
  88. },
  89. &TxOutput{
  90. Address: []byte("output2"),
  91. Amount: 444,
  92. },
  93. },
  94. }
  95. signBytes := acm.SignBytes(chainID, bondTx)
  96. signStr := string(signBytes)
  97. 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}]}]}`,
  98. config.GetString("chain_id"))
  99. if signStr != expected {
  100. t.Errorf("Unexpected sign string for BondTx. \nGot %s\nExpected %s", signStr, expected)
  101. }
  102. }
  103. func TestUnbondTxSignable(t *testing.T) {
  104. unbondTx := &UnbondTx{
  105. Address: []byte("address1"),
  106. Height: 111,
  107. }
  108. signBytes := acm.SignBytes(chainID, unbondTx)
  109. signStr := string(signBytes)
  110. expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`,
  111. config.GetString("chain_id"))
  112. if signStr != expected {
  113. t.Errorf("Got unexpected sign string for UnbondTx")
  114. }
  115. }
  116. func TestRebondTxSignable(t *testing.T) {
  117. rebondTx := &RebondTx{
  118. Address: []byte("address1"),
  119. Height: 111,
  120. }
  121. signBytes := acm.SignBytes(chainID, rebondTx)
  122. signStr := string(signBytes)
  123. expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`,
  124. config.GetString("chain_id"))
  125. if signStr != expected {
  126. t.Errorf("Got unexpected sign string for RebondTx")
  127. }
  128. }