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.

135 lines
4.1 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package blockchain
  17. //import "fmt"
  18. import "testing"
  19. import "github.com/arnaucode/derosuite/walletapi"
  20. import "github.com/arnaucode/derosuite/crypto"
  21. //import "github.com/arnaucode/derosuite/address"
  22. import "github.com/arnaucode/derosuite/transaction"
  23. // file to test whether the miner tx is created successfully and can be serialized/decoded successfully by the block miner
  24. func Test_Create_Miner_TX(t *testing.T) {
  25. for i := 0; i < 1; i++ {
  26. hf_version := uint64(0)
  27. height := uint64(i)
  28. reward := uint64(i + 1)
  29. account, _ := walletapi.Generate_Keys_From_Random()
  30. miner_address := account.GetAddress()
  31. miner_tx_original, err := Create_Miner_TX(hf_version, height, reward, miner_address, 0)
  32. if err != nil {
  33. t.Fatalf("error creating miner tx, err :%s", err)
  34. }
  35. miner_tx_original_serialized := miner_tx_original.Serialize()
  36. var miner_tx_parsed transaction.Transaction
  37. err = miner_tx_parsed.DeserializeHeader(miner_tx_original_serialized)
  38. if err != nil {
  39. t.Fatalf("error parsing created miner tx, err :%s", err)
  40. }
  41. miner_tx_parsed.Parse_Extra() // parse the extra
  42. if miner_tx_parsed.Vin[0].(transaction.Txin_gen).Height != height {
  43. t.Fatalf("miner tx height mismatch")
  44. }
  45. if miner_tx_parsed.Vout[0].Amount != reward {
  46. t.Fatalf("miner tx reward mismatch")
  47. }
  48. // check whether we can decode it output
  49. public_key := miner_tx_parsed.Extra_map[transaction.TX_PUBLIC_KEY].(crypto.Key)
  50. vout_key := miner_tx_parsed.Vout[0].Target.(transaction.Txout_to_key).Key
  51. index_within_tx := uint64(0)
  52. if !account.Is_Output_Ours(public_key, index_within_tx, vout_key) {
  53. t.Fatalf("miner tx cannot be decrypted by the wallet")
  54. }
  55. }
  56. }
  57. func Test_Create_Miner_TX_with_extra(t *testing.T) {
  58. for i := 0; i < 1; i++ {
  59. hf_version := uint64(0)
  60. height := uint64(i)
  61. reward := uint64(i + 1)
  62. account, _ := walletapi.Generate_Keys_From_Random()
  63. miner_address := account.GetAddress()
  64. miner_tx_original, err := Create_Miner_TX(hf_version, height, reward, miner_address, 60)
  65. if err != nil {
  66. t.Fatalf("error creating miner tx, err :%s", err)
  67. }
  68. miner_tx_original_serialized := miner_tx_original.Serialize()
  69. var miner_tx_parsed transaction.Transaction
  70. err = miner_tx_parsed.DeserializeHeader(miner_tx_original_serialized)
  71. if err != nil {
  72. t.Fatalf("error parsing created miner tx, err :%s", err)
  73. }
  74. miner_tx_parsed.Parse_Extra() // parse the extra
  75. if miner_tx_parsed.Vin[0].(transaction.Txin_gen).Height != height {
  76. t.Fatalf("miner tx height mismatch")
  77. }
  78. if miner_tx_parsed.Vout[0].Amount != reward {
  79. t.Fatalf("miner tx reward mismatch")
  80. }
  81. // check whether we can decode it output
  82. public_key := miner_tx_parsed.Extra_map[transaction.TX_PUBLIC_KEY].(crypto.Key)
  83. vout_key := miner_tx_parsed.Vout[0].Target.(transaction.Txout_to_key).Key
  84. index_within_tx := uint64(0)
  85. if !account.Is_Output_Ours(public_key, index_within_tx, vout_key) {
  86. t.Fatalf("miner tx cannot be decrypted by the wallet")
  87. }
  88. extra_data := miner_tx_parsed.Extra_map[transaction.TX_EXTRA_NONCE].([]byte)
  89. if len(extra_data) != 60 {
  90. t.Fatalf("miner tx extra data mismatch")
  91. }
  92. }
  93. }