github.com/bchainhub/blockbook@v0.3.2/bchain/coins/unobtanium/unobtaniumparser_test.go (about)

     1  // +build unittest
     2  
     3  package unobtanium
     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: "uNv31DrZT8DMGK4eV3FmvvxteCW2h26xXK"},
    36  			want:    "76a9142b9db958ed02331e9fe78eff8c33cc9b276e40f188ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "ued4doshafG2qqzGZ5T7RBEm34sdMVm46e"},
    42  			want:    "76a914d7ea06ca9357862a9d5855cc54ceb093e69a4bc088ac",
    43  			wantErr: false,
    44  		},
    45  	}
    46  	parser := NewUnobtaniumParser(GetChainParams("main"), &btc.Configuration{})
    47  
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    51  			if (err != nil) != tt.wantErr {
    52  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    53  				return
    54  			}
    55  			h := hex.EncodeToString(got)
    56  			if !reflect.DeepEqual(h, tt.want) {
    57  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  var (
    64  	testTx1       bchain.Tx
    65  	testTxPacked1 = "0004bd8d8ab4b387540100000002128690c8080ff5b648ad4e38e391f28843cb92c55fe1611a952ea3c9b8eafb2f000000006a47304402200df61ba8dcc1b7228f50eb40346ad237a1d5fae9445a4251f3acfce5728148c402202c43452804855fc08f42fe7db612d9f622a5ee46044279a7a9f7f7c18154a1a1012102248a489fb192cdf37124e15a898f08794a42c5ae2cab3de09a6f92073ae6c904ffffffffe0e35a761650d32866b56d5b4417acd5c6be9d27517be55682da1c4fe83fc6e2010000006c493046022100c40143c5fb6051986921b42565dd20f3201ff1a5365bf0d4110ab0f33c38b22d022100e851372081f381a3edee1b74b54dc6a7806fee6b152a2be855d6bc39974b461a0121028f6f386e276c56ea7ff8894c417a20e82c488ac51400fe51f41c2a243e2de736ffffffff0245420f00000000001976a914d7ea06ca9357862a9d5855cc54ceb093e69a4bc088acb86ef808000000001976a914f943a1cc03a501143ad57fd82394c2952b99e47888ac00000000"
    66  )
    67  
    68  func init() {
    69  	testTx1 = bchain.Tx{
    70  		Hex:       "0100000002128690c8080ff5b648ad4e38e391f28843cb92c55fe1611a952ea3c9b8eafb2f000000006a47304402200df61ba8dcc1b7228f50eb40346ad237a1d5fae9445a4251f3acfce5728148c402202c43452804855fc08f42fe7db612d9f622a5ee46044279a7a9f7f7c18154a1a1012102248a489fb192cdf37124e15a898f08794a42c5ae2cab3de09a6f92073ae6c904ffffffffe0e35a761650d32866b56d5b4417acd5c6be9d27517be55682da1c4fe83fc6e2010000006c493046022100c40143c5fb6051986921b42565dd20f3201ff1a5365bf0d4110ab0f33c38b22d022100e851372081f381a3edee1b74b54dc6a7806fee6b152a2be855d6bc39974b461a0121028f6f386e276c56ea7ff8894c417a20e82c488ac51400fe51f41c2a243e2de736ffffffff0245420f00000000001976a914d7ea06ca9357862a9d5855cc54ceb093e69a4bc088acb86ef808000000001976a914f943a1cc03a501143ad57fd82394c2952b99e47888ac00000000",
    71  		Blocktime: 1397121514,
    72  		Txid:      "9888815899d3b2e0f26b1eab51229082cf1faf4cd03a12fea2c8afa66701541f",
    73  		LockTime:  0,
    74  		Version:   1,
    75  		Vin: []bchain.Vin{
    76  			{
    77  				ScriptSig: bchain.ScriptSig{
    78  					Hex: "47304402200df61ba8dcc1b7228f50eb40346ad237a1d5fae9445a4251f3acfce5728148c402202c43452804855fc08f42fe7db612d9f622a5ee46044279a7a9f7f7c18154a1a1012102248a489fb192cdf37124e15a898f08794a42c5ae2cab3de09a6f92073ae6c904",
    79  				},
    80  				Txid:     "2ffbeab8c9a32e951a61e15fc592cb4388f291e3384ead48b6f50f08c8908612",
    81  				Vout:     0,
    82  				Sequence: 4294967295,
    83  			},
    84  			{
    85  				ScriptSig: bchain.ScriptSig{
    86  					Hex: "493046022100c40143c5fb6051986921b42565dd20f3201ff1a5365bf0d4110ab0f33c38b22d022100e851372081f381a3edee1b74b54dc6a7806fee6b152a2be855d6bc39974b461a0121028f6f386e276c56ea7ff8894c417a20e82c488ac51400fe51f41c2a243e2de736",
    87  				},
    88  				Txid:     "e2c63fe84f1cda8256e57b51279dbec6d5ac17445b6db56628d35016765ae3e0",
    89  				Vout:     1,
    90  				Sequence: 4294967295,
    91  			},
    92  		},
    93  		Vout: []bchain.Vout{
    94  			{
    95  				ValueSat: *big.NewInt(1000005),
    96  				N:        0,
    97  				ScriptPubKey: bchain.ScriptPubKey{
    98  					Hex: "76a914d7ea06ca9357862a9d5855cc54ceb093e69a4bc088ac",
    99  					Addresses: []string{
   100  						"ued4doshafG2qqzGZ5T7RBEm34sdMVm46e",
   101  					},
   102  				},
   103  			},
   104  			{
   105  				ValueSat: *big.NewInt(150499000),
   106  				N:        1,
   107  				ScriptPubKey: bchain.ScriptPubKey{
   108  					Hex: "76a914f943a1cc03a501143ad57fd82394c2952b99e47888ac",
   109  					Addresses: []string{
   110  						"uhfQH3AD3huadZuzHTB7TWHoWXbJpJhS6B",
   111  					},
   112  				},
   113  			},
   114  		},
   115  	}
   116  }
   117  
   118  func Test_PackTx(t *testing.T) {
   119  	type args struct {
   120  		tx        bchain.Tx
   121  		height    uint32
   122  		blockTime int64
   123  		parser    *UnobtaniumParser
   124  	}
   125  	tests := []struct {
   126  		name    string
   127  		args    args
   128  		want    string
   129  		wantErr bool
   130  	}{
   131  		{
   132  			name: "Unobtanium-1",
   133  			args: args{
   134  				tx:        testTx1,
   135  				height:    310669,
   136  				blockTime: 1397121514,
   137  				parser:    NewUnobtaniumParser(GetChainParams("main"), &btc.Configuration{}),
   138  			},
   139  			want:    testTxPacked1,
   140  			wantErr: false,
   141  		},
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   146  			if (err != nil) != tt.wantErr {
   147  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   148  				return
   149  			}
   150  			h := hex.EncodeToString(got)
   151  			if !reflect.DeepEqual(h, tt.want) {
   152  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   153  			}
   154  		})
   155  	}
   156  }
   157  
   158  func Test_UnpackTx(t *testing.T) {
   159  	type args struct {
   160  		packedTx string
   161  		parser   *UnobtaniumParser
   162  	}
   163  	tests := []struct {
   164  		name    string
   165  		args    args
   166  		want    *bchain.Tx
   167  		want1   uint32
   168  		wantErr bool
   169  	}{
   170  		{
   171  			name: "Unobtanium-1",
   172  			args: args{
   173  				packedTx: testTxPacked1,
   174  				parser:   NewUnobtaniumParser(GetChainParams("main"), &btc.Configuration{}),
   175  			},
   176  			want:    &testTx1,
   177  			want1:   310669,
   178  			wantErr: false,
   179  		},
   180  	}
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			b, _ := hex.DecodeString(tt.args.packedTx)
   184  			got, got1, err := tt.args.parser.UnpackTx(b)
   185  			if (err != nil) != tt.wantErr {
   186  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   187  				return
   188  			}
   189  			if !reflect.DeepEqual(got, tt.want) {
   190  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   191  			}
   192  			if got1 != tt.want1 {
   193  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   194  			}
   195  		})
   196  	}
   197  }