OSDN Git Service

add account_store.go wallet-interface-account
authorChengcheng Zhang <943420582@qq.com>
Sun, 23 Jun 2019 12:40:03 +0000 (20:40 +0800)
committerChengcheng Zhang <943420582@qq.com>
Sun, 23 Jun 2019 12:40:03 +0000 (20:40 +0800)
account/account_store.go [new file with mode: 0644]
account/accounts.go
account/image.go
wallet/annotated.go
wallet/recovery_test.go
wallet/wallet_store.go

diff --git a/account/account_store.go b/account/account_store.go
new file mode 100644 (file)
index 0000000..dd9a598
--- /dev/null
@@ -0,0 +1,251 @@
+package account
+
+import (
+       "encoding/json"
+       "strings"
+
+       log "github.com/sirupsen/logrus"
+       "github.com/vapor/common"
+       "github.com/vapor/crypto/ed25519/chainkd"
+       dbm "github.com/vapor/database/leveldb"
+       "github.com/vapor/protocol/bc"
+)
+
+// AccountStorer interface contains account storage functions.
+type AccountStorer interface {
+       SetAccount(string, string, []byte)
+       SetAccountIndex([]chainkd.XPub, uint64)
+       GetAccountByAccountAlias(string) []byte
+       GetAccountByAccountID(string) []byte // duplicate in WalletStorer
+       GetAccountIndex([]chainkd.XPub) []byte
+       DeleteAccountByAccountAlias(string)
+       DeleteAccountByAccountID(string)
+       DeleteRawProgram(common.Hash)
+       DeleteBip44ContractIndex(string)
+       DeleteContractIndex(string)
+       GetContractIndex(string) []byte
+       DeleteAccountUTXOs(string) error
+       GetCoinbaseArbitrary() []byte
+       SetCoinbaseArbitrary([]byte)
+       GetMiningAddress() []byte
+       GetFirstAccount() (*Account, error)
+       SetMiningAddress([]byte)
+       GetBip44ContractIndex(string, bool) []byte
+       GetRawProgram(common.Hash) []byte // duplicate in WalletStorer
+       GetAccounts(string) ([]*Account, error)
+       GetControlPrograms() ([]*CtrlProgram, error)
+       SetRawProgram(common.Hash, []byte)
+       SetContractIndex(string, uint64)
+       SetBip44ContractIndex(string, bool, uint64)
+       GetUTXOs(string) []*UTXO
+       GetStandardUTXO(bc.Hash) []byte // duplicate in WalletStorer
+       GetContractUTXO(bc.Hash) []byte
+}
+
+// AccountStore satisfies AccountStorer interface.
+type AccountStore struct {
+       DB dbm.DB
+}
+
+// NewAccountStore create new AccountStore.
+func NewAccountStore(db dbm.DB) *AccountStore {
+       return &AccountStore{
+               DB: db,
+       }
+}
+
+// SetAccount set account account ID, account alias and raw account.
+func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
+       batch := store.DB.NewBatch()
+       batch.Set(Key(accountID), rawAccount)
+       batch.Set(aliasKey(accountAlias), []byte(accountID))
+       batch.Write()
+}
+
+// SetAccountIndex set account index
+func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
+       store.DB.Set(GetAccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
+}
+
+// GetAccountByAccountAlias get account by account alias
+func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
+       return store.DB.Get(aliasKey(accountAlias))
+}
+
+// GetAccountByAccountID get account by accountID
+func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
+       return store.DB.Get(Key(accountID))
+}
+
+// GetAccountIndex get account index by account xpubs
+func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
+       return store.DB.Get(GetAccountIndexKey(xpubs))
+}
+
+// DeleteAccountByAccountAlias delete account by account alias
+func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
+       store.DB.Delete(aliasKey(accountAlias))
+}
+
+// DeleteAccountByAccountID delete account by accountID
+func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
+       store.DB.Delete(Key(accountID))
+}
+
+// DeleteRawProgram delete raw control program by hash
+func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
+       store.DB.Delete(ContractKey(hash))
+}
+
+// DeleteBip44ContractIndex delete bip44 contract index by accountID
+func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
+       batch := store.DB.NewBatch()
+       batch.Delete(bip44ContractIndexKey(accountID, false))
+       batch.Delete(bip44ContractIndexKey(accountID, true))
+       batch.Write()
+}
+
+// DeleteContractIndex delete contract index by accountID
+func (store *AccountStore) DeleteContractIndex(accountID string) {
+       store.DB.Delete(contractIndexKey(accountID))
+}
+
+// GetContractIndex get contract index
+func (store *AccountStore) GetContractIndex(accountID string) []byte {
+       return store.DB.Get(contractIndexKey(accountID))
+}
+
+// DeleteAccountUTXOs delete account utxos by accountID
+func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
+       accountUtxoIter := store.DB.IteratorPrefix([]byte(UTXOPreFix))
+       defer accountUtxoIter.Release()
+       for accountUtxoIter.Next() {
+               accountUtxo := &UTXO{}
+               if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
+                       return err
+               }
+
+               if accountID == accountUtxo.AccountID {
+                       store.DB.Delete(StandardUTXOKey(accountUtxo.OutputID))
+               }
+       }
+       return nil
+}
+
+// GetCoinbaseArbitrary get coinbase arbitrary
+func (store *AccountStore) GetCoinbaseArbitrary() []byte {
+       return store.DB.Get(CoinbaseAbKey)
+}
+
+// SetCoinbaseArbitrary set coinbase arbitrary
+func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
+       store.DB.Set(CoinbaseAbKey, arbitrary)
+}
+
+// GetMiningAddress get mining address
+func (store *AccountStore) GetMiningAddress() []byte {
+       return store.DB.Get(miningAddressKey)
+}
+
+// GetFirstAccount get first account
+func (store *AccountStore) GetFirstAccount() (*Account, error) {
+       accountIter := store.DB.IteratorPrefix([]byte(accountPrefix))
+       defer accountIter.Release()
+       if !accountIter.Next() {
+               return nil, ErrFindAccount
+       }
+
+       account := &Account{}
+       if err := json.Unmarshal(accountIter.Value(), account); err != nil {
+               return nil, err
+       }
+       return account, nil
+}
+
+// SetMiningAddress set mining address
+func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
+       store.DB.Set(miningAddressKey, rawProgram)
+}
+
+// GetBip44ContractIndex get bip44 contract index
+func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
+       return store.DB.Get(bip44ContractIndexKey(accountID, change))
+}
+
+// GetRawProgram get raw control program
+func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
+       return store.DB.Get(ContractKey(hash))
+}
+
+// GetAccounts get all accounts which name prfix is id.
+func (store *AccountStore) GetAccounts(id string) ([]*Account, error) {
+       accounts := []*Account{}
+       accountIter := store.DB.IteratorPrefix(Key(strings.TrimSpace(id)))
+       defer accountIter.Release()
+
+       for accountIter.Next() {
+               account := &Account{}
+               if err := json.Unmarshal(accountIter.Value(), &account); err != nil {
+                       return nil, err
+               }
+               accounts = append(accounts, account)
+       }
+       return accounts, nil
+}
+
+// GetControlPrograms get all local control programs
+func (store *AccountStore) GetControlPrograms() ([]*CtrlProgram, error) {
+       cps := []*CtrlProgram{}
+       cpIter := store.DB.IteratorPrefix(contractPrefix)
+       defer cpIter.Release()
+
+       for cpIter.Next() {
+               cp := &CtrlProgram{}
+               if err := json.Unmarshal(cpIter.Value(), cp); err != nil {
+                       return nil, err
+               }
+               cps = append(cps, cp)
+       }
+       return cps, nil
+}
+
+// SetRawProgram set raw program
+func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
+       store.DB.Set(ContractKey(hash), program)
+}
+
+// SetContractIndex set contract index
+func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
+       store.DB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
+}
+
+// SetBip44ContractIndex set contract index
+func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
+       store.DB.Set(bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
+}
+
+// GetUTXOs get utxos by accountID
+func (store *AccountStore) GetUTXOs(accountID string) []*UTXO {
+       utxos := []*UTXO{}
+       utxoIter := store.DB.IteratorPrefix([]byte(UTXOPreFix))
+       defer utxoIter.Release()
+       for utxoIter.Next() {
+               u := &UTXO{}
+               if err := json.Unmarshal(utxoIter.Value(), u); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
+                       continue
+               }
+               utxos = append(utxos, u)
+       }
+       return utxos
+}
+
+// GetStandardUTXO get standard utxo by id
+func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
+       return store.DB.Get(StandardUTXOKey(outid))
+}
+
+// GetContractUTXO get contract utxo
+func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
+       return store.DB.Get(ContractUTXOKey(outid))
+}
index 0526efe..eb82d3f 100644 (file)
@@ -39,10 +39,10 @@ var (
        accountIndexPrefix  = []byte("AccountIndex:")
        accountPrefix       = []byte("Account:")
        aliasPrefix         = []byte("AccountAlias:")
-       contractIndexPrefix = []byte("ContractIndex")
+       contractIndexPrefix = []byte("ContractIndex:")
        contractPrefix      = []byte("Contract:")
-       miningAddressKey    = []byte("MiningAddress")
-       CoinbaseAbKey       = []byte("CoinbaseArbitrary")
+       miningAddressKey    = []byte("MiningAddress:")
+       CoinbaseAbKey       = []byte("CoinbaseArbitrary:")
 )
 
 // pre-define errors for supporting bytom errorFormatter
