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"
15 // DB interface contains wallet storage functions.
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
30 // LevelDBStore store wallet using leveldb
31 type LevelDBStore struct {
35 // NewLevelDBStore create new LevelDBStore struct
36 func NewLevelDBStore(db dbm.DB) *LevelDBStore {
42 // GetAssetDefinitionByAssetID get asset definition by assetiD
43 func (store *LevelDBStore) GetAssetDefinitionByAssetID(assetID *bc.AssetID) []byte {
44 return store.DB.Get(asset.ExtAssetKey(assetID))
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)
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))
58 // GetAccountValueByAccountID get account value by account ID
59 func (store *LevelDBStore) GetAccountValueByAccountID(accountID string) []byte {
60 return store.DB.Get(account.Key(accountID))
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()
71 if err := json.Unmarshal(txIter.Value(), &tmpTx); err == nil {
72 batch.Delete(calcTxIndexKey(tmpTx.ID.String()))
74 batch.Delete(txIter.Key())
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)
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)))
90 // DeleteUnconfirmedTxByTxID delete unconfirmed tx by txID
91 func (store *LevelDBStore) DeleteUnconfirmedTxByTxID(txID string) {
92 batch := store.DB.NewBatch()
93 batch.Delete(calcUnconfirmedTxKey(txID))
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))
102 // GetStandardUTXOByID get standard utxo by id
103 func (store *LevelDBStore) GetStandardUTXOByID(outid bc.Hash) []byte {
104 return store.DB.Get(account.StandardUTXOKey(outid))