package database
import (
+ "encoding/json"
"strings"
+ acc "github.com/vapor/account"
"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 {
- InitBatch()
- CommitBatch()
- SetAccount(string, string, []byte)
- SetAccountIndex([]chainkd.XPub, uint64)
- GetAccountByAccountAlias(string) []byte
- GetAccountByAccountID(string) []byte
- GetAccountIndex([]chainkd.XPub) []byte
- DeleteAccountByAccountAlias(string)
- DeleteAccountByAccountID(string)
- DeleteRawProgram(common.Hash)
- DeleteBip44ContractIndex(string)
- DeleteContractIndex(string)
- GetContractIndex(string) []byte
- GetAccountUTXOs(string) [][]byte
- DeleteStandardUTXO(bc.Hash)
- GetCoinbaseArbitrary() []byte
- SetCoinbaseArbitrary([]byte)
- GetMiningAddress() []byte
- GetFirstAccount() ([]byte, error)
- SetMiningAddress([]byte)
- GetBip44ContractIndex(string, bool) []byte
- GetRawProgram(common.Hash) []byte
- GetAccounts(string) [][]byte
- GetControlPrograms() ([][]byte, error)
- SetRawProgram(common.Hash, []byte)
- SetContractIndex(string, uint64)
- SetBip44ContractIndex(string, bool, uint64)
- GetUTXOs() [][]byte
- GetStandardUTXO(bc.Hash) []byte
- GetContractUTXO(bc.Hash) []byte
- SetStandardUTXO(bc.Hash, []byte)
-}
-
// AccountStore satisfies AccountStorer interface.
type AccountStore struct {
accountDB dbm.DB
}
// SetAccount set account account ID, account alias and raw account.
-func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
+func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) error {
+ rawAccount, err := json.Marshal(account)
+ if err != nil {
+ return acc.ErrMarshalAccount
+ }
+
batch := store.accountDB.NewBatch()
if store.batch != nil {
batch = store.batch
}
- batch.Set(AccountIDKey(accountID), rawAccount)
- batch.Set(AccountAliasKey(accountAlias), []byte(accountID))
+ batch.Set(AccountIDKey(account.ID), rawAccount)
+ batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
+ if updateIndex {
+ batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+ }
+
if store.batch == nil {
batch.Write()
}
+ return nil
}
// DeleteAccount set account account ID, account alias and raw account.
batch = store.batch
}
batch.Delete(AccountIDKey(accountID))
- batch.Delete(AccountAliasKey(accountAlias))
+ batch.Delete(accountAliasKey(accountAlias))
if store.batch == nil {
batch.Write()
}
}
-// SetAccountIndex set account index
-func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
- if store.batch == nil {
- store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
- } else {
- store.batch.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
- }
-}
-
-// GetAccountByAccountAlias get account by account alias
-func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
- return store.accountDB.Get(AccountAliasKey(accountAlias))
+// GetAccountIDByAccountAlias get account ID by account alias
+func (store *AccountStore) GetAccountIDByAccountAlias(accountAlias string) string {
+ accountID := store.accountDB.Get(accountAliasKey(accountAlias))
+ return string(accountID)
}
// GetAccountByAccountID get account by accountID
-func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
- return store.accountDB.Get(AccountIDKey(accountID))
+func (store *AccountStore) GetAccountByAccountID(accountID string) (*acc.Account, error) {
+ account := new(acc.Account)
+ rawAccount := store.accountDB.Get(AccountIDKey(accountID))
+ if rawAccount == nil {
+ return nil, acc.ErrFindAccount
+ }
+ if err := json.Unmarshal(rawAccount, account); err != nil {
+ return nil, err
+ }
+ return account, nil
}
// GetAccountIndex get account index by account xpubs
-func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
- return store.accountDB.Get(AccountIndexKey(xpubs))
+func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) uint64 {
+ currentIndex := uint64(0)
+ if rawIndexBytes := store.accountDB.Get(accountIndexKey(xpubs)); rawIndexBytes != nil {
+ currentIndex = common.BytesToUnit64(rawIndexBytes)
+ }
+ return currentIndex
}
// DeleteAccountByAccountAlias delete account by account alias
func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
if store.batch == nil {
- store.accountDB.Delete(AccountAliasKey(accountAlias))
+ store.accountDB.Delete(accountAliasKey(accountAlias))
} else {
- store.batch.Delete(AccountAliasKey(accountAlias))
+ store.batch.Delete(accountAliasKey(accountAlias))
}
}
// DeleteContractIndex delete contract index by accountID
func (store *AccountStore) DeleteContractIndex(accountID string) {
if store.batch == nil {
- store.accountDB.Delete(ContractIndexKey(accountID))
+ store.accountDB.Delete(contractIndexKey(accountID))
} else {
- store.batch.Delete(ContractIndexKey(accountID))
+ store.batch.Delete(contractIndexKey(accountID))
}
}
// GetContractIndex get contract index
-func (store *AccountStore) GetContractIndex(accountID string) []byte {
- return store.accountDB.Get(ContractIndexKey(accountID))
+func (store *AccountStore) GetContractIndex(accountID string) uint64 {
+ index := uint64(0)
+ if rawIndexBytes := store.accountDB.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
+ index = common.BytesToUnit64(rawIndexBytes)
+ }
+ return index
}
// GetAccountUTXOs get account utxos by account id
return store.accountDB.Get([]byte(MiningAddressKey))
}
-// GetFirstAccount get first account
-func (store *AccountStore) GetFirstAccount() ([]byte, error) {
- accountIter := store.accountDB.IteratorPrefix([]byte(AccountPrefix))
- defer accountIter.Release()
-
- if !accountIter.Next() {
- return nil, ErrFindAccount
- }
- return accountIter.Value(), nil
-}
-
// SetMiningAddress set mining address
func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
if store.batch == nil {
// SetContractIndex set contract index
func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
if store.batch == nil {
- store.accountDB.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
+ store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
} else {
- store.batch.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
+ store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
}
}