OSDN Git Service

add store_key_test.go
authorChengcheng Zhang <943420582@qq.com>
Tue, 25 Jun 2019 12:05:17 +0000 (20:05 +0800)
committerChengcheng Zhang <943420582@qq.com>
Tue, 25 Jun 2019 12:05:17 +0000 (20:05 +0800)
account/accounts.go
account/accounts_test.go
database/account_store.go
database/store_key.go
database/store_key_test.go [new file with mode: 0644]

index 49665a3..da18e57 100644 (file)
@@ -4,7 +4,6 @@ package account
 import (
        "encoding/json"
        "reflect"
-       "sort"
        "strings"
        "sync"
 
@@ -125,7 +124,7 @@ func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
        storeBatch.Set(database.AccountIDKey(account.ID), rawAccount)
        storeBatch.Set(database.AccountAliasKey(account.Alias), []byte(account.ID))
        if updateIndex {
-               storeBatch.Set(GetAccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+               storeBatch.Set(database.AccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
        }
        storeBatch.Write()
        return nil
@@ -150,7 +149,7 @@ func (m *Manager) SaveAccount(account *Account) error {
        }
 
        currentIndex := uint64(0)
-       if rawIndexBytes := m.db.Get(GetAccountIndexKey(account.XPubs)); rawIndexBytes != nil {
+       if rawIndexBytes := m.db.Get(database.AccountIndexKey(account.XPubs)); rawIndexBytes != nil {
                currentIndex = common.BytesToUnit64(rawIndexBytes)
        }
        return m.saveAccount(account, account.KeyIndex > currentIndex)
@@ -166,7 +165,7 @@ func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveR
        }
 
        acctIndex := uint64(1)
-       if rawIndexBytes := m.db.Get(GetAccountIndexKey(xpubs)); rawIndexBytes != nil {
+       if rawIndexBytes := m.db.Get(database.AccountIndexKey(xpubs)); rawIndexBytes != nil {
                acctIndex = common.BytesToUnit64(rawIndexBytes) + 1
        }
        account, err := CreateAccount(xpubs, quorum, alias, acctIndex, deriveRule)
@@ -662,19 +661,6 @@ func createP2SH(account *Account, path [][]byte) (*CtrlProgram, error) {
        }, nil
 }
 
-// copy to database...
-func GetAccountIndexKey(xpubs []chainkd.XPub) []byte {
-       var hash [32]byte
-       var xPubs []byte
-       cpy := append([]chainkd.XPub{}, xpubs[:]...)
-       sort.Sort(signers.SortKeys(cpy))
-       for _, xpub := range cpy {
-               xPubs = append(xPubs, xpub[:]...)
-       }
-       sha3pool.Sum256(hash[:], xPubs)
-       return append([]byte(database.AccountIndexPrefix), hash[:]...)
-}
-
 func (m *Manager) getCurrentContractIndex(account *Account, change bool) (uint64, error) {
        switch account.DeriveRule {
        case signers.BIP0032:
index cd57219..201cc46 100644 (file)
@@ -3,11 +3,9 @@ package account
 import (
        "io/ioutil"
        "os"
-       "reflect"
        "strings"
        "testing"
 
-       "github.com/vapor/blockchain/pseudohsm"
        "github.com/vapor/blockchain/signers"
        "github.com/vapor/config"
        "github.com/vapor/crypto/ed25519/chainkd"
@@ -172,39 +170,6 @@ func TestFindByAlias(t *testing.T) {
        }
 }
 
-func TestGetAccountIndexKey(t *testing.T) {
-       dirPath, err := ioutil.TempDir(".", "TestAccount")
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer os.RemoveAll(dirPath)
-
-       hsm, err := pseudohsm.New(dirPath)
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
-       if err != nil {
-               t.Fatal(err)
-       }
-
-       xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
-       xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
-       if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
-               t.Fatal("GetAccountIndexKey test err")
-       }
-
-       if reflect.DeepEqual(xpubs1, xpubs2) {
-               t.Fatal("GetAccountIndexKey test err")
-       }
-}
-
 func mockAccountManager(t *testing.T) *Manager {
        dirPath, err := ioutil.TempDir(".", "")
        if err != nil {
index 230b4fd..5c4ceb5 100644 (file)
@@ -62,7 +62,7 @@ func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount
 
 // SetAccountIndex set account index
 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
-       store.accountDB.Set(GetAccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
+       store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
 }
 
 // GetAccountByAccountAlias get account by account alias
@@ -77,7 +77,7 @@ func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
 
 // GetAccountIndex get account index by account xpubs
 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
-       return store.accountDB.Get(GetAccountIndexKey(xpubs))
+       return store.accountDB.Get(AccountIndexKey(xpubs))
 }
 
 // DeleteAccountByAccountAlias delete account by account alias
index d19bf40..0c0e8fd 100644 (file)
@@ -34,7 +34,7 @@ var (
        ErrFindAccount = errors.New("Failed to find account")
 )
 
-func GetAccountIndexKey(xpubs []chainkd.XPub) []byte {
+func AccountIndexKey(xpubs []chainkd.XPub) []byte {
        var hash [32]byte
        var xPubs []byte
        cpy := append([]chainkd.XPub{}, xpubs[:]...)
diff --git a/database/store_key_test.go b/database/store_key_test.go
new file mode 100644 (file)
index 0000000..317369d
--- /dev/null
@@ -0,0 +1,44 @@
+package database
+
+import (
+       "io/ioutil"
+       "os"
+       "reflect"
+       "testing"
+
+       "github.com/vapor/blockchain/pseudohsm"
+       "github.com/vapor/crypto/ed25519/chainkd"
+)
+
+func TestAccountIndexKey(t *testing.T) {
+       dirPath, err := ioutil.TempDir(".", "TestAccount")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dirPath)
+
+       hsm, err := pseudohsm.New(dirPath)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
+       xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
+       if !reflect.DeepEqual(AccountIndexKey(xpubs1), AccountIndexKey(xpubs2)) {
+               t.Fatal("AccountIndexKey test err")
+       }
+
+       if reflect.DeepEqual(xpubs1, xpubs2) {
+               t.Fatal("AccountIndexKey test err")
+       }
+}