OSDN Git Service

add some interface functions
[bytom/vapor.git] / wallet / store.go
1 package wallet
2
3 import (
4         "encoding/json"
5
6         "github.com/vapor/account"
7         "github.com/vapor/asset"
8         "github.com/vapor/blockchain/query"
9         "github.com/vapor/common"
10         dbm "github.com/vapor/database/leveldb"
11         "github.com/vapor/protocol/bc"
12         "github.com/vapor/protocol/bc/types"
13 )
14
15 // DB interface contains wallet storage functions.
16 type DB interface {
17         GetAssetDefinitionByAssetID(*bc.AssetID) []byte
18         SetAssetDefinition(*bc.AssetID, []byte)
19         GetRawProgramByAccountHash(common.Hash) []byte
20         GetAccountValueByAccountID(string) []byte
21         DeleteTransactionByHeight(uint64)
22         SetRawTransaction(uint64, uint32, []byte)
23         SaveExternalAssetDefinition(*types.Block)
24         SetHeightAndPostion(string, uint64, uint32)
25         DeleteUnconfirmedTxByTxID(string)
26         SetGlobalTxIndex(string, *bc.Hash, uint64)
27         GetStandardUTXOByID(bc.Hash) []byte
28 }
29
30 // LevelDBStore store wallet using leveldb
31 type LevelDBStore struct {
32         DB dbm.DB
33 }
34
35 // NewLevelDBStore create new LevelDBStore struct
36 func NewLevelDBStore(db dbm.DB) *LevelDBStore {
37         return &LevelDBStore{
38                 DB: db,
39         }
40 }
41
42 // GetAssetDefinitionByAssetID get asset definition by assetiD
43 func (store *LevelDBStore) GetAssetDefinitionByAssetID(assetID *bc.AssetID) []byte {
44         return store.DB.Get(asset.ExtAssetKey(assetID))
45 }
46
47 // SetAssetDefinition set assetID and definition
48 func (store *LevelDBStore) SetAssetDefinition(assetID *bc.AssetID, definition []byte) {
49         batch := store.DB.NewBatch()
50         batch.Set(asset.ExtAssetKey(assetID), definition)
51 }
52
53 // GetRawProgramByAccountHash get raw program by account hash
54 func (store *LevelDBStore) GetRawProgramByAccountHash(hash common.Hash) []byte {
55         return store.DB.Get(account.ContractKey(hash))
56 }
57
58 // GetAccountValueByAccountID get account value by account ID
59 func (store *LevelDBStore) GetAccountValueByAccountID(accountID string) []byte {
60         return store.DB.Get(account.Key(accountID))
61 }
62
63 // DeleteTransactionByHeight delete transactions when orphan block rollback
64 func (store *LevelDBStore) DeleteTransactionByHeight(height uint64) {
65         tmpTx := query.AnnotatedTx{}
66         batch := store.DB.NewBatch()
67         txIter := store.DB.IteratorPrefix(calcDeleteKey(height))
68         defer txIter.Release()
69
70         for txIter.Next() {
71                 if err := json.Unmarshal(txIter.Value(), &tmpTx); err == nil {
72                         batch.Delete(calcTxIndexKey(tmpTx.ID.String()))
73                 }
74                 batch.Delete(txIter.Key())
75         }
76 }
77
78 // SetRawTransaction set raw transaction by block height and tx position
79 func (store *LevelDBStore) SetRawTransaction(height uint64, position uint32, rawTx []byte) {
80         batch := store.DB.NewBatch()
81         batch.Set(calcAnnotatedKey(formatKey(height, position)), rawTx)
82 }
83
84 // SetHeightAndPostion set block height and tx position according to tx ID
85 func (store *LevelDBStore) SetHeightAndPostion(txID string, height uint64, position uint32) {
86         batch := store.DB.NewBatch()
87         batch.Set(calcTxIndexKey(txID), []byte(formatKey(height, position)))
88 }
89
90 // DeleteUnconfirmedTxByTxID delete unconfirmed tx by txID
91 func (store *LevelDBStore) DeleteUnconfirmedTxByTxID(txID string) {
92         batch := store.DB.NewBatch()
93         batch.Delete(calcUnconfirmedTxKey(txID))
94 }
95
96 // SetGlobalTxIndex set global tx index by blockhash and position
97 func (store *LevelDBStore) SetGlobalTxIndex(globalTxID string, blockHash *bc.Hash, position uint64) {
98         batch := store.DB.NewBatch()
99         batch.Set(calcGlobalTxIndexKey(globalTxID), calcGlobalTxIndex(blockHash, position))
100 }
101
102 // GetStandardUTXOByID get standard utxo by id
103 func (store *LevelDBStore) GetStandardUTXOByID(outid bc.Hash) []byte {
104         return store.DB.Get(account.StandardUTXOKey(outid))
105 }