github.com/bchainhub/blockbook@v0.3.2/bchain/coins/divi/diviparser_test.go (about)

     1  // +build unittest
     2  
     3  package divi
     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: "DDSsBchWiVfvPVn6Ldp1nL7k4L77cSDqM7"},
    42  			want:    "76a9145b1d583a4c270f2f14be77b298f0a9c6df97471388ac",
    43  			wantErr: false,
    44  		},
    45  	}
    46  	parser := NewDiviParser(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  func Test_GetAddressesFromAddrDesc(t *testing.T) {
    64  	type args struct {
    65  		script string
    66  	}
    67  	tests := []struct {
    68  		name    string
    69  		args    args
    70  		want    []string
    71  		want2   bool
    72  		wantErr bool
    73  	}{
    74  		{
    75  			name:    "Normal",
    76  			args:    args{script: "76a914cb1196fb1b98d04b0cb8d2ffde3c2de3eb83d9fe88ac"},
    77  			want:    []string{"DPepnMkaNHKCa6cQi7oBThrdiFEwSSYFzv"},
    78  			want2:   true,
    79  			wantErr: false,
    80  		},
    81  	}
    82  
    83  	parser := NewDiviParser(GetChainParams("main"), &btc.Configuration{})
    84  
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			b, _ := hex.DecodeString(tt.args.script)
    88  			got, got2, err := parser.GetAddressesFromAddrDesc(b)
    89  			if (err != nil) != tt.wantErr {
    90  				t.Errorf("GetAddressesFromAddrDesc() error = %v, wantErr %v", err, tt.wantErr)
    91  				return
    92  			}
    93  			if !reflect.DeepEqual(got, tt.want) {
    94  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got, tt.want)
    95  			}
    96  			if !reflect.DeepEqual(got2, tt.want2) {
    97  				t.Errorf("GetAddressesFromAddrDesc() = %v, want %v", got2, tt.want2)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  // Test the packing and unpacking of raw transaction data
   104  
   105  var (
   106  	// Mint transaction
   107  	testTx1       bchain.Tx
   108  	testTxPacked1 = "0a20f7a5324866ba18058ab032196f34458d19f7ec5a4ac284670c3ef07bfa724644124201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0603de3d060101ffffffff010000000000000000000000000018aefd9ce905200028defb1832160a0c303364653364303630313031180028ffffffff0f3a0210004000"
   109  
   110  	// Normal transaction
   111  	testTx2       bchain.Tx
   112  	testTxPacked2 = "0a20eace41778a2940ff423b72a42033990eb5d6092810734a5806da6f3e5b34086412ea010100000001084b029489e1cddf726080c447c8a2b1d4bbe43024db31b8b19bc07585db9555010000006a473044022017422b9e3414d6233fa75f9eb7778469bebbb40686b0f7eb77d90a04c80149610220411f1063086fe205ea821ceb0de89e8158e202aba00f5ebb92b51f97381311fd012102ccb10a2f0603a0624b8708abefb5f4700631fc131c5de38b51e0359e2ffa7d1cffffffff03000000000000000000f260de1a580100001976a9145b1d583a4c270f2f14be77b298f0a9c6df97471388ac009ca6920c0000001976a914cb1196fb1b98d04b0cb8d2ffde3c2de3eb83d9fe88ac0000000018aefd9ce905200028defb183298010a0012205595db8575c09bb1b831db2430e4bbd4b1a2c847c4806072dfcde18994024b081801226a473044022017422b9e3414d6233fa75f9eb7778469bebbb40686b0f7eb77d90a04c80149610220411f1063086fe205ea821ceb0de89e8158e202aba00f5ebb92b51f97381311fd012102ccb10a2f0603a0624b8708abefb5f4700631fc131c5de38b51e0359e2ffa7d1c28ffffffff0f3a0210003a490a0601581ade60f210011a1976a9145b1d583a4c270f2f14be77b298f0a9c6df97471388ac222244445373426368576956667650566e364c6470316e4c376b344c3737635344714d373a480a050c92a69c0010021a1976a914cb1196fb1b98d04b0cb8d2ffde3c2de3eb83d9fe88ac2222445065706e4d6b614e484b436136635169376f425468726469464577535359467a764000"
   113  )
   114  
   115  func init() {
   116  	testTx1 = bchain.Tx{
   117  		Hex:      "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0603de3d060101ffffffff0100000000000000000000000000",
   118  		Txid:     "f7a5324866ba18058ab032196f34458d19f7ec5a4ac284670c3ef07bfa724644",
   119  		LockTime: 0,
   120  		Vin: []bchain.Vin{
   121  			{
   122  				Coinbase: "03de3d060101",
   123  				Sequence: 4294967295,
   124  			},
   125  		},
   126  		Vout: []bchain.Vout{
   127  			{
   128  				ValueSat: *big.NewInt(0),
   129  				N:        0,
   130  				ScriptPubKey: bchain.ScriptPubKey{
   131  					Hex: "",
   132  				},
   133  			},
   134  		},
   135  		Blocktime: 1562853038,
   136  		Time:      1562853038,
   137  	}
   138  
   139  	testTx2 = bchain.Tx{
   140  		Hex:      "0100000001084b029489e1cddf726080c447c8a2b1d4bbe43024db31b8b19bc07585db9555010000006a473044022017422b9e3414d6233fa75f9eb7778469bebbb40686b0f7eb77d90a04c80149610220411f1063086fe205ea821ceb0de89e8158e202aba00f5ebb92b51f97381311fd012102ccb10a2f0603a0624b8708abefb5f4700631fc131c5de38b51e0359e2ffa7d1cffffffff03000000000000000000f260de1a580100001976a9145b1d583a4c270f2f14be77b298f0a9c6df97471388ac009ca6920c0000001976a914cb1196fb1b98d04b0cb8d2ffde3c2de3eb83d9fe88ac00000000",
   141  		Txid:     "eace41778a2940ff423b72a42033990eb5d6092810734a5806da6f3e5b340864",
   142  		LockTime: 0,
   143  		Vin: []bchain.Vin{
   144  			{
   145  				ScriptSig: bchain.ScriptSig{
   146  					Hex: "473044022017422b9e3414d6233fa75f9eb7778469bebbb40686b0f7eb77d90a04c80149610220411f1063086fe205ea821ceb0de89e8158e202aba00f5ebb92b51f97381311fd012102ccb10a2f0603a0624b8708abefb5f4700631fc131c5de38b51e0359e2ffa7d1c",
   147  				},
   148  				Txid:     "5595db8575c09bb1b831db2430e4bbd4b1a2c847c4806072dfcde18994024b08",
   149  				Vout:     1,
   150  				Sequence: 4294967295,
   151  			},
   152  		},
   153  		Vout: []bchain.Vout{
   154  			{
   155  				ValueSat: *big.NewInt(0),
   156  				N:        0,
   157  				ScriptPubKey: bchain.ScriptPubKey{
   158  					Hex: "",
   159  				},
   160  			},
   161  			{
   162  				ValueSat: *big.NewInt(1477919531250),
   163  				N:        1,
   164  				ScriptPubKey: bchain.ScriptPubKey{
   165  					Hex: "76a9145b1d583a4c270f2f14be77b298f0a9c6df97471388ac",
   166  					Addresses: []string{
   167  						"DDSsBchWiVfvPVn6Ldp1nL7k4L77cSDqM7",
   168  					},
   169  				},
   170  			},
   171  			{
   172  				ValueSat: *big.NewInt(54000000000),
   173  				N:        2,
   174  				ScriptPubKey: bchain.ScriptPubKey{
   175  					Hex: "76a914cb1196fb1b98d04b0cb8d2ffde3c2de3eb83d9fe88ac",
   176  					Addresses: []string{
   177  						"DPepnMkaNHKCa6cQi7oBThrdiFEwSSYFzv",
   178  					},
   179  				},
   180  			},
   181  		},
   182  		Blocktime: 1562853038,
   183  		Time:      1562853038,
   184  	}
   185  }
   186  
   187  func Test_PackTx(t *testing.T) {
   188  	type args struct {
   189  		tx        bchain.Tx
   190  		height    uint32
   191  		blockTime int64
   192  		parser    *DivicoinParser
   193  	}
   194  	tests := []struct {
   195  		name    string
   196  		args    args
   197  		want    string
   198  		wantErr bool
   199  	}{
   200  		{
   201  			name: "divi-1",
   202  			args: args{
   203  				tx:        testTx1,
   204  				height:    409054,
   205  				blockTime: 1562853038,
   206  				parser:    NewDiviParser(GetChainParams("main"), &btc.Configuration{}),
   207  			},
   208  			want:    testTxPacked1,
   209  			wantErr: false,
   210  		},
   211  		{
   212  			name: "divi-2",
   213  			args: args{
   214  				tx:        testTx2,
   215  				height:    409054,
   216  				blockTime: 1562853038,
   217  				parser:    NewDiviParser(GetChainParams("main"), &btc.Configuration{}),
   218  			},
   219  			want:    testTxPacked2,
   220  			wantErr: false,
   221  		},
   222  	}
   223  	for _, tt := range tests {
   224  		t.Run(tt.name, func(t *testing.T) {
   225  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   226  			if (err != nil) != tt.wantErr {
   227  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   228  				return
   229  			}
   230  			h := hex.EncodeToString(got)
   231  			if !reflect.DeepEqual(h, tt.want) {
   232  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   233  			}
   234  		})
   235  	}
   236  }
   237  
   238  func Test_UnpackTx(t *testing.T) {
   239  	type args struct {
   240  		packedTx string
   241  		parser   *DivicoinParser
   242  	}
   243  	tests := []struct {
   244  		name    string
   245  		args    args
   246  		want    *bchain.Tx
   247  		want1   uint32
   248  		wantErr bool
   249  	}{
   250  		{
   251  			name: "divi-1",
   252  			args: args{
   253  				packedTx: testTxPacked1,
   254  				parser:   NewDiviParser(GetChainParams("main"), &btc.Configuration{}),
   255  			},
   256  			want:    &testTx1,
   257  			want1:   409054,
   258  			wantErr: false,
   259  		},
   260  		{
   261  			name: "divi-2",
   262  			args: args{
   263  				packedTx: testTxPacked2,
   264  				parser:   NewDiviParser(GetChainParams("main"), &btc.Configuration{}),
   265  			},
   266  			want:    &testTx2,
   267  			want1:   409054,
   268  			wantErr: false,
   269  		},
   270  	}
   271  	for _, tt := range tests {
   272  		t.Run(tt.name, func(t *testing.T) {
   273  			b, _ := hex.DecodeString(tt.args.packedTx)
   274  			got, got1, err := tt.args.parser.UnpackTx(b)
   275  			if (err != nil) != tt.wantErr {
   276  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   277  				return
   278  			}
   279  			if !reflect.DeepEqual(got, tt.want) {
   280  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   281  			}
   282  			if got1 != tt.want1 {
   283  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   284  			}
   285  		})
   286  	}
   287  }
   288  
   289  // Block test - looks for size, time, and transaction hashes
   290  
   291  type testBlock struct {
   292  	size int
   293  	time int64
   294  	tx   []string
   295  }
   296  
   297  var testParseBlockTxs = map[int]testBlock{
   298  	407407: {
   299  		size: 479,
   300  		time: 1562753629,
   301  		tx: []string{
   302  			"3f8f01aec6717ede0e167f267fe486f18ddd25a13afd910dc1d41537aa1c6658",
   303  			"b25224449d0f5266073876e924c4d6a4f127175aae151a66db6619e4ca41fe1d",
   304  		},
   305  	},
   306  	409054: {
   307  		size: 479,
   308  		time: 1562853038,
   309  		tx: []string{
   310  			"f7a5324866ba18058ab032196f34458d19f7ec5a4ac284670c3ef07bfa724644",
   311  			"eace41778a2940ff423b72a42033990eb5d6092810734a5806da6f3e5b340864",
   312  		},
   313  	},
   314  	408074: {
   315  		size: 1303,
   316  		time: 1562794078,
   317  		tx: []string{
   318  			"bf0004680570d49eefab2ab806bd41f99587b6f3e65d1e0fb1d8e8f766f211f3",
   319  			"8a334d86443d5e54d3d112b7ab4eff79ed0b879cbc62c580beee080b3c9e1142",
   320  			"1ba350ba68b8db6af589136a85246c961694434ec2ffd1ad9c86831965b96932",
   321  			"e05dcfece505455e8b4bcaeeb9ae1060fcf9c95ad1402c4fbd3b2c2bf1778683",
   322  			"d3980118dedde2666d5bcd03ebf2c2d91ad6056404503afe0c37ed6cdd549f62",
   323  		},
   324  	},
   325  }
   326  
   327  func helperLoadBlock(t *testing.T, height int) []byte {
   328  	name := fmt.Sprintf("block_dump.%d", height)
   329  	path := filepath.Join("testdata", name)
   330  
   331  	d, err := ioutil.ReadFile(path)
   332  	if err != nil {
   333  		t.Fatal(err)
   334  	}
   335  
   336  	d = bytes.TrimSpace(d)
   337  
   338  	b := make([]byte, hex.DecodedLen(len(d)))
   339  	_, err = hex.Decode(b, d)
   340  	if err != nil {
   341  		t.Fatal(err)
   342  	}
   343  
   344  	return b
   345  }
   346  
   347  func TestParseBlock(t *testing.T) {
   348  	p := NewDiviParser(GetChainParams("main"), &btc.Configuration{})
   349  
   350  	for height, tb := range testParseBlockTxs {
   351  		b := helperLoadBlock(t, height)
   352  
   353  		blk, err := p.ParseBlock(b)
   354  		if err != nil {
   355  			t.Fatal(err)
   356  		}
   357  
   358  		if blk.Size != tb.size {
   359  			t.Errorf("ParseBlock() block size: got %d, want %d", blk.Size, tb.size)
   360  		}
   361  
   362  		if blk.Time != tb.time {
   363  			t.Errorf("ParseBlock() block time: got %d, want %d", blk.Time, tb.time)
   364  		}
   365  
   366  		if len(blk.Txs) != len(tb.tx) {
   367  			t.Errorf("ParseBlock() number of transactions: got %d, want %d", len(blk.Txs), len(tb.tx))
   368  		}
   369  
   370  		for ti, tx := range tb.tx {
   371  			if blk.Txs[ti].Txid != tx {
   372  				t.Errorf("ParseBlock() transaction %d: got %s, want %s", ti, blk.Txs[ti].Txid, tx)
   373  			}
   374  		}
   375  	}
   376  }