github.com/bchainhub/blockbook@v0.3.2/bchain/coins/ravencoin/ravencoinparser_test.go (about)

     1  // +build unittest
     2  
     3  package ravencoin
     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: "RAoGkGhKwzxLnstApumYPD2eTrAJ849cga"},
    36  			want:    "76a91410a8805f1a6af1a5927088544b0b6ec7d6f0ab8b88ac",
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name:    "P2PKH2",
    41  			args:    args{address: "RTq37kPJqMS36tZYunxo2abrBMLeYSCAaa"},
    42  			want:    "76a914cb78181d62d312fdb9aacca433570150dcf0dec288ac",
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name:    "P2SH1",
    47  			args:    args{address: "rCzjkBoY2duVn2WizKxfBedTVWAg6UhfLZ"},
    48  			want:    "a9144a2a40987c74578ee517d426aa2c43fc568f7e0887",
    49  			wantErr: false,
    50  		},
    51  		{
    52  			name:    "P2SH2",
    53  			args:    args{address: "rDzGemZkv9FbDDh5pvWfr7TWtMUnNRRE7T"},
    54  			want:    "a914550bc2fcc1992afade4d298326ee6a03ab975a9387",
    55  			wantErr: false,
    56  		},
    57  	}
    58  	parser := NewRavencoinParser(GetChainParams("main"), &btc.Configuration{})
    59  
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			got, err := parser.GetAddrDescFromAddress(tt.args.address)
    63  			if (err != nil) != tt.wantErr {
    64  				t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    65  				return
    66  			}
    67  			h := hex.EncodeToString(got)
    68  			if !reflect.DeepEqual(h, tt.want) {
    69  				t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  var (
    76  	testTx1       bchain.Tx
    77  	testTxPacked1 = "000a08848bcae7c30e0200000001c171348ffc8976074fa064e48598a816fce3798afc635fb67d99580e50b8e614000000006a473044022009e07574fa543ad259bd3334eb365c655c96d310c578b64c24d7f77fa7dc591c0220427d8ae6eacd1ca2d1994e9ec49cb322aacdde98e4bdb065e0fce81162fb3aa9012102d46827546548b9b47ae1e9e84fc4e53513e0987eeb1dd41220ba39f67d3bf46affffffff02f8137114000000001976a914587a2afa560ccaeaeb67cb72a0db7e2573a179e488ace0c48110000000001976a914d85e6ab66ab0b2c4cfd40ca3b0a779529da5799288ac00000000"
    78  
    79  	testTx2       bchain.Tx
    80  	testTxPacked2 = "000a08848bcae7c30e02000000029e2e14113b2f55726eebaa440edec707fcec3a31ce28fa125afea1e755fb6850010000006a47304402204034c3862f221551cffb2aa809f621f989a75cdb549c789a5ceb3a82c0bcc21c022001b4638f5d73fdd406a4dd9bf99be3dfca4a572b8f40f09b8fd495a7756c0db70121027a32ef45aef2f720ccf585f6fb0b8a7653db89cacc3320e5b385146851aba705fefffffff3b240ae32c542786876fcf23b4b2ab4c34ef077912898ee529756ed4ba35910000000006a47304402204d442645597b13abb85e96e5acd34eff50a4418822fe6a37ed378cdd24574dff02205ae667c56eab63cc45a51063f15b72136fd76e97c46af29bd28e8c4d405aa211012102cde27d7b29331ea3fef909a8d91f6f7753e99a3dd129914be50df26eed73fab3feffffff028447bf38000000001976a9146d7badec5426b880df25a3afc50e476c2423b34b88acb26b556a740000001976a914b3020d0ab85710151fa509d5d9a4e783903d681888ac83080a00"
    81  )
    82  
    83  func init() {
    84  	testTx1 = bchain.Tx{
    85  		Hex:       "0200000001c171348ffc8976074fa064e48598a816fce3798afc635fb67d99580e50b8e614000000006a473044022009e07574fa543ad259bd3334eb365c655c96d310c578b64c24d7f77fa7dc591c0220427d8ae6eacd1ca2d1994e9ec49cb322aacdde98e4bdb065e0fce81162fb3aa9012102d46827546548b9b47ae1e9e84fc4e53513e0987eeb1dd41220ba39f67d3bf46affffffff02f8137114000000001976a914587a2afa560ccaeaeb67cb72a0db7e2573a179e488ace0c48110000000001976a914d85e6ab66ab0b2c4cfd40ca3b0a779529da5799288ac00000000",
    86  		Blocktime: 1554837703,
    87  		Txid:      "d4d3a093586eae0c3668fd288d9e24955928a894c20b551b38dd18c99b123a7c",
    88  		LockTime:  0,
    89  		Version:   2,
    90  		Vin: []bchain.Vin{
    91  			{
    92  				ScriptSig: bchain.ScriptSig{
    93  					Hex: "473044022009e07574fa543ad259bd3334eb365c655c96d310c578b64c24d7f77fa7dc591c0220427d8ae6eacd1ca2d1994e9ec49cb322aacdde98e4bdb065e0fce81162fb3aa9012102d46827546548b9b47ae1e9e84fc4e53513e0987eeb1dd41220ba39f67d3bf46a",
    94  				},
    95  				Txid:     "14e6b8500e58997db65f63fc8a79e3fc16a89885e464a04f077689fc8f3471c1",
    96  				Vout:     0,
    97  				Sequence: 4294967295,
    98  			},
    99  		},
   100  		Vout: []bchain.Vout{
   101  			{
   102  				ValueSat: *big.NewInt(342955000),
   103  				N:        0,
   104  				ScriptPubKey: bchain.ScriptPubKey{
   105  					Hex: "76a914587a2afa560ccaeaeb67cb72a0db7e2573a179e488ac",
   106  					Addresses: []string{
   107  						"RHM1tmdvkk7vDoiGxwUJAMNNmDqywZ5tEn",
   108  					},
   109  				},
   110  			},
   111  			{
   112  				ValueSat: *big.NewInt(276940000),
   113  				N:        1,
   114  				ScriptPubKey: bchain.ScriptPubKey{
   115  					Hex: "76a914d85e6ab66ab0b2c4cfd40ca3b0a779529da5799288ac",
   116  					Addresses: []string{
   117  						"RV1F99b9UBBrCM8aNKugsqsDM8iqoCq7Mt",
   118  					},
   119  				},
   120  			},
   121  		},
   122  	}
   123  
   124  	testTx2 = bchain.Tx{
   125  		Hex:       "02000000029e2e14113b2f55726eebaa440edec707fcec3a31ce28fa125afea1e755fb6850010000006a47304402204034c3862f221551cffb2aa809f621f989a75cdb549c789a5ceb3a82c0bcc21c022001b4638f5d73fdd406a4dd9bf99be3dfca4a572b8f40f09b8fd495a7756c0db70121027a32ef45aef2f720ccf585f6fb0b8a7653db89cacc3320e5b385146851aba705fefffffff3b240ae32c542786876fcf23b4b2ab4c34ef077912898ee529756ed4ba35910000000006a47304402204d442645597b13abb85e96e5acd34eff50a4418822fe6a37ed378cdd24574dff02205ae667c56eab63cc45a51063f15b72136fd76e97c46af29bd28e8c4d405aa211012102cde27d7b29331ea3fef909a8d91f6f7753e99a3dd129914be50df26eed73fab3feffffff028447bf38000000001976a9146d7badec5426b880df25a3afc50e476c2423b34b88acb26b556a740000001976a914b3020d0ab85710151fa509d5d9a4e783903d681888ac83080a00",
   126  		Blocktime: 1554837703,
   127  		Txid:      "8e480d5c1bf7f11d1cbe396ab7dc14e01ea4e1aff45de7c055924f61304ad434",
   128  		LockTime:  657539,
   129  		Version:   2,
   130  		Vin: []bchain.Vin{
   131  			{
   132  				ScriptSig: bchain.ScriptSig{
   133  					Hex: "47304402204034c3862f221551cffb2aa809f621f989a75cdb549c789a5ceb3a82c0bcc21c022001b4638f5d73fdd406a4dd9bf99be3dfca4a572b8f40f09b8fd495a7756c0db70121027a32ef45aef2f720ccf585f6fb0b8a7653db89cacc3320e5b385146851aba705",
   134  				},
   135  				Txid:     "5068fb55e7a1fe5a12fa28ce313aecfc07c7de0e44aaeb6e72552f3b11142e9e",
   136  				Vout:     1,
   137  				Sequence: 4294967294,
   138  			},
   139  			{
   140  				ScriptSig: bchain.ScriptSig{
   141  					Hex: "47304402204d442645597b13abb85e96e5acd34eff50a4418822fe6a37ed378cdd24574dff02205ae667c56eab63cc45a51063f15b72136fd76e97c46af29bd28e8c4d405aa211012102cde27d7b29331ea3fef909a8d91f6f7753e99a3dd129914be50df26eed73fab3",
   142  				},
   143  				Txid:     "1059a34bed569752ee98289177f04ec3b42a4b3bf2fc76687842c532ae40b2f3",
   144  				Vout:     0,
   145  				Sequence: 4294967294,
   146  			},
   147  		},
   148  		Vout: []bchain.Vout{
   149  			{
   150  				ValueSat: *big.NewInt(952059780),
   151  				N:        0,
   152  				ScriptPubKey: bchain.ScriptPubKey{
   153  					Hex: "76a9146d7badec5426b880df25a3afc50e476c2423b34b88ac",
   154  					Addresses: []string{
   155  						"RKG5tpWwjhtqddTgA3QhUh7QmKcuvBnhBX",
   156  					},
   157  				},
   158  			},
   159  			{
   160  				ValueSat: *big.NewInt(500000189362),
   161  				N:        1,
   162  				ScriptPubKey: bchain.ScriptPubKey{
   163  					Hex: "76a914b3020d0ab85710151fa509d5d9a4e783903d681888ac",
   164  					Addresses: []string{
   165  						"RRbhVMbLfuezHPwUMujTmDFAzv64Y9mJqd",
   166  					},
   167  				},
   168  			},
   169  		},
   170  	}
   171  }
   172  
   173  func Test_PackTx(t *testing.T) {
   174  	type args struct {
   175  		tx        bchain.Tx
   176  		height    uint32
   177  		blockTime int64
   178  		parser    *RavencoinParser
   179  	}
   180  	tests := []struct {
   181  		name    string
   182  		args    args
   183  		want    string
   184  		wantErr bool
   185  	}{
   186  		{
   187  			name: "ravencoin-1",
   188  			args: args{
   189  				tx:        testTx1,
   190  				height:    657540,
   191  				blockTime: 1554837703,
   192  				parser:    NewRavencoinParser(GetChainParams("main"), &btc.Configuration{}),
   193  			},
   194  			want:    testTxPacked1,
   195  			wantErr: false,
   196  		},
   197  		{
   198  			name: "ravencoin-2",
   199  			args: args{
   200  				tx:        testTx2,
   201  				height:    657540,
   202  				blockTime: 1554837703,
   203  				parser:    NewRavencoinParser(GetChainParams("main"), &btc.Configuration{}),
   204  			},
   205  			want:    testTxPacked2,
   206  			wantErr: false,
   207  		},
   208  	}
   209  	for _, tt := range tests {
   210  		t.Run(tt.name, func(t *testing.T) {
   211  			got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime)
   212  			if (err != nil) != tt.wantErr {
   213  				t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr)
   214  				return
   215  			}
   216  			h := hex.EncodeToString(got)
   217  			if !reflect.DeepEqual(h, tt.want) {
   218  				t.Errorf("packTx() = %v, want %v", h, tt.want)
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  func Test_UnpackTx(t *testing.T) {
   225  	type args struct {
   226  		packedTx string
   227  		parser   *RavencoinParser
   228  	}
   229  	tests := []struct {
   230  		name    string
   231  		args    args
   232  		want    *bchain.Tx
   233  		want1   uint32
   234  		wantErr bool
   235  	}{
   236  		{
   237  			name: "ravencoin-1",
   238  			args: args{
   239  				packedTx: testTxPacked1,
   240  				parser:   NewRavencoinParser(GetChainParams("main"), &btc.Configuration{}),
   241  			},
   242  			want:    &testTx1,
   243  			want1:   657540,
   244  			wantErr: false,
   245  		},
   246  		{
   247  			name: "ravencoin-2",
   248  			args: args{
   249  				packedTx: testTxPacked2,
   250  				parser:   NewRavencoinParser(GetChainParams("main"), &btc.Configuration{}),
   251  			},
   252  			want:    &testTx2,
   253  			want1:   657540,
   254  			wantErr: false,
   255  		},
   256  	}
   257  	for _, tt := range tests {
   258  		t.Run(tt.name, func(t *testing.T) {
   259  			b, _ := hex.DecodeString(tt.args.packedTx)
   260  			got, got1, err := tt.args.parser.UnpackTx(b)
   261  			if (err != nil) != tt.wantErr {
   262  				t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr)
   263  				return
   264  			}
   265  			if !reflect.DeepEqual(got, tt.want) {
   266  				t.Errorf("unpackTx() got = %v, want %v", got, tt.want)
   267  			}
   268  			if got1 != tt.want1 {
   269  				t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1)
   270  			}
   271  		})
   272  	}
   273  }