OSDN Git Service

add store_key_test.go
[bytom/vapor.git] / database / account_store.go
1 package database
2
3 import (
4         "strings"
5
6         "github.com/vapor/common"
7         "github.com/vapor/crypto/ed25519/chainkd"
8         dbm "github.com/vapor/database/leveldb"
9         "github.com/vapor/protocol/bc"
10 )
11
12 // AccountStorer interface contains account storage functions.
13 type AccountStorer interface {
14         SetAccount(string, string, []byte)
15         SetAccountIndex([]chainkd.XPub, uint64)
16         GetAccountByAccountAlias(string) []byte
17         GetAccountByAccountID(string) []byte // duplicate in WalletStorer
18         GetAccountIndex([]chainkd.XPub) []byte
19         DeleteAccountByAccountAlias(string)
20         DeleteAccountByAccountID(string)
21         DeleteRawProgram(common.Hash)
22         DeleteBip44ContractIndex(string)
23         DeleteContractIndex(string)
24         GetContractIndex(string) []byte
25         // DeleteAccountUTXOs(string) error
26         GetCoinbaseArbitrary() []byte
27         SetCoinbaseArbitrary([]byte)
28         GetMiningAddress() []byte
29         GetFirstAccount() ([]byte, error)
30         SetMiningAddress([]byte)
31         GetBip44ContractIndex(string, bool) []byte
32         GetRawProgram(common.Hash) []byte // duplicate in WalletStorer
33         GetAccounts(string) ([][]byte, error)
34         GetControlPrograms() ([][]byte, error)
35         SetRawProgram(common.Hash, []byte)
36         SetContractIndex(string, uint64)
37         SetBip44ContractIndex(string, bool, uint64)
38         GetUTXOs(string) [][]byte
39         GetStandardUTXO(bc.Hash) []byte // duplicate in WalletStorer
40         GetContractUTXO(bc.Hash) []byte
41 }
42
43 // AccountStore satisfies AccountStorer interface.
44 type AccountStore struct {
45         accountDB dbm.DB
46 }
47
48 // NewAccountStore create new AccountStore.
49 func NewAccountStore(db dbm.DB) *AccountStore {
50         return &AccountStore{
51                 accountDB: db,
52         }
53 }
54
55 // SetAccount set account account ID, account alias and raw account.
56 func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
57         batch := store.accountDB.NewBatch()
58         batch.Set(AccountIDKey(accountID), rawAccount)
59         batch.Set(AccountAliasKey(accountAlias), []byte(accountID))
60         batch.Write()
61 }
62
63 // SetAccountIndex set account index
64 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
65         store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
66 }
67
68 // GetAccountByAccountAlias get account by account alias
69 func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
70         return store.accountDB.Get(AccountAliasKey(accountAlias))
71 }
72
73 // GetAccountByAccountID get account by accountID
74 func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
75         return store.accountDB.Get(AccountIDKey(accountID))
76 }
77
78 // GetAccountIndex get account index by account xpubs
79 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
80         return store.accountDB.Get(AccountIndexKey(xpubs))
81 }
82
83 // DeleteAccountByAccountAlias delete account by account alias
84 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
85         store.accountDB.Delete(AccountAliasKey(accountAlias))
86 }
87
88 // DeleteAccountByAccountID delete account by accountID
89 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
90         store.accountDB.Delete(AccountIDKey(accountID))
91 }
92
93 // DeleteRawProgram delete raw control program by hash
94 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
95         store.accountDB.Delete(ContractKey(hash))
96 }
97
98 // DeleteBip44ContractIndex delete bip44 contract index by accountID
99 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
100         batch := store.accountDB.NewBatch()
101         batch.Delete(Bip44ContractIndexKey(accountID, false))
102         batch.Delete(Bip44ContractIndexKey(accountID, true))
103         batch.Write()
104 }
105
106 // DeleteContractIndex delete contract index by accountID
107 func (store *AccountStore) DeleteContractIndex(accountID string) {
108         store.accountDB.Delete(ContractIndexKey(accountID))
109 }
110
111 // GetContractIndex get contract index
112 func (store *AccountStore) GetContractIndex(accountID string) []byte {
113         return store.accountDB.Get(ContractIndexKey(accountID))
114 }
115
116 // // DeleteAccountUTXOs delete account utxos by accountID
117 // func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
118 //      accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
119 //      defer accountUtxoIter.Release()
120 //      for accountUtxoIter.Next() {
121 //              accountUtxo := &UTXO{}
122 //              if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
123 //                      return err
124 //              }
125
126 //              if accountID == accountUtxo.AccountID {
127 //                      store.accountDB.Delete(StandardUTXOKey(accountUtxo.OutputID))
128 //              }
129 //      }
130 //      return nil
131 // }
132
133 // GetCoinbaseArbitrary get coinbase arbitrary
134 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
135         return store.accountDB.Get([]byte(CoinbaseAbKey))
136 }
137
138 // SetCoinbaseArbitrary set coinbase arbitrary
139 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
140         store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
141 }
142
143 // GetMiningAddress get mining address
144 func (store *AccountStore) GetMiningAddress() []byte {
145         return store.accountDB.Get([]byte(MiningAddressKey))
146 }
147
148 // GetFirstAccount get first account
149 func (store *AccountStore) GetFirstAccount() ([]byte, error) {
150         accountIter := store.accountDB.IteratorPrefix([]byte(AccountPrefix))
151         defer accountIter.Release()
152         if !accountIter.Next() {
153                 return nil, ErrFindAccount
154         }
155         return accountIter.Value(), nil
156 }
157
158 // SetMiningAddress set mining address
159 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
160         store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
161 }
162
163 // GetBip44ContractIndex get bip44 contract index
164 func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
165         return store.accountDB.Get(Bip44ContractIndexKey(accountID, change))
166 }
167
168 // GetRawProgram get raw control program
169 func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
170         return store.accountDB.Get(ContractKey(hash))
171 }
172
173 // GetAccounts get all accounts which name prfix is id.
174 func (store *AccountStore) GetAccounts(id string) ([][]byte, error) {
175         accounts := make([][]byte, 0)
176         accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
177         defer accountIter.Release()
178
179         for accountIter.Next() {
180                 accounts = append(accounts, accountIter.Value())
181         }
182         return accounts, nil
183 }
184
185 // GetControlPrograms get all local control programs
186 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
187         cps := make([][]byte, 0)
188         cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
189         defer cpIter.Release()
190
191         for cpIter.Next() {
192                 cps = append(cps, cpIter.Value())
193         }
194         return cps, nil
195 }
196
197 // SetRawProgram set raw program
198 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
199         store.accountDB.Set(ContractKey(hash), program)
200 }
201
202 // SetContractIndex set contract index
203 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
204         store.accountDB.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
205 }
206
207 // SetBip44ContractIndex set contract index
208 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
209         store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
210 }
211
212 // GetUTXOs get utxos by accountID
213 func (store *AccountStore) GetUTXOs(accountID string) [][]byte {
214         utxos := make([][]byte, 0)
215         utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
216         defer utxoIter.Release()
217
218         for utxoIter.Next() {
219                 utxos = append(utxos, utxoIter.Value())
220         }
221         return utxos
222 }
223
224 // GetStandardUTXO get standard utxo by id
225 func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
226         return store.accountDB.Get(StandardUTXOKey(outid))
227 }
228
229 // GetContractUTXO get contract utxo
230 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
231         return store.accountDB.Get(ContractUTXOKey(outid))
232 }