7 log "github.com/sirupsen/logrus"
8 "github.com/tendermint/tmlibs/db"
10 "github.com/vapor/account"
11 "github.com/vapor/asset"
12 "github.com/vapor/blockchain/pseudohsm"
13 "github.com/vapor/protocol"
14 "github.com/vapor/protocol/bc"
15 "github.com/vapor/protocol/bc/types"
23 var walletKey = []byte("walletInfo")
25 //StatusInfo is base valid block info to handle orphan block rollback
26 type StatusInfo struct {
33 //Wallet is related to storing account unspent outputs
38 AccountMgr *account.Manager
39 AssetReg *asset.Registry
42 RecoveryMgr *recoveryManager
43 rescanCh chan struct{}
46 //NewWallet return a new wallet instance
47 func NewWallet(walletDB db.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, chain *protocol.Chain) (*Wallet, error) {
54 RecoveryMgr: newRecoveryManager(walletDB, account),
55 rescanCh: make(chan struct{}, 1),
58 if err := w.loadWalletInfo(); err != nil {
62 if err := w.RecoveryMgr.LoadStatusInfo(); err != nil {
67 go w.delUnconfirmedTx()
71 //GetWalletInfo return stored wallet info and nil,if error,
72 //return initial wallet info and err
73 func (w *Wallet) loadWalletInfo() error {
74 if rawWallet := w.DB.Get(walletKey); rawWallet != nil {
75 return json.Unmarshal(rawWallet, &w.status)
78 block, err := w.chain.GetBlockByHeight(0)
82 return w.AttachBlock(block)
85 func (w *Wallet) commitWalletInfo(batch db.Batch) error {
86 rawWallet, err := json.Marshal(w.status)
88 log.WithField("err", err).Error("save wallet info")
92 batch.Set(walletKey, rawWallet)
97 // AttachBlock attach a new block
98 func (w *Wallet) AttachBlock(block *types.Block) error {
102 if block.PreviousBlockHash != w.status.WorkHash {
103 log.Warn("wallet skip attachBlock due to status hash not equal to previous hash")
107 blockHash := block.Hash()
108 txStatus, err := w.chain.GetTransactionStatus(&blockHash)
113 if err := w.RecoveryMgr.FilterRecoveryTxs(block); err != nil {
117 storeBatch := w.DB.NewBatch()
118 if err := w.indexTransactions(storeBatch, block, txStatus); err != nil {
122 w.attachUtxos(storeBatch, block, txStatus)
123 w.status.WorkHeight = block.Height
124 w.status.WorkHash = block.Hash()
125 if w.status.WorkHeight >= w.status.BestHeight {
126 w.status.BestHeight = w.status.WorkHeight
127 w.status.BestHash = w.status.WorkHash
129 return w.commitWalletInfo(storeBatch)
132 // DetachBlock detach a block and rollback state
133 func (w *Wallet) DetachBlock(block *types.Block) error {
137 blockHash := block.Hash()
138 txStatus, err := w.chain.GetTransactionStatus(&blockHash)
143 storeBatch := w.DB.NewBatch()
144 w.detachUtxos(storeBatch, block, txStatus)
145 w.deleteTransactions(storeBatch, w.status.BestHeight)
147 w.status.BestHeight = block.Height - 1
148 w.status.BestHash = block.PreviousBlockHash
150 if w.status.WorkHeight > w.status.BestHeight {
151 w.status.WorkHeight = w.status.BestHeight
152 w.status.WorkHash = w.status.BestHash
155 return w.commitWalletInfo(storeBatch)
158 //WalletUpdate process every valid block and reverse every invalid block which need to rollback
159 func (w *Wallet) walletUpdater() {
161 w.getRescanNotification()
162 for !w.chain.InMainChain(w.status.BestHash) {
163 block, err := w.chain.GetBlockByHash(&w.status.BestHash)
165 log.WithField("err", err).Error("walletUpdater GetBlockByHash")
169 if err := w.DetachBlock(block); err != nil {
170 log.WithField("err", err).Error("walletUpdater detachBlock stop")
175 block, _ := w.chain.GetBlockByHeight(w.status.WorkHeight + 1)
177 w.walletBlockWaiter()
181 if err := w.AttachBlock(block); err != nil {
182 log.WithField("err", err).Error("walletUpdater AttachBlock stop")
188 //RescanBlocks provide a trigger to rescan blocks
189 func (w *Wallet) RescanBlocks() {
191 case w.rescanCh <- struct{}{}:
197 // deleteAccountTxs deletes all txs in wallet
198 func (w *Wallet) deleteAccountTxs() {
199 storeBatch := w.DB.NewBatch()
201 txIter := w.DB.IteratorPrefix([]byte(TxPrefix))
202 defer txIter.Release()
205 storeBatch.Delete(txIter.Key())
208 txIndexIter := w.DB.IteratorPrefix([]byte(TxIndexPrefix))
209 defer txIndexIter.Release()
211 for txIndexIter.Next() {
212 storeBatch.Delete(txIndexIter.Key())
218 // DeleteAccount deletes account matching accountID, then rescan wallet
219 func (w *Wallet) DeleteAccount(accountID string) (err error) {
223 if err := w.AccountMgr.DeleteAccount(accountID); err != nil {
232 func (w *Wallet) UpdateAccountAlias(accountID string, newAlias string) (err error) {
236 if err := w.AccountMgr.UpdateAccountAlias(accountID, newAlias); err != nil {
245 func (w *Wallet) getRescanNotification() {
254 func (w *Wallet) setRescanStatus() {
255 block, _ := w.chain.GetBlockByHeight(0)
256 w.status.WorkHash = bc.Hash{}
260 func (w *Wallet) walletBlockWaiter() {
262 case <-w.chain.BlockWaiter(w.status.WorkHeight + 1):
268 // GetWalletStatusInfo return current wallet StatusInfo
269 func (w *Wallet) GetWalletStatusInfo() StatusInfo {