Skip to content

Commit

Permalink
udb: add script storage testdata.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Jul 9, 2019
1 parent 0988aa6 commit 18d887d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions wallet/udb/testdata/v12.db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2019 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 (
"compress/gzip"
"encoding/hex"
"fmt"
"io"
"os"

"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/hdkeychain"
_ "github.com/decred/dcrwallet/wallet/v2/drivers/bdb"
"github.com/decred/dcrwallet/wallet/v2/udb"
"github.com/decred/dcrwallet/wallet/v2/walletdb"
"github.com/decred/dcrwallet/walletseed"
)

const dbname = "v12.db"

var (
pubPass = []byte("public")
privPass = []byte("private")
)

var chainParams = &chaincfg.TestNet3Params

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)
}
}

func hexToBytes(origHex string) []byte {
buf, err := hex.DecodeString(origHex)
if err != nil {
panic(err)
}
return buf
}

func setup() error {
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
}

amgr, txStore, _, err := udb.Open(db, chainParams, pubPass)
if err != nil {
return err
}

err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
amgrns := tx.ReadWriteBucket([]byte("waddrmgr"))
txmgrns := tx.ReadWriteBucket([]byte("wtxmgr"))

if err := amgr.Unlock(amgrns, privPass); err != nil {
return err
}

script := hexToBytes("51210373c717acda38b5aa4c00c33932e059cdbc" +
"11deceb5f00490a9101704cc444c5151ae")

_, err := amgr.ImportScript(amgrns, script)
if err != nil {
return err
}

return txStore.InsertTxScript(txmgrns, script)
})

return err
}

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()
}
Binary file added wallet/udb/testdata/v12.db.gz
Binary file not shown.

0 comments on commit 18d887d

Please sign in to comment.