index 3562c5e..dd24170 100644 (file)
@@ -27,6 +27,7 @@ func (m *Manager) Backup() (*Image, error) {
                Slice: []*ImageSlice{},
        }
 
+       // GetAccounts()
        accountIter := m.db.IteratorPrefix(accountPrefix)
        defer accountIter.Release()
        for accountIter.Next() {
index f813fb2..dbb0350 100644 (file)
@@ -121,7 +121,7 @@ func getAccountFromACP(program []byte, store WalletStorer) (*account.Account, er
                return nil, err
        }
 
-       accountValue := store.GetAccount(accountCP.AccountID)
+       accountValue := store.GetAccountByAccountID(accountCP.AccountID)
        if accountValue == nil {
                return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
        }
index b653183..757cf2b 100644 (file)
@@ -613,7 +613,7 @@ func TestStateForScope(t *testing.T) {
 }
 
 func bip44ContractIndexKey(accountID string, change bool) []byte {
-       contractIndexPrefix := []byte("ContractIndex")
+       contractIndexPrefix := []byte("ContractIndex:")
        key := append(contractIndexPrefix, accountID...)
        if change {
                return append(key, []byte{1}...)
index 5ebfcbd..2320b18 100644 (file)
@@ -17,7 +17,7 @@ type WalletStorer interface {
        GetAssetDefinition(*bc.AssetID) []byte
        SetAssetDefinition(*bc.AssetID, []byte)
        GetRawProgram(common.Hash) []byte
-       GetAccount(string) []byte
+       GetAccountByAccountID(string) []byte
        DeleteTransaction(uint64)
        SetTransaction(uint64, uint32, string, []byte)
        DeleteUnconfirmedTransaction(string)
@@ -71,7 +71,7 @@ func (store *WalletStore) GetRawProgram(hash common.Hash) []byte {
 }
 
 // GetAccount get account value by account ID
-func (store *WalletStore) GetAccount(accountID string) []byte {
+func (store *WalletStore) GetAccountByAccountID(accountID string) []byte {
        return store.DB.Get(account.Key(accountID))
 }