github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/grs/grsparser_test.go (about)

     1  // +build unittest
     2  
     3  package grs
     4  
     5  import (
     6  	"blockbook/bchain"
     7  	"blockbook/bchain/coins/btc"
     8  	"bytes"
     9  	"encoding/hex"
    10  	"math/big"
    11  	"os"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"github.com/martinboehm/btcutil/chaincfg"
    16  )
    17  
    18  var (
    19  	testTx1, testTx2 bchain.Tx
    20  
    21  	testTxPacked1 = "0a20f56521b17b828897f72b30dd21b0192fd942342e89acbb06abf1d446282c30f512bf0101000000014a9d1fdba915e0907ab02f04f88898863112a2b4fdcf872c7414588c47c874cb000000006a47304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a7430236876101210331693756f749180aeed0a65a0fab0625a2250bd9abca502282a4cf0723152e67ffffffff01a0330300000000001976a914fe40329c95c5598ac60752a5310b320cb52d18e688ac0000000018ffff87da05200028a6f383013298010a001220cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a1800226a47304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a7430236876101210331693756f749180aeed0a65a0fab0625a2250bd9abca502282a4cf0723152e6728ffffffff0f3a460a030333a010001a1976a914fe40329c95c5598ac60752a5310b320cb52d18e688ac222246744d347a416e39615659674867786d616d5742675750795a7362365268766b41394000"
    22  	testTxPacked2 = "0a209b5c4859a8a31e69788cb4402812bb28f14ad71cbd8c60b09903478bc56f79a312e00101000000000101d1613f483f2086d076c82fe34674385a86beb08f052d5405fe1aed397f852f4f0000000000feffffff02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987a8386f0000000000160014cc8067093f6f843d6d3e22004a4290cd0c0f336b02483045022100ea8780bc1e60e14e945a80654a41748bbf1aa7d6f2e40a88d91dfc2de1f34bd10220181a474a3420444bd188501d8d270736e1e9fe379da9970de992ff445b0972e3012103adc58245cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f862d9ed090018caa384da0520d9db2728dadb27322c0a0012204f2f857f39ed1afe05542d058fb0be865a387446e32fc876d086203f483f61d1180028feffffff0f3a450a034c4b4010001a17a9147a55d61848e77ca266e79a39bfc85c580a6426c9872223324e345135466855323439374272794666556762716b414a453837614b4476335633653a4d0a036f38a810011a160014cc8067093f6f843d6d3e22004a4290cd0c0f336b222c746772733171656a7178777a666c64377a72366d663779677179357335736535787137766d74396c6b6435374000"
    23  )
    24  
    25  func init() {
    26  	testTx1 = bchain.Tx{
    27  		Hex:       "01000000014a9d1fdba915e0907ab02f04f88898863112a2b4fdcf872c7414588c47c874cb000000006a47304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a7430236876101210331693756f749180aeed0a65a0fab0625a2250bd9abca502282a4cf0723152e67ffffffff01a0330300000000001976a914fe40329c95c5598ac60752a5310b320cb52d18e688ac00000000",
    28  		Blocktime: 1531052031,
    29  		Time:      1531052031,
    30  		Txid:      "f56521b17b828897f72b30dd21b0192fd942342e89acbb06abf1d446282c30f5",
    31  		LockTime:  0,
    32  		Vin: []bchain.Vin{
    33  			{
    34  				ScriptSig: bchain.ScriptSig{
    35  					Hex: "47304402201fb96d20d0778f54520ab59afe70d5fb20e500ecc9f02281cf57934e8029e8e10220383d5a3e80f2e1eb92765b6da0f23d454aecbd8236f083d483e9a7430236876101210331693756f749180aeed0a65a0fab0625a2250bd9abca502282a4cf0723152e67",
    36  				},
    37  				Txid:     "cb74c8478c5814742c87cffdb4a21231869888f8042fb07a90e015a9db1f9d4a",
    38  				Vout:     0,
    39  				Sequence: 4294967295,
    40  			},
    41  		},
    42  		Vout: []bchain.Vout{
    43  			{
    44  				ValueSat: *big.NewInt(209824),
    45  				N:        0,
    46  				ScriptPubKey: bchain.ScriptPubKey{
    47  					Hex: "76a914fe40329c95c5598ac60752a5310b320cb52d18e688ac",
    48  					Addresses: []string{
    49  						"FtM4zAn9aVYgHgxmamWBgWPyZsb6RhvkA9",
    50  					},
    51  				},
    52  			},
    53  		},
    54  	}
    55  
    56  	testTx2 = bchain.Tx{
    57  		Hex:       "01000000000101d1613f483f2086d076c82fe34674385a86beb08f052d5405fe1aed397f852f4f0000000000feffffff02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987a8386f0000000000160014cc8067093f6f843d6d3e22004a4290cd0c0f336b02483045022100ea8780bc1e60e14e945a80654a41748bbf1aa7d6f2e40a88d91dfc2de1f34bd10220181a474a3420444bd188501d8d270736e1e9fe379da9970de992ff445b0972e3012103adc58245cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f862d9ed0900",
    58  		Blocktime: 1530991050,
    59  		Time:      1530991050,
    60  		Txid:      "9b5c4859a8a31e69788cb4402812bb28f14ad71cbd8c60b09903478bc56f79a3",
    61  		LockTime:  650713,
    62  		Vin: []bchain.Vin{
    63  			{
    64  				ScriptSig: bchain.ScriptSig{
    65  					Hex: "",
    66  				},
    67  				Txid:     "4f2f857f39ed1afe05542d058fb0be865a387446e32fc876d086203f483f61d1",
    68  				Vout:     0,
    69  				Sequence: 4294967294,
    70  			},
    71  		},
    72  		Vout: []bchain.Vout{
    73  			{
    74  				ValueSat: *big.NewInt(5000000),
    75  				N:        0,
    76  				ScriptPubKey: bchain.ScriptPubKey{
    77  					Hex: "a9147a55d61848e77ca266e79a39bfc85c580a6426c987",
    78  					Addresses: []string{
    79  						"2N4Q5FhU2497BryFfUgbqkAJE87aKDv3V3e",
    80  					},
    81  				},
    82  			},
    83  			{
    84  				ValueSat: *big.NewInt(7289000),
    85  				N:        1,
    86  				ScriptPubKey: bchain.ScriptPubKey{
    87  					Hex: "0014cc8067093f6f843d6d3e22004a4290cd0c0f336b",
    88  					Addresses: []string{
    89  						"tgrs1qejqxwzfld7zr6mf7ygqy5s5se5xq7vmt9lkd57",
    90  					},
    91  				},
    92  			},
    93  		},
    94  	}
    95  }
    96  
    97  func TestMain(m *testing.M) {
    98  	c := m.Run()
    99  	chaincfg.ResetParams()
   100  	os.Exit(c)
   101  }
   102  
   103  func TestGetAddrDesc(t *testing.T) {
   104  	type args struct {
   105  		tx     bchain.Tx
   106  		parser *GroestlcoinParser
   107  	}
   108  	tests := []struct {
   109  		name string
   110  		args args
   111  	}{
   112  		{
   113  			name: "grs-1",
   114  			args: args{
   115  				tx:     testTx1,
   116  				parser: NewGroestlcoinParser(GetChainParams("main"), &btc.Configuration{}),
   117  			},
   118  		},
   119  		{
   120  			name: "grs-2",
   121  			args: args{
   122  				tx:     testTx2,
   123  				parser: NewGroestlcoinParser(GetChainParams("test"), &btc.Configuration{}),
   124  			},
   125  		},
   126  	}
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129  			for n, vout := range tt.args.tx.Vout {
   130  				got1, err := tt.args.parser.GetAddrDescFromVout(&vout)
   131  				if err != nil {
   132  					t.Errorf("getAddrDescFromVout() error = %v, vout = %d", err, n)
   133  					return
   134  				}
   135  				got2, err := tt.args.parser.GetAddrDescFromAddress(vout.ScriptPubKey.Addresses[0])
   136  				if err != nil {
   137  					t.Errorf("getAddrDescFromAddress() error = %v, vout = %d", err, n)
   138  					return
   139  				}
   140  				if !bytes.Equal(got1, got2) {
   141  					t.Errorf("Address descriptors mismatch: got1 = %v, got2 = %v", got1, got2)
   142  				}
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestPackTx(t *testing.T) {
   149  	type args struct {
   150  		tx        bchain.Tx
   151  		height    uint32
   152  		blockTime int64
   153  		parser    *GroestlcoinParser
   154  	}
   155  	tests := []struct {
   156  		name    string
   157  		args    args
   158  		want    string
   159  		wantErr bool
   160  	}{
   161  		{
   162  			name: "grs-1",
   163  			args: args{
   164  				tx:        testTx1,
   165  				height:    2161062,
   166  				blockTime: 1531052031,
   167  				parser:    NewGroestlcoinParser(GetChainParams("main"), &btc.Configuration{}),
   168  			},
   169  			want:    testTxPacked1,
   170  			wantErr: false,
   171  		},
   172  		{
   173  			name: "grs-2",
   174  			args: args{
   175  				tx:        testTx2,
   176  				height:    650714,
   177  				blockTime: 1530991050,
   178  				parser:    NewGroestlcoinParser(GetChainParams("test"), &btc.Configuration{}),
   179  			},
   180  			want:    testTxPacked2,
   181  			wantErr: false,
   182  		},
   183  	}
   184  	for _, tt := range tests {
   185  		t.Run(tt.name, func(t *testing.T) {
   186  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   187  			if (err != nil) != tt.wantErr {
   188  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   189  				return
   190  			}
   191  			h := hex.EncodeToString(got)
   192  			if !reflect.DeepEqual(h, tt.want) {
   193  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   194  			}
   195  		})
   196  	}
   197  }
   198  
   199  func TestUnpackTx(t *testing.T) {
   200  	type args struct {
   201  		packedTx string
   202  		parser   *GroestlcoinParser
   203  	}
   204  	tests := []struct {
   205  		name    string
   206  		args    args
   207  		want    *bchain.Tx
   208  		want1   uint32
   209  		wantErr bool
   210  	}{
   211  		{
   212  			name: "grs-1",
   213  			args: args{
   214  				packedTx: testTxPacked1,
   215  				parser:   NewGroestlcoinParser(GetChainParams("main"), &btc.Configuration{}),
   216  			},
   217  			want:    &testTx1,
   218  			want1:   2161062,
   219  			wantErr: false,
   220  		},
   221  		{
   222  			name: "grs-2",
   223  			args: args{
   224  				packedTx: testTxPacked2,
   225  				parser:   NewGroestlcoinParser(GetChainParams("test"), &btc.Configuration{}),
   226  			},
   227  			want:    &testTx2,
   228  			want1:   650714,
   229  			wantErr: false,
   230  		},
   231  	}
   232  	for _, tt := range tests {
   233  		t.Run(tt.name, func(t *testing.T) {
   234  			b, _ := hex.DecodeString(tt.args.packedTx)
   235  			got, got1, err := tt.args.parser.UnpackTx(b)
   236  			if (err != nil) != tt.wantErr {
   237  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   238  				return
   239  			}
   240  			if !reflect.DeepEqual(got, tt.want) {
   241  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   242  			}
   243  			if got1 != tt.want1 {
   244  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   245  			}
   246  		})
   247  	}
   248  }