github.com/bchainhub/blockbook@v0.3.2/bchain/coins/eth/ethparser_test.go (about)

     1  // +build unittest
     2  
     3  package eth
     4  
     5  import (
     6  	"blockbook/bchain"
     7  	"blockbook/tests/dbtestdata"
     8  	"encoding/hex"
     9  	"fmt"
    10  	"math/big"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func TestEthParser_GetAddrDescFromAddress(t *testing.T) {
    16  	type args struct {
    17  		address string
    18  	}
    19  	tests := []struct {
    20  		name    string
    21  		args    args
    22  		want    string
    23  		wantErr bool
    24  	}{
    25  		{
    26  			name: "with 0x prefix",
    27  			args: args{address: "0x81b7e08f65bdf5648606c89998a9cc8164397647"},
    28  			want: "81b7e08f65bdf5648606c89998a9cc8164397647",
    29  		},
    30  		{
    31  			name: "without 0x prefix",
    32  			args: args{address: "47526228d673e9f079630d6cdaff5a2ed13e0e60"},
    33  			want: "47526228d673e9f079630d6cdaff5a2ed13e0e60",
    34  		},
    35  		{
    36  			name:    "address of wrong length",
    37  			args:    args{address: "7526228d673e9f079630d6cdaff5a2ed13e0e60"},
    38  			want:    "",
    39  			wantErr: true,
    40  		},
    41  		{
    42  			name:    "ErrAddressMissing",
    43  			args:    args{address: ""},
    44  			want:    "",
    45  			wantErr: true,
    46  		},
    47  		{
    48  			name:    "error - not eth address",
    49  			args:    args{address: "1JKgN43B9SyLuZH19H5ECvr4KcfrbVHzZ6"},
    50  			want:    "",
    51  			wantErr: true,
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			p := NewEthereumParser(1)
    57  			got, err := p.GetAddrDescFromAddress(tt.args.address)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("EthParser.GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  			h := hex.EncodeToString(got)
    63  			if !reflect.DeepEqual(h, tt.want) {
    64  				t.Errorf("EthParser.GetAddrDescFromAddress() = %v, want %v", h, tt.want)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  var testTx1, testTx2 bchain.Tx
    71  
    72  func init() {
    73  
    74  	testTx1 = bchain.Tx{
    75  		Blocktime: 1534858022,
    76  		Time:      1534858022,
    77  		Txid:      "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
    78  		Vin: []bchain.Vin{
    79  			{
    80  				Addresses: []string{"0x3E3a3D69dc66bA10737F531ed088954a9EC89d97"},
    81  			},
    82  		},
    83  		Vout: []bchain.Vout{
    84  			{
    85  				ValueSat: *big.NewInt(1999622000000000000),
    86  				ScriptPubKey: bchain.ScriptPubKey{
    87  					Addresses: []string{"0x555Ee11FBDDc0E49A9bAB358A8941AD95fFDB48f"},
    88  				},
    89  			},
    90  		},
    91  		CoinSpecificData: completeTransaction{
    92  			Tx: &rpcTransaction{
    93  				AccountNonce:     "0xb26c",
    94  				GasPrice:         "0x430e23400",
    95  				GasLimit:         "0x5208",
    96  				To:               "0x555Ee11FBDDc0E49A9bAB358A8941AD95fFDB48f",
    97  				Value:            "0x1bc0159d530e6000",
    98  				Payload:          "0x",
    99  				Hash:             "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
   100  				BlockNumber:      "0x41eee8",
   101  				From:             "0x3E3a3D69dc66bA10737F531ed088954a9EC89d97",
   102  				TransactionIndex: "0xa",
   103  			},
   104  			Receipt: &rpcReceipt{
   105  				GasUsed: "0x5208",
   106  				Status:  "0x1",
   107  				Logs:    []*rpcLog{},
   108  			},
   109  		},
   110  	}
   111  
   112  	testTx2 = bchain.Tx{
   113  		Blocktime: 1534858022,
   114  		Time:      1534858022,
   115  		Txid:      "0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101",
   116  		Vin: []bchain.Vin{
   117  			{
   118  				Addresses: []string{"0x20cD153de35D469BA46127A0C8F18626b59a256A"},
   119  			},
   120  		},
   121  		Vout: []bchain.Vout{
   122  			{
   123  				ValueSat: *big.NewInt(0),
   124  				ScriptPubKey: bchain.ScriptPubKey{
   125  					Addresses: []string{"0x4af4114F73d1c1C903aC9E0361b379D1291808A2"},
   126  				},
   127  			},
   128  		},
   129  		CoinSpecificData: completeTransaction{
   130  			Tx: &rpcTransaction{
   131  				AccountNonce:     "0xd0",
   132  				GasPrice:         "0x9502f9000",
   133  				GasLimit:         "0x130d5",
   134  				To:               "0x4af4114F73d1c1C903aC9E0361b379D1291808A2",
   135  				Value:            "0x0",
   136  				Payload:          "0xa9059cbb000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f00000000000000000000000000000000000000000000021e19e0c9bab2400000",
   137  				Hash:             "0xa9cd088aba2131000da6f38a33c20169baee476218deea6b78720700b895b101",
   138  				BlockNumber:      "0x41eee8",
   139  				From:             "0x20cD153de35D469BA46127A0C8F18626b59a256A",
   140  				TransactionIndex: "0x0"},
   141  			Receipt: &rpcReceipt{
   142  				GasUsed: "0xcb39",
   143  				Status:  "0x1",
   144  				Logs: []*rpcLog{
   145  					{
   146  						Address: "0x4af4114F73d1c1C903aC9E0361b379D1291808A2",
   147  						Data:    "0x00000000000000000000000000000000000000000000021e19e0c9bab2400000",
   148  						Topics: []string{
   149  							"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
   150  							"0x00000000000000000000000020cd153de35d469ba46127a0c8f18626b59a256a",
   151  							"0x000000000000000000000000555ee11fbddc0e49a9bab358a8941ad95ffdb48f",
   152  						},
   153  					},
   154  				},
   155  			},
   156  		},
   157  	}
   158  }
   159  
   160  func TestEthereumParser_PackTx(t *testing.T) {
   161  	type args struct {
   162  		tx        *bchain.Tx
   163  		height    uint32
   164  		blockTime int64
   165  	}
   166  	tests := []struct {
   167  		name    string
   168  		p       *EthereumParser
   169  		args    args
   170  		want    string
   171  		wantErr bool
   172  	}{
   173  		{
   174  			name: "1",
   175  			args: args{
   176  				tx:        &testTx1,
   177  				height:    4321000,
   178  				blockTime: 1534858022,
   179  			},
   180  			want: dbtestdata.EthTx1Packed,
   181  		},
   182  		{
   183  			name: "2",
   184  			args: args{
   185  				tx:        &testTx2,
   186  				height:    4321000,
   187  				blockTime: 1534858022,
   188  			},
   189  			want: dbtestdata.EthTx2Packed,
   190  		},
   191  	}
   192  	p := NewEthereumParser(1)
   193  	for _, tt := range tests {
   194  		t.Run(tt.name, func(t *testing.T) {
   195  			got, err := p.PackTx(tt.args.tx, tt.args.height, tt.args.blockTime)
   196  			if (err != nil) != tt.wantErr {
   197  				t.Errorf("EthereumParser.PackTx() error = %v, wantErr %v", err, tt.wantErr)
   198  				return
   199  			}
   200  			h := hex.EncodeToString(got)
   201  			if !reflect.DeepEqual(h, tt.want) {
   202  				t.Errorf("EthereumParser.PackTx() = %v, want %v", h, tt.want)
   203  			}
   204  		})
   205  	}
   206  }
   207  
   208  func TestEthereumParser_UnpackTx(t *testing.T) {
   209  	type args struct {
   210  		hex string
   211  	}
   212  	tests := []struct {
   213  		name    string
   214  		p       *EthereumParser
   215  		args    args
   216  		want    *bchain.Tx
   217  		want1   uint32
   218  		wantErr bool
   219  	}{
   220  		{
   221  			name:  "1",
   222  			args:  args{hex: dbtestdata.EthTx1Packed},
   223  			want:  &testTx1,
   224  			want1: 4321000,
   225  		},
   226  		{
   227  			name:  "2",
   228  			args:  args{hex: dbtestdata.EthTx2Packed},
   229  			want:  &testTx2,
   230  			want1: 4321000,
   231  		},
   232  	}
   233  	p := NewEthereumParser(1)
   234  	for _, tt := range tests {
   235  		t.Run(tt.name, func(t *testing.T) {
   236  			b, err := hex.DecodeString(tt.args.hex)
   237  			if err != nil {
   238  				panic(err)
   239  			}
   240  			got, got1, err := p.UnpackTx(b)
   241  			if (err != nil) != tt.wantErr {
   242  				t.Errorf("EthereumParser.UnpackTx() error = %v, wantErr %v", err, tt.wantErr)
   243  				return
   244  			}
   245  			// DeepEqual has problems with pointers in completeTransaction
   246  			gs := got.CoinSpecificData.(completeTransaction)
   247  			ws := tt.want.CoinSpecificData.(completeTransaction)
   248  			gc := *got
   249  			wc := *tt.want
   250  			gc.CoinSpecificData = nil
   251  			wc.CoinSpecificData = nil
   252  			if fmt.Sprint(gc) != fmt.Sprint(wc) {
   253  				// if !reflect.DeepEqual(gc, wc) {
   254  				t.Errorf("EthereumParser.UnpackTx() gc got = %+v, want %+v", gc, wc)
   255  			}
   256  			if !reflect.DeepEqual(gs.Tx, ws.Tx) {
   257  				t.Errorf("EthereumParser.UnpackTx() gs.Tx got = %+v, want %+v", gs.Tx, ws.Tx)
   258  			}
   259  			if !reflect.DeepEqual(gs.Receipt, ws.Receipt) {
   260  				t.Errorf("EthereumParser.UnpackTx() gs.Receipt got = %+v, want %+v", gs.Receipt, ws.Receipt)
   261  			}
   262  			if got1 != tt.want1 {
   263  				t.Errorf("EthereumParser.UnpackTx() got1 = %v, want %v", got1, tt.want1)
   264  			}
   265  		})
   266  	}
   267  }