github.com/codingfuture/orig-energi3@v0.8.4/energi/api/migration_test.go (about)

     1  // Copyright 2019 The Energi Core Authors
     2  // This file is part of the Energi Core library.
     3  //
     4  // The Energi Core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The Energi Core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/crypto"
    25  	"github.com/ethereum/go-ethereum/log"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  const (
    31  	testWalletDump = `
    32  # bla bla bla bla
    33  
    34  cPiXpw35kL9diQjw3phzUJFtJGouiHZY822KSkGg6u2xwwdz2VvL 2019-05-17T23:08:56Z change=1 # addr=tRGs7s5rYCPkAiVnCKmse8ZRpZ9BBFvtSL
    35  cQ8ChCLsNbvWZwGDrTXQVqPtM1srL5GgmGYxJkDYZuFp2n3E54nH 2019-06-13T17:26:00Z reserve=1 # addr=tSvHLh3KR1KwwtgthaaPT3r1teec36NVKz
    36  
    37  # ----
    38  `
    39  )
    40  
    41  func TestGen2Parse(t *testing.T) {
    42  	t.Parallel()
    43  	log.Root().SetHandler(log.StdoutHandler)
    44  
    45  	m := NewMigrationAPI(nil)
    46  	res := m.parseGen2Dump(testWalletDump)
    47  	assert.Equal(t, 2, len(res))
    48  	assert.Equal(t,
    49  		"0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6",
    50  		res[0].RawOwner.String())
    51  	assert.Equal(t,
    52  		"0x3fb403e4227a129129e9c0f01ba3ed79294f1ae18bbd961e7ad7fa0996680c40",
    53  		common.ToHex(crypto.FromECDSA(res[0].Key)))
    54  	assert.Equal(t,
    55  		"0xDB52E60435e09e998b6077eE65e3719836fA0d2e",
    56  		res[1].RawOwner.String())
    57  	assert.Equal(t,
    58  		"0x4be146dcb88089732cdbea785bcf5c2c188d67152f0b80329e275c2b553174bc",
    59  		common.ToHex(crypto.FromECDSA(res[1].Key)))
    60  }
    61  
    62  func TestSearchCoins(t *testing.T) {
    63  	t.Parallel()
    64  	log.Root().SetHandler(log.StdoutHandler)
    65  
    66  	m := NewMigrationAPI(nil)
    67  
    68  	listCoins := func() ([]Gen2Coin, error) {
    69  		return []Gen2Coin{
    70  			{
    71  				ItemID:   77,
    72  				RawOwner: common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
    73  				Amount:   big.NewInt(0),
    74  			},
    75  			{
    76  				ItemID:   78,
    77  				RawOwner: common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
    78  				Amount:   big.NewInt(10),
    79  			},
    80  			{
    81  				ItemID:   79,
    82  				RawOwner: common.HexToAddress("0xDB52E60435e09e998b6077eE65e3719836fA0d2e"),
    83  				Amount:   big.NewInt(10),
    84  			},
    85  		}, nil
    86  	}
    87  
    88  	res, err := m.searchGen2Coins(
    89  		[]common.Address{
    90  			common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
    91  		},
    92  		listCoins,
    93  		true,
    94  	)
    95  	assert.Equal(t, 2, len(res))
    96  	assert.Equal(t, err, nil)
    97  	assert.Equal(t, uint64(77), res[0].ItemID)
    98  	assert.Equal(t, uint64(78), res[1].ItemID)
    99  
   100  	listCoins = func() ([]Gen2Coin, error) {
   101  		return []Gen2Coin{
   102  			{
   103  				ItemID:   77,
   104  				RawOwner: common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
   105  				Amount:   big.NewInt(0),
   106  			},
   107  			{
   108  				ItemID:   78,
   109  				RawOwner: common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
   110  				Amount:   big.NewInt(10),
   111  			},
   112  			{
   113  				ItemID:   79,
   114  				RawOwner: common.HexToAddress("0xDB52E60435e09e998b6077eE65e3719836fA0d2e"),
   115  				Amount:   big.NewInt(10),
   116  			},
   117  		}, nil
   118  	}
   119  
   120  	res, err = m.searchGen2Coins(
   121  		[]common.Address{
   122  			common.HexToAddress("0xC94729d0212C2D1074d858EB6c9ee44Fb19D76e6"),
   123  		},
   124  		listCoins,
   125  		false,
   126  	)
   127  	assert.Equal(t, err, nil)
   128  	assert.Equal(t, 1, len(res))
   129  	assert.Equal(t, uint64(78), res[0].ItemID)
   130  }