github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/create_miner_tx_test.go1 (about) 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 17 package blockchain 18 19 //import "fmt" 20 21 import "testing" 22 23 import "github.com/deroproject/derosuite/walletapi" 24 import "github.com/deroproject/derosuite/crypto" 25 26 //import "github.com/deroproject/derosuite/address" 27 import "github.com/deroproject/derosuite/transaction" 28 29 // file to test whether the miner tx is created successfully and can be serialized/decoded successfully by the block miner 30 31 func Test_Create_Miner_TX(t *testing.T) { 32 33 for i := 0; i < 1; i++ { 34 35 hf_version := uint64(0) 36 height := uint64(i) 37 reward := uint64(i + 1) 38 39 account, _ := walletapi.Generate_Keys_From_Random() 40 41 miner_address := account.GetAddress() 42 43 miner_tx_original, err := Create_Miner_TX(hf_version, height, reward, miner_address, 0) 44 45 if err != nil { 46 t.Fatalf("error creating miner tx, err :%s", err) 47 } 48 49 miner_tx_original_serialized := miner_tx_original.Serialize() 50 51 var miner_tx_parsed transaction.Transaction 52 53 err = miner_tx_parsed.DeserializeHeader(miner_tx_original_serialized) 54 if err != nil { 55 t.Fatalf("error parsing created miner tx, err :%s", err) 56 } 57 58 miner_tx_parsed.Parse_Extra() // parse the extra 59 60 if miner_tx_parsed.Vin[0].(transaction.Txin_gen).Height != height { 61 t.Fatalf("miner tx height mismatch") 62 } 63 64 if miner_tx_parsed.Vout[0].Amount != reward { 65 t.Fatalf("miner tx reward mismatch") 66 } 67 68 // check whether we can decode it output 69 70 public_key := miner_tx_parsed.Extra_map[transaction.TX_PUBLIC_KEY].(crypto.Key) 71 vout_key := miner_tx_parsed.Vout[0].Target.(transaction.Txout_to_key).Key 72 index_within_tx := uint64(0) 73 if !account.Is_Output_Ours(public_key, index_within_tx, vout_key) { 74 t.Fatalf("miner tx cannot be decrypted by the wallet") 75 } 76 77 } 78 79 } 80 81 func Test_Create_Miner_TX_with_extra(t *testing.T) { 82 83 for i := 0; i < 1; i++ { 84 85 hf_version := uint64(0) 86 height := uint64(i) 87 reward := uint64(i + 1) 88 89 account, _ := walletapi.Generate_Keys_From_Random() 90 91 miner_address := account.GetAddress() 92 93 miner_tx_original, err := Create_Miner_TX(hf_version, height, reward, miner_address, 60) 94 95 if err != nil { 96 t.Fatalf("error creating miner tx, err :%s", err) 97 } 98 99 miner_tx_original_serialized := miner_tx_original.Serialize() 100 101 var miner_tx_parsed transaction.Transaction 102 103 err = miner_tx_parsed.DeserializeHeader(miner_tx_original_serialized) 104 if err != nil { 105 t.Fatalf("error parsing created miner tx, err :%s", err) 106 } 107 108 miner_tx_parsed.Parse_Extra() // parse the extra 109 110 if miner_tx_parsed.Vin[0].(transaction.Txin_gen).Height != height { 111 t.Fatalf("miner tx height mismatch") 112 } 113 114 if miner_tx_parsed.Vout[0].Amount != reward { 115 t.Fatalf("miner tx reward mismatch") 116 } 117 118 // check whether we can decode it output 119 120 public_key := miner_tx_parsed.Extra_map[transaction.TX_PUBLIC_KEY].(crypto.Key) 121 vout_key := miner_tx_parsed.Vout[0].Target.(transaction.Txout_to_key).Key 122 index_within_tx := uint64(0) 123 if !account.Is_Output_Ours(public_key, index_within_tx, vout_key) { 124 t.Fatalf("miner tx cannot be decrypted by the wallet") 125 } 126 127 extra_data := miner_tx_parsed.Extra_map[transaction.TX_EXTRA_NONCE].([]byte) 128 129 if len(extra_data) != 60 { 130 t.Fatalf("miner tx extra data mismatch") 131 } 132 133 } 134 135 }