From e56c40cda3367660f84b7df4bffb06ac9680eaad Mon Sep 17 00:00:00 2001 From: niklr Date: Tue, 24 May 2022 10:11:23 +0000 Subject: [PATCH] fix: resolve errors in vm and wallet tests --- client/abi_client_test.go | 12 +- client/client_test.go | 70 ++--- client/rpc_test.go | 383 +++++++---------------- client/sbp_upgrade_test.go | 3 +- interfaces/core/snapshot_block_test.go | 5 +- interfaces/core/vm_log_list_test.go | 3 + vm/abi/abi_test.go | 39 +-- vm/contracts/contracts_test.go | 4 + wallet/entropystore/crypto_store_test.go | 21 +- wallet/entropystore/manager_test.go | 7 +- wallet/hd-bip/derivation/main_test.go | 111 ++----- 11 files changed, 204 insertions(+), 454 deletions(-) diff --git a/client/abi_client_test.go b/client/abi_client_test.go index 0c4f8431d..7ffe97912 100644 --- a/client/abi_client_test.go +++ b/client/abi_client_test.go @@ -12,11 +12,9 @@ import ( ) func TestAbiCli_CallOffChain(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + t.Skip("Skipped by default. This test can be used to call off-chain contract methods.") + + rpc := PreTestRpc(t, RawUrl) abi := `` offchainCode := `` @@ -34,10 +32,11 @@ func TestAbiCli_CallOffChain(t *testing.T) { for k, v := range result { t.Log(k, v) } - } func TestUnpack(t *testing.T) { + t.Skip("Skipped by default. This test can be used to unpack contract methods.") + abiCode := `` contract, err := abi.JSONToABIContract(strings.NewReader(abiCode)) assert.NoError(t, err) @@ -54,5 +53,4 @@ func TestUnpack(t *testing.T) { assert.NoError(t, err) t.Log(inputs) - } diff --git a/client/client_test.go b/client/client_test.go index 885e35944..ca366d2f1 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -25,6 +25,19 @@ func init() { var Wallet2 *entropystore.Manager +func PreTestRpc(t *testing.T, rawurl string) (RpcClient) { + rpc, err := NewRpcClient(rawurl) + if err != nil { + t.Skipf("Skipping RPC test: %+v", err) + } + height := rpc.GetSnapshotChainHeight() + if height == "" { + t.Skip("Skipping RPC test due to empty SnapshotChainHeight") + } + fmt.Printf("Height: %s\n", height) + return rpc +} + func PreTest() error { w := wallet.New(&config.Wallet{ DataDir: WalletDir, @@ -34,23 +47,20 @@ func PreTest() error { w2, err := w.RecoverEntropyStoreFromMnemonic("extend excess vibrant crop split vehicle order veteran then fog panel appear frozen deer logic path yard tenant bag nuclear witness annual silent fold", "123456") if err != nil { - fmt.Errorf("wallet error, %+v", err) - return err + return fmt.Errorf("wallet error, %+v", err) } err = w2.Unlock("123456") if err != nil { - - fmt.Errorf("wallet error, %+v", err) - return err + return fmt.Errorf("wallet error, %+v", err) } Wallet2 = w2 + return nil } func TestWallet(t *testing.T) { - err := PreTest() - if err != nil { + if err := PreTest(); err != nil { t.Error(err) t.FailNow() } @@ -81,15 +91,13 @@ func TestWallet(t *testing.T) { } func TestClient_SubmitRequestTx(t *testing.T) { + t.Skip("Skipped by default. This test can be used to submit a request transaction.") + if err := PreTest(); err != nil { t.Error(err) t.FailNow() } - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + rpc := PreTestRpc(t, RawUrl) client, e := NewClient(rpc) if e != nil { @@ -132,15 +140,13 @@ func TestClient_SubmitRequestTx(t *testing.T) { } func TestClient_CreateContract(t *testing.T) { + t.Skip("Skipped by default. This test can be used to create a contract.") + if err := PreTest(); err != nil { t.Error(err) t.FailNow() } - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + rpc := PreTestRpc(t, RawUrl) client, e := NewClient(rpc) if e != nil { @@ -183,19 +189,20 @@ func TestClient_CreateContract(t *testing.T) { } func TestClient_SubmitResponseTx(t *testing.T) { - PreTest() - to, err := types.HexToAddress("vite_2ca3c5f1f18b38f865eb47196027ae0c50d0c21e67774abdda") - if err != nil { + t.Skip("Skipped by default. This test can be used to submit a response transaction.") + + if err := PreTest(); err != nil { t.Error(err) - return + t.FailNow() } + rpc := PreTestRpc(t, RawUrl) - t.Log(to) - rpc, err := NewRpcClient(RawUrl) + to, err := types.HexToAddress("vite_2ca3c5f1f18b38f865eb47196027ae0c50d0c21e67774abdda") if err != nil { t.Error(err) return } + t.Log(to) client, e := NewClient(rpc) if e != nil { @@ -228,10 +235,7 @@ func TestClient_SubmitResponseTx(t *testing.T) { } func TestClient_QueryOnroad(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Fatal(err) - } + rpc := PreTestRpc(t, RawUrl) addr, err := types.HexToAddress("vite_2ca3c5f1f18b38f865eb47196027ae0c50d0c21e67774abdda") if err != nil { @@ -251,11 +255,7 @@ func TestClient_QueryOnroad(t *testing.T) { } func TestClient_GetBalanceAll(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + rpc := PreTestRpc(t, RawUrl) client, e := NewClient(rpc) if e != nil { @@ -282,11 +282,7 @@ func TestClient_GetBalanceAll(t *testing.T) { } func TestClient_GetBalance(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + rpc := PreTestRpc(t, RawUrl) client, e := NewClient(rpc) if e != nil { diff --git a/client/rpc_test.go b/client/rpc_test.go index 2963ea0d3..1bf2ba4d8 100644 --- a/client/rpc_test.go +++ b/client/rpc_test.go @@ -9,278 +9,119 @@ import ( "github.com/vitelabs/go-vite/v2/common/types" ledger "github.com/vitelabs/go-vite/v2/interfaces/core" "github.com/vitelabs/go-vite/v2/ledger/consensus/core" + "github.com/vitelabs/go-vite/v2/rpcapi/api" ) +var RawUrl = "http://127.0.0.1:23456" //var RawUrl = "http://127.0.0.1:48133" - //var RawUrl = "http://118.25.182.202:48132" -var RawUrl = "http://139.199.74.104:48132" - +//var RawUrl = "http://139.199.74.104:48132" //var RawUrl = "http://134.175.105.236:48132" -// -//func TestSendRaw(t *testing.T) { -// //client, err := NewRpcClient("http://45.40.197.46:48132") -// //client, err := NewRpcClient(RawUrl) -// //if err != nil { -// // t.Error(err) -// // return -// //} -// // -// //accountAddress, _ := types.HexToAddress("vite_00000000000000000000000000000000000000056ad6d26692") -// //toAddress, _ := types.HexToAddress("vite_098dfae02679a4ca05a4c8bf5dd00a8757f0c622bfccce7d68") -// //tokenId, _ := types.HexToTokenTypeId("tti_3cd880a76b7524fc2694d607") -// //snapshotHash, _ := types.HexToHash("68d458d52a13d5594c069a365345d2067ccbceb63680ec384697dda88de2ada8") -// //publicKey, _ := hex.DecodeString("4sYVHCR0fnpUZy3Acj8Wy0JOU81vH/khAW1KLYb19Hk=") -// // -// //amount := big.NewInt(1000000000).String() -// //block := RawBlock{ -// // BlockType: 3, -// // //PrevHash: prevHash, -// // AccountAddress: accountAddress, -// // PublicKey: publicKey, -// // ToAddress: toAddress, -// // TokenId: tokenId, -// // SnapshotHash: snapshotHash, -// // Height: "6", -// // Amount: &amount, -// // Timestamp: time.Now().Unix(), -// //} -// //err = client.SubmitRaw(block) -// //if err != nil { -// // t.Error(err) -// // return -// //} -//} -// -//func TestFittest(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// hashes, e := client.GetFittestSnapshot() -// if e != nil { -// t.Error(e) -// return -// } -// t.Log(hashes) -//} -// -//func TestGetSnapshotByHeight(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// hash, err := types.HexToHash("b3725777f3b8a3c6a1d126a934e0757d9b9e55df791639b2e241c78c75b8137f") -// if err != nil { -// t.Fatal(err) -// } -// -// h, e := client.GetSnapshotByHash(hash) -// if e != nil { -// t.Error(e) -// return -// } -// -// t.Log(h) -// -// heightInt, e := strconv.ParseUint(h.Height, 10, 64) -// if e != nil { -// t.Fatal(e) -// } -// h2, e := client.GetSnapshotByHeight(heightInt) -// if e != nil { -// t.Fatal(e) -// } -// t.Log(h2) -//} -// -//func TestAccBlock(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// hash, err := types.HexToHash("bfff83c40823c60ff8b28430f988334e60f49a9adacfc4b94b2fce224aa97d14") -// if err != nil { -// t.Error(err) -// return -// } -// block, e := client.GetAccBlock(hash) -// if e != nil { -// t.Error(e) -// return -// } -// t.Log(block) -// t.Log(block.TokenId) -//} -// -//func TestQueryOnroad(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// addr, err := types.HexToAddress("vite_c4a8fe0c93156fe3fd5dc965cc5aea3fcb46f5a0777f9d1304") -// if err != nil { -// t.Error(addr) -// return -// } -// bs, e := client.GetOnroad(OnroadQuery{ -// Address: addr, -// Index: 1, -// Cnt: 10, -// }) -// if e != nil { -// t.Error(e) -// return -// } -// if len(bs) > 0 { -// for _, v := range bs { -// t.Log(v) -// } -// } -//} -// -//func TestQueryBalance(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// addr, err := types.HexToAddress("vite_2ca3c5f1f18b38f865eb47196027ae0c50d0c21e67774abdda") -// if err != nil { -// t.Error(addr) -// return -// } -// bs, e := client.Balance(BalanceQuery{ -// Addr: addr, -// TokenId: ledger.ViteTokenId, -// }) -// if e != nil { -// t.Error(e) -// return -// } -// t.Log(bs) -//} -// -//func TestQueryBalanceAll(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// addr, err := types.HexToAddress("vite_c4a8fe0c93156fe3fd5dc965cc5aea3fcb46f5a0777f9d1304") -// if err != nil { -// t.Error(addr) -// return -// } -// bs, e := client.BalanceAll(BalanceAllQuery{ -// Addr: addr, -// }) -// if e != nil { -// t.Error(e) -// return -// } -// for _, v := range bs { -// t.Log(v) -// } -// -//} -// -//func TestQueryDifficulty(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// self, err := types.HexToAddress("vite_165a295e214421ef1276e79990533953e901291d29b2d4851f") -// if err != nil { -// t.Error(err) -// return -// } -// -// to, err := types.HexToAddress("vite_228f578d58842437fb52104b25750aa84a6f8558b6d9e970b1") -// if err != nil { -// t.Error(err) -// return -// } -// -// prevHash, err := types.HexToHash("58cb3cd2d00c6c0c883ec3aee9069445b826a165eacc75ece9e1fd008f6ccc5e") -// if err != nil { -// t.Fatal(err) -// } -// -// snapshotHash, err := types.HexToHash("0579d6bbcd227d87db2caefd244769507e07a12bcb757ad253dd6c8c68bdea67") -// if err != nil { -// t.Fatal(err) -// } -// bs, e := client.GetDifficulty(DifficultyQuery{ -// SelfAddr: self, -// PrevHash: prevHash, -// SnapshotHash: snapshotHash, -// BlockType: ledger.BlockTypeSendCall, -// ToAddr: &to, -// Data: []byte("hello world"), -// UseStakeQuota: false, -// }) -// -// if e != nil { -// t.Error(e) -// return -// } -// t.Log(bs) -//} -// -//func TestQueryReward(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// bs, e := client.GetRewardByIndex(0) -// -// if e != nil { -// t.Error(e) -// return -// } -// byt, _ := json.Marshal(bs) -// t.Log(string(byt)) -//} -// -//func TestQueryVoteDetails(t *testing.T) { -// client, err := NewRpcClient(RawUrl) -// if err != nil { -// t.Error(err) -// return -// } -// -// bs, e := client.GetVoteDetailsByIndex(0) -// -// if e != nil { -// t.Error(e) -// return -// } -// byt, _ := json.Marshal(bs) -// t.Log(string(byt)) -//} +func TestGetBlockByHash(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + + hash, err := types.HexToHash("bfff83c40823c60ff8b28430f988334e60f49a9adacfc4b94b2fce224aa97d14") + if err != nil { + t.Error(err) + return + } + block, e := rpc.GetBlockByHash(hash) + if e != nil { + t.Error(e) + return + } + t.Log(block) + t.Log(block.TokenId) +} -func Test_GetConfirmedBalances(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) +func TestGetOnroadBlocksByAddress(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + + addr, err := types.HexToAddress("vite_c4a8fe0c93156fe3fd5dc965cc5aea3fcb46f5a0777f9d1304") + if err != nil { + t.Error(addr) + return + } + bs, e := rpc.GetOnroadBlocksByAddress(addr, 1, 10) + if e != nil { + t.Error(e) + return + } + if len(bs) > 0 { + for _, v := range bs { + t.Log(v) + } + } +} + +func TestCalcPoWDifficulty(t *testing.T) { + t.Skip("Skipped by default. This test can be used to calculate PoW difficulty.") + + rpc := PreTestRpc(t, RawUrl) + + self, err := types.HexToAddress("vite_165a295e214421ef1276e79990533953e901291d29b2d4851f") + if err != nil { + t.Error(err) + return + } + + to, err := types.HexToAddress("vite_228f578d58842437fb52104b25750aa84a6f8558b6d9e970b1") if err != nil { t.Error(err) return } + prevHash, err := types.HexToHash("58cb3cd2d00c6c0c883ec3aee9069445b826a165eacc75ece9e1fd008f6ccc5e") + if err != nil { + t.Fatal(err) + } + + bs, e := rpc.CalcPoWDifficulty(api.CalcPoWDifficultyParam{ + SelfAddr: self, + PrevHash: prevHash, + BlockType: ledger.BlockTypeSendCall, + ToAddr: &to, + Data: []byte("hello world"), + UseStakeQuota: false, + }) + + if e != nil { + t.Error(e) + return + } + t.Log(bs) +} + +func TestQueryReward(t *testing.T) { + client := PreTestRpc(t, RawUrl) + + bs, e := client.GetRewardByIndex(0) + + if e != nil { + t.Error(e) + return + } + byt, _ := json.Marshal(bs) + t.Log(string(byt)) +} + +func TestQueryVoteDetails(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + + bs, e := rpc.GetVoteDetailsByIndex(0) + + if e != nil { + t.Error(e) + return + } + byt, _ := json.Marshal(bs) + t.Log(string(byt)) +} + +func Test_GetConfirmedBalances(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + shash := types.HexToHashPanic("25e11b16de62fe5863266cac3c318cf603759a647049672fdb9db5524cc26282") gids := []types.TokenTypeId{ledger.ViteTokenId} @@ -317,11 +158,8 @@ func Test_GetConfirmedBalances(t *testing.T) { } func TestSBPStats(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } + rpc := PreTestRpc(t, RawUrl) + stats, err := rpc.GetHourSBPStats(1, 0) if err != nil { t.Fatal(err) @@ -388,6 +226,8 @@ func TestSBPStats(t *testing.T) { } func TestSbpHash(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + hashs := []string{ "f348100aa8ef02f3dfa0938bc1c050073ddb2d73259357d3cbcb0610374350fc", "b111255964406a4c319fd41c941b6da7921273dea3139373bb7ce686623d6022", @@ -399,12 +239,6 @@ func TestSbpHash(t *testing.T) { "c69280cc3daf4be24187fe3132046efa9dd4c4eba5e264dba55d2e4090b635c9", } - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } - for _, v := range hashs { block, err := rpc.GetSnapshotBlockByHash(types.HexToHashPanic(v)) if err != nil { @@ -415,14 +249,11 @@ func TestSbpHash(t *testing.T) { } func TestSbpddd(t *testing.T) { + rpc := PreTestRpc(t, RawUrl) + hash1 := types.HexToHashPanic("108f714be8fa1662eed21891d74694eae839408d9e28e19670be52dd177818a5") hash2 := types.HexToHashPanic("da5eb52fe8e39ca52698c47e7c6384b7a6c08177d44fc828f8dc562bf3157ad2") - rpc, err := NewRpcClient(RawUrl) - if err != nil { - t.Error(err) - return - } block, err := rpc.GetBlockByHash(hash1) if err != nil { panic(err) diff --git a/client/sbp_upgrade_test.go b/client/sbp_upgrade_test.go index 1dd1521b6..f472940cd 100644 --- a/client/sbp_upgrade_test.go +++ b/client/sbp_upgrade_test.go @@ -11,8 +11,7 @@ import ( ) func TestSBPUpgrade(t *testing.T) { - rpc, err := NewRpcClient(RawUrl) - assert.NoError(t, err) + rpc := PreTestRpc(t, RawUrl) sbpMap := make(map[types.Address]string) versionMap := make(map[string]uint32) diff --git a/interfaces/core/snapshot_block_test.go b/interfaces/core/snapshot_block_test.go index f582d4c74..550e241b9 100644 --- a/interfaces/core/snapshot_block_test.go +++ b/interfaces/core/snapshot_block_test.go @@ -212,9 +212,13 @@ func BenchmarkSnapshotBlock_VerifySignature(b *testing.B) { } func TestForkComputeHash(t *testing.T) { + upgrade.CleanupUpgradeBox(t) + upgrade.InitUpgradeBox(upgrade.NewEmptyUpgradeBox()) snapshotBlock := createSnapshotBlock(1, 10000000000000) hashold := snapshotBlock.Hash + + upgrade.CleanupUpgradeBox(t) upgrade.InitUpgradeBox(upgrade.NewEmptyUpgradeBox().AddPoint(1, 90)) hashnew := snapshotBlock.ComputeHash() @@ -224,5 +228,4 @@ func TestForkComputeHash(t *testing.T) { } fmt.Println("old and new hash:", hashold, hashnew) - } diff --git a/interfaces/core/vm_log_list_test.go b/interfaces/core/vm_log_list_test.go index 92225a8d6..3e4b9156a 100644 --- a/interfaces/core/vm_log_list_test.go +++ b/interfaces/core/vm_log_list_test.go @@ -13,6 +13,7 @@ import ( func TestVmLogList_Hash(t *testing.T) { var vmLogList VmLogList + upgrade.CleanupUpgradeBox(t) upgrade.InitUpgradeBox(upgrade.NewEmptyUpgradeBox().AddPoint(1, 90)) hash1, err1 := types.HexToHash("0dede580455f970517210ae2b9c0fbba74d5b7eea07eb0c62725e06c45061711") @@ -49,6 +50,8 @@ func TestVmLogList_Hash(t *testing.T) { if *vmLogHash100 == *vmLogHash1 { t.Fatal(fmt.Sprintf("vmloghash1 should not be equal with vmloghash100 , %+v, %+v", vmLogHash100, vmLogHash1)) } + + upgrade.CleanupUpgradeBox(t) upgrade.InitUpgradeBox(upgrade.NewEmptyUpgradeBox().AddPoint(1, 101)) vmLogHash95 := vmLogList.Hash(95, address, prehash) diff --git a/vm/abi/abi_test.go b/vm/abi/abi_test.go index 9cafef958..a8bc34cae 100644 --- a/vm/abi/abi_test.go +++ b/vm/abi/abi_test.go @@ -7,6 +7,7 @@ import ( "log" "math/big" "reflect" + "regexp" "strings" "testing" @@ -107,7 +108,9 @@ func TestReader(t *testing.T) { if !reflect.DeepEqual(gotM, expM) { t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM) } - gotM.String() + if !regexp.MustCompile(name).MatchString(gotM.String()) { + t.Errorf("\nGot string: \n%v\ndoes not contain\n%v", gotM.String(), name) + } } if len(exp.Events) != len(abi.Events) { @@ -122,7 +125,9 @@ func TestReader(t *testing.T) { if !reflect.DeepEqual(gotE, expE) { t.Errorf("\nGot abi event: \n%v\ndoes not match expected event\n%v", gotE, expE) } - gotE.String() + if !regexp.MustCompile(name).MatchString(gotE.String()) { + t.Errorf("\nGot string: \n%v\ndoes not contain\n%v", gotE.String(), name) + } } if len(exp.Variables) != len(abi.Variables) { @@ -137,7 +142,9 @@ func TestReader(t *testing.T) { if !reflect.DeepEqual(gotV, expV) { t.Errorf("\nGot abi evevariablent: \n%v\ndoes not match expected variable\n%v", gotV, expV) } - gotV.String() + if !regexp.MustCompile(name).MatchString(gotV.String()) { + t.Errorf("\nGot string: \n%v\ndoes not contain\n%v", gotV.String(), name) + } } } @@ -264,7 +271,7 @@ func TestMultiPack(t *testing.T) { } } -func ExampleJSON() { +func TestPackMethod(t *testing.T) { const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","type":"function"}]` abi, err := JSONToABIContract(strings.NewReader(definition)) @@ -280,6 +287,9 @@ func ExampleJSON() { fmt.Printf("%x\n", out) // Output: // 65591c8e0000000000000000000000000000000000000000000000000000000000000001 + if hex.EncodeToString(out) != "65591c8e0000000000000000000000000000000000000000000000000000000000000001" { + t.Error("Unexpected output", out) + } } func TestInputVariableInputLength(t *testing.T) { @@ -815,7 +825,7 @@ func TestABI_MethodById(t *testing.T) { } } -func TestPackEvent(t *testing.T) { +func TestPackEvents(t *testing.T) { genesisAccount := types.HexToAddressPanic("vite_60e292f0ac471c73d914aeff10bb25925e13b2a9fddb6e6122") var testCases = []caseArgs{ { @@ -842,25 +852,6 @@ func TestPackEvent(t *testing.T) { } } -func TestPackEvents(t *testing.T) { - genesisAccount := types.HexToAddressPanic("vite_60e292f0ac471c73d914aeff10bb25925e13b2a9fddb6e6122") - - var case = caseArgs{ - `[{"type":"event","name":"burn","inputs":[{"name":"tokenId","type":"tokenId","indexed":true},{"name":"address","type":"address"},{"name":"amount","type":"uint256"}]}]`, - "burn", - []interface{}{ledger.ViteTokenId, genesisAccount, big.NewInt(1e18)}, - []types.Hash{ - {97, 183, 48, 235, 101, 233, 99, 47, 158, 102, 219, 185, 87, 54, 55, 226, 222, 80, 17, 178, 245, 18, 137, 74, 55, 152, 28, 98, 222, 60, 189, 64}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'V', 'I', 'T', 'E', ' ', 'T', 'O', 'K', 'E', 'N'}}, - []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 226, 146, 240, 172, 71, 28, 115, 217, 20, 174, 255, 16, 187, 37, 146, 94, 19, 178, 169, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182, 179, 167, 100, 0, 0}, - } - var args Arguments - - args = append(args, Argument) - - -} - type caseArgs struct { abiJSON string eventName string diff --git a/vm/contracts/contracts_test.go b/vm/contracts/contracts_test.go index da9a79878..495ad4b87 100644 --- a/vm/contracts/contracts_test.go +++ b/vm/contracts/contracts_test.go @@ -8,6 +8,7 @@ import ( "github.com/vitelabs/go-vite/v2/common/helper" "github.com/vitelabs/go-vite/v2/common/types" + "github.com/vitelabs/go-vite/v2/common/upgrade" "github.com/vitelabs/go-vite/v2/vm/abi" abi2 "github.com/vitelabs/go-vite/v2/vm/contracts/abi" ) @@ -26,6 +27,9 @@ func printEvent(addr types.Address, contract abi.ABIContract) { } func TestName(t *testing.T) { + upgrade.CleanupUpgradeBox(t) + upgrade.InitUpgradeBox(upgrade.NewLatestUpgradeBox()) + addr := types.AddressAsset data1 := []byte{26, 219, 85, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, 73, 84, 69, 32, 84, 79, 75, 69, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139, 187, 108, 2, 179, 1, 103, 43, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 227, 104, 113, 237, 111, 178, 60, 70, 212, 110, 202, 253, 24, 138, 204, 168, 11, 111, 110, 0} data2 := []byte{26, 219, 85, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, 73, 84, 69, 32, 84, 79, 75, 69, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139, 187, 117, 191, 10, 28, 85, 119, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 227, 104, 113, 237, 111, 178, 60, 70, 212, 110, 202, 253, 24, 138, 204, 168, 11, 111, 110, 0} diff --git a/wallet/entropystore/crypto_store_test.go b/wallet/entropystore/crypto_store_test.go index 5425e6300..5e426a3ab 100644 --- a/wallet/entropystore/crypto_store_test.go +++ b/wallet/entropystore/crypto_store_test.go @@ -22,25 +22,23 @@ func TestCryptoStore_StoreEntropy(t *testing.T) { seed := bip39.NewSeed(m, "") fmt.Println("hexSeed:", hex.EncodeToString(seed)) - addresses, _ := derivation.GetPrimaryAddress(seed) + primaryAddr, _ := derivation.GetPrimaryAddress(seed) - filename := filepath.Join(common.DefaultDataDir(), "UTSeed", addresses.String()) + filename := filepath.Join(common.DefaultDataDir(), "UTSeed", primaryAddr.String()) store := entropystore.CryptoStore{filename} - err := store.StoreEntropy(entropy, *addresses, "123456") + err := store.StoreEntropy(entropy, *primaryAddr, "123456") if err != nil { t.Fatal(err) } fmt.Println(filename) - } func TestCryptoStore_ExtractSeed(t *testing.T) { + seed, _ := hex.DecodeString(TestSeed) + primaryAddr, _ := entropystore.MnemonicToPrimaryAddr(TestMnemonic) - seed, _ := hex.DecodeString("pass your seed") - addresses, _ := derivation.GetPrimaryAddress(seed) - - filename := filepath.Join(common.DefaultDataDir(), "UTSeed", addresses.String()) + filename := filepath.Join(common.DefaultDataDir(), "UTSeed", primaryAddr.String()) store := entropystore.CryptoStore{filename} seedExtract, entropy, err := store.ExtractSeed("123456") @@ -55,7 +53,6 @@ func TestCryptoStore_ExtractSeed(t *testing.T) { } func TestCryptoStore_ExtractMnemonic(t *testing.T) { - store := entropystore.CryptoStore{"filename"} seed, entropy, _ := store.ExtractSeed("password") @@ -74,9 +71,9 @@ func TestDecryptEntropy(t *testing.T) { } seed := bip39.NewSeed(mnemonic, "") - addresses, _ := derivation.GetPrimaryAddress(seed) + primaryAddr, _ := derivation.GetPrimaryAddress(seed) - json, e := entropystore.EncryptEntropy(entropy, *addresses, "123456") + json, e := entropystore.EncryptEntropy(entropy, *primaryAddr, "123456") if e != nil { t.Fatal(e) } @@ -92,7 +89,6 @@ func TestDecryptEntropy(t *testing.T) { if !bytes.Equal(entropy, decryptentropy) { t.Fatal("not equal") } - } { @@ -102,5 +98,4 @@ func TestDecryptEntropy(t *testing.T) { t.Fatal("no error") } } - } diff --git a/wallet/entropystore/manager_test.go b/wallet/entropystore/manager_test.go index 53bc08573..cf054d12e 100644 --- a/wallet/entropystore/manager_test.go +++ b/wallet/entropystore/manager_test.go @@ -53,7 +53,6 @@ func init() { seedToChild[TestSeed] = testTuples testSeedStoreManager, _ = entropystore.StoreNewEntropy(utFilePath, TestMnemonic, "123456", entropystore.DefaultMaxIndex) - } func GetManagerFromStoreNewSeed() *entropystore.Manager { @@ -67,7 +66,6 @@ func GetManagerFromStoreNewSeed() *entropystore.Manager { manager, e := entropystore.StoreNewEntropy(utFilePath, mnemonic, "123456", entropystore.DefaultMaxIndex) if e != nil { panic(e) - return nil } return manager @@ -113,7 +111,6 @@ func TestManager_FindAddr(t *testing.T) { if e != walleterrors.ErrAddressNotFound { t.Fatal(e) } - } func TestManager_LockAndUnlock(t *testing.T) { @@ -165,7 +162,6 @@ func TestManager_LockAndUnlock(t *testing.T) { } sm.Lock() - } func TestFindAddrFromSeed(t *testing.T) { @@ -183,11 +179,10 @@ func TestMapDelete(t *testing.T) { for k, v := range m { fmt.Println(k, v) } - for k, _ := range m { + for k := range m { delete(m, k) } for k, v := range m { fmt.Println(k, v) } - } diff --git a/wallet/hd-bip/derivation/main_test.go b/wallet/hd-bip/derivation/main_test.go index a0f909fd0..c5b1306d3 100644 --- a/wallet/hd-bip/derivation/main_test.go +++ b/wallet/hd-bip/derivation/main_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func ExampleDeriveFromPath() { +func TestDeriveFromPath(t *testing.T) { seed, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f") key, err := DeriveForPath(VitePrimaryAccountPath, seed) if err != nil { @@ -21,10 +21,9 @@ func ExampleDeriveFromPath() { } fmt.Println(VitePrimaryAccountPath, s, " "+address) - } -func ExampleDeriveMultipleKeys() { +func TestDeriveMultipleKeys(t *testing.T) { seed, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f") for i := 0; i < 10; i++ { @@ -41,10 +40,9 @@ func ExampleDeriveMultipleKeys() { fmt.Println(path, s, address) } - } -func ExampleDeriveMultipleKeysFaster() { +func TestDeriveMultipleKeysFaster(t *testing.T) { seed, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f") mainKey, err := DeriveForPath(ViteAccountPrefix, seed) if err != nil { @@ -64,7 +62,6 @@ func ExampleDeriveMultipleKeysFaster() { fmt.Println(fmt.Sprintf(ViteAccountPathFormat, i), s, address) } - } func BenchmarkDerive(b *testing.B) { @@ -94,7 +91,7 @@ func BenchmarkDeriveFast(b *testing.B) { } func TestIsValidPath(t *testing.T) { - ExampleDeriveMultipleKeysFaster() + TestDeriveMultipleKeysFaster(t) assert.True(t, isValidPath("m/0'")) assert.True(t, isValidPath("m/0'/100'")) assert.True(t, isValidPath("m/0'/100'/200'")) @@ -114,11 +111,11 @@ func TestDeriveVector1(t *testing.T) { key, err := NewMasterKey(seed) assert.NoError(t, err) - assert.Equal(t, "2b4be7f19ee27bbf30c667b642d5f4aa69fd169872f8fc3059c08ebae2eb19e7", hex.EncodeToString(key.Key)) - assert.Equal(t, "90046a93de5380a72b5e45010748567d5ea02bbf6522f979e05c0d8d8ca9fffb", hex.EncodeToString(key.ChainCode)) + assert.Equal(t, "963d056ba6d197ef5a46d4965ed699cc35e47fc0841d0b5da3e4a819fbe1e2a1", hex.EncodeToString(key.Key)) + assert.Equal(t, "9d7e39189b93135c9b9bc29131d7fc1ed01a2f96165bbae036cd5baf871e3f23", hex.EncodeToString(key.ChainCode)) publicKey, err := key.PublicKey() assert.NoError(t, err) - assert.Equal(t, "00a4b2856bfec510abab89753fac1ac0e1112364e7d250545963f135f2a33188ed", hex.EncodeToString(append([]byte{0x0}, publicKey...))) + assert.Equal(t, "00c39ad5f7156fd43eab9daa32a1230bcb75e4b4387f0afb3d5cd5159a9339b7ad", hex.EncodeToString(append([]byte{0x0}, publicKey...))) tests := []struct { Path string @@ -128,33 +125,33 @@ func TestDeriveVector1(t *testing.T) { }{ { Path: "m/0'", - ChainCode: "8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69", - PrivateKey: "68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3", - PublicKey: "008c8a13df77a28f3445213a0f432fde644acaa215fc72dcdf300d5efaa85d350c", + ChainCode: "c09508830a25ddf11c85411a3722937829cae8b338bbb8d2ecdfd0cd53c8011b", + PrivateKey: "bc632e40074ab1d1dd7fd2463e9837218852a00a998ac43e1f4907f67834e77e", + PublicKey: "00ca3f8440fdd050a9af004538a6e5e2d329138aeaf25aa72119403717569edcab", }, { Path: "m/0'/1'", - ChainCode: "a320425f77d1b5c2505a6b1b27382b37368ee640e3557c315416801243552f14", - PrivateKey: "b1d0bad404bf35da785a64ca1ac54b2617211d2777696fbffaf208f746ae84f2", - PublicKey: "001932a5270f335bed617d5b935c80aedb1a35bd9fc1e31acafd5372c30f5c1187", + ChainCode: "f90c07c845c94be14db0615de107f281f7de7a6af333d6d6ad4c8cd07fe76f0e", + PrivateKey: "5351f75816232b0d0799a1d2c5e2d90b3fa26fd0452c2214f046d4e92096f07e", + PublicKey: "008c4093af763b1d7c161151a12796be0285806ea46c7fa06ea31bcf718d531059", }, { Path: "m/0'/1'/2'", - ChainCode: "2e69929e00b5ab250f49c3fb1c12f252de4fed2c1db88387094a0f8c4c9ccd6c", - PrivateKey: "92a5b23c0b8a99e37d07df3fb9966917f5d06e02ddbd909c7e184371463e9fc9", - PublicKey: "00ae98736566d30ed0e9d2f4486a64bc95740d89c7db33f52121f8ea8f76ff0fc1", + ChainCode: "52f1714edb01c49dc9393dba4b82b6c14789bdcdb79b1a4cd188f8d82673e871", + PrivateKey: "32d9c872085e9fedf7af3eec9f2dc478c3e5dccdfcfb7ad13b0ba44a6e89a55d", + PublicKey: "00851d89ef83518ec04832a6d56e37c7f5f937c207ca123154e8f4fc663f2baeac", }, { Path: "m/0'/1'/2'/2'", - ChainCode: "8f6d87f93d750e0efccda017d662a1b31a266e4a6f5993b15f5c1f07f74dd5cc", - PrivateKey: "30d1dc7e5fc04c31219ab25a27ae00b50f6fd66622f6e9c913253d6511d1e662", - PublicKey: "008abae2d66361c879b900d204ad2cc4984fa2aa344dd7ddc46007329ac76c429c", + ChainCode: "46a360b9633485a7d4a82847fc94524a65ff84d599784d1fd45a6f994d04a923", + PrivateKey: "48f318a03f3f9873c759fc8adeceb01b35c50825522fe486cec5471b30347cdf", + PublicKey: "0024251409a38e214b7b161cc3ae267a8175500af8b7c2827d403c480a5dce3478", }, { Path: "m/0'/1'/2'/2'/1000000000'", - ChainCode: "68789923a0cac2cd5a29172a475fe9e0fb14cd6adb5ad98a3fa70333e7afa230", - PrivateKey: "8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793", - PublicKey: "003c24da049451555d51a7014a37337aa4e12d41e485abccfa46b47dfb2af54b7a", + ChainCode: "6a6242b2466565d478a8bfa54b65dc402ff79433f091f57960c67e1d1dc9ccbe", + PrivateKey: "eaaf7e3ea8e2dedf08bf872932c8c57ad5ee0c15282ed2cbd6095842fd429d36", + PublicKey: "00d8df4d8cfd365c3da2b6cb7135397c3f2ee7786d823516123bec9458120bc107", }, } @@ -167,66 +164,4 @@ func TestDeriveVector1(t *testing.T) { assert.NoError(t, err) assert.Equal(t, test.PublicKey, hex.EncodeToString(append([]byte{0x0}, publicKey...))) } -} - -// https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-2-for-ed25519 -func TestDeriveVector2(t *testing.T) { - seed, err := hex.DecodeString("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542") - assert.NoError(t, err) - - key, err := NewMasterKey(seed) - assert.NoError(t, err) - assert.Equal(t, "171cb88b1b3c1db25add599712e36245d75bc65a1a5c9e18d76f9f2b1eab4012", hex.EncodeToString(key.Key)) - assert.Equal(t, "ef70a74db9c3a5af931b5fe73ed8e1a53464133654fd55e7a66f8570b8e33c3b", hex.EncodeToString(key.ChainCode)) - publicKey, err := key.PublicKey() - assert.NoError(t, err) - assert.Equal(t, "008fe9693f8fa62a4305a140b9764c5ee01e455963744fe18204b4fb948249308a", hex.EncodeToString(append([]byte{0x0}, publicKey...))) - - tests := []struct { - Path string - ChainCode string - PrivateKey string - PublicKey string - }{ - { - Path: "m/0'", - ChainCode: "0b78a3226f915c082bf118f83618a618ab6dec793752624cbeb622acb562862d", - PrivateKey: "1559eb2bbec5790b0c65d8693e4d0875b1747f4970ae8b650486ed7470845635", - PublicKey: "0086fab68dcb57aa196c77c5f264f215a112c22a912c10d123b0d03c3c28ef1037", - }, - { - Path: "m/0'/2147483647'", - ChainCode: "138f0b2551bcafeca6ff2aa88ba8ed0ed8de070841f0c4ef0165df8181eaad7f", - PrivateKey: "ea4f5bfe8694d8bb74b7b59404632fd5968b774ed545e810de9c32a4fb4192f4", - PublicKey: "005ba3b9ac6e90e83effcd25ac4e58a1365a9e35a3d3ae5eb07b9e4d90bcf7506d", - }, - { - Path: "m/0'/2147483647'/1'", - ChainCode: "73bd9fff1cfbde33a1b846c27085f711c0fe2d66fd32e139d3ebc28e5a4a6b90", - PrivateKey: "3757c7577170179c7868353ada796c839135b3d30554bbb74a4b1e4a5a58505c", - PublicKey: "002e66aa57069c86cc18249aecf5cb5a9cebbfd6fadeab056254763874a9352b45", - }, - { - Path: "m/0'/2147483647'/1'/2147483646'", - ChainCode: "0902fe8a29f9140480a00ef244bd183e8a13288e4412d8389d140aac1794825a", - PrivateKey: "5837736c89570de861ebc173b1086da4f505d4adb387c6a1b1342d5e4ac9ec72", - PublicKey: "00e33c0f7d81d843c572275f287498e8d408654fdf0d1e065b84e2e6f157aab09b", - }, - { - Path: "m/0'/2147483647'/1'/2147483646'/2'", - ChainCode: "5d70af781f3a37b829f0d060924d5e960bdc02e85423494afc0b1a41bbe196d4", - PrivateKey: "551d333177df541ad876a60ea71f00447931c0a9da16f227c11ea080d7391b8d", - PublicKey: "0047150c75db263559a70d5778bf36abbab30fb061ad69f69ece61a72b0cfa4fc0", - }, - } - - for _, test := range tests { - key, err = DeriveForPath(test.Path, seed) - assert.NoError(t, err) - assert.Equal(t, test.PrivateKey, hex.EncodeToString(key.Key)) - assert.Equal(t, test.ChainCode, hex.EncodeToString(key.ChainCode)) - publicKey, err := key.PublicKey() - assert.NoError(t, err) - assert.Equal(t, test.PublicKey, hex.EncodeToString(append([]byte{0x0}, publicKey...))) - } -} +} \ No newline at end of file