github.com/bchainhub/blockbook@v0.3.2/bchain/coins/dash/dashparser_test.go (about)

     1  // build unittest
     2  
     3  package dash
     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  	"path/filepath"
    14  	"reflect"
    15  	"testing"
    16  )
    17  
    18  type testBlock struct {
    19  	size int
    20  	time int64
    21  	txs  []string
    22  }
    23  
    24  var testParseBlockTxs = map[int]testBlock{
    25  	500001: {
    26  		size: 6521,
    27  		time: 1468043164,
    28  		txs: []string{
    29  			"6d45c761892eb07b7651140aa42901b03cb501a82585fb360ff8f155d46727b0",
    30  			"5f6cbefca8a48cccd40805013e5f6c602e0d35c3511ccb7a2ae25e51dd95d38f",
    31  			"0eb37faa8b2f24c68c8e4a3009a051caded005d5f13f0dc216ff6422256f6b7b",
    32  			"2565cfacb7f8cbc73dc1053b06ca527d9798bf6bf29a778cb5924a17dd167a39",
    33  			"85f93911e8a4d8d9bbf3de009a666ed594d62aa41a34a9e3763058067e64f084",
    34  			"46712f0f32a392c71df443798e120d0f40eb93063631a992b3dedd4d4afc04e4",
    35  			"1790efaa05caad7ab546ef479041c3cb1cb9bca7b9cc7992566d2e2344701167",
    36  		},
    37  	},
    38  	// last block without special transactions, valid for bitcoin parser
    39  	1028159: {
    40  		size: 8608,
    41  		time: 1551246608,
    42  		txs: []string{
    43  			"a800f5b2dde5d48bda08d9d6fc5647c41cec902ce690a5a2be0665e6acf77c35",
    44  			"981d6668e65b70fcd97ddd68319f3c5e5163e510cc0ed479be5667bf1782f036",
    45  			"b9fd19d37ec97d038da2ccad9414ef311275d5fd3762bdec3e76f535e2295f4c",
    46  			"1b4051d02c9919ef8d482cadf6ca2002442d9436b444923cd295fe56009ec52b",
    47  			"6f1ec8472624b8e7481024ee8b228086b9b32606790e94f161589d3fe2b3a826",
    48  			"b12c512803d733f3f7afce846e18c6a46c713533cdb18a13392cbda88866523c",
    49  			"69e6d67946ed660b440c8e457933ae594ce60acdbe17ded091ce0ac6f41ed186",
    50  			"755c3b7cad9b569def3f69f897da2ee7732ee2e0a165965512680b4fe9086e12",
    51  			"63de772ff400789c2c3ad9be653817bebf92551e139d80c8e735bb0610865500",
    52  			"1566bd9bb2413c63412a13d16c7017814363f668d8b3bfe66a5478734e73f010",
    53  			"d7f441b0abca7df6530a0620661c839244bb6f26e1b4a53b783fb3acc1f5f42a",
    54  			"ec7762d0e02a87e311b128662db8ef4161dcc9d9f2831250c7366eed98fc744b",
    55  			"b977669bfc0ac3b9ca9de7512fda564a69fe49dde8a286fcb7ea99147db54b5f",
    56  		},
    57  	},
    58  	// first block with special transactions, invalid for bitcoin parser
    59  	// 1028160: {
    60  	// 	size: 2347,
    61  	// 	time: 1551246710,
    62  	// 	txs: []string{
    63  	// 		"71d6975e3b79b52baf26c3269896a34f3bedfb04561c692ffa31f64dada1f9c4",
    64  	// 		"ed732a404cdfd4e0475a7a016200b7eef191f2c9de0ffdef8a20091c0499299c",
    65  	// 		"99d0613f82ea1f928bbc98318665adbdf5b40d206bd487fe77d542e86c903f55",
    66  	// 		"05cbf334a563468d0e378c56b43fb5254ee9c0e35ca8fab5cb242ebd825ae97b",
    67  	// 		"ee91fae7be36b3b81bc60992e904e1ae91e7dffdd5751ccaef557ba62ea80a4f",
    68  	// 	},
    69  	// },
    70  }
    71  
    72  func helperLoadBlock(t *testing.T, height int) []byte {
    73  	name := fmt.Sprintf("block_dump.%d", height)
    74  	path := filepath.Join("testdata", name)
    75  
    76  	d, err := ioutil.ReadFile(path)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	d = bytes.TrimSpace(d)
    82  
    83  	b := make([]byte, hex.DecodedLen(len(d)))
    84  	_, err = hex.Decode(b, d)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	return b
    90  }
    91  
    92  func TestParseBlock(t *testing.T) {
    93  	p := NewDashParser(GetChainParams("main"), &btc.Configuration{})
    94  
    95  	for height, tb := range testParseBlockTxs {
    96  		b := helperLoadBlock(t, height)
    97  
    98  		blk, err := p.ParseBlock(b)
    99  		if err != nil {
   100  			t.Errorf("ParseBlock() error %v", err)
   101  		}
   102  
   103  		if blk.Size != tb.size {
   104  			t.Errorf("ParseBlock() block size: got %d, want %d", blk.Size, tb.size)
   105  		}
   106  
   107  		if blk.Time != tb.time {
   108  			t.Errorf("ParseBlock() block time: got %d, want %d", blk.Time, tb.time)
   109  		}
   110  
   111  		if len(blk.Txs) != len(tb.txs) {
   112  			t.Errorf("ParseBlock() number of transactions: got %d, want %d", len(blk.Txs), len(tb.txs))
   113  		}
   114  
   115  		for ti, tx := range tb.txs {
   116  			if blk.Txs[ti].Txid != tx {
   117  				t.Errorf("ParseBlock() transaction %d: got %s, want %s", ti, blk.Txs[ti].Txid, tx)
   118  			}
   119  		}
   120  	}
   121  }
   122  
   123  var (
   124  	testTx1 = bchain.Tx{
   125  		Blocktime:     1551246710,
   126  		Confirmations: 0,
   127  		Hex:           "0100000001f85264d11a747bdba77d411e5e4a3d35e3aeb5843b34a95234a2121ac65496bd000000006b483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190ffffffff02ec3f8a2a010000001976a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac3116491d000000001976a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac00000000",
   128  		LockTime:      0,
   129  		Time:          1551246710,
   130  		Txid:          "ed732a404cdfd4e0475a7a016200b7eef191f2c9de0ffdef8a20091c0499299c",
   131  		Version:       1,
   132  		Vin: []bchain.Vin{
   133  			{
   134  				Txid: "bd9654c61a12a23452a9343b84b5aee3353d4a5e1e417da7db7b741ad16452f8",
   135  				Vout: 0,
   136  				ScriptSig: bchain.ScriptSig{
   137  					Hex: "483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190",
   138  				},
   139  				Sequence: 4294967295,
   140  			},
   141  		},
   142  		Vout: []bchain.Vout{
   143  			{
   144  				N: 0,
   145  				ScriptPubKey: bchain.ScriptPubKey{
   146  					Addresses: []string{"XkycBX1ykVXXs92pAi6ZQwZPEre9kSHHKH"},
   147  					Hex:       "76a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac",
   148  				},
   149  				ValueSat: *big.NewInt(5008670700),
   150  			},
   151  			{
   152  				N: 1,
   153  				ScriptPubKey: bchain.ScriptPubKey{
   154  					Addresses: []string{"Xm1R9thKBm2EZKZevXsmMX4DVwQQuTohZu"},
   155  					Hex:       "76a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac",
   156  				},
   157  				ValueSat: *big.NewInt(491329073),
   158  			},
   159  		},
   160  	}
   161  	testTxPacked1 = "0a20ed732a404cdfd4e0475a7a016200b7eef191f2c9de0ffdef8a20091c0499299c12e2010100000001f85264d11a747bdba77d411e5e4a3d35e3aeb5843b34a95234a2121ac65496bd000000006b483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190ffffffff02ec3f8a2a010000001976a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac3116491d000000001976a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac0000000018f6cad8e305200028c0e03e3299010a001220bd9654c61a12a23452a9343b84b5aee3353d4a5e1e417da7db7b741ad16452f81800226b483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c219028ffffffff0f3a480a05012a8a3fec10001a1976a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac2222586b7963425831796b565858733932704169365a51775a50457265396b5348484b483a470a041d49163110011a1976a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac2222586d31523974684b426d32455a4b5a657658736d4d5834445677515175546f685a754001"
   162  
   163  	testTx2 = bchain.Tx{
   164  		Blocktime:     1551246710,
   165  		Confirmations: 0,
   166  		Hex:           "03000500010000000000000000000000000000000000000000000000000000000000000000ffffffff170340b00f1291af3c09542bc8349901000000002f4e614effffffff024181f809000000001976a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac3581f809000000001976a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac0000000026010040b00f000000000000000000000000000000000000000000000000000000000000000000",
   167  		LockTime:      0,
   168  		Time:          1551246710,
   169  		Txid:          "71d6975e3b79b52baf26c3269896a34f3bedfb04561c692ffa31f64dada1f9c4",
   170  		Version:       3,
   171  		Vin: []bchain.Vin{
   172  			{
   173  				Coinbase: "0340b00f1291af3c09542bc8349901000000002f4e614e",
   174  				Sequence: 4294967295,
   175  			},
   176  		},
   177  		Vout: []bchain.Vout{
   178  			{
   179  				N: 0,
   180  				ScriptPubKey: bchain.ScriptPubKey{
   181  					Addresses: []string{"XkNPrBSJtrHZUvUqb3JF4g5rMB3uzaJfEL"},
   182  					Hex:       "76a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac",
   183  				},
   184  				ValueSat: *big.NewInt(167280961),
   185  			},
   186  			{
   187  				N: 1,
   188  				ScriptPubKey: bchain.ScriptPubKey{
   189  					Addresses: []string{"XbswPXhcLqm5AN5gwcTTyiUGSP2YndWwk9"},
   190  					Hex:       "76a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac",
   191  				},
   192  				ValueSat: *big.NewInt(167280949),
   193  			},
   194  		},
   195  	}
   196  
   197  	testTxPacked2 = "0a2071d6975e3b79b52baf26c3269896a34f3bedfb04561c692ffa31f64dada1f9c412b50103000500010000000000000000000000000000000000000000000000000000000000000000ffffffff170340b00f1291af3c09542bc8349901000000002f4e614effffffff024181f809000000001976a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac3581f809000000001976a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac0000000026010040b00f00000000000000000000000000000000000000000000000000000000000000000018f6cad8e305200028c0e03e32380a2e30333430623030663132393161663363303935343262633833343939303130303030303030303266346536313465180028ffffffff0f3a470a0409f8814110001a1976a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac2222586b4e507242534a7472485a5576557162334a46346735724d4233757a614a66454c3a470a0409f8813510011a1976a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac222258627377505868634c716d35414e35677763545479695547535032596e6457776b394003"
   198  )
   199  
   200  func TestBaseParser_ParseTxFromJson(t *testing.T) {
   201  	p := NewDashParser(GetChainParams("main"), &btc.Configuration{})
   202  	tests := []struct {
   203  		name    string
   204  		msg     string
   205  		want    *bchain.Tx
   206  		wantErr bool
   207  	}{
   208  		{
   209  			name: "normal tx",
   210  			msg:  `{"hex":"0100000001f85264d11a747bdba77d411e5e4a3d35e3aeb5843b34a95234a2121ac65496bd000000006b483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190ffffffff02ec3f8a2a010000001976a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac3116491d000000001976a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac00000000","txid":"ed732a404cdfd4e0475a7a016200b7eef191f2c9de0ffdef8a20091c0499299c","size":226,"version":1,"type":0,"locktime":0,"vin":[{"txid":"bd9654c61a12a23452a9343b84b5aee3353d4a5e1e417da7db7b741ad16452f8","vout":0,"scriptSig":{"asm":"3045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6[ALL]03093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190","hex":"483045022100dfa158fbd9773fab4f6f329c807e040af0c3a40967cbe01667169b914ed5ad960220061c5876364caa3e3c9c990ad2b4cc8b1a53d4f954dbda8434b0e67cc8348ff6012103093865e1e132b33a2a5ed01c79d2edba3473826a66cb26b8311bfa42749c2190"},"value":55.00000000,"valueSat":5500000000,"address":"Xgcv4bKAXaWf5sjX9KR49L98jeMwNgeXWh","sequence":4294967295}],"vout":[{"value":50.08670700,"valueSat":5008670700,"n":0,"scriptPubKey":{"asm":"OP_DUPOP_HASH16070dcef2a22575d7a8f0779fb1d6cdd48135bd227OP_EQUALVERIFYOP_CHECKSIG","hex":"76a91470dcef2a22575d7a8f0779fb1d6cdd48135bd22788ac","reqSigs":1,"type":"pubkeyhash","addresses":["XkycBX1ykVXXs92pAi6ZQwZPEre9kSHHKH"]}},{"value":4.91329073,"valueSat":491329073,"n":1,"scriptPubKey":{"asm":"OP_DUPOP_HASH16071348f7780e955a2a60eba17ecc4c826ebc23a98OP_EQUALVERIFYOP_CHECKSIG","hex":"76a91471348f7780e955a2a60eba17ecc4c826ebc23a9888ac","reqSigs":1,"type":"pubkeyhash","addresses":["Xm1R9thKBm2EZKZevXsmMX4DVwQQuTohZu"]}}],"blockhash":"000000000000002099caaf1a877911d99a5980ede9b981280eecb291afedf87b","height":1028160,"confirmations":0,"time":1551246710,"blocktime":1551246710,"instantlock":false}`,
   211  			want: &testTx1,
   212  		},
   213  		{
   214  			name: "special tx - DIP2",
   215  			msg:  `{"hex":"03000500010000000000000000000000000000000000000000000000000000000000000000ffffffff170340b00f1291af3c09542bc8349901000000002f4e614effffffff024181f809000000001976a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac3581f809000000001976a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac0000000026010040b00f000000000000000000000000000000000000000000000000000000000000000000","txid":"71d6975e3b79b52baf26c3269896a34f3bedfb04561c692ffa31f64dada1f9c4","size":181,"version":3,"type":5,"locktime":0,"vin":[{"coinbase":"0340b00f1291af3c09542bc8349901000000002f4e614e","sequence":4294967295}],"vout":[{"value":1.67280961,"valueSat":167280961,"n":0,"scriptPubKey":{"asm":"OP_DUPOP_HASH1606a341485a9444b35dc9cb90d24e7483de7d37e00OP_EQUALVERIFYOP_CHECKSIG","hex":"76a9146a341485a9444b35dc9cb90d24e7483de7d37e0088ac","reqSigs":1,"type":"pubkeyhash","addresses":["XkNPrBSJtrHZUvUqb3JF4g5rMB3uzaJfEL"]}},{"value":1.67280949,"valueSat":167280949,"n":1,"scriptPubKey":{"asm":"OP_DUPOP_HASH1600d1156f6026bf975ea3553b03fb534d0959c294cOP_EQUALVERIFYOP_CHECKSIG","hex":"76a9140d1156f6026bf975ea3553b03fb534d0959c294c88ac","reqSigs":1,"type":"pubkeyhash","addresses":["XbswPXhcLqm5AN5gwcTTyiUGSP2YndWwk9"]}}],"extraPayloadSize":38,"extraPayload":"010040b00f000000000000000000000000000000000000000000000000000000000000000000","cbTx":{"version":1,"height":1028160,"merkleRootMNList":"0000000000000000000000000000000000000000000000000000000000000000"},"blockhash":"000000000000002099caaf1a877911d99a5980ede9b981280eecb291afedf87b","height":1028160,"confirmations":0,"time":1551246710,"blocktime":1551246710,"instantlock":false}`,
   216  			want: &testTx2,
   217  		},
   218  	}
   219  	for _, tt := range tests {
   220  		t.Run(tt.name, func(t *testing.T) {
   221  			got, err := p.ParseTxFromJson([]byte(tt.msg))
   222  			if (err != nil) != tt.wantErr {
   223  				t.Errorf("DashParser.ParseTxFromJson() error = %v, wantErr %v", err, tt.wantErr)
   224  				return
   225  			}
   226  			if !reflect.DeepEqual(got, tt.want) {
   227  				t.Errorf("DashParser.ParseTxFromJson() = %+v, want %+v", got, tt.want)
   228  			}
   229  		})
   230  	}
   231  }
   232  
   233  func Test_PackTx(t *testing.T) {
   234  	type args struct {
   235  		tx        bchain.Tx
   236  		height    uint32
   237  		blockTime int64
   238  		parser    *DashParser
   239  	}
   240  	tests := []struct {
   241  		name    string
   242  		args    args
   243  		want    string
   244  		wantErr bool
   245  	}{
   246  		{
   247  			name: "dash-1",
   248  			args: args{
   249  				tx:        testTx1,
   250  				height:    1028160,
   251  				blockTime: 1551246710,
   252  				parser:    NewDashParser(GetChainParams("main"), &btc.Configuration{}),
   253  			},
   254  			want:    testTxPacked1,
   255  			wantErr: false,
   256  		},
   257  		{
   258  			name: "dash-2",
   259  			args: args{
   260  				tx:        testTx2,
   261  				height:    1028160,
   262  				blockTime: 1551246710,
   263  				parser:    NewDashParser(GetChainParams("main"), &btc.Configuration{}),
   264  			},
   265  			want:    testTxPacked2,
   266  			wantErr: false,
   267  		},
   268  	}
   269  	for _, tt := range tests {
   270  		t.Run(tt.name, func(t *testing.T) {
   271  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   272  			if (err != nil) != tt.wantErr {
   273  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   274  				return
   275  			}
   276  			h := hex.EncodeToString(got)
   277  			if !reflect.DeepEqual(h, tt.want) {
   278  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   279  			}
   280  		})
   281  	}
   282  }
   283  
   284  func Test_UnpackTx(t *testing.T) {
   285  	type args struct {
   286  		packedTx string
   287  		parser   *DashParser
   288  	}
   289  	tests := []struct {
   290  		name    string
   291  		args    args
   292  		want    *bchain.Tx
   293  		want1   uint32
   294  		wantErr bool
   295  	}{
   296  		{
   297  			name: "dash-1",
   298  			args: args{
   299  				packedTx: testTxPacked1,
   300  				parser:   NewDashParser(GetChainParams("main"), &btc.Configuration{}),
   301  			},
   302  			want:    &testTx1,
   303  			want1:   1028160,
   304  			wantErr: false,
   305  		},
   306  		{
   307  			name: "dash-2",
   308  			args: args{
   309  				packedTx: testTxPacked2,
   310  				parser:   NewDashParser(GetChainParams("main"), &btc.Configuration{}),
   311  			},
   312  			want:    &testTx2,
   313  			want1:   1028160,
   314  			wantErr: false,
   315  		},
   316  	}
   317  	for _, tt := range tests {
   318  		t.Run(tt.name, func(t *testing.T) {
   319  			b, _ := hex.DecodeString(tt.args.packedTx)
   320  			got, got1, err := tt.args.parser.UnpackTx(b)
   321  			if (err != nil) != tt.wantErr {
   322  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   323  				return
   324  			}
   325  			if !reflect.DeepEqual(got, tt.want) {
   326  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   327  			}
   328  			if got1 != tt.want1 {
   329  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   330  			}
   331  		})
   332  	}
   333  }