github.com/palcoin-project/palcd@v1.0.0/blockchain/upgrade_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  // TestDeserializeUtxoEntryV0 ensures deserializing unspent trasaction output
    13  // entries from the legacy version 0 format works as expected.
    14  func TestDeserializeUtxoEntryV0(t *testing.T) {
    15  	tests := []struct {
    16  		name       string
    17  		entries    map[uint32]*UtxoEntry
    18  		serialized []byte
    19  	}{
    20  		// From tx in main blockchain:
    21  		// 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098
    22  		{
    23  			name: "Only output 0, coinbase",
    24  			entries: map[uint32]*UtxoEntry{
    25  				0: {
    26  					amount:      5000000000,
    27  					pkScript:    hexToBytes("410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac"),
    28  					blockHeight: 1,
    29  					packedFlags: tfCoinBase,
    30  				},
    31  			},
    32  			serialized: hexToBytes("010103320496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52"),
    33  		},
    34  		// From tx in main blockchain:
    35  		// 8131ffb0a2c945ecaf9b9063e59558784f9c3a74741ce6ae2a18d0571dac15bb
    36  		{
    37  			name: "Only output 1, not coinbase",
    38  			entries: map[uint32]*UtxoEntry{
    39  				1: {
    40  					amount:      1000000,
    41  					pkScript:    hexToBytes("76a914ee8bd501094a7d5ca318da2506de35e1cb025ddc88ac"),
    42  					blockHeight: 100001,
    43  					packedFlags: 0,
    44  				},
    45  			},
    46  			serialized: hexToBytes("01858c21040700ee8bd501094a7d5ca318da2506de35e1cb025ddc"),
    47  		},
    48  		// Adapted from tx in main blockchain:
    49  		// df3f3f442d9699857f7f49de4ff0b5d0f3448bec31cdc7b5bf6d25f2abd637d5
    50  		{
    51  			name: "Only output 2, coinbase",
    52  			entries: map[uint32]*UtxoEntry{
    53  				2: {
    54  					amount:      100937281,
    55  					pkScript:    hexToBytes("76a914da33f77cee27c2a975ed5124d7e4f7f97513510188ac"),
    56  					blockHeight: 99004,
    57  					packedFlags: tfCoinBase,
    58  				},
    59  			},
    60  			serialized: hexToBytes("0185843c010182b095bf4100da33f77cee27c2a975ed5124d7e4f7f975135101"),
    61  		},
    62  		// Adapted from tx in main blockchain:
    63  		// 4a16969aa4764dd7507fc1de7f0baa4850a246de90c45e59a3207f9a26b5036f
    64  		{
    65  			name: "outputs 0 and 2 not coinbase",
    66  			entries: map[uint32]*UtxoEntry{
    67  				0: {
    68  					amount:      20000000,
    69  					pkScript:    hexToBytes("76a914e2ccd6ec7c6e2e581349c77e067385fa8236bf8a88ac"),
    70  					blockHeight: 113931,
    71  					packedFlags: 0,
    72  				},
    73  				2: {
    74  					amount:      15000000,
    75  					pkScript:    hexToBytes("76a914b8025be1b3efc63b0ad48e7f9f10e87544528d5888ac"),
    76  					blockHeight: 113931,
    77  					packedFlags: 0,
    78  				},
    79  			},
    80  			serialized: hexToBytes("0185f90b0a011200e2ccd6ec7c6e2e581349c77e067385fa8236bf8a800900b8025be1b3efc63b0ad48e7f9f10e87544528d58"),
    81  		},
    82  		// Adapted from tx in main blockchain:
    83  		// 1b02d1c8cfef60a189017b9a420c682cf4a0028175f2f563209e4ff61c8c3620
    84  		{
    85  			name: "Only output 22, not coinbase",
    86  			entries: map[uint32]*UtxoEntry{
    87  				22: {
    88  					amount:      366875659,
    89  					pkScript:    hexToBytes("a9141dd46a006572d820e448e12d2bbb38640bc718e687"),
    90  					blockHeight: 338156,
    91  					packedFlags: 0,
    92  				},
    93  			},
    94  			serialized: hexToBytes("0193d06c100000108ba5b9e763011dd46a006572d820e448e12d2bbb38640bc718e6"),
    95  		},
    96  	}
    97  
    98  	for i, test := range tests {
    99  		// Deserialize to map of utxos keyed by the output index.
   100  		entries, err := deserializeUtxoEntryV0(test.serialized)
   101  		if err != nil {
   102  			t.Errorf("deserializeUtxoEntryV0 #%d (%s) unexpected "+
   103  				"error: %v", i, test.name, err)
   104  			continue
   105  		}
   106  
   107  		// Ensure the deserialized entry has the same properties as the
   108  		// ones in the test entry.
   109  		if !reflect.DeepEqual(entries, test.entries) {
   110  			t.Errorf("deserializeUtxoEntryV0 #%d (%s) unexpected "+
   111  				"entries: got %v, want %v", i, test.name,
   112  				entries, test.entries)
   113  			continue
   114  		}
   115  	}
   116  }