OSDN Git Service

3b5e281d994952b5539f967fcab7b5e148405fe3
[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 // AccountStore satisfies AccountStorer interface.
13 type AccountStore struct {
14         accountDB dbm.DB
15         batch     dbm.Batch
16 }
17
18 // NewAccountStore create new AccountStore.
19 func NewAccountStore(db dbm.DB) *AccountStore {
20         return &AccountStore{
21                 accountDB: db,
22                 batch:     nil,
23         }
24 }
25
26 // InitBatch initial batch
27 func (store *AccountStore) InitBatch() {
28         if store.batch == nil {
29                 store.batch = store.accountDB.NewBatch()
30         }
31 }
32
33 // CommitBatch commit batch
34 func (store *AccountStore) CommitBatch() {
35         if store.batch != nil {
36                 store.batch.Write()
37                 store.batch = nil
38         }
39 }
40
41 // SetAccount set account account ID, account alias and raw account.
42 func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
43         batch := store.accountDB.NewBatch()
44         if store.batch != nil {
45                 batch = store.batch
46         }
47         batch.Set(AccountIDKey(accountID), rawAccount)
48         batch.Set(accountAliasKey(accountAlias), []byte(accountID))
49         if store.batch == nil {
50                 batch.Write()
51         }
52 }
53
54 // DeleteAccount set account account ID, account alias and raw account.
55 func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
56         batch := store.accountDB.NewBatch()
57         if store.batch != nil {
58                 batch = store.batch
59         }
60         batch.Delete(AccountIDKey(accountID))
61         batch.Delete(accountAliasKey(accountAlias))
62         if store.batch == nil {
63                 batch.Write()
64         }
65 }
66
67 // SetAccountIndex set account index
68 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
69         if store.batch == nil {
70                 store.accountDB.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
71         } else {
72                 store.batch.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
73         }
74 }
75
76 // GetAccountByAccountAlias get account by account alias
77 func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
78         return store.accountDB.Get(accountAliasKey(accountAlias))
79 }
80
81 // GetAccountByAccountID get account by accountID
82 func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
83         return store.accountDB.Get(AccountIDKey(accountID))
84 }
85
86 // GetAccountIndex get account index by account xpubs
87 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
88         return store.accountDB.Get(accountIndexKey(xpubs))
89 }
90
91 // DeleteAccountByAccountAlias delete account by account alias
92 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
93         if store.batch == nil {
94                 store.accountDB.Delete(accountAliasKey(accountAlias))
95         } else {
96                 store.batch.Delete(accountAliasKey(accountAlias))
97         }
98 }
99
100 // DeleteAccountByAccountID delete account by accountID
101 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
102         if store.batch == nil {
103                 store.accountDB.Delete(AccountIDKey(accountID))
104         } else {
105                 store.batch.Delete(AccountIDKey(accountID))
106         }
107 }
108
109 // DeleteRawProgram delete raw control program by hash
110 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
111         if store.batch == nil {
112                 store.accountDB.Delete(ContractKey(hash))
113         } else {
114                 store.batch.Delete(ContractKey(hash))
115         }
116 }
117
118 // DeleteBip44ContractIndex delete bip44 contract index by accountID
119 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
120         batch := store.accountDB.NewBatch()
121         if store.batch != nil {
122                 batch = store.batch
123         }
124         batch.Delete(Bip44ContractIndexKey(accountID, false))
125         batch.Delete(Bip44ContractIndexKey(accountID, true))
126         if store.batch == nil {
127                 batch.Write()
128         }
129 }
130
131 // DeleteContractIndex delete contract index by accountID
132 func (store *AccountStore) DeleteContractIndex(accountID string) {
133         if store.batch == nil {
134                 store.accountDB.Delete(contractIndexKey(accountID))
135         } else {
136                 store.batch.Delete(contractIndexKey(accountID))
137         }
138 }
139
140 // GetContractIndex get contract index
141 func (store *AccountStore) GetContractIndex(accountID string) []byte {
142         return store.accountDB.Get(contractIndexKey(accountID))
143 }
144
145 // GetAccountUTXOs get account utxos by account id
146 func (store *AccountStore) GetAccountUTXOs(accountID string) [][]byte {
147         accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
148         defer accountUtxoIter.Release()
149
150         utxos := make([][]byte, 0)
151         for accountUtxoIter.Next() {
152                 utxos = append(utxos, accountUtxoIter.Value())
153         }
154         return utxos
155 }
156
157 // DeleteStandardUTXO delete utxo by outpu id
158 func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
159         if store.batch == nil {
160                 store.accountDB.Delete(StandardUTXOKey(outputID))
161         } else {
162                 store.batch.Delete(StandardUTXOKey(outputID))
163         }
164 }
165
166 // GetCoinbaseArbitrary get coinbase arbitrary
167 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
168         return store.accountDB.Get([]byte(CoinbaseAbKey))
169 }
170
171 // SetCoinbaseArbitrary set coinbase arbitrary
172 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
173         if store.batch == nil {
174                 store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
175         } else {
176                 store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
177         }
178 }
179
180 // GetMiningAddress get mining address
181 func (store *AccountStore) GetMiningAddress() []byte {
182         return store.accountDB.Get([]byte(MiningAddressKey))
183 }
184
185 // SetMiningAddress set mining address
186 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
187         if store.batch == nil {
188                 store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
189         } else {
190                 store.batch.Set([]byte(MiningAddressKey), rawProgram)
191         }
192 }
193
194 // GetBip44ContractIndex get bip44 contract index
195 func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
196         return store.accountDB.Get(Bip44ContractIndexKey(accountID, change))
197 }
198
199 // GetRawProgram get raw control program
200 func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
201         return store.accountDB.Get(ContractKey(hash))
202 }
203
204 // GetAccounts get all accounts which name prfix is id.
205 func (store *AccountStore) GetAccounts(id string) [][]byte {
206         accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
207         defer accountIter.Release()
208
209         accounts := make([][]byte, 0)
210         for accountIter.Next() {
211                 accounts = append(accounts, accountIter.Value())
212         }
213         return accounts
214 }
215
216 // GetControlPrograms get all local control programs
217 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
218         cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
219         defer cpIter.Release()
220
221         cps := make([][]byte, 0)
222         for cpIter.Next() {
223                 cps = append(cps, cpIter.Value())
224         }
225         return cps, nil
226 }
227
228 // SetRawProgram set raw program
229 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
230         if store.batch == nil {
231                 store.accountDB.Set(ContractKey(hash), program)
232         } else {
233                 store.batch.Set(ContractKey(hash), program)
234         }
235 }
236
237 // SetContractIndex set contract index
238 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
239         if store.batch == nil {
240                 store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
241         } else {
242                 store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
243         }
244 }
245
246 // SetBip44ContractIndex set contract index
247 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
248         if store.batch == nil {
249                 store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
250         } else {
251                 store.batch.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
252         }
253 }
254
255 // GetUTXOs get utxos by accountID
256 func (store *AccountStore) GetUTXOs() [][]byte {
257         utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
258         defer utxoIter.Release()
259
260         utxos := make([][]byte, 0)
261         for utxoIter.Next() {
262                 utxos = append(utxos, utxoIter.Value())
263         }
264         return utxos
265 }
266
267 // GetStandardUTXO get standard utxo by id
268 func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
269         return store.accountDB.Get(StandardUTXOKey(outid))
270 }
271
272 // GetContractUTXO get contract utxo
273 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
274         return store.accountDB.Get(ContractUTXOKey(outid))
275 }
276
277 // SetStandardUTXO set standard utxo
278 func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, data []byte) {
279         if store.batch == nil {
280                 store.accountDB.Set(StandardUTXOKey(outputID), data)
281         } else {
282                 store.batch.Set(StandardUTXOKey(outputID), data)
283         }
284 }