github.com/bchainhub/blockbook@v0.3.2/bchain/coins/viacoin/viacoinparser_test.go (about)

     1  // +build unittest
     2  
     3  package viacoin
     4  
     5  import (
     6  	"blockbook/bchain"
     7  	"blockbook/bchain/coins/btc"
     8  	"encoding/hex"
     9  	"math/big"
    10  	"os"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/martinboehm/btcutil/chaincfg"
    15  )
    16  
    17  func TestMain(m *testing.M) {
    18  	c := m.Run()
    19  	chaincfg.ResetParams()
    20  	os.Exit(c)
    21  }
    22  
    23  func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
    24  	type args struct {
    25  		address string
    26  	}
    27  	tests := []struct {
    28  		name    string
    29  		args    args
    30  		want    string
    31  		wantErr bool
    32  	}{
    33  		{
    34  			name:    "P2PKH1",
    35  			args:    args{address: "VhyGT8kJU9x28dHwjf1jEDG8gMY8yhckDR"},
    36  			want:    "76a91457757edd001d16528c7aa337b314a7bab303ee8088ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "VdMPvn7vUTSzbYjiMDs1jku9wAh1Ri2Y1A"},
    42  			want:    "76a91424cc424c1e5e977175d2b20012554d39024bd68f88ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "EUtqKT17p1LdHTDyGL1b2WPJiUFidS6gVq"},
    48  			want:    "a91480c7c8faece680bab1ae81a5969815a05b7565f087",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "P2SH2",
    53  			args:    args{address: "EMdC3QPzx2MsJG56x2QbSR727dRM73B1rK"},
    54  			want:    "a91431098c569891a8ff1fa11d1cbd3d46ca5e245c6b87",
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name:    "witness_v0_keyhash",
    59  			args:    args{address: "via1q95qlu98cpj23xy6w9tdnfn65n5vkpkey99g6wl"},
    60  			want:    "00142d01fe14f80c9513134e2adb34cf549d1960db24",
    61  			wantErr: false,
    62  		},
    63  	}
    64  	parser := NewViacoinParser(GetChainParams("main"), &btc.Configuration{})
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    69  			if (err != nil) != tt.wantErr {
    70  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    71  				return
    72  			}
    73  			h := hex.EncodeToString(got)
    74  			if !reflect.DeepEqual(h, tt.want) {
    75  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  var (
    82  	testTx1       bchain.Tx
    83  	testTxPacked1 = "004eb96d8bb3b6c6140200000001ddc431a8f5c4e74296de8a6bff796ece148b9bd6827a80ecde8671df41a51fc7000000006a47304402204f929a1e1e40bd352bbd5d3c5ae6c29740e5a8b29dd8c53a15d3eab29aecee7c02206a514e5e4561cfb9330d98f4a4fe1385af56d87eb1d3e1a379d7a50276788cfe0121034a9305644fbcb56d4fc0bc15959b917f7753ae8e581acc97f9cfe771ad1e8249feffffff0200ca9a3b000000001976a91456c7359ed52d61c1ca371d7dc136632148169c5e88acd0e8cc10000000001976a914112e29df5df4866e40ef98e0857036b275380fe088ac6ab94e00"
    84  )
    85  
    86  func init() {
    87  	testTx1 = bchain.Tx{
    88  		Hex:       "0200000001ddc431a8f5c4e74296de8a6bff796ece148b9bd6827a80ecde8671df41a51fc7000000006a47304402204f929a1e1e40bd352bbd5d3c5ae6c29740e5a8b29dd8c53a15d3eab29aecee7c02206a514e5e4561cfb9330d98f4a4fe1385af56d87eb1d3e1a379d7a50276788cfe0121034a9305644fbcb56d4fc0bc15959b917f7753ae8e581acc97f9cfe771ad1e8249feffffff0200ca9a3b000000001976a91456c7359ed52d61c1ca371d7dc136632148169c5e88acd0e8cc10000000001976a914112e29df5df4866e40ef98e0857036b275380fe088ac6ab94e00",
    89  		Blocktime: 1530319242,
    90  		Txid:      "d0284c75a389a07cc256e0bb913110d8d8059efd04daa8147ecf2fa0b3bdf6ff",
    91  		LockTime:  5159274,
    92  		Version:   2,
    93  		Vin: []bchain.Vin{
    94  			{
    95  				ScriptSig: bchain.ScriptSig{
    96  					Hex: "47304402204f929a1e1e40bd352bbd5d3c5ae6c29740e5a8b29dd8c53a15d3eab29aecee7c02206a514e5e4561cfb9330d98f4a4fe1385af56d87eb1d3e1a379d7a50276788cfe0121034a9305644fbcb56d4fc0bc15959b917f7753ae8e581acc97f9cfe771ad1e8249",
    97  				},
    98  				Txid:     "c71fa541df7186deec807a82d69b8b14ce6e79ff6b8ade9642e7c4f5a831c4dd",
    99  				Vout:     0,
   100  				Sequence: 4294967294,
   101  			},
   102  		},
   103  		Vout: []bchain.Vout{
   104  			{
   105  				ValueSat: *big.NewInt(1000000000),
   106  				N:        0,
   107  				ScriptPubKey: bchain.ScriptPubKey{
   108  					Hex: "76a91456c7359ed52d61c1ca371d7dc136632148169c5e88ac",
   109  					Addresses: []string{
   110  						"VhuffXKNA3j9hgp2JYGrj6uHQ6KUU6zNbS",
   111  					},
   112  				},
   113  			},
   114  			{
   115  				ValueSat: *big.NewInt(281864400),
   116  				N:        1,
   117  				ScriptPubKey: bchain.ScriptPubKey{
   118  					Hex: "76a914112e29df5df4866e40ef98e0857036b275380fe088ac",
   119  					Addresses: []string{
   120  						"VbZfhUMCUJHDjqjby6ynYFPZSNVYhfe4cK",
   121  					},
   122  				},
   123  			},
   124  		},
   125  	}
   126  }
   127  
   128  func Test_PackTx(t *testing.T) {
   129  	type args struct {
   130  		tx        bchain.Tx
   131  		height    uint32
   132  		blockTime int64
   133  		parser    *ViacoinParser
   134  	}
   135  	tests := []struct {
   136  		name    string
   137  		args    args
   138  		want    string
   139  		wantErr bool
   140  	}{
   141  		{
   142  			name: "viacoin-1",
   143  			args: args{
   144  				tx:        testTx1,
   145  				height:    5159277,
   146  				blockTime: 1530319242,
   147  				parser:    NewViacoinParser(GetChainParams("main"), &btc.Configuration{}),
   148  			},
   149  			want:    testTxPacked1,
   150  			wantErr: false,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   156  			if (err != nil) != tt.wantErr {
   157  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   158  				return
   159  			}
   160  			h := hex.EncodeToString(got)
   161  			if !reflect.DeepEqual(h, tt.want) {
   162  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   163  			}
   164  		})
   165  	}
   166  }
   167  
   168  func Test_UnpackTx(t *testing.T) {
   169  	type args struct {
   170  		packedTx string
   171  		parser   *ViacoinParser
   172  	}
   173  	tests := []struct {
   174  		name    string
   175  		args    args
   176  		want    *bchain.Tx
   177  		want1   uint32
   178  		wantErr bool
   179  	}{
   180  		{
   181  			name: "viacoin-1",
   182  			args: args{
   183  				packedTx: testTxPacked1,
   184  				parser:   NewViacoinParser(GetChainParams("main"), &btc.Configuration{}),
   185  			},
   186  			want:    &testTx1,
   187  			want1:   5159277,
   188  			wantErr: false,
   189  		},
   190  	}
   191  	for _, tt := range tests {
   192  		t.Run(tt.name, func(t *testing.T) {
   193  			b, _ := hex.DecodeString(tt.args.packedTx)
   194  			got, got1, err := tt.args.parser.UnpackTx(b)
   195  			if (err != nil) != tt.wantErr {
   196  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   197  				return
   198  			}
   199  			if !reflect.DeepEqual(got, tt.want) {
   200  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   201  			}
   202  			if got1 != tt.want1 {
   203  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   204  			}
   205  		})
   206  	}
   207  }