• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision25dc665492fefc26e896f21fcf43ea8000428380 (tree)
Time2019-06-26 00:19:06
AuthorChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Log Message

finish account

Change Summary

Incremental Difference

--- a/account/accounts.go
+++ b/account/accounts.go
@@ -67,7 +67,7 @@ type CtrlProgram struct {
6767
6868 // Manager stores accounts and their associated control programs.
6969 type Manager struct {
70- db dbm.DB
70+ store database.AccountStorer
7171 chain *protocol.Chain
7272 utxoKeeper *utxoKeeper
7373
@@ -83,11 +83,12 @@ type Manager struct {
8383 }
8484
8585 // NewManager creates a new account manager
86-func NewManager(walletDB dbm.DB, chain *protocol.Chain) *Manager {
86+func NewManager(accountdb dbm.DB, chain *protocol.Chain) *Manager {
87+ store := database.NewAccountStore(accountdb)
8788 return &Manager{
88- db: walletDB,
89+ store: store,
8990 chain: chain,
90- utxoKeeper: newUtxoKeeper(chain.BestBlockHeight, walletDB),
91+ utxoKeeper: newUtxoKeeper(chain.BestBlockHeight, store),
9192 cache: lru.New(maxAccountCache),
9293 aliasCache: lru.New(maxAccountCache),
9394 delayedACPs: make(map[*txbuilder.TemplateBuilder][]*CtrlProgram),
@@ -120,13 +121,14 @@ func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
120121 return ErrMarshalAccount
121122 }
122123
123- storeBatch := m.db.NewBatch()
124- storeBatch.Set(database.AccountIDKey(account.ID), rawAccount)
125- storeBatch.Set(database.AccountAliasKey(account.Alias), []byte(account.ID))
124+ m.store.InitBatch()
125+ defer m.store.CommitBatch()
126+
127+ m.store.SetAccount(account.ID, account.Alias, rawAccount)
126128 if updateIndex {
127- storeBatch.Set(database.AccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
129+ m.store.SetAccountIndex(account.XPubs, account.KeyIndex)
128130 }
129- storeBatch.Write()
131+
130132 return nil
131133 }
132134
@@ -135,7 +137,7 @@ func (m *Manager) SaveAccount(account *Account) error {
135137 m.accountMu.Lock()
136138 defer m.accountMu.Unlock()
137139
138- if existed := m.db.Get(database.AccountAliasKey(account.Alias)); existed != nil {
140+ if existed := m.store.GetAccountByAccountAlias(account.Alias); existed != nil {
139141 return ErrDuplicateAlias
140142 }
141143
@@ -149,7 +151,7 @@ func (m *Manager) SaveAccount(account *Account) error {
149151 }
150152
151153 currentIndex := uint64(0)
152- if rawIndexBytes := m.db.Get(database.AccountIndexKey(account.XPubs)); rawIndexBytes != nil {
154+ if rawIndexBytes := m.store.GetAccountIndex(account.XPubs); rawIndexBytes != nil {
153155 currentIndex = common.BytesToUnit64(rawIndexBytes)
154156 }
155157 return m.saveAccount(account, account.KeyIndex > currentIndex)
@@ -160,12 +162,12 @@ func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveR
160162 m.accountMu.Lock()
161163 defer m.accountMu.Unlock()
162164
163- if existed := m.db.Get(database.AccountAliasKey(alias)); existed != nil {
165+ if existed := m.store.GetAccountByAccountAlias(alias); existed != nil {
164166 return nil, ErrDuplicateAlias
165167 }
166168
167169 acctIndex := uint64(1)
168- if rawIndexBytes := m.db.Get(database.AccountIndexKey(xpubs)); rawIndexBytes != nil {
170+ if rawIndexBytes := m.store.GetAccountIndex(xpubs); rawIndexBytes != nil {
169171 acctIndex = common.BytesToUnit64(rawIndexBytes) + 1
170172 }
171173 account, err := CreateAccount(xpubs, quorum, alias, acctIndex, deriveRule)
@@ -191,7 +193,7 @@ func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err err
191193 oldAlias := account.Alias
192194
193195 normalizedAlias := strings.ToLower(strings.TrimSpace(newAlias))
194- if existed := m.db.Get(database.AccountAliasKey(normalizedAlias)); existed != nil {
196+ if existed := m.store.GetAccountByAccountAlias(normalizedAlias); existed != nil {
195197 return ErrDuplicateAlias
196198 }
197199
@@ -205,11 +207,12 @@ func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err err
205207 return ErrMarshalAccount
206208 }
207209
208- storeBatch := m.db.NewBatch()
209- storeBatch.Delete(database.AccountAliasKey(oldAlias))
210- storeBatch.Set(database.AccountIDKey(accountID), rawAccount)
211- storeBatch.Set(database.AccountAliasKey(normalizedAlias), []byte(accountID))
212- storeBatch.Write()
210+ m.store.InitBatch()
211+ defer m.store.CommitBatch()
212+
213+ m.store.DeleteAccountByAccountAlias(oldAlias)
214+ m.store.SetAccount(accountID, normalizedAlias, rawAccount)
215+
213216 return nil
214217 }
215218
@@ -276,29 +279,30 @@ func (m *Manager) deleteAccountControlPrograms(accountID string) error {
276279 for _, cp := range cps {
277280 if cp.AccountID == accountID {
278281 sha3pool.Sum256(hash[:], cp.ControlProgram)
279- m.db.Delete(database.ContractKey(hash))
282+ m.store.DeleteRawProgram(hash)
280283 }
281284 }
282- m.db.Delete(database.Bip44ContractIndexKey(accountID, false))
283- m.db.Delete(database.Bip44ContractIndexKey(accountID, true))
284- m.db.Delete(database.ContractIndexKey(accountID))
285+
286+ m.store.DeleteBip44ContractIndex(accountID)
287+ m.store.DeleteContractIndex(accountID)
288+
285289 return nil
286290 }
287291
288292 // deleteAccountUtxos deletes utxos matching accountID
289293 func (m *Manager) deleteAccountUtxos(accountID string) error {
290- accountUtxoIter := m.db.IteratorPrefix([]byte(UTXOPreFix))
291- defer accountUtxoIter.Release()
292- for accountUtxoIter.Next() {
293- accountUtxo := &UTXO{}
294- if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
294+ rawUTXOs := m.store.GetAccountUTXOs(accountID)
295+
296+ for _, rawUTXO := range rawUTXOs {
297+ utxo := new(UTXO)
298+ if err := json.Unmarshal(rawUTXO, utxo); err != nil {
295299 return err
296300 }
297-
298- if accountID == accountUtxo.AccountID {
299- m.db.Delete(StandardUTXOKey(accountUtxo.OutputID))
301+ if accountID == utxo.AccountID {
302+ m.store.DeleteUTXO(utxo.OutputID)
300303 }
301304 }
305+
302306 return nil
303307 }
304308
@@ -323,10 +327,11 @@ func (m *Manager) DeleteAccount(accountID string) (err error) {
323327 m.aliasCache.Remove(account.Alias)
324328 m.cacheMu.Unlock()
325329
326- storeBatch := m.db.NewBatch()
327- storeBatch.Delete(database.AccountAliasKey(account.Alias))
328- storeBatch.Delete(database.AccountIDKey(account.ID))
329- storeBatch.Write()
330+ m.store.InitBatch()
331+ defer m.store.CommitBatch()
332+
333+ m.store.DeleteAccountByAccountAlias(account.Alias)
334+ m.store.DeleteAccountByAccountID(account.ID)
330335 return nil
331336 }
332337
@@ -339,7 +344,7 @@ func (m *Manager) FindByAlias(alias string) (*Account, error) {
339344 return m.FindByID(cachedID.(string))
340345 }
341346
342- rawID := m.db.Get(database.AccountAliasKey(alias))
347+ rawID := m.store.GetAccountByAccountAlias(alias)
343348 if rawID == nil {
344349 return nil, ErrFindAccount
345350 }
@@ -360,7 +365,7 @@ func (m *Manager) FindByID(id string) (*Account, error) {
360365 return cachedAccount.(*Account), nil
361366 }
362367
363- rawAccount := m.db.Get(database.AccountIDKey(id))
368+ rawAccount := m.store.GetAccountByAccountID(id)
364369 if rawAccount == nil {
365370 return nil, ErrFindAccount
366371 }
@@ -378,7 +383,7 @@ func (m *Manager) FindByID(id string) (*Account, error) {
378383
379384 // GetAccountByProgram return Account by given CtrlProgram
380385 func (m *Manager) GetAccountByProgram(program *CtrlProgram) (*Account, error) {
381- rawAccount := m.db.Get(database.AccountIDKey(program.AccountID))
386+ rawAccount := m.store.GetAccountByAccountID(program.AccountID)
382387 if rawAccount == nil {
383388 return nil, ErrFindAccount
384389 }
@@ -404,7 +409,7 @@ func (m *Manager) GetAccountByXPubsIndex(xPubs []chainkd.XPub, index uint64) (*A
404409
405410 // GetAliasByID return the account alias by given ID
406411 func (m *Manager) GetAliasByID(id string) string {
407- rawAccount := m.db.Get(database.AccountIDKey(id))
412+ rawAccount := m.store.GetAccountByAccountID(id)
408413 if rawAccount == nil {
409414 log.Warn("GetAliasByID fail to find account")
410415 return ""
@@ -418,7 +423,7 @@ func (m *Manager) GetAliasByID(id string) string {
418423 }
419424
420425 func (m *Manager) GetCoinbaseArbitrary() []byte {
421- if arbitrary := m.db.Get([]byte(database.CoinbaseAbKey)); arbitrary != nil {
426+ if arbitrary := m.store.GetCoinbaseArbitrary(); arbitrary != nil {
422427 return arbitrary
423428 }
424429 return []byte{}
@@ -439,19 +444,18 @@ func (m *Manager) GetCoinbaseControlProgram() ([]byte, error) {
439444
440445 // GetCoinbaseCtrlProgram will return the coinbase CtrlProgram
441446 func (m *Manager) GetCoinbaseCtrlProgram() (*CtrlProgram, error) {
442- if data := m.db.Get([]byte(database.MiningAddressKey)); data != nil {
447+ if data := m.store.GetMiningAddress(); data != nil {
443448 cp := &CtrlProgram{}
444449 return cp, json.Unmarshal(data, cp)
445450 }
446451
447- accountIter := m.db.IteratorPrefix([]byte(database.AccountPrefix))
448- defer accountIter.Release()
449- if !accountIter.Next() {
450- return nil, ErrFindAccount
452+ fistAccount, err := m.store.GetFirstAccount()
453+ if err != nil {
454+ return nil, err
451455 }
452456
453457 account := &Account{}
454- if err := json.Unmarshal(accountIter.Value(), account); err != nil {
458+ if err := json.Unmarshal(fistAccount, account); err != nil {
455459 return nil, err
456460 }
457461
@@ -465,14 +469,15 @@ func (m *Manager) GetCoinbaseCtrlProgram() (*CtrlProgram, error) {
465469 return nil, err
466470 }
467471
468- m.db.Set([]byte(database.MiningAddressKey), rawCP)
472+ m.store.SetMiningAddress(rawCP)
473+
469474 return program, nil
470475 }
471476
472477 // GetContractIndex return the current index
473478 func (m *Manager) GetContractIndex(accountID string) uint64 {
474479 index := uint64(0)
475- if rawIndexBytes := m.db.Get(database.ContractIndexKey(accountID)); rawIndexBytes != nil {
480+ if rawIndexBytes := m.store.GetContractIndex(accountID); rawIndexBytes != nil {
476481 index = common.BytesToUnit64(rawIndexBytes)
477482 }
478483 return index
@@ -481,7 +486,7 @@ func (m *Manager) GetContractIndex(accountID string) uint64 {
481486 // GetBip44ContractIndex return the current bip44 contract index
482487 func (m *Manager) GetBip44ContractIndex(accountID string, change bool) uint64 {
483488 index := uint64(0)
484- if rawIndexBytes := m.db.Get(database.Bip44ContractIndexKey(accountID, change)); rawIndexBytes != nil {
489+ if rawIndexBytes := m.store.GetBip44ContractIndex(accountID, change); rawIndexBytes != nil {
485490 index = common.BytesToUnit64(rawIndexBytes)
486491 }
487492 return index
@@ -496,7 +501,7 @@ func (m *Manager) GetLocalCtrlProgramByAddress(address string) (*CtrlProgram, er
496501
497502 var hash [32]byte
498503 sha3pool.Sum256(hash[:], program)
499- rawProgram := m.db.Get(database.ContractKey(hash))
504+ rawProgram := m.store.GetRawProgram(hash)
500505 if rawProgram == nil {
501506 return nil, ErrFindCtrlProgram
502507 }
@@ -518,40 +523,43 @@ func (m *Manager) GetMiningAddress() (string, error) {
518523 func (m *Manager) IsLocalControlProgram(prog []byte) bool {
519524 var hash common.Hash
520525 sha3pool.Sum256(hash[:], prog)
521- bytes := m.db.Get(database.ContractKey(hash))
526+ bytes := m.store.GetRawProgram(hash)
522527 return bytes != nil
523528 }
524529
525530 // ListAccounts will return the accounts in the db
526531 func (m *Manager) ListAccounts(id string) ([]*Account, error) {
527- accounts := []*Account{}
528- accountIter := m.db.IteratorPrefix(database.AccountIDKey(strings.TrimSpace(id)))
529- defer accountIter.Release()
532+ rawAccounts := m.store.GetAccounts(id)
530533
531- for accountIter.Next() {
532- account := &Account{}
533- if err := json.Unmarshal(accountIter.Value(), &account); err != nil {
534+ accounts := []*Account{}
535+ for _, rawAccount := range rawAccounts {
536+ account := new(Account)
537+ if err := json.Unmarshal(rawAccount, &account); err != nil {
534538 return nil, err
535539 }
536540 accounts = append(accounts, account)
537541 }
542+
538543 return accounts, nil
539544 }
540545
541546 // ListControlProgram return all the local control program
542547 func (m *Manager) ListControlProgram() ([]*CtrlProgram, error) {
543- cps := []*CtrlProgram{}
544- cpIter := m.db.IteratorPrefix([]byte(database.ContractPrefix))
545- defer cpIter.Release()
548+ rawControlPrograms, err := m.store.GetControlPrograms()
549+ if err != nil {
550+ return nil, err
551+ }
546552
547- for cpIter.Next() {
548- cp := &CtrlProgram{}
549- if err := json.Unmarshal(cpIter.Value(), cp); err != nil {
553+ controlPrograms := []*CtrlProgram{}
554+ for _, rawControlProgram := range rawControlPrograms {
555+ controlProgram := new(CtrlProgram)
556+ if err := json.Unmarshal(rawControlProgram, controlProgram); err != nil {
550557 return nil, err
551558 }
552- cps = append(cps, cp)
559+ controlPrograms = append(controlPrograms, controlProgram)
553560 }
554- return cps, nil
561+
562+ return controlPrograms, nil
555563 }
556564
557565 func (m *Manager) ListUnconfirmedUtxo(accountID string, isSmartContract bool) []*UTXO {
@@ -586,12 +594,12 @@ func (m *Manager) SetMiningAddress(miningAddress string) (string, error) {
586594 return "", err
587595 }
588596
589- m.db.Set([]byte(database.MiningAddressKey), rawCP)
597+ m.store.SetMiningAddress(rawCP)
590598 return m.GetMiningAddress()
591599 }
592600
593601 func (m *Manager) SetCoinbaseArbitrary(arbitrary []byte) {
594- m.db.Set([]byte(database.CoinbaseAbKey), arbitrary)
602+ m.store.SetCoinbaseArbitrary(arbitrary)
595603 }
596604
597605 // CreateCtrlProgram generate an address for the select account
@@ -706,17 +714,18 @@ func (m *Manager) saveControlProgram(prog *CtrlProgram, updateIndex bool) error
706714 return err
707715 }
708716
709- storeBatch := m.db.NewBatch()
710- storeBatch.Set(database.ContractKey(hash), accountCP)
717+ m.store.InitBatch()
718+ defer m.store.CommitBatch()
719+
720+ m.store.SetRawProgram(hash, accountCP)
711721 if updateIndex {
712722 switch acct.DeriveRule {
713723 case signers.BIP0032:
714- storeBatch.Set(database.ContractIndexKey(acct.ID), common.Unit64ToBytes(prog.KeyIndex))
724+ m.store.SetContractIndex(acct.ID, prog.KeyIndex)
715725 case signers.BIP0044:
716- storeBatch.Set(database.Bip44ContractIndexKey(acct.ID, prog.Change), common.Unit64ToBytes(prog.KeyIndex))
726+ m.store.SetBip44ContractIndex(acct.ID, prog.Change, prog.KeyIndex)
717727 }
718728 }
719- storeBatch.Write()
720729
721730 return nil
722731 }
--- a/account/builder_test.go
+++ b/account/builder_test.go
@@ -31,7 +31,7 @@ func TestReserveBtmUtxoChain(t *testing.T) {
3131 t.Fatal(err)
3232 }
3333
34- m.db.Set(StandardUTXOKey(utxo.OutputID), data)
34+ m.store.SetStandardUTXO(utxo.OutputID, data)
3535 }
3636
3737 cases := []struct {
--- a/account/image.go
+++ b/account/image.go
@@ -5,7 +5,6 @@ import (
55 "encoding/json"
66
77 log "github.com/sirupsen/logrus"
8- "github.com/vapor/database"
98 )
109
1110 // ImageSlice record info of single account
@@ -29,17 +28,16 @@ func (m *Manager) Backup() (*Image, error) {
2928 }
3029
3130 // GetAccounts()
32- accountIter := m.db.IteratorPrefix([]byte(database.AccountPrefix))
33- defer accountIter.Release()
34- for accountIter.Next() {
35- a := &Account{}
36- if err := json.Unmarshal(accountIter.Value(), a); err != nil {
31+ rawAccounts := m.store.GetAccounts("")
32+
33+ for _, rawAccount := range rawAccounts {
34+ account := new(Account)
35+ if err := json.Unmarshal(rawAccount, account); err != nil {
3736 return nil, err
3837 }
39-
4038 image.Slice = append(image.Slice, &ImageSlice{
41- Account: a,
42- ContractIndex: m.GetContractIndex(a.ID),
39+ Account: account,
40+ ContractIndex: m.GetContractIndex(account.ID),
4341 })
4442 }
4543 return image, nil
@@ -50,9 +48,11 @@ func (m *Manager) Restore(image *Image) error {
5048 m.accountMu.Lock()
5149 defer m.accountMu.Unlock()
5250
53- storeBatch := m.db.NewBatch()
51+ m.store.InitBatch()
52+ defer m.store.CommitBatch()
53+
5454 for _, slice := range image.Slice {
55- if existed := m.db.Get(database.AccountIDKey(slice.Account.ID)); existed != nil {
55+ if existed := m.store.GetAccountByAccountID(slice.Account.ID); existed != nil {
5656 log.WithFields(log.Fields{
5757 "module": logModule,
5858 "alias": slice.Account.Alias,
@@ -60,7 +60,7 @@ func (m *Manager) Restore(image *Image) error {
6060 }).Warning("skip restore account due to already existed")
6161 continue
6262 }
63- if existed := m.db.Get(database.AccountAliasKey(slice.Account.Alias)); existed != nil {
63+ if existed := m.store.GetAccountByAccountAlias(slice.Account.Alias); existed != nil {
6464 return ErrDuplicateAlias
6565 }
6666
@@ -69,10 +69,8 @@ func (m *Manager) Restore(image *Image) error {
6969 return ErrMarshalAccount
7070 }
7171
72- storeBatch.Set(database.AccountIDKey(slice.Account.ID), rawAccount)
73- storeBatch.Set(database.AccountAliasKey(slice.Account.Alias), []byte(slice.Account.ID))
72+ m.store.SetAccount(slice.Account.ID, slice.Account.Alias, rawAccount)
7473 }
7574
76- storeBatch.Write()
7775 return nil
7876 }
--- a/account/indexer.go
+++ b/account/indexer.go
@@ -2,28 +2,8 @@ package account
22
33 import (
44 "github.com/vapor/blockchain/query"
5- "github.com/vapor/protocol/bc"
65 )
76
8-const (
9- //UTXOPreFix is StandardUTXOKey prefix
10- UTXOPreFix = "ACU:"
11- //SUTXOPrefix is ContractUTXOKey prefix
12- SUTXOPrefix = "SCU:"
13-)
14-
15-// StandardUTXOKey makes an account unspent outputs key to store
16-func StandardUTXOKey(id bc.Hash) []byte {
17- name := id.String()
18- return []byte(UTXOPreFix + name)
19-}
20-
21-// ContractUTXOKey makes a smart contract unspent outputs key to store
22-func ContractUTXOKey(id bc.Hash) []byte {
23- name := id.String()
24- return []byte(SUTXOPrefix + name)
25-}
26-
277 //Annotated init an annotated account object
288 func Annotated(a *Account) *query.AnnotatedAccount {
299 return &query.AnnotatedAccount{
--- a/account/utxo_keeper.go
+++ b/account/utxo_keeper.go
@@ -11,7 +11,7 @@ import (
1111
1212 log "github.com/sirupsen/logrus"
1313
14- dbm "github.com/vapor/database/leveldb"
14+ "github.com/vapor/database"
1515 "github.com/vapor/errors"
1616 "github.com/vapor/protocol/bc"
1717 )
@@ -56,7 +56,7 @@ type utxoKeeper struct {
5656 // `sync/atomic` expects the first word in an allocated struct to be 64-bit
5757 // aligned on both ARM and x86-32. See https://goo.gl/zW7dgq for more details.
5858 nextIndex uint64
59- db dbm.DB
59+ store database.AccountStorer
6060 mtx sync.RWMutex
6161 currentHeight func() uint64
6262
@@ -65,9 +65,9 @@ type utxoKeeper struct {
6565 reservations map[uint64]*reservation
6666 }
6767
68-func newUtxoKeeper(f func() uint64, walletdb dbm.DB) *utxoKeeper {
68+func newUtxoKeeper(f func() uint64, store database.AccountStorer) *utxoKeeper {
6969 uk := &utxoKeeper{
70- db: walletdb,
70+ store: store,
7171 currentHeight: f,
7272 unconfirmed: make(map[bc.Hash]*UTXO),
7373 reserved: make(map[bc.Hash]uint64),
@@ -223,16 +223,16 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
223223 }
224224 }
225225
226- utxoIter := uk.db.IteratorPrefix([]byte(UTXOPreFix))
227- defer utxoIter.Release()
228- for utxoIter.Next() {
229- u := &UTXO{}
230- if err := json.Unmarshal(utxoIter.Value(), u); err != nil {
226+ rawUTXOs := uk.store.GetUTXOs()
227+ for _, rawUTXO := range rawUTXOs {
228+ utxo := new(UTXO)
229+ if err := json.Unmarshal(rawUTXO, utxo); err != nil {
231230 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
232231 continue
233232 }
234- appendUtxo(u)
233+ appendUtxo(utxo)
235234 }
235+
236236 if !useUnconfirmed {
237237 return utxos, immatureAmount
238238 }
@@ -249,10 +249,10 @@ func (uk *utxoKeeper) findUtxo(outHash bc.Hash, useUnconfirmed bool) (*UTXO, err
249249 }
250250
251251 u := &UTXO{}
252- if data := uk.db.Get(StandardUTXOKey(outHash)); data != nil {
252+ if data := uk.store.GetStandardUTXO(outHash); data != nil {
253253 return u, json.Unmarshal(data, u)
254254 }
255- if data := uk.db.Get(ContractUTXOKey(outHash)); data != nil {
255+ if data := uk.store.GetContractUTXO(outHash); data != nil {
256256 return u, json.Unmarshal(data, u)
257257 }
258258 return nil, ErrMatchUTXO
--- a/account/utxo_keeper_test.go
+++ b/account/utxo_keeper_test.go
@@ -6,6 +6,8 @@ import (
66 "testing"
77 "time"
88
9+ "github.com/vapor/database"
10+
911 dbm "github.com/vapor/database/leveldb"
1012 "github.com/vapor/protocol/bc"
1113 "github.com/vapor/testutil"
@@ -288,13 +290,13 @@ func TestReserve(t *testing.T) {
288290 }{
289291 {
290292 before: utxoKeeper{
291- db: testDB,
293+ store: database.NewAccountStore(testDB),
292294 currentHeight: currentHeight,
293295 reserved: map[bc.Hash]uint64{},
294296 reservations: map[uint64]*reservation{},
295297 },
296298 after: utxoKeeper{
297- db: testDB,
299+ store: database.NewAccountStore(testDB),
298300 currentHeight: currentHeight,
299301 reserved: map[bc.Hash]uint64{},
300302 reservations: map[uint64]*reservation{},
@@ -304,7 +306,7 @@ func TestReserve(t *testing.T) {
304306 },
305307 {
306308 before: utxoKeeper{
307- db: testDB,
309+ store: database.NewAccountStore(testDB),
308310 currentHeight: currentHeight,
309311 unconfirmed: map[bc.Hash]*UTXO{
310312 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -317,7 +319,7 @@ func TestReserve(t *testing.T) {
317319 reservations: map[uint64]*reservation{},
318320 },
319321 after: utxoKeeper{
320- db: testDB,
322+ store: database.NewAccountStore(testDB),
321323 currentHeight: currentHeight,
322324 unconfirmed: map[bc.Hash]*UTXO{
323325 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -334,7 +336,7 @@ func TestReserve(t *testing.T) {
334336 },
335337 {
336338 before: utxoKeeper{
337- db: testDB,
339+ store: database.NewAccountStore(testDB),
338340 currentHeight: currentHeight,
339341 unconfirmed: map[bc.Hash]*UTXO{
340342 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -348,7 +350,7 @@ func TestReserve(t *testing.T) {
348350 reservations: map[uint64]*reservation{},
349351 },
350352 after: utxoKeeper{
351- db: testDB,
353+ store: database.NewAccountStore(testDB),
352354 currentHeight: currentHeight,
353355 unconfirmed: map[bc.Hash]*UTXO{
354356 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -366,7 +368,7 @@ func TestReserve(t *testing.T) {
366368 },
367369 {
368370 before: utxoKeeper{
369- db: testDB,
371+ store: database.NewAccountStore(testDB),
370372 currentHeight: currentHeight,
371373 unconfirmed: map[bc.Hash]*UTXO{
372374 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -381,7 +383,7 @@ func TestReserve(t *testing.T) {
381383 reservations: map[uint64]*reservation{},
382384 },
383385 after: utxoKeeper{
384- db: testDB,
386+ store: database.NewAccountStore(testDB),
385387 currentHeight: currentHeight,
386388 unconfirmed: map[bc.Hash]*UTXO{
387389 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -400,7 +402,7 @@ func TestReserve(t *testing.T) {
400402 },
401403 {
402404 before: utxoKeeper{
403- db: testDB,
405+ store: database.NewAccountStore(testDB),
404406 currentHeight: currentHeight,
405407 unconfirmed: map[bc.Hash]*UTXO{
406408 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -413,7 +415,7 @@ func TestReserve(t *testing.T) {
413415 reservations: map[uint64]*reservation{},
414416 },
415417 after: utxoKeeper{
416- db: testDB,
418+ store: database.NewAccountStore(testDB),
417419 currentHeight: currentHeight,
418420 unconfirmed: map[bc.Hash]*UTXO{
419421 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -446,7 +448,7 @@ func TestReserve(t *testing.T) {
446448 },
447449 {
448450 before: utxoKeeper{
449- db: testDB,
451+ store: database.NewAccountStore(testDB),
450452 currentHeight: currentHeight,
451453 nextIndex: 1,
452454 unconfirmed: map[bc.Hash]*UTXO{
@@ -472,7 +474,7 @@ func TestReserve(t *testing.T) {
472474 reservations: map[uint64]*reservation{},
473475 },
474476 after: utxoKeeper{
475- db: testDB,
477+ store: database.NewAccountStore(testDB),
476478 currentHeight: currentHeight,
477479 unconfirmed: map[bc.Hash]*UTXO{
478480 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -522,7 +524,7 @@ func TestReserve(t *testing.T) {
522524 },
523525 {
524526 before: utxoKeeper{
525- db: testDB,
527+ store: database.NewAccountStore(testDB),
526528 currentHeight: currentHeight,
527529 unconfirmed: map[bc.Hash]*UTXO{
528530 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -536,7 +538,7 @@ func TestReserve(t *testing.T) {
536538 reservations: map[uint64]*reservation{},
537539 },
538540 after: utxoKeeper{
539- db: testDB,
541+ store: database.NewAccountStore(testDB),
540542 currentHeight: currentHeight,
541543 unconfirmed: map[bc.Hash]*UTXO{
542544 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -594,7 +596,7 @@ func TestReserveParticular(t *testing.T) {
594596 }{
595597 {
596598 before: utxoKeeper{
597- db: testDB,
599+ store: database.NewAccountStore(testDB),
598600 currentHeight: currentHeight,
599601 unconfirmed: map[bc.Hash]*UTXO{
600602 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -609,7 +611,7 @@ func TestReserveParticular(t *testing.T) {
609611 reservations: map[uint64]*reservation{},
610612 },
611613 after: utxoKeeper{
612- db: testDB,
614+ store: database.NewAccountStore(testDB),
613615 currentHeight: currentHeight,
614616 unconfirmed: map[bc.Hash]*UTXO{
615617 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -628,7 +630,7 @@ func TestReserveParticular(t *testing.T) {
628630 },
629631 {
630632 before: utxoKeeper{
631- db: testDB,
633+ store: database.NewAccountStore(testDB),
632634 currentHeight: currentHeight,
633635 unconfirmed: map[bc.Hash]*UTXO{
634636 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -642,7 +644,7 @@ func TestReserveParticular(t *testing.T) {
642644 reservations: map[uint64]*reservation{},
643645 },
644646 after: utxoKeeper{
645- db: testDB,
647+ store: database.NewAccountStore(testDB),
646648 currentHeight: currentHeight,
647649 unconfirmed: map[bc.Hash]*UTXO{
648650 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -660,7 +662,7 @@ func TestReserveParticular(t *testing.T) {
660662 },
661663 {
662664 before: utxoKeeper{
663- db: testDB,
665+ store: database.NewAccountStore(testDB),
664666 currentHeight: currentHeight,
665667 unconfirmed: map[bc.Hash]*UTXO{
666668 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -673,7 +675,7 @@ func TestReserveParticular(t *testing.T) {
673675 reservations: map[uint64]*reservation{},
674676 },
675677 after: utxoKeeper{
676- db: testDB,
678+ store: database.NewAccountStore(testDB),
677679 currentHeight: currentHeight,
678680 unconfirmed: map[bc.Hash]*UTXO{
679681 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -753,7 +755,7 @@ func TestFindUtxos(t *testing.T) {
753755 }{
754756 {
755757 uk: utxoKeeper{
756- db: testDB,
758+ store: database.NewAccountStore(testDB),
757759 currentHeight: currentHeight,
758760 unconfirmed: map[bc.Hash]*UTXO{},
759761 },
@@ -764,7 +766,7 @@ func TestFindUtxos(t *testing.T) {
764766 },
765767 {
766768 uk: utxoKeeper{
767- db: testDB,
769+ store: database.NewAccountStore(testDB),
768770 currentHeight: currentHeight,
769771 unconfirmed: map[bc.Hash]*UTXO{},
770772 },
@@ -793,7 +795,7 @@ func TestFindUtxos(t *testing.T) {
793795 },
794796 {
795797 uk: utxoKeeper{
796- db: testDB,
798+ store: database.NewAccountStore(testDB),
797799 currentHeight: currentHeight,
798800 unconfirmed: map[bc.Hash]*UTXO{},
799801 },
@@ -811,7 +813,7 @@ func TestFindUtxos(t *testing.T) {
811813 },
812814 {
813815 uk: utxoKeeper{
814- db: testDB,
816+ store: database.NewAccountStore(testDB),
815817 currentHeight: currentHeight,
816818 unconfirmed: map[bc.Hash]*UTXO{
817819 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -840,7 +842,7 @@ func TestFindUtxos(t *testing.T) {
840842 },
841843 {
842844 uk: utxoKeeper{
843- db: testDB,
845+ store: database.NewAccountStore(testDB),
844846 currentHeight: currentHeight,
845847 unconfirmed: map[bc.Hash]*UTXO{
846848 bc.NewHash([32]byte{0x11}): &UTXO{
@@ -874,7 +876,7 @@ func TestFindUtxos(t *testing.T) {
874876 },
875877 {
876878 uk: utxoKeeper{
877- db: testDB,
879+ store: database.NewAccountStore(testDB),
878880 currentHeight: currentHeight,
879881 unconfirmed: map[bc.Hash]*UTXO{
880882 bc.NewHash([32]byte{0x01}): &UTXO{
@@ -918,7 +920,7 @@ func TestFindUtxos(t *testing.T) {
918920 },
919921 {
920922 uk: utxoKeeper{
921- db: testDB,
923+ store: database.NewAccountStore(testDB),
922924 currentHeight: currentHeight,
923925 unconfirmed: map[bc.Hash]*UTXO{},
924926 },
@@ -950,7 +952,7 @@ func TestFindUtxos(t *testing.T) {
950952 if err != nil {
951953 t.Error(err)
952954 }
953- testDB.Set(StandardUTXOKey(u.OutputID), data)
955+ testDB.Set(database.StandardUTXOKey(u.OutputID), data)
954956 }
955957
956958 gotUtxos, immatureAmount := c.uk.findUtxos("testAccount", &bc.AssetID{}, c.useUnconfirmed, c.vote)
@@ -962,7 +964,7 @@ func TestFindUtxos(t *testing.T) {
962964 }
963965
964966 for _, u := range c.dbUtxos {
965- testDB.Delete(StandardUTXOKey(u.OutputID))
967+ testDB.Delete(database.StandardUTXOKey(u.OutputID))
966968 }
967969 }
968970 }
@@ -982,7 +984,7 @@ func TestFindUtxo(t *testing.T) {
982984 }{
983985 {
984986 uk: utxoKeeper{
985- db: testDB,
987+ store: database.NewAccountStore(testDB),
986988 currentHeight: currentHeight,
987989 unconfirmed: map[bc.Hash]*UTXO{},
988990 },
@@ -993,7 +995,7 @@ func TestFindUtxo(t *testing.T) {
993995 },
994996 {
995997 uk: utxoKeeper{
996- db: testDB,
998+ store: database.NewAccountStore(testDB),
997999 currentHeight: currentHeight,
9981000 unconfirmed: map[bc.Hash]*UTXO{
9991001 bc.NewHash([32]byte{0x01}): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
@@ -1007,7 +1009,7 @@ func TestFindUtxo(t *testing.T) {
10071009 },
10081010 {
10091011 uk: utxoKeeper{
1010- db: testDB,
1012+ store: database.NewAccountStore(testDB),
10111013 currentHeight: currentHeight,
10121014 unconfirmed: map[bc.Hash]*UTXO{
10131015 bc.NewHash([32]byte{0x01}): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
@@ -1021,12 +1023,12 @@ func TestFindUtxo(t *testing.T) {
10211023 },
10221024 {
10231025 uk: utxoKeeper{
1024- db: testDB,
1026+ store: database.NewAccountStore(testDB),
10251027 currentHeight: currentHeight,
10261028 unconfirmed: map[bc.Hash]*UTXO{},
10271029 },
10281030 dbUtxos: map[string]*UTXO{
1029- string(StandardUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
1031+ string(database.StandardUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
10301032 },
10311033 outHash: bc.NewHash([32]byte{0x01}),
10321034 wantUtxo: &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
@@ -1035,12 +1037,12 @@ func TestFindUtxo(t *testing.T) {
10351037 },
10361038 {
10371039 uk: utxoKeeper{
1038- db: testDB,
1040+ store: database.NewAccountStore(testDB),
10391041 currentHeight: currentHeight,
10401042 unconfirmed: map[bc.Hash]*UTXO{},
10411043 },
10421044 dbUtxos: map[string]*UTXO{
1043- string(ContractUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
1045+ string(database.ContractUTXOKey(bc.NewHash([32]byte{0x01}))): &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
10441046 },
10451047 outHash: bc.NewHash([32]byte{0x01}),
10461048 wantUtxo: &UTXO{OutputID: bc.NewHash([32]byte{0x01})},
@@ -1067,7 +1069,7 @@ func TestFindUtxo(t *testing.T) {
10671069 }
10681070
10691071 for _, u := range c.dbUtxos {
1070- testDB.Delete(StandardUTXOKey(u.OutputID))
1072+ testDB.Delete(database.StandardUTXOKey(u.OutputID))
10711073 }
10721074 }
10731075 }
--- a/database/account_store.go
+++ b/database/account_store.go
@@ -11,10 +11,12 @@ import (
1111
1212 // AccountStorer interface contains account storage functions.
1313 type AccountStorer interface {
14+ InitBatch()
15+ CommitBatch()
1416 SetAccount(string, string, []byte)
1517 SetAccountIndex([]chainkd.XPub, uint64)
1618 GetAccountByAccountAlias(string) []byte
17- GetAccountByAccountID(string) []byte // duplicate in WalletStorer
19+ GetAccountByAccountID(string) []byte
1820 GetAccountIndex([]chainkd.XPub) []byte
1921 DeleteAccountByAccountAlias(string)
2022 DeleteAccountByAccountID(string)
@@ -23,21 +25,24 @@ type AccountStorer interface {
2325 DeleteContractIndex(string)
2426 GetContractIndex(string) []byte
2527 // DeleteAccountUTXOs(string) error
28+ GetAccountUTXOs(string) [][]byte
29+ DeleteUTXO(bc.Hash)
2630 GetCoinbaseArbitrary() []byte
2731 SetCoinbaseArbitrary([]byte)
2832 GetMiningAddress() []byte
2933 GetFirstAccount() ([]byte, error)
3034 SetMiningAddress([]byte)
3135 GetBip44ContractIndex(string, bool) []byte
32- GetRawProgram(common.Hash) []byte // duplicate in WalletStorer
33- GetAccounts(string) ([][]byte, error)
36+ GetRawProgram(common.Hash) []byte
37+ GetAccounts(string) [][]byte
3438 GetControlPrograms() ([][]byte, error)
3539 SetRawProgram(common.Hash, []byte)
3640 SetContractIndex(string, uint64)
3741 SetBip44ContractIndex(string, bool, uint64)
38- GetUTXOs(string) [][]byte
39- GetStandardUTXO(bc.Hash) []byte // duplicate in WalletStorer
42+ GetUTXOs() [][]byte
43+ GetStandardUTXO(bc.Hash) []byte
4044 GetContractUTXO(bc.Hash) []byte
45+ SetStandardUTXO(bc.Hash, []byte)
4146 }
4247
4348 // AccountStore satisfies AccountStorer interface.
@@ -54,17 +59,54 @@ func NewAccountStore(db dbm.DB) *AccountStore {
5459 }
5560 }
5661
62+// InitBatch initial batch
63+func (store *AccountStore) InitBatch() {
64+ if store.batch == nil {
65+ store.batch = store.accountDB.NewBatch()
66+ }
67+}
68+
69+// CommitBatch commit batch
70+func (store *AccountStore) CommitBatch() {
71+ if store.batch != nil {
72+ store.batch.Write()
73+ store.batch = nil
74+ }
75+}
76+
5777 // SetAccount set account account ID, account alias and raw account.
5878 func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
5979 batch := store.accountDB.NewBatch()
80+ if store.batch != nil {
81+ batch = store.batch
82+ }
6083 batch.Set(AccountIDKey(accountID), rawAccount)
6184 batch.Set(AccountAliasKey(accountAlias), []byte(accountID))
62- batch.Write()
85+ if store.batch == nil {
86+ batch.Write()
87+ }
88+}
89+
90+// DeleteAccount set account account ID, account alias and raw account.
91+func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
92+ batch := store.accountDB.NewBatch()
93+ if store.batch != nil {
94+ batch = store.batch
95+ }
96+ batch.Delete(AccountIDKey(accountID))
97+ batch.Delete(AccountAliasKey(accountAlias))
98+ if store.batch == nil {
99+ batch.Write()
100+ }
63101 }
64102
65103 // SetAccountIndex set account index
66104 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
67- store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
105+ if store.batch == nil {
106+ store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
107+ } else {
108+ store.batch.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
109+ }
68110 }
69111
70112 // GetAccountByAccountAlias get account by account alias
@@ -84,30 +126,51 @@ func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
84126
85127 // DeleteAccountByAccountAlias delete account by account alias
86128 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
87- store.accountDB.Delete(AccountAliasKey(accountAlias))
129+ if store.batch == nil {
130+ store.accountDB.Delete(AccountAliasKey(accountAlias))
131+ } else {
132+ store.batch.Delete(AccountAliasKey(accountAlias))
133+ }
88134 }
89135
90136 // DeleteAccountByAccountID delete account by accountID
91137 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
92- store.accountDB.Delete(AccountIDKey(accountID))
138+ if store.batch == nil {
139+ store.accountDB.Delete(AccountIDKey(accountID))
140+ } else {
141+ store.batch.Delete(AccountIDKey(accountID))
142+ }
93143 }
94144
95145 // DeleteRawProgram delete raw control program by hash
96146 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
97- store.accountDB.Delete(ContractKey(hash))
147+ if store.batch == nil {
148+ store.accountDB.Delete(ContractKey(hash))
149+ } else {
150+ store.batch.Delete(ContractKey(hash))
151+ }
98152 }
99153
100154 // DeleteBip44ContractIndex delete bip44 contract index by accountID
101155 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
102156 batch := store.accountDB.NewBatch()
157+ if store.batch != nil {
158+ batch = store.batch
159+ }
103160 batch.Delete(Bip44ContractIndexKey(accountID, false))
104161 batch.Delete(Bip44ContractIndexKey(accountID, true))
105- batch.Write()
162+ if store.batch == nil {
163+ batch.Write()
164+ }
106165 }
107166
108167 // DeleteContractIndex delete contract index by accountID
109168 func (store *AccountStore) DeleteContractIndex(accountID string) {
110- store.accountDB.Delete(ContractIndexKey(accountID))
169+ if store.batch == nil {
170+ store.accountDB.Delete(ContractIndexKey(accountID))
171+ } else {
172+ store.batch.Delete(ContractIndexKey(accountID))
173+ }
111174 }
112175
113176 // GetContractIndex get contract index
@@ -115,22 +178,26 @@ func (store *AccountStore) GetContractIndex(accountID string) []byte {
115178 return store.accountDB.Get(ContractIndexKey(accountID))
116179 }
117180
118-// // DeleteAccountUTXOs delete account utxos by accountID
119-// func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
120-// accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
121-// defer accountUtxoIter.Release()
122-// for accountUtxoIter.Next() {
123-// accountUtxo := &UTXO{}
124-// if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
125-// return err
126-// }
181+// GetAccountUTXOs get account utxos by account id
182+func (store *AccountStore) GetAccountUTXOs(accountID string) [][]byte {
183+ accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
184+ defer accountUtxoIter.Release()
185+
186+ utxos := make([][]byte, 0)
187+ for accountUtxoIter.Next() {
188+ utxos = append(utxos, accountUtxoIter.Value())
189+ }
190+ return utxos
191+}
127192
128-// if accountID == accountUtxo.AccountID {
129-// store.accountDB.Delete(StandardUTXOKey(accountUtxo.OutputID))
130-// }
131-// }
132-// return nil
133-// }
193+// DeleteUTXO delete utxo by outpu id
194+func (store *AccountStore) DeleteUTXO(outputID bc.Hash) {
195+ if store.batch == nil {
196+ store.accountDB.Delete(StandardUTXOKey(outputID))
197+ } else {
198+ store.batch.Delete(StandardUTXOKey(outputID))
199+ }
200+}
134201
135202 // GetCoinbaseArbitrary get coinbase arbitrary
136203 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
@@ -139,7 +206,11 @@ func (store *AccountStore) GetCoinbaseArbitrary() []byte {
139206
140207 // SetCoinbaseArbitrary set coinbase arbitrary
141208 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
142- store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
209+ if store.batch == nil {
210+ store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
211+ } else {
212+ store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
213+ }
143214 }
144215
145216 // GetMiningAddress get mining address
@@ -151,6 +222,7 @@ func (store *AccountStore) GetMiningAddress() []byte {
151222 func (store *AccountStore) GetFirstAccount() ([]byte, error) {
152223 accountIter := store.accountDB.IteratorPrefix([]byte(AccountPrefix))
153224 defer accountIter.Release()
225+
154226 if !accountIter.Next() {
155227 return nil, ErrFindAccount
156228 }
@@ -159,7 +231,11 @@ func (store *AccountStore) GetFirstAccount() ([]byte, error) {
159231
160232 // SetMiningAddress set mining address
161233 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
162- store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
234+ if store.batch == nil {
235+ store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
236+ } else {
237+ store.batch.Set([]byte(MiningAddressKey), rawProgram)
238+ }
163239 }
164240
165241 // GetBip44ContractIndex get bip44 contract index
@@ -173,23 +249,23 @@ func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
173249 }
174250
175251 // GetAccounts get all accounts which name prfix is id.
176-func (store *AccountStore) GetAccounts(id string) ([][]byte, error) {
177- accounts := make([][]byte, 0)
252+func (store *AccountStore) GetAccounts(id string) [][]byte {
178253 accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
179254 defer accountIter.Release()
180255
256+ accounts := make([][]byte, 0)
181257 for accountIter.Next() {
182258 accounts = append(accounts, accountIter.Value())
183259 }
184- return accounts, nil
260+ return accounts
185261 }
186262
187263 // GetControlPrograms get all local control programs
188264 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
189- cps := make([][]byte, 0)
190265 cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
191266 defer cpIter.Release()
192267
268+ cps := make([][]byte, 0)
193269 for cpIter.Next() {
194270 cps = append(cps, cpIter.Value())
195271 }
@@ -198,25 +274,37 @@ func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
198274
199275 // SetRawProgram set raw program
200276 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
201- store.accountDB.Set(ContractKey(hash), program)
277+ if store.batch == nil {
278+ store.accountDB.Set(ContractKey(hash), program)
279+ } else {
280+ store.batch.Set(ContractKey(hash), program)
281+ }
202282 }
203283
204284 // SetContractIndex set contract index
205285 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
206- store.accountDB.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
286+ if store.batch == nil {
287+ store.accountDB.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
288+ } else {
289+ store.batch.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
290+ }
207291 }
208292
209293 // SetBip44ContractIndex set contract index
210294 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
211- store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
295+ if store.batch == nil {
296+ store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
297+ } else {
298+ store.batch.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
299+ }
212300 }
213301
214302 // GetUTXOs get utxos by accountID
215-func (store *AccountStore) GetUTXOs(accountID string) [][]byte {
216- utxos := make([][]byte, 0)
303+func (store *AccountStore) GetUTXOs() [][]byte {
217304 utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
218305 defer utxoIter.Release()
219306
307+ utxos := make([][]byte, 0)
220308 for utxoIter.Next() {
221309 utxos = append(utxos, utxoIter.Value())
222310 }
@@ -232,3 +320,12 @@ func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
232320 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
233321 return store.accountDB.Get(ContractUTXOKey(outid))
234322 }
323+
324+// SetStandardUTXO set standard utxo
325+func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, data []byte) {
326+ if store.batch == nil {
327+ store.accountDB.Set(StandardUTXOKey(outputID), data)
328+ } else {
329+ store.batch.Set(StandardUTXOKey(outputID), data)
330+ }
331+}
--- a/wallet/utxo.go
+++ b/wallet/utxo.go
@@ -9,6 +9,7 @@ import (
99 "github.com/vapor/consensus"
1010 "github.com/vapor/consensus/segwit"
1111 "github.com/vapor/crypto/sha3pool"
12+ "github.com/vapor/database"
1213 "github.com/vapor/errors"
1314 "github.com/vapor/protocol/bc"
1415 "github.com/vapor/protocol/bc/types"
@@ -16,9 +17,9 @@ import (
1617
1718 // GetAccountUtxos return all account unspent outputs
1819 func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
19- prefix := account.UTXOPreFix
20+ prefix := database.UTXOPrefix
2021 if isSmartContract {
21- prefix = account.SUTXOPrefix
22+ prefix = database.SUTXOPrefix
2223 }
2324
2425 accountUtxos := []*account.UTXO{}
--- a/wallet/utxo_test.go
+++ b/wallet/utxo_test.go
@@ -42,16 +42,16 @@ func TestGetAccountUtxos(t *testing.T) {
4242 },
4343 {
4444 dbUtxos: map[string]*account.UTXO{
45- string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
45+ string(database.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
4646 OutputID: bc.Hash{V0: 1},
4747 },
48- string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
48+ string(database.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
4949 OutputID: bc.Hash{V0: 2},
5050 },
51- string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
51+ string(database.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
5252 OutputID: bc.Hash{V0: 3},
5353 },
54- string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
54+ string(database.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
5555 OutputID: bc.Hash{V0: 4},
5656 },
5757 },
@@ -66,16 +66,16 @@ func TestGetAccountUtxos(t *testing.T) {
6666 },
6767 {
6868 dbUtxos: map[string]*account.UTXO{
69- string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
69+ string(database.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
7070 OutputID: bc.Hash{V0: 1},
7171 },
72- string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
72+ string(database.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
7373 OutputID: bc.Hash{V0: 2},
7474 },
75- string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
75+ string(database.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
7676 OutputID: bc.Hash{V0: 3},
7777 },
78- string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
78+ string(database.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
7979 OutputID: bc.Hash{V0: 4},
8080 },
8181 },
@@ -94,16 +94,16 @@ func TestGetAccountUtxos(t *testing.T) {
9494 },
9595 {
9696 dbUtxos: map[string]*account.UTXO{
97- string(account.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
97+ string(database.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
9898 OutputID: bc.Hash{V0: 1},
9999 },
100- string(account.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
100+ string(database.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
101101 OutputID: bc.Hash{V0: 1, V1: 2},
102102 },
103- string(account.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
103+ string(database.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
104104 OutputID: bc.Hash{V0: 2},
105105 },
106- string(account.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
106+ string(database.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
107107 OutputID: bc.Hash{V0: 2, V1: 2},
108108 },
109109 },
@@ -123,10 +123,10 @@ func TestGetAccountUtxos(t *testing.T) {
123123 },
124124 {
125125 dbUtxos: map[string]*account.UTXO{
126- string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
126+ string(database.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
127127 OutputID: bc.Hash{V0: 3},
128128 },
129- string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
129+ string(database.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
130130 OutputID: bc.Hash{V0: 4},
131131 },
132132 },
@@ -155,10 +155,10 @@ func TestGetAccountUtxos(t *testing.T) {
155155 },
156156 {
157157 dbUtxos: map[string]*account.UTXO{
158- string(account.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
158+ string(database.StandardUTXOKey(bc.Hash{V0: 3})): &account.UTXO{
159159 OutputID: bc.Hash{V0: 3},
160160 },
161- string(account.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
161+ string(database.ContractUTXOKey(bc.Hash{V0: 4})): &account.UTXO{
162162 OutputID: bc.Hash{V0: 4},
163163 },
164164 },
Show on old repository browser