dropwallethistory.go raw

   1  package wallet
   2  
   3  import (
   4  	"encoding/binary"
   5  	"path/filepath"
   6  
   7  	"github.com/p9c/p9/pkg/walletdb"
   8  	"github.com/p9c/p9/pkg/wtxmgr"
   9  	"github.com/p9c/p9/pod/config"
  10  )
  11  
  12  func DropWalletHistory(w *Wallet, cfg *config.Config) (e error) {
  13  	var (
  14  		// Namespace keys.
  15  		syncBucketName    = []byte("sync")
  16  		waddrmgrNamespace = []byte("waddrmgr")
  17  		wtxmgrNamespace   = []byte("wtxmgr")
  18  		// Sync related key names (sync bucket).
  19  		syncedToName     = []byte("syncedto")
  20  		startBlockName   = []byte("startblock")
  21  		recentBlocksName = []byte("recentblocks")
  22  	)
  23  	dbPath := filepath.Join(
  24  		cfg.DataDir.V(),
  25  		cfg.Network.V(), "wallet.db",
  26  	)
  27  	// I.Ln("dbPath", dbPath)
  28  	var db walletdb.DB
  29  	db, e = walletdb.Open("bdb", dbPath)
  30  	if E.Chk(e) {
  31  		// DBError("failed to open database:", err)
  32  		return e
  33  	}
  34  	defer db.Close()
  35  	D.Ln("dropping wtxmgr namespace")
  36  	e = walletdb.Update(
  37  		db, func(tx walletdb.ReadWriteTx) (e error) {
  38  			D.Ln("deleting top level bucket")
  39  			if e = tx.DeleteTopLevelBucket(wtxmgrNamespace); E.Chk(e) {
  40  			}
  41  			if e != nil && e != walletdb.ErrBucketNotFound {
  42  				return e
  43  			}
  44  			var ns walletdb.ReadWriteBucket
  45  			D.Ln("creating new top level bucket")
  46  			if ns, e = tx.CreateTopLevelBucket(wtxmgrNamespace); E.Chk(e) {
  47  				return e
  48  			}
  49  			if e = wtxmgr.Create(ns); E.Chk(e) {
  50  				return e
  51  			}
  52  			ns = tx.ReadWriteBucket(waddrmgrNamespace).NestedReadWriteBucket(syncBucketName)
  53  			startBlock := ns.Get(startBlockName)
  54  			D.Ln("putting start block", startBlock)
  55  			if e = ns.Put(syncedToName, startBlock); E.Chk(e) {
  56  				return e
  57  			}
  58  			recentBlocks := make([]byte, 40)
  59  			copy(recentBlocks[0:4], startBlock[0:4])
  60  			copy(recentBlocks[8:], startBlock[4:])
  61  			binary.LittleEndian.PutUint32(recentBlocks[4:8], uint32(1))
  62  			defer D.Ln("put recent blocks")
  63  			return ns.Put(recentBlocksName, recentBlocks)
  64  		},
  65  	)
  66  	if E.Chk(e) {
  67  		return e
  68  	}
  69  	D.Ln("updated wallet")
  70  	// if w != nil {
  71  	// 	// Rescan chain to ensure balance is correctly regenerated
  72  	// 	job := &wallet.RescanJob{
  73  	// 		InitialSync: true,
  74  	// 	}
  75  	// 	// Submit rescan job and log when the import has completed.
  76  	// 	// Do not block on finishing the rescan.  The rescan success
  77  	// 	// or failure is logged elsewhere, and the channel is not
  78  	// 	// required to be read, so discard the return value.
  79  	// 	errC := w.SubmitRescan(job)
  80  	// 	select {
  81  	// 	case e := <-errC:
  82  	// 		DB		// 		// case <-time.After(time.Second * 5):
  83  	// 		// 	break
  84  	// 	}
  85  	// }
  86  	return e
  87  }
  88