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

     1  // +build unittest
     2  
     3  package omotenashicoin
     4  
     5  import (
     6  	"blockbook/bchain"
     7  	"blockbook/bchain/coins/btc"
     8  	"bytes"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"math/big"
    13  	"os"
    14  	"path/filepath"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/martinboehm/btcutil/chaincfg"
    19  )
    20  
    21  func TestMain(m *testing.M) {
    22  	c := m.Run()
    23  	chaincfg.ResetParams()
    24  	os.Exit(c)
    25  }
    26  
    27  // Test getting the address details from the address hash
    28  
    29  func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) {
    30  	type args struct {
    31  		address string
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		args    args
    36  		want    string
    37  		wantErr bool
    38  	}{
    39  		{
    40  			name:    "P2PKH1",
    41  			args:    args{address: "SSpnQKkfgwKhdsp7FDm3BziRFKfX1zbmWL"},
    42  			want:    "76a9143caafb26e97bc26f27543a523f7e2ab8b841d76988ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "8Hkth4xWKCemL8u88urWXwSqUV4WSX5FDm"},
    48  			want:    "a9141d521dcf4983772b3c1e6ef937103ebdfaa1ad7787",
    49  			wantErr: false,
    50  		},
    51  	}
    52  	parser := NewOmotenashiCoinParser(GetChainParams("main"), &btc.Configuration{})
    53  
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  			h := hex.EncodeToString(got)
    62  			if !reflect.DeepEqual(h, tt.want) {
    63  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func Test_GetAddrDescFromAddress_Testnet(t *testing.T) {
    70  	type args struct {
    71  		address string
    72  	}
    73  	tests := []struct {
    74  		name    string
    75  		args    args
    76  		want    string
    77  		wantErr bool
    78  	}{
    79  		{
    80  			name:    "P2PKH1",
    81  			args:    args{address: "aRimakbKh2CiG4PSTYcnFGbVzcCZFQr2kd"},
    82  			want:    "76a914124a09dbceec6cf2060d72959ba7009649a7783088ac",
    83  			wantErr: false,
    84  		},
    85  		{
    86  			name:    "P2SH1",
    87  			args:    args{address: "8GkZahDbYJ5m6tKosGy4j7uN5pTF21ktHU"},
    88  			want:    "a914124a09dbceec6cf2060d72959ba7009649a7783087",
    89  			wantErr: false,
    90  		},
    91  	}
    92  	parser := NewOmotenashiCoinParser(GetChainParams("test"), &btc.Configuration{})
    93  
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    97  			if (err != nil) != tt.wantErr {
    98  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    99  				return
   100  			}
   101  			h := hex.EncodeToString(got)
   102  			if !reflect.DeepEqual(h, tt.want) {
   103  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func Test_GetAddressesFromAddrDesc(t *testing.T) {
   110  	type args struct {
   111  		script string
   112  	}
   113  	tests := []struct {
   114  		name    string
   115  		args    args
   116  		want    []string
   117  		want2   bool
   118  		wantErr bool
   119  	}{
   120  		{
   121  			name:    "Normal",
   122  			args:    args{script: "76a914f1684a035088c20e76ece8e4dd79bdead0e1569a88ac"},
   123  			want:    []string{"SjJSpCzWBYacyj6wGmaFoPoj79QSQq8wcw"},
   124  			want2:   true,
   125  			wantErr: false,
   126  		},
   127  	}
   128  
   129  	parser := NewOmotenashiCoinParser(GetChainParams("main"), &btc.Configuration{})
   130  
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			b, _ := hex.DecodeString(tt.args.script)
   134  			got, got2, err := parser.GetAddressesFromAddrDesc(b)
   135  			if (err != nil) != tt.wantErr {
   136  				t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr)
   137  				return
   138  			}
   139  			if !reflect.DeepEqual(got, tt.want) {
   140  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
   141  			}
   142  			if !reflect.DeepEqual(got2, tt.want2) {
   143  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  // Test the packing and unpacking of raw transaction data
   150  
   151  var (
   152  	// Block Height 600
   153  	testTx1                bchain.Tx
   154  	testTxPacked_testnet_1 = "0a2054af08185cf5c5d312ebd9865b4b224c6120801b209343cfb9dc3332af28a2a5126401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff050258020101ffffffff0100e87648170000002321024a9c0d55966c7a46d8ac15830c6c26555a2b570a3e78c51534ccc8dadc7943c8ac000000001894e38ff105200028d80432140a0a30323538303230313031180028ffffffff0f3a520a05174876e80010001a2321024a9c0d55966c7a46d8ac15830c6c26555a2b570a3e78c51534ccc8dadc7943c8ac2222616e667766545642725934795a4e54543167625a61584e664854377951544856674d4000"
   155  
   156  	// Block Height 135001
   157  	testTx2                bchain.Tx
   158  	testTxPacked_mainnet_1 = "0a20a2eedb3990bddcace3a5211332e86f70d0195a2a7efaad2de18698172ff9fc6d128f0102000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1803590f020445b8075e088104e0b22c0000007969696d70000000000002807c814a000000001976a91487bac515ab40891b58a05c913f908194c9d73bd588ac807584df000000001976a914a1441e207bd13f80b2142026ad39a58b5f47434d88ac0000000018c4f09ef005200028d99e0832360a30303335393066303230343435623830373565303838313034653062323263303030303030373936393639366437303030180028003a470a044a817c8010001a1976a91487bac515ab40891b58a05c913f908194c9d73bd588ac2222535a66667a6a666454486f7a394675684a5444453847704378455762544c433654743a470a04df84758010001a1976a914a1441e207bd13f80b2142026ad39a58b5f47434d88ac222253627a685264475855475245556b70557a67716877615847666f4244447565366b364000"
   159  )
   160  
   161  func init() {
   162  	testTx1 = bchain.Tx{
   163  		Hex:      "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff050258020101ffffffff0100e87648170000002321024a9c0d55966c7a46d8ac15830c6c26555a2b570a3e78c51534ccc8dadc7943c8ac00000000",
   164  		Txid:     "54af08185cf5c5d312ebd9865b4b224c6120801b209343cfb9dc3332af28a2a5",
   165  		LockTime: 0,
   166  		Vin: []bchain.Vin{
   167  			{
   168  				Coinbase: "0258020101",
   169  				Sequence: 4294967295,
   170  			},
   171  		},
   172  		Vout: []bchain.Vout{
   173  			{
   174  				ValueSat: *big.NewInt(100000000000),
   175  				N:        0,
   176  				ScriptPubKey: bchain.ScriptPubKey{
   177  					Hex: "21024a9c0d55966c7a46d8ac15830c6c26555a2b570a3e78c51534ccc8dadc7943c8ac",
   178  					Addresses: []string{
   179  						"anfwfTVBrY4yZNTT1gbZaXNfHT7yQTHVgM",
   180  					},
   181  				},
   182  			},
   183  		},
   184  		Blocktime: 1579413908,
   185  		Time:      1579413908,
   186  	}
   187  
   188  	//135001
   189  	testTx2 = bchain.Tx{
   190  		Hex:      "02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1803590f020445b8075e088104e0b22c0000007969696d70000000000002807c814a000000001976a91487bac515ab40891b58a05c913f908194c9d73bd588ac807584df000000001976a914a1441e207bd13f80b2142026ad39a58b5f47434d88ac00000000",
   191  		Txid:     "a2eedb3990bddcace3a5211332e86f70d0195a2a7efaad2de18698172ff9fc6d",
   192  		LockTime: 0,
   193  		Vin: []bchain.Vin{
   194  			{
   195  				Coinbase: "03590f020445b8075e088104e0b22c0000007969696d7000",
   196  				Sequence: 0,
   197  			},
   198  		},
   199  		Vout: []bchain.Vout{
   200  			{
   201  				ValueSat: *big.NewInt(1250000000),
   202  				N:        0,
   203  				ScriptPubKey: bchain.ScriptPubKey{
   204  					Hex: "76a91487bac515ab40891b58a05c913f908194c9d73bd588ac",
   205  					Addresses: []string{
   206  						"SZffzjfdTHoz9FuhJTDE8GpCxEWbTLC6Tt",
   207  					},
   208  				},
   209  			},
   210  			{
   211  				ValueSat: *big.NewInt(3750000000),
   212  				N:        0,
   213  				ScriptPubKey: bchain.ScriptPubKey{
   214  					Hex: "76a914a1441e207bd13f80b2142026ad39a58b5f47434d88ac",
   215  					Addresses: []string{
   216  						"SbzhRdGXUGREUkpUzgqhwaXGfoBDDue6k6",
   217  					},
   218  				},
   219  			},
   220  		},
   221  		Blocktime: 1577564228,
   222  		Time:      1577564228,
   223  	}
   224  }
   225  
   226  func Test_PackTx(t *testing.T) {
   227  	type args struct {
   228  		tx        bchain.Tx
   229  		height    uint32
   230  		blockTime int64
   231  		parser    *OmotenashiCoinParser
   232  	}
   233  	tests := []struct {
   234  		name    string
   235  		args    args
   236  		want    string
   237  		wantErr bool
   238  	}{
   239  		{
   240  			name: "mtnstestnet_1",
   241  			args: args{
   242  				tx:        testTx1,
   243  				height:    600,
   244  				blockTime: 1579413908,
   245  				parser:    NewOmotenashiCoinParser(GetChainParams("test"), &btc.Configuration{}),
   246  			},
   247  			want:    testTxPacked_testnet_1,
   248  			wantErr: false,
   249  		},
   250  		{
   251  			name: "mtnsmainnet_1",
   252  			args: args{
   253  				tx:        testTx2,
   254  				height:    135001,
   255  				blockTime: 1577564228,
   256  				parser:    NewOmotenashiCoinParser(GetChainParams("main"), &btc.Configuration{}),
   257  			},
   258  			want:    testTxPacked_mainnet_1,
   259  			wantErr: false,
   260  		},
   261  	}
   262  	for _, tt := range tests {
   263  		t.Run(tt.name, func(t *testing.T) {
   264  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   265  			if (err != nil) != tt.wantErr {
   266  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   267  				return
   268  			}
   269  			h := hex.EncodeToString(got)
   270  			if !reflect.DeepEqual(h, tt.want) {
   271  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   272  			}
   273  		})
   274  	}
   275  }
   276  
   277  func Test_UnpackTx(t *testing.T) {
   278  	type args struct {
   279  		packedTx string
   280  		parser   *OmotenashiCoinParser
   281  	}
   282  	tests := []struct {
   283  		name    string
   284  		args    args
   285  		want    *bchain.Tx
   286  		want1   uint32
   287  		wantErr bool
   288  	}{
   289  		{
   290  			name: "mtns_1",
   291  			args: args{
   292  				packedTx: testTxPacked_testnet_1,
   293  				parser:   NewOmotenashiCoinParser(GetChainParams("test"), &btc.Configuration{}),
   294  			},
   295  			want:    &testTx1,
   296  			want1:   600,
   297  			wantErr: false,
   298  		},
   299  		{
   300  			name: "mtns_2",
   301  			args: args{
   302  				packedTx: testTxPacked_mainnet_1,
   303  				parser:   NewOmotenashiCoinParser(GetChainParams("main"), &btc.Configuration{}),
   304  			},
   305  			want:    &testTx2,
   306  			want1:   135001,
   307  			wantErr: false,
   308  		},
   309  	}
   310  	for _, tt := range tests {
   311  		t.Run(tt.name, func(t *testing.T) {
   312  			b, _ := hex.DecodeString(tt.args.packedTx)
   313  			got, got1, err := tt.args.parser.UnpackTx(b)
   314  			if (err != nil) != tt.wantErr {
   315  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   316  				return
   317  			}
   318  			if !reflect.DeepEqual(got, tt.want) {
   319  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   320  			}
   321  			if got1 != tt.want1 {
   322  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   323  			}
   324  		})
   325  	}
   326  }
   327  
   328  // Block test - looks for size, time, and transaction hashes
   329  
   330  type testBlock struct {
   331  	size int
   332  	time int64
   333  	tx   []string
   334  }
   335  
   336  var testParseBlockTxs = map[int]testBlock{
   337  	600: {
   338  		size: 181,
   339  		time: 1579413908,
   340  		tx: []string{
   341  			"54af08185cf5c5d312ebd9865b4b224c6120801b209343cfb9dc3332af28a2a5",
   342  		},
   343  	},
   344  	135001: {
   345  		size: 224,
   346  		time: 1577564228,
   347  		tx: []string{
   348  			"a2eedb3990bddcace3a5211332e86f70d0195a2a7efaad2de18698172ff9fc6d",
   349  		},
   350  	},
   351  }
   352  
   353  func helperLoadBlock(t *testing.T, height int) []byte {
   354  	name := fmt.Sprintf("block_dump.%d", height)
   355  	path := filepath.Join("testdata", name)
   356  
   357  	d, err := ioutil.ReadFile(path)
   358  	if err != nil {
   359  		t.Fatal(err)
   360  	}
   361  
   362  	d = bytes.TrimSpace(d)
   363  
   364  	b := make([]byte, hex.DecodedLen(len(d)))
   365  	_, err = hex.Decode(b, d)
   366  	if err != nil {
   367  		t.Fatal(err)
   368  	}
   369  
   370  	return b
   371  }
   372  
   373  func TestParseBlock(t *testing.T) {
   374  	p := NewOmotenashiCoinParser(GetChainParams("main"), &btc.Configuration{})
   375  
   376  	for height, tb := range testParseBlockTxs {
   377  		b := helperLoadBlock(t, height)
   378  
   379  		blk, err := p.ParseBlock(b)
   380  		if err != nil {
   381  			t.Fatal(err)
   382  		}
   383  
   384  		if blk.Size != tb.size {
   385  			t.Errorf("ParseBlock() block size: got %d, want %d", blk.Size, tb.size)
   386  		}
   387  
   388  		if blk.Time != tb.time {
   389  			t.Errorf("ParseBlock() block time: got %d, want %d", blk.Time, tb.time)
   390  		}
   391  
   392  		if len(blk.Txs) != len(tb.tx) {
   393  			t.Errorf("ParseBlock() number of transactions: got %d, want %d", len(blk.Txs), len(tb.tx))
   394  		}
   395  
   396  		for ti, tx := range tb.tx {
   397  			if blk.Txs[ti].Txid != tx {
   398  				t.Errorf("ParseBlock() transaction %d: got %s, want %s", ti, blk.Txs[ti].Txid, tx)
   399  			}
   400  		}
   401  	}
   402  }