decred.org/dcrdex@v1.0.5/dex/networks/ltc/block_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org
     3  
     4  package ltc
     5  
     6  import (
     7  	"bytes"
     8  	_ "embed"
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/btcsuite/btcd/txscript"
    13  	"github.com/btcsuite/btcd/wire"
    14  )
    15  
    16  var (
    17  	// Testnet4 block 1821752 is pre-MWEB activation, 3 txns.
    18  	// But version 20000000.
    19  	//go:embed test-data/testnet4Block1821752.dat
    20  	block1821752 []byte
    21  
    22  	// Block 2215584 is the first with MW txns, a peg-in with witness version 9
    23  	// script, an integ tx with witness version 8 script, block version 20000000
    24  	// (with a MWEB), and 5 txns.
    25  	// 7e35fabe7b3c694ebeb0368a1a1c31e83962f3c5b4cc8dcede3ae94ed3deb306
    26  	//go:embed test-data/testnet4Block2215584.dat
    27  	block2215584 []byte
    28  
    29  	// Block 2321749 is version 20000000 with a MWEB, 4 txns, the last one being
    30  	// an integration / hogex txn that fails to decode.
    31  	// 57929846db4a92d937eb596354d10949e33c815ee45df0c9b3bbdfb283e15bcd
    32  	//go:embed test-data/testnet4Block2321749.dat
    33  	block2321749 []byte
    34  
    35  	// Block 2319633 is version 20000000 with a MWEB, 2 txns, one coinbase and
    36  	// one integration.
    37  	// e9fe2c6496aedefa8bf6529bdc5c1f9fd4af565ca4c98cab73e3a1f616fb3502
    38  	//go:embed test-data/testnet4Block2319633.dat
    39  	block2319633 []byte
    40  
    41  	//go:embed test-data/testnet4Block2215586.dat
    42  	block2215586 []byte
    43  )
    44  
    45  func TestDeserializeBlockBytes(t *testing.T) {
    46  	tests := []struct {
    47  		name       string
    48  		blk        []byte
    49  		wantHash   string
    50  		wantNumTx  int
    51  		wantLastTx string
    52  	}{
    53  		{
    54  			"block 1821752 pre-MWEB activation",
    55  			block1821752,
    56  			"ece484c02e84e4b1c551fbbdde3045e9096c970fbd3e31f2586b68d50dad6b24",
    57  			3,
    58  			"cb4d9d2d7ab7211ddf030a667d320fe499c849623e9d4a130e1901391e9d4947",
    59  		},
    60  		{
    61  			"block 2215584 MWEB",
    62  			block2215584,
    63  			"7e35fabe7b3c694ebeb0368a1a1c31e83962f3c5b4cc8dcede3ae94ed3deb306",
    64  			5,
    65  			"4c86658e64861c2f2b7fbbf26bbf7a6640ae3824d24293a009ad5ea1e8ab4418",
    66  		},
    67  		{
    68  			"block 2215586 MWEB",
    69  			block2215586,
    70  			"3000cc2076a568a8eb5f56a06112a57264446e2c7d2cca28cdc85d91820dfa17",
    71  			37,
    72  			"3a7299f5e6ee9975bdcc2d754ff5de3312d92db177b55c68753a1cdf9ce63a7c",
    73  		},
    74  		{
    75  			"block 2321749 MWEB",
    76  			block2321749,
    77  			"57929846db4a92d937eb596354d10949e33c815ee45df0c9b3bbdfb283e15bcd",
    78  			4,
    79  			"1bad5e78b145947d32eeeb1d24295891ba03359508d5f09921bada3be66bbe17",
    80  		},
    81  		{
    82  			"block 2319633 MWEB",
    83  			block2319633,
    84  			"e9fe2c6496aedefa8bf6529bdc5c1f9fd4af565ca4c98cab73e3a1f616fb3502",
    85  			2,
    86  			"3cd43df64e9382040eff0bf54ba1c2389d5111eb5ab0968ab7af67e3c30cac04",
    87  		},
    88  	}
    89  
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			msgBlk, err := DeserializeBlockBytes(tt.blk)
    93  			if err != nil {
    94  				t.Fatal(err)
    95  			}
    96  
    97  			blkHash := msgBlk.BlockHash()
    98  			if blkHash.String() != tt.wantHash {
    99  				t.Errorf("Wanted block hash %v, got %v", tt.wantHash, blkHash)
   100  			}
   101  
   102  			if len(msgBlk.Transactions) != tt.wantNumTx {
   103  				t.Errorf("Wanted %d txns, found %d", tt.wantNumTx, len(msgBlk.Transactions))
   104  			}
   105  
   106  			lastTxHash := msgBlk.Transactions[len(msgBlk.Transactions)-1].TxHash()
   107  			if lastTxHash.String() != tt.wantLastTx {
   108  				t.Errorf("Wanted last tx hash %v, got %v", tt.wantLastTx, lastTxHash)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestDecodeTransaction(t *testing.T) {
   115  	// pegin 84b7ea499d5650cc220afac8b972527cef10ed402da5a5b000f994199044f450
   116  	// output has witness ver 9
   117  	peginTx, _ := hex.DecodeString("02000000000101d4cfc0df00ced17d9f05460b20be3f8b362e213fbfb22514c5a7e566bd9bd6a10000000000feffffff012a" +
   118  		"c07b050000000022592056961cee5a05f60a40f4730bef10786b0b54e42e460ee9102b2756b69efe37210247304402205f44" +
   119  		"1fa690d41056ab5c635fd8f96427cdb7825ba5cdb4a977758fb09ac2bb2e02204c068a057469fd22176ec45bd1bc780bb6db" +
   120  		"34e299a601ae735b8f3db5abd4b10121029e94825ddd9ed088ca2c270b3ccb5cb5573a8204215912cac1f68f288190270c9f" +
   121  		"ce2100")
   122  	msgTx := &wire.MsgTx{}
   123  	err := msgTx.Deserialize(bytes.NewReader(peginTx))
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if msgTx.TxOut[0].PkScript[0] != txscript.OP_9 {
   128  		t.Errorf("did not get witness version 9")
   129  	}
   130  
   131  	// integ 3cd43df64e9382040eff0bf54ba1c2389d5111eb5ab0968ab7af67e3c30cac04
   132  	// output has witness ver 8
   133  	// tx flag has bit 8 set, indicating hogex
   134  	integTx, _ := hex.DecodeString("02000000000801bba0a561a904465fe2215f77822e04045ca01491c358bb16" +
   135  		"89200d71ada3836b0000000000ffffffff01ec069e659a320000225820399cda16" +
   136  		"3b49fdac1f669f69e63b56756d3bc6f2523eb10615710154959cc1360000000000")
   137  	msgTx = &wire.MsgTx{}
   138  	err = msgTx.Deserialize(bytes.NewReader(integTx))
   139  	if err == nil { // witness tx but flag byte is 08
   140  		t.Fatal("expected error") //fails because of flag
   141  	}
   142  }