Skip to content

Instantly share code, notes, and snippets.

@dnldd
Last active December 27, 2017 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnldd/bc551d127f584c5d8424fb2d2aaaee42 to your computer and use it in GitHub Desktop.
Save dnldd/bc551d127f584c5d8424fb2d2aaaee42 to your computer and use it in GitHub Desktop.
v7 db upgrade
// Copyright (c) 2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// This file should compiled from the commit the file was introduced, otherwise
// it may not compile due to API changes, or may not create the database with
// the correct old version. This file should not be updated for API changes.
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"time"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/hdkeychain"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrwallet/wallet/udb"
"github.com/decred/dcrwallet/walletdb"
_ "github.com/decred/dcrwallet/walletdb/bdb"
"github.com/decred/dcrwallet/walletseed"
)
const dbname = "v7.db"
var (
epoch time.Time
pubPass = []byte("public")
privPass = []byte("private")
)
func main() {
err := setup()
if err != nil {
fmt.Fprintf(os.Stderr, "setup: %v\n", err)
os.Exit(1)
}
err = compress()
if err != nil {
fmt.Fprintf(os.Stderr, "compress: %v\n", err)
os.Exit(1)
}
}
// addSStxChangeWithExpiry adds a new output with the given amount
// and address, and randomizes the index (and returns it) of the newly added output.
func addSStxChangeWithExpiry(msgtx *wire.MsgTx, change dcrutil.Amount,
changeAddr dcrutil.Address) error {
pkScript, err := txscript.PayToSStxChange(changeAddr)
if err != nil {
return fmt.Errorf("cannot create txout script: %s", err)
}
msgtx.AddTxOut(wire.NewTxOut(int64(change), pkScript))
msgtx.Expiry = 500
return nil
}
func setup() error {
var chainParams = &chaincfg.TestNet2Params
var addr, err = dcrutil.DecodeAddress("Tso2MVTUeVrjHTBFedFhiyM7yVTbieqp91h")
if err != nil {
return err
}
os.Remove(dbname)
db, err := walletdb.Create("bdb", dbname)
if err != nil {
return err
}
defer db.Close()
seed, err := walletseed.GenerateRandomSeed(hdkeychain.RecommendedSeedLen)
if err != nil {
return err
}
err = udb.Initialize(db, chainParams, seed, pubPass, privPass)
if err != nil {
return err
}
_, txmgr, _, err := udb.Open(db, chainParams, pubPass)
if err != nil {
return err
}
return walletdb.Update(db, func(dbtx walletdb.ReadWriteTx) error {
txmgrns := dbtx.ReadWriteBucket([]byte("wtxmgr"))
prevBlock := chainParams.GenesisHash
buf := bytes.Buffer{}
err = (&wire.BlockHeader{
Version: 1,
PrevBlock: *prevBlock,
StakeVersion: 1,
VoteBits: 1,
Height: uint32(1),
}).Serialize(&buf)
if err != nil {
return err
}
headerData := udb.BlockHeaderData{
BlockHash: chainhash.Hash{31: byte(1)},
}
copy(headerData.SerializedHeader[:], buf.Bytes())
err = txmgr.ExtendMainChain(txmgrns, &headerData)
if err != nil {
return err
}
// Create and add 2 sstxchange outputs
for count := 1; count < 3; count++ {
msgTx := &wire.MsgTx{}
err = addSStxChangeWithExpiry(msgTx, dcrutil.Amount(1*count), addr)
if err != nil {
return err
}
rec, err := udb.NewTxRecordFromMsgTx(msgTx, epoch)
if err != nil {
return err
}
blockMeta := &udb.BlockMeta{
Block: udb.Block{
Hash: headerData.BlockHash,
Height: int32(1),
},
Time: epoch,
}
err = txmgr.AddCredit(txmgrns, rec, blockMeta, 0, true, 0)
if err != nil {
return err
}
}
return nil
})
}
func compress() error {
db, err := os.Open(dbname)
if err != nil {
return err
}
defer os.Remove(dbname)
defer db.Close()
dbgz, err := os.Create(dbname + ".gz")
if err != nil {
return err
}
defer dbgz.Close()
gz := gzip.NewWriter(dbgz)
_, err = io.Copy(gz, db)
if err != nil {
return err
}
return gz.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment