• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision2441eb401c9b935f382adde453455564c0fa08d5 (tree)
Time2019-06-24 03:09:01
AuthorChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Log Message

move wallet_store.go

Change Summary

Incremental Difference

--- a/wallet/wallet_store.go
+++ b/database/wallet_store.go
@@ -1,20 +1,30 @@
1-package wallet
1+package database
22
33 import (
4+ "encoding/binary"
45 "encoding/json"
6+ "fmt"
57
68 "github.com/vapor/asset"
79 "github.com/vapor/blockchain/query"
810 "github.com/vapor/common"
911 dbm "github.com/vapor/database/leveldb"
12+ "github.com/vapor/errors"
1013 "github.com/vapor/protocol/bc"
1114 )
1215
16+var errAccntTxIDNotFound = errors.New("account TXID not found")
17+
1318 const (
14- UTXOPrefix = "ACU:" //UTXOPrefix is StandardUTXOKey prefix
15- SUTXOPrefix = "SCU:" //SUTXOPrefix is ContractUTXOKey prefix
16- contractPrefix = "Contract:"
17- accountPrefix = "Account:"
19+ UTXOPrefix = "ACU:" //UTXOPrefix is StandardUTXOKey prefix
20+ SUTXOPrefix = "SCU:" //SUTXOPrefix is ContractUTXOKey prefix
21+ contractPrefix = "Contract:"
22+ accountPrefix = "Account:"
23+ TxPrefix = "TXS:" //TxPrefix is wallet database transactions prefix
24+ TxIndexPrefix = "TID:" //TxIndexPrefix is wallet database tx index prefix
25+ UnconfirmedTxPrefix = "UTXS:" //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
26+ GlobalTxIndexPrefix = "GTID:" //GlobalTxIndexPrefix is wallet database global tx index prefix
27+ walletKey = "walletInfo"
1828 )
1929
2030 // WalletStorer interface contains wallet storage functions.
@@ -82,6 +92,37 @@ func ContractUTXOKey(id bc.Hash) []byte {
8292 return []byte(SUTXOPrefix + name)
8393 }
8494
95+func calcDeleteKey(blockHeight uint64) []byte {
96+ return []byte(fmt.Sprintf("%s%016x", TxPrefix, blockHeight))
97+}
98+
99+func calcTxIndexKey(txID string) []byte {
100+ return []byte(TxIndexPrefix + txID)
101+}
102+
103+func calcAnnotatedKey(formatKey string) []byte {
104+ return []byte(TxPrefix + formatKey)
105+}
106+
107+func calcUnconfirmedTxKey(formatKey string) []byte {
108+ return []byte(UnconfirmedTxPrefix + formatKey)
109+}
110+
111+func calcGlobalTxIndexKey(txID string) []byte {
112+ return []byte(GlobalTxIndexPrefix + txID)
113+}
114+
115+func CalcGlobalTxIndex(blockHash *bc.Hash, position uint64) []byte {
116+ txIdx := make([]byte, 40)
117+ copy(txIdx[:32], blockHash.Bytes())
118+ binary.BigEndian.PutUint64(txIdx[32:], position)
119+ return txIdx
120+}
121+
122+func formatKey(blockHeight uint64, position uint32) string {
123+ return fmt.Sprintf("%016x%08x", blockHeight, position)
124+}
125+
85126 // GetAssetDefinition get asset definition by assetiD
86127 func (store *WalletStore) GetAssetDefinition(assetID *bc.AssetID) []byte {
87128 return store.DB.Get(asset.ExtAssetKey(assetID))
@@ -133,7 +174,7 @@ func (store *WalletStore) DeleteUnconfirmedTransaction(txID string) {
133174
134175 // SetGlobalTransactionIndex set global tx index by blockhash and position
135176 func (store *WalletStore) SetGlobalTransactionIndex(globalTxID string, blockHash *bc.Hash, position uint64) {
136- store.DB.Set(calcGlobalTxIndexKey(globalTxID), calcGlobalTxIndex(blockHash, position))
177+ store.DB.Set(calcGlobalTxIndexKey(globalTxID), CalcGlobalTxIndex(blockHash, position))
137178 }
138179
139180 // GetStandardUTXO get standard utxo by id
@@ -221,12 +262,12 @@ func (store *WalletStore) SetContractUTXO(outputID bc.Hash, data []byte) {
221262
222263 // GetWalletInfo get wallet information
223264 func (store *WalletStore) GetWalletInfo() []byte {
224- return store.DB.Get(walletKey)
265+ return store.DB.Get([]byte(walletKey))
225266 }
226267
227268 // SetWalletInfo get wallet information
228269 func (store *WalletStore) SetWalletInfo(rawWallet []byte) {
229- store.DB.Set(walletKey, rawWallet)
270+ store.DB.Set([]byte(walletKey), rawWallet)
230271 }
231272
232273 // DeleteWalletTransactions delete all txs in wallet
--- a/node/node.go
+++ b/node/node.go
@@ -110,7 +110,7 @@ func NewNode(config *cfg.Config) *Node {
110110
111111 if !config.Wallet.Disable {
112112 walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir())
113- walletStore := w.NewWalletStore(walletDB)
113+ walletStore := database.NewWalletStore(walletDB)
114114 accounts = account.NewManager(walletDB, chain)
115115 assets = asset.NewRegistry(walletDB, chain)
116116 wallet, err = w.NewWallet(walletStore, accounts, assets, hsm, chain, dispatcher, config.Wallet.TxIndex)
--- a/test/wallet_test_util.go
+++ b/test/wallet_test_util.go
@@ -12,6 +12,7 @@ import (
1212 "github.com/vapor/blockchain/pseudohsm"
1313 "github.com/vapor/blockchain/signers"
1414 "github.com/vapor/crypto/ed25519/chainkd"
15+ "github.com/vapor/database"
1516 dbm "github.com/vapor/database/leveldb"
1617 "github.com/vapor/event"
1718 "github.com/vapor/protocol"
@@ -241,7 +242,7 @@ func (cfg *walletTestConfig) Run() error {
241242 return err
242243 }
243244 walletDB := dbm.NewDB("wallet", "leveldb", path.Join(dirPath, "wallet_db"))
244- walletStore := w.NewWalletStore(walletDB)
245+ walletStore := database.NewWalletStore(walletDB)
245246 accountManager := account.NewManager(walletDB, chain)
246247 assets := asset.NewRegistry(walletDB, chain)
247248 dispatcher := event.NewDispatcher()
--- a/wallet/annotated.go
+++ b/wallet/annotated.go
@@ -13,6 +13,7 @@ import (
1313 "github.com/vapor/consensus"
1414 "github.com/vapor/consensus/segwit"
1515 "github.com/vapor/crypto/sha3pool"
16+ "github.com/vapor/database"
1617 "github.com/vapor/protocol/bc"
1718 "github.com/vapor/protocol/bc/types"
1819 )
@@ -81,7 +82,7 @@ func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage
8182 }
8283
8384 // annotateTxs adds account data to transactions
84-func annotateTxsAccount(txs []*query.AnnotatedTx, store WalletStorer) {
85+func annotateTxsAccount(txs []*query.AnnotatedTx, store database.WalletStorer) {
8586 for i, tx := range txs {
8687 for j, input := range tx.Inputs {
8788 //issue asset tx input SpentOutputID is nil
@@ -106,7 +107,7 @@ func annotateTxsAccount(txs []*query.AnnotatedTx, store WalletStorer) {
106107 }
107108 }
108109
109-func getAccountFromACP(program []byte, store WalletStorer) (*account.Account, error) {
110+func getAccountFromACP(program []byte, store database.WalletStorer) (*account.Account, error) {
110111 var hash common.Hash
111112 accountCP := account.CtrlProgram{}
112113 localAccount := account.Account{}
--- a/wallet/indexer.go
+++ b/wallet/indexer.go
@@ -13,50 +13,12 @@ import (
1313 "github.com/vapor/blockchain/query"
1414 "github.com/vapor/consensus"
1515 "github.com/vapor/crypto/sha3pool"
16+ "github.com/vapor/database"
1617 chainjson "github.com/vapor/encoding/json"
17- "github.com/vapor/errors"
1818 "github.com/vapor/protocol/bc"
1919 "github.com/vapor/protocol/bc/types"
2020 )
2121
22-const (
23- //TxPrefix is wallet database transactions prefix
24- TxPrefix = "TXS:"
25- //TxIndexPrefix is wallet database tx index prefix
26- TxIndexPrefix = "TID:"
27- //GlobalTxIndexPrefix is wallet database global tx index prefix
28- GlobalTxIndexPrefix = "GTID:"
29-)
30-
31-var errAccntTxIDNotFound = errors.New("account TXID not found")
32-
33-func formatKey(blockHeight uint64, position uint32) string {
34- return fmt.Sprintf("%016x%08x", blockHeight, position)
35-}
36-
37-func calcAnnotatedKey(formatKey string) []byte {
38- return []byte(TxPrefix + formatKey)
39-}
40-
41-func calcDeleteKey(blockHeight uint64) []byte {
42- return []byte(fmt.Sprintf("%s%016x", TxPrefix, blockHeight))
43-}
44-
45-func calcTxIndexKey(txID string) []byte {
46- return []byte(TxIndexPrefix + txID)
47-}
48-
49-func calcGlobalTxIndexKey(txID string) []byte {
50- return []byte(GlobalTxIndexPrefix + txID)
51-}
52-
53-func calcGlobalTxIndex(blockHash *bc.Hash, position uint64) []byte {
54- txIdx := make([]byte, 40)
55- copy(txIdx[:32], blockHash.Bytes())
56- binary.BigEndian.PutUint64(txIdx[32:], position)
57- return txIdx
58-}
59-
6022 func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) {
6123 var hashBytes [32]byte
6224 copy(hashBytes[:], globalTxIdx[:32])
@@ -68,7 +30,7 @@ func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) {
6830 // saveExternalAssetDefinition save external and local assets definition,
6931 // when query ,query local first and if have no then query external
7032 // details see getAliasDefinition
71-func saveExternalAssetDefinition(b *types.Block, store WalletStorer) {
33+func saveExternalAssetDefinition(b *types.Block, store database.WalletStorer) {
7234 for _, tx := range b.Transactions {
7335 for _, orig := range tx.Inputs {
7436 if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
--- a/wallet/recovery.go
+++ b/wallet/recovery.go
@@ -12,6 +12,7 @@ import (
1212 "github.com/vapor/blockchain/signers"
1313 "github.com/vapor/crypto/ed25519/chainkd"
1414 "github.com/vapor/crypto/sha3pool"
15+ "github.com/vapor/database"
1516 "github.com/vapor/errors"
1617 "github.com/vapor/protocol/bc"
1718 "github.com/vapor/protocol/bc/types"
@@ -169,7 +170,7 @@ func (rs *recoveryState) stateForScope(account *account.Account) {
169170 type recoveryManager struct {
170171 mu sync.Mutex
171172
172- store WalletStorer
173+ store database.WalletStorer
173174 accountMgr *account.Manager
174175
175176 locked int32
@@ -186,7 +187,7 @@ type recoveryManager struct {
186187 }
187188
188189 // newRecoveryManager create recovery manger.
189-func newRecoveryManager(store WalletStorer, accountMgr *account.Manager) *recoveryManager {
190+func newRecoveryManager(store database.WalletStorer, accountMgr *account.Manager) *recoveryManager {
190191 return &recoveryManager{
191192 store: store,
192193 accountMgr: accountMgr,
--- a/wallet/recovery_test.go
+++ b/wallet/recovery_test.go
@@ -16,6 +16,7 @@ import (
1616 "github.com/vapor/common"
1717 "github.com/vapor/consensus"
1818 "github.com/vapor/crypto/ed25519/chainkd"
19+ "github.com/vapor/database"
1920 dbm "github.com/vapor/database/leveldb"
2021 "github.com/vapor/errors"
2122 "github.com/vapor/protocol/bc"
@@ -136,7 +137,7 @@ func TestXPubsRecoveryLock(t *testing.T) {
136137 defer os.RemoveAll(dirPath)
137138
138139 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
139- testStore := NewWalletStore(testDB)
140+ testStore := database.NewWalletStore(testDB)
140141 hsm, err := pseudohsm.New(dirPath)
141142 if err != nil {
142143 t.Fatal(err)
@@ -186,7 +187,7 @@ func TestExtendScanAddresses(t *testing.T) {
186187 defer os.RemoveAll(dirPath)
187188
188189 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
189- testStore := NewWalletStore(testDB)
190+ testStore := database.NewWalletStore(testDB)
190191 hsm, err := pseudohsm.New(dirPath)
191192 if err != nil {
192193 t.Fatal(err)
@@ -243,7 +244,7 @@ func TestRecoveryFromXPubs(t *testing.T) {
243244
244245 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
245246 recoveryDB := dbm.NewDB("recdb", "leveldb", dirPath)
246- recoveryStore := NewWalletStore(recoveryDB)
247+ recoveryStore := database.NewWalletStore(recoveryDB)
247248 hsm, err := pseudohsm.New(dirPath)
248249 if err != nil {
249250 t.Fatal(err)
@@ -318,7 +319,7 @@ func TestRecoveryByRescanAccount(t *testing.T) {
318319
319320 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
320321 recoveryDB := dbm.NewDB("recdb", "leveldb", dirPath)
321- recoveryStore := NewWalletStore(recoveryDB)
322+ recoveryStore := database.NewWalletStore(recoveryDB)
322323 hsm, err := pseudohsm.New(dirPath)
323324 if err != nil {
324325 t.Fatal(err)
@@ -407,7 +408,7 @@ func TestReportFound(t *testing.T) {
407408 defer os.RemoveAll(dirPath)
408409
409410 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
410- testStore := NewWalletStore(testDB)
411+ testStore := database.NewWalletStore(testDB)
411412 hsm, err := pseudohsm.New(dirPath)
412413 if err != nil {
413414 t.Fatal(err)
@@ -495,7 +496,7 @@ func TestLoadStatusInfo(t *testing.T) {
495496 defer os.RemoveAll(dirPath)
496497
497498 testDB := dbm.NewDB("testdb", "leveldb", "temp")
498- testStore := NewWalletStore(testDB)
499+ testStore := database.NewWalletStore(testDB)
499500 defer os.RemoveAll("temp")
500501
501502 hsm, err := pseudohsm.New(dirPath)
@@ -570,7 +571,7 @@ func TestLock(t *testing.T) {
570571 defer os.RemoveAll(dirPath)
571572
572573 testDB := dbm.NewDB("testdb", "leveldb", "temp")
573- testStore := NewWalletStore(testDB)
574+ testStore := database.NewWalletStore(testDB)
574575 defer os.RemoveAll("temp")
575576
576577 acctMgr := account.NewManager(testDB, nil)
@@ -629,7 +630,7 @@ func TestContractIndexResidue(t *testing.T) {
629630 defer os.RemoveAll(dirPath)
630631
631632 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
632- testStore := NewWalletStore(testDB)
633+ testStore := database.NewWalletStore(testDB)
633634 hsm, err := pseudohsm.New(dirPath)
634635 if err != nil {
635636 t.Fatal(err)
--- a/wallet/unconfirmed.go
+++ b/wallet/unconfirmed.go
@@ -21,10 +21,6 @@ const (
2121 MaxUnconfirmedTxDuration = 24 * time.Hour
2222 )
2323
24-func calcUnconfirmedTxKey(formatKey string) []byte {
25- return []byte(UnconfirmedTxPrefix + formatKey)
26-}
27-
2824 // SortByTimestamp implements sort.Interface for AnnotatedTx slices
2925 type SortByTimestamp []*query.AnnotatedTx
3026
--- a/wallet/unconfirmed_test.go
+++ b/wallet/unconfirmed_test.go
@@ -12,6 +12,7 @@ import (
1212 "github.com/vapor/blockchain/signers"
1313 "github.com/vapor/consensus"
1414 "github.com/vapor/crypto/ed25519/chainkd"
15+ "github.com/vapor/database"
1516 dbm "github.com/vapor/database/leveldb"
1617 "github.com/vapor/event"
1718 "github.com/vapor/protocol/bc"
@@ -27,7 +28,7 @@ func TestWalletUnconfirmedTxs(t *testing.T) {
2728 defer os.RemoveAll(dirPath)
2829
2930 testDB := dbm.NewDB("testdb", "leveldb", "temp")
30- testStore := NewWalletStore(testDB)
31+ testStore := database.NewWalletStore(testDB)
3132 defer os.RemoveAll("temp")
3233
3334 accountManager := account.NewManager(testDB, nil)
--- a/wallet/utxo_test.go
+++ b/wallet/utxo_test.go
@@ -10,6 +10,7 @@ import (
1010
1111 "github.com/vapor/account"
1212 "github.com/vapor/consensus"
13+ "github.com/vapor/database"
1314 dbm "github.com/vapor/database/leveldb"
1415 "github.com/vapor/protocol/bc"
1516 "github.com/vapor/protocol/bc/types"
@@ -18,7 +19,7 @@ import (
1819
1920 func TestGetAccountUtxos(t *testing.T) {
2021 testDB := dbm.NewDB("testdb", "leveldb", "temp")
21- testStore := NewWalletStore(testDB)
22+ testStore := database.NewWalletStore(testDB)
2223 defer func() {
2324 testDB.Close()
2425 os.RemoveAll("temp")
@@ -211,7 +212,7 @@ func TestGetAccountUtxos(t *testing.T) {
211212
212213 func TestFilterAccountUtxo(t *testing.T) {
213214 testDB := dbm.NewDB("testdb", "leveldb", "temp")
214- testStore := NewWalletStore(testDB)
215+ testStore := database.NewWalletStore(testDB)
215216 defer func() {
216217 testDB.Close()
217218 os.RemoveAll("temp")
--- a/wallet/wallet.go
+++ b/wallet/wallet.go
@@ -9,6 +9,7 @@ import (
99 "github.com/vapor/account"
1010 "github.com/vapor/asset"
1111 "github.com/vapor/blockchain/pseudohsm"
12+ "github.com/vapor/database"
1213 "github.com/vapor/errors"
1314 "github.com/vapor/event"
1415 "github.com/vapor/protocol"
@@ -24,7 +25,6 @@ const (
2425
2526 var (
2627 currentVersion = uint(1)
27- walletKey = []byte("walletInfo")
2828
2929 errBestBlockNotFoundInCore = errors.New("best block not found in core")
3030 errWalletVersionMismatch = errors.New("wallet version mismatch")
@@ -41,7 +41,7 @@ type StatusInfo struct {
4141
4242 //Wallet is related to storing account unspent outputs
4343 type Wallet struct {
44- store WalletStorer
44+ store database.WalletStorer
4545 rw sync.RWMutex
4646 status StatusInfo
4747 TxIndexFlag bool
@@ -57,7 +57,7 @@ type Wallet struct {
5757 }
5858
5959 //NewWallet return a new wallet instance
60-func NewWallet(store WalletStorer, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) (*Wallet, error) {
60+func NewWallet(store database.WalletStorer, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) (*Wallet, error) {
6161 w := &Wallet{
6262 store: store,
6363 AccountMgr: account,
--- a/wallet/wallet_test.go
+++ b/wallet/wallet_test.go
@@ -33,7 +33,7 @@ func TestEncodeDecodeGlobalTxIndex(t *testing.T) {
3333 Position: 1,
3434 }
3535
36- globalTxIdx := calcGlobalTxIndex(&want.BlockHash, want.Position)
36+ globalTxIdx := database.CalcGlobalTxIndex(&want.BlockHash, want.Position)
3737 blockHashGot, positionGot := parseGlobalTxIdx(globalTxIdx)
3838 if *blockHashGot != want.BlockHash {
3939 t.Errorf("blockHash mismatch. Get: %v. Expect: %v", *blockHashGot, want.BlockHash)
@@ -53,7 +53,7 @@ func TestWalletVersion(t *testing.T) {
5353 defer os.RemoveAll(dirPath)
5454
5555 testDB := dbm.NewDB("testdb", "leveldb", "temp")
56- testStore := NewWalletStore(testDB)
56+ testStore := database.NewWalletStore(testDB)
5757 defer func() {
5858 testDB.Close()
5959 os.RemoveAll("temp")
@@ -119,7 +119,7 @@ func TestWalletUpdate(t *testing.T) {
119119
120120 config.CommonConfig = config.DefaultConfig()
121121 testDB := dbm.NewDB("testdb", "leveldb", "temp")
122- testStore := NewWalletStore(testDB)
122+ testStore := database.NewWalletStore(testDB)
123123 defer func() {
124124 testDB.Close()
125125 os.RemoveAll("temp")
@@ -200,7 +200,7 @@ func TestWalletUpdate(t *testing.T) {
200200 for position, tx := range block.Transactions {
201201 get := w.store.GetGlobalTransaction(tx.ID.String())
202202 bh := block.BlockHeader.Hash()
203- expect := calcGlobalTxIndex(&bh, uint64(position))
203+ expect := database.CalcGlobalTxIndex(&bh, uint64(position))
204204 if !reflect.DeepEqual(get, expect) {
205205 t.Fatalf("position#%d: compare retrieved globalTxIdx err", position)
206206 }
@@ -217,7 +217,7 @@ func TestRescanWallet(t *testing.T) {
217217
218218 config.CommonConfig = config.DefaultConfig()
219219 testDB := dbm.NewDB("testdb", "leveldb", "temp")
220- testStore := NewWalletStore(testDB)
220+ testStore := database.NewWalletStore(testDB)
221221 defer func() {
222222 testDB.Close()
223223 os.RemoveAll("temp")
@@ -269,7 +269,7 @@ func TestMemPoolTxQueryLoop(t *testing.T) {
269269 }
270270 config.CommonConfig = config.DefaultConfig()
271271 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
272- testStore := NewWalletStore(testDB)
272+ testStore := database.NewWalletStore(testDB)
273273 defer func() {
274274 testDB.Close()
275275 os.RemoveAll(dirPath)
@@ -382,7 +382,7 @@ func mockTxData(utxos []*account.UTXO, testAccount *account.Account) (*txbuilder
382382 return tplBuilder.Build()
383383 }
384384
385-func mockWallet(store WalletStorer, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) *Wallet {
385+func mockWallet(store database.WalletStorer, account *account.Manager, asset *asset.Registry, chain *protocol.Chain, dispatcher *event.Dispatcher, txIndexFlag bool) *Wallet {
386386 wallet := &Wallet{
387387 store: store,
388388 AccountMgr: account,
Show on old repository browser