2014-11-04 18:22:13 +01:00
|
|
|
/*
|
2016-08-08 21:49:09 +02:00
|
|
|
* Copyright (c) 2014-2016 The btcsuite developers
|
2014-11-04 18:22:13 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Helpers to create parameterized objects to use in tests.
|
|
|
|
|
|
|
|
package votingpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
2015-04-07 17:40:13 +02:00
|
|
|
"time"
|
2014-11-04 18:22:13 +01:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2016-08-08 21:49:09 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2014-11-04 18:22:13 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcutil/hdkeychain"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2015-04-07 17:40:13 +02:00
|
|
|
"github.com/btcsuite/btcwallet/wtxmgr"
|
2014-11-04 18:22:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// seed is the master seed used to create extended keys.
|
|
|
|
seed = bytes.Repeat([]byte{0x2a, 0x64, 0xdf, 0x08}, 8)
|
|
|
|
pubPassphrase = []byte("_DJr{fL4H0O}*-0\n:V1izc)(6BomK")
|
|
|
|
privPassphrase = []byte("81lUHXnOMZ@?XXd7O9xyDIWIbXX-lj")
|
|
|
|
uniqueCounter = uint32(0)
|
|
|
|
// The block height where all our test inputs are created.
|
|
|
|
TstInputsBlock = int32(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
func getUniqueID() uint32 {
|
|
|
|
return atomic.AddUint32(&uniqueCounter, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createWithdrawalTx creates a withdrawalTx with the given input and output amounts.
|
2017-01-19 21:24:57 +01:00
|
|
|
func createWithdrawalTx(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, inputAmounts []int64, outputAmounts []int64) *withdrawalTx {
|
2014-11-04 18:22:13 +01:00
|
|
|
net := pool.Manager().ChainParams()
|
2015-04-03 14:14:52 +02:00
|
|
|
tx := newWithdrawalTx(defaultTxOptions)
|
2017-01-19 21:24:57 +01:00
|
|
|
_, credits := TstCreateCreditsOnNewSeries(t, dbtx, pool, inputAmounts)
|
2014-11-04 18:22:13 +01:00
|
|
|
for _, c := range credits {
|
|
|
|
tx.addInput(c)
|
|
|
|
}
|
|
|
|
for i, amount := range outputAmounts {
|
|
|
|
request := TstNewOutputRequest(
|
|
|
|
t, uint32(i), "34eVkREKgvvGASZW7hkgE2uNc1yycntMK6", btcutil.Amount(amount), net)
|
|
|
|
tx.addOutput(request)
|
|
|
|
}
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMsgTx(pkScript []byte, amts []int64) *wire.MsgTx {
|
|
|
|
msgtx := &wire.MsgTx{
|
|
|
|
Version: 1,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
2016-08-08 21:49:09 +02:00
|
|
|
Hash: chainhash.Hash{},
|
2014-11-04 18:22:13 +01:00
|
|
|
Index: 0xffffffff,
|
|
|
|
},
|
|
|
|
SignatureScript: []byte{txscript.OP_NOP},
|
|
|
|
Sequence: 0xffffffff,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LockTime: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, amt := range amts {
|
|
|
|
msgtx.AddTxOut(wire.NewTxOut(amt, pkScript))
|
|
|
|
}
|
|
|
|
return msgtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstNewDepositScript(t *testing.T, p *Pool, seriesID uint32, branch Branch, idx Index) []byte {
|
|
|
|
script, err := p.DepositScript(seriesID, branch, idx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create deposit script for series %d, branch %d, index %d: %v",
|
|
|
|
seriesID, branch, idx, err)
|
|
|
|
}
|
|
|
|
return script
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstRNamespaces(tx walletdb.ReadTx) (votingpoolNs, addrmgrNs walletdb.ReadBucket) {
|
|
|
|
return tx.ReadBucket(votingpoolNamespaceKey), tx.ReadBucket(addrmgrNamespaceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstRWNamespaces(tx walletdb.ReadWriteTx) (votingpoolNs, addrmgrNs walletdb.ReadWriteBucket) {
|
|
|
|
return tx.ReadWriteBucket(votingpoolNamespaceKey), tx.ReadWriteBucket(addrmgrNamespaceKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstTxStoreRWNamespace(tx walletdb.ReadWriteTx) walletdb.ReadWriteBucket {
|
|
|
|
return tx.ReadWriteBucket(txmgrNamespaceKey)
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// TstEnsureUsedAddr ensures the addresses defined by the given series/branch and
|
|
|
|
// index==0..idx are present in the set of used addresses for the given Pool.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstEnsureUsedAddr(t *testing.T, dbtx walletdb.ReadWriteTx, p *Pool, seriesID uint32, branch Branch, idx Index) []byte {
|
|
|
|
ns, addrmgrNs := TstRWNamespaces(dbtx)
|
|
|
|
addr, err := p.getUsedAddr(ns, addrmgrNs, seriesID, branch, idx)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if addr != nil {
|
|
|
|
var script []byte
|
2017-01-19 21:24:57 +01:00
|
|
|
TstRunWithManagerUnlocked(t, p.Manager(), addrmgrNs, func() {
|
2014-11-04 18:22:13 +01:00
|
|
|
script, err = addr.Script()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return script
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
TstRunWithManagerUnlocked(t, p.Manager(), addrmgrNs, func() {
|
|
|
|
err = p.EnsureUsedAddr(ns, addrmgrNs, seriesID, branch, idx)
|
2014-11-04 18:22:13 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return TstNewDepositScript(t, p, seriesID, branch, idx)
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreatePkScript(t *testing.T, dbtx walletdb.ReadWriteTx, p *Pool, seriesID uint32, branch Branch, idx Index) []byte {
|
|
|
|
script := TstEnsureUsedAddr(t, dbtx, p, seriesID, branch, idx)
|
2014-11-04 18:22:13 +01:00
|
|
|
addr, err := p.addressFor(script)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pkScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return pkScript
|
|
|
|
}
|
|
|
|
|
|
|
|
type TstSeriesDef struct {
|
|
|
|
ReqSigs uint32
|
|
|
|
PubKeys []string
|
|
|
|
PrivKeys []string
|
|
|
|
SeriesID uint32
|
|
|
|
Inactive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// TstCreateSeries creates a new Series for every definition in the given slice
|
|
|
|
// of TstSeriesDef. If the definition includes any private keys, the Series is
|
|
|
|
// empowered with them.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreateSeries(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, definitions []TstSeriesDef) {
|
|
|
|
ns, addrmgrNs := TstRWNamespaces(dbtx)
|
2014-11-04 18:22:13 +01:00
|
|
|
for _, def := range definitions {
|
2017-01-19 21:24:57 +01:00
|
|
|
err := pool.CreateSeries(ns, CurrentVersion, def.SeriesID, def.ReqSigs, def.PubKeys)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot creates series %d: %v", def.SeriesID, err)
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
TstRunWithManagerUnlocked(t, pool.Manager(), addrmgrNs, func() {
|
2014-11-04 18:22:13 +01:00
|
|
|
for _, key := range def.PrivKeys {
|
2017-01-19 21:24:57 +01:00
|
|
|
if err := pool.EmpowerSeries(ns, def.SeriesID, key); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
pool.Series(def.SeriesID).active = !def.Inactive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstCreateMasterKey(t *testing.T, seed []byte) *hdkeychain.ExtendedKey {
|
2015-11-10 19:05:16 +01:00
|
|
|
key, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
// createMasterKeys creates count master ExtendedKeys with unique seeds.
|
|
|
|
func createMasterKeys(t *testing.T, count int) []*hdkeychain.ExtendedKey {
|
|
|
|
keys := make([]*hdkeychain.ExtendedKey, count)
|
|
|
|
for i := range keys {
|
|
|
|
keys[i] = TstCreateMasterKey(t, bytes.Repeat(uint32ToBytes(getUniqueID()), 4))
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// TstCreateSeriesDef creates a TstSeriesDef with a unique SeriesID, the given
|
|
|
|
// reqSigs and the raw public/private keys extracted from the list of private
|
|
|
|
// keys. The new series will be empowered with all private keys.
|
|
|
|
func TstCreateSeriesDef(t *testing.T, pool *Pool, reqSigs uint32, keys []*hdkeychain.ExtendedKey) TstSeriesDef {
|
|
|
|
pubKeys := make([]string, len(keys))
|
|
|
|
privKeys := make([]string, len(keys))
|
|
|
|
for i, key := range keys {
|
|
|
|
privKeys[i] = key.String()
|
|
|
|
pubkey, _ := key.Neuter()
|
|
|
|
pubKeys[i] = pubkey.String()
|
|
|
|
}
|
2015-04-07 17:40:13 +02:00
|
|
|
seriesID := uint32(len(pool.seriesLookup)) + 1
|
2014-11-04 18:22:13 +01:00
|
|
|
return TstSeriesDef{
|
|
|
|
ReqSigs: reqSigs, SeriesID: seriesID, PubKeys: pubKeys, PrivKeys: privKeys}
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreatePoolAndTxStore(t *testing.T) (tearDown func(), db walletdb.DB, pool *Pool, store *wtxmgr.Store) {
|
|
|
|
teardown, db, pool := TstCreatePool(t)
|
|
|
|
store = TstCreateTxStore(t, db)
|
|
|
|
return teardown, db, pool, store
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
// TstCreateCreditsOnNewSeries creates a new Series (with a unique ID) and a
|
|
|
|
// slice of credits locked to the series' address with branch==1 and index==0.
|
|
|
|
// The new Series will use a 2-of-3 configuration and will be empowered with
|
|
|
|
// all of its private keys.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreateCreditsOnNewSeries(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, amounts []int64) (uint32, []credit) {
|
2014-11-04 18:22:13 +01:00
|
|
|
masters := []*hdkeychain.ExtendedKey{
|
|
|
|
TstCreateMasterKey(t, bytes.Repeat(uint32ToBytes(getUniqueID()), 4)),
|
|
|
|
TstCreateMasterKey(t, bytes.Repeat(uint32ToBytes(getUniqueID()), 4)),
|
|
|
|
TstCreateMasterKey(t, bytes.Repeat(uint32ToBytes(getUniqueID()), 4)),
|
|
|
|
}
|
|
|
|
def := TstCreateSeriesDef(t, pool, 2, masters)
|
2017-01-19 21:24:57 +01:00
|
|
|
TstCreateSeries(t, dbtx, pool, []TstSeriesDef{def})
|
|
|
|
return def.SeriesID, TstCreateSeriesCredits(t, dbtx, pool, def.SeriesID, amounts)
|
2015-04-07 17:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TstCreateSeriesCredits creates a new credit for every item in the amounts
|
|
|
|
// slice, locked to the given series' address with branch==1 and index==0.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreateSeriesCredits(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, seriesID uint32, amounts []int64) []credit {
|
|
|
|
addr := TstNewWithdrawalAddress(t, dbtx, pool, seriesID, Branch(1), Index(0))
|
2015-04-07 17:40:13 +02:00
|
|
|
pkScript, err := txscript.PayToAddrScript(addr.addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
msgTx := createMsgTx(pkScript, amounts)
|
2016-08-08 21:49:09 +02:00
|
|
|
txHash := msgTx.TxHash()
|
2015-04-07 17:40:13 +02:00
|
|
|
credits := make([]credit, len(amounts))
|
|
|
|
for i := range msgTx.TxOut {
|
|
|
|
c := wtxmgr.Credit{
|
|
|
|
OutPoint: wire.OutPoint{
|
2016-08-08 21:49:09 +02:00
|
|
|
Hash: txHash,
|
2015-04-07 17:40:13 +02:00
|
|
|
Index: uint32(i),
|
|
|
|
},
|
|
|
|
BlockMeta: wtxmgr.BlockMeta{
|
|
|
|
Block: wtxmgr.Block{Height: TstInputsBlock},
|
|
|
|
},
|
|
|
|
Amount: btcutil.Amount(msgTx.TxOut[i].Value),
|
|
|
|
PkScript: msgTx.TxOut[i].PkScript,
|
|
|
|
}
|
|
|
|
credits[i] = newCredit(c, *addr)
|
|
|
|
}
|
|
|
|
return credits
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
// TstCreateSeriesCreditsOnStore inserts a new credit in the given store for
|
|
|
|
// every item in the amounts slice. These credits are locked to the votingpool
|
|
|
|
// address composed of the given seriesID, branch==1 and index==0.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreateSeriesCreditsOnStore(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, seriesID uint32, amounts []int64,
|
2015-04-07 17:40:13 +02:00
|
|
|
store *wtxmgr.Store) []credit {
|
2014-11-04 18:22:13 +01:00
|
|
|
branch := Branch(1)
|
|
|
|
idx := Index(0)
|
2017-01-19 21:24:57 +01:00
|
|
|
pkScript := TstCreatePkScript(t, dbtx, pool, seriesID, branch, idx)
|
2015-04-07 17:40:13 +02:00
|
|
|
eligible := make([]credit, len(amounts))
|
2017-01-19 21:24:57 +01:00
|
|
|
for i, credit := range TstCreateCreditsOnStore(t, dbtx, store, pkScript, amounts) {
|
|
|
|
eligible[i] = newCredit(credit, *TstNewWithdrawalAddress(t, dbtx, pool, seriesID, branch, idx))
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
return eligible
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
// TstCreateCreditsOnStore inserts a new credit in the given store for
|
|
|
|
// every item in the amounts slice.
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstCreateCreditsOnStore(t *testing.T, dbtx walletdb.ReadWriteTx, s *wtxmgr.Store, pkScript []byte, amounts []int64) []wtxmgr.Credit {
|
2014-11-04 18:22:13 +01:00
|
|
|
msgTx := createMsgTx(pkScript, amounts)
|
2015-04-07 17:40:13 +02:00
|
|
|
meta := &wtxmgr.BlockMeta{
|
|
|
|
Block: wtxmgr.Block{Height: TstInputsBlock},
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
rec, err := wtxmgr.NewTxRecordFromMsgTx(msgTx, time.Now())
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
2015-04-07 17:40:13 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
txmgrNs := dbtx.ReadWriteBucket(txmgrNamespaceKey)
|
|
|
|
if err := s.InsertTx(txmgrNs, rec, meta); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
t.Fatal("Failed to create inputs: ", err)
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:40:13 +02:00
|
|
|
credits := make([]wtxmgr.Credit, len(msgTx.TxOut))
|
2014-11-04 18:22:13 +01:00
|
|
|
for i := range msgTx.TxOut {
|
2017-01-19 21:24:57 +01:00
|
|
|
if err := s.AddCredit(txmgrNs, rec, meta, uint32(i), false); err != nil {
|
2014-11-04 18:22:13 +01:00
|
|
|
t.Fatal("Failed to create inputs: ", err)
|
|
|
|
}
|
2015-04-07 17:40:13 +02:00
|
|
|
credits[i] = wtxmgr.Credit{
|
|
|
|
OutPoint: wire.OutPoint{
|
|
|
|
Hash: rec.Hash,
|
|
|
|
Index: uint32(i),
|
|
|
|
},
|
|
|
|
BlockMeta: *meta,
|
|
|
|
Amount: btcutil.Amount(msgTx.TxOut[i].Value),
|
|
|
|
PkScript: msgTx.TxOut[i].PkScript,
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
return credits
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
var (
|
|
|
|
addrmgrNamespaceKey = []byte("waddrmgr")
|
|
|
|
votingpoolNamespaceKey = []byte("votingpool")
|
|
|
|
txmgrNamespaceKey = []byte("testtxstore")
|
|
|
|
)
|
|
|
|
|
2014-11-04 18:22:13 +01:00
|
|
|
// TstCreatePool creates a Pool on a fresh walletdb and returns it. It also
|
2017-01-19 21:24:57 +01:00
|
|
|
// returns a teardown function that closes the Manager and removes the directory
|
|
|
|
// used to store the database.
|
|
|
|
func TstCreatePool(t *testing.T) (tearDownFunc func(), db walletdb.DB, pool *Pool) {
|
2014-11-04 18:22:13 +01:00
|
|
|
// This should be moved somewhere else eventually as not all of our tests
|
|
|
|
// call this function, but right now the only option would be to have the
|
|
|
|
// t.Parallel() call in each of our tests.
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a new wallet DB and addr manager.
|
|
|
|
dir, err := ioutil.TempDir("", "pool_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create db dir: %v", err)
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
db, err = walletdb.Create("bdb", filepath.Join(dir, "wallet.db"))
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create wallet DB: %v", err)
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
var addrMgr *waddrmgr.Manager
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
addrmgrNs, err := tx.CreateTopLevelBucket(addrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
votingpoolNs, err := tx.CreateTopLevelBucket(votingpoolNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fastScrypt := &waddrmgr.ScryptOptions{N: 16, R: 8, P: 1}
|
|
|
|
err = waddrmgr.Create(addrmgrNs, seed, pubPassphrase, privPassphrase,
|
|
|
|
&chaincfg.MainNetParams, fastScrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addrMgr, err = waddrmgr.Open(addrmgrNs, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pool, err = Create(votingpoolNs, addrMgr, []byte{0x00})
|
|
|
|
return err
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
2017-01-19 21:24:57 +01:00
|
|
|
t.Fatalf("Could not set up DB: %v", err)
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
tearDownFunc = func() {
|
2017-01-19 21:24:57 +01:00
|
|
|
addrMgr.Close()
|
2014-11-04 18:22:13 +01:00
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
return tearDownFunc, db, pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstCreateTxStore(t *testing.T, db walletdb.DB) *wtxmgr.Store {
|
|
|
|
var store *wtxmgr.Store
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
txmgrNs, err := tx.CreateTopLevelBucket(txmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = wtxmgr.Create(txmgrNs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store, err = wtxmgr.Open(txmgrNs, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create txmgr: %v", err)
|
|
|
|
}
|
|
|
|
return store
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TstNewOutputRequest(t *testing.T, transaction uint32, address string, amount btcutil.Amount,
|
|
|
|
net *chaincfg.Params) OutputRequest {
|
|
|
|
addr, err := btcutil.DecodeAddress(address, net)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to decode address %s", address)
|
|
|
|
}
|
|
|
|
pkScript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to generate pkScript for %v", addr)
|
|
|
|
}
|
|
|
|
return OutputRequest{
|
|
|
|
PkScript: pkScript,
|
|
|
|
Address: addr,
|
|
|
|
Amount: amount,
|
|
|
|
Server: "server",
|
|
|
|
Transaction: transaction,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstNewWithdrawalOutput(r OutputRequest, status outputStatus,
|
|
|
|
outpoints []OutBailmentOutpoint) *WithdrawalOutput {
|
|
|
|
output := &WithdrawalOutput{
|
|
|
|
request: r,
|
|
|
|
status: status,
|
|
|
|
outpoints: outpoints,
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func TstNewWithdrawalAddress(t *testing.T, dbtx walletdb.ReadWriteTx, p *Pool, seriesID uint32, branch Branch,
|
2014-11-04 18:22:13 +01:00
|
|
|
index Index) (addr *WithdrawalAddress) {
|
2017-01-19 21:24:57 +01:00
|
|
|
TstEnsureUsedAddr(t, dbtx, p, seriesID, branch, index)
|
|
|
|
ns, addrmgrNs := TstRNamespaces(dbtx)
|
2014-11-04 18:22:13 +01:00
|
|
|
var err error
|
2017-01-19 21:24:57 +01:00
|
|
|
TstRunWithManagerUnlocked(t, p.Manager(), addrmgrNs, func() {
|
|
|
|
addr, err = p.WithdrawalAddress(ns, addrmgrNs, seriesID, branch, index)
|
2014-11-04 18:22:13 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get WithdrawalAddress: %v", err)
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TstNewChangeAddress(t *testing.T, p *Pool, seriesID uint32, idx Index) (addr *ChangeAddress) {
|
|
|
|
addr, err := p.ChangeAddress(seriesID, idx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get ChangeAddress: %v", err)
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:14:52 +02:00
|
|
|
func TstConstantFee(fee btcutil.Amount) func() btcutil.Amount {
|
|
|
|
return func() btcutil.Amount { return fee }
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
2015-02-23 17:07:12 +01:00
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
func createAndFulfillWithdrawalRequests(t *testing.T, dbtx walletdb.ReadWriteTx, pool *Pool, roundID uint32) withdrawalInfo {
|
2015-02-23 17:07:12 +01:00
|
|
|
|
|
|
|
params := pool.Manager().ChainParams()
|
2017-01-19 21:24:57 +01:00
|
|
|
seriesID, eligible := TstCreateCreditsOnNewSeries(t, dbtx, pool, []int64{2e6, 4e6})
|
2015-02-23 17:07:12 +01:00
|
|
|
requests := []OutputRequest{
|
|
|
|
TstNewOutputRequest(t, 1, "34eVkREKgvvGASZW7hkgE2uNc1yycntMK6", 3e6, params),
|
|
|
|
TstNewOutputRequest(t, 2, "3PbExiaztsSYgh6zeMswC49hLUwhTQ86XG", 2e6, params),
|
|
|
|
}
|
|
|
|
changeStart := TstNewChangeAddress(t, pool, seriesID, 0)
|
|
|
|
dustThreshold := btcutil.Amount(1e4)
|
2017-01-19 21:24:57 +01:00
|
|
|
startAddr := TstNewWithdrawalAddress(t, dbtx, pool, seriesID, 1, 0)
|
2015-02-23 17:07:12 +01:00
|
|
|
lastSeriesID := seriesID
|
|
|
|
w := newWithdrawal(roundID, requests, eligible, *changeStart)
|
|
|
|
if err := w.fulfillRequests(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return withdrawalInfo{
|
|
|
|
requests: requests,
|
|
|
|
startAddress: *startAddr,
|
|
|
|
changeStart: *changeStart,
|
|
|
|
lastSeriesID: lastSeriesID,
|
|
|
|
dustThreshold: dustThreshold,
|
|
|
|
status: *w.status,
|
|
|
|
}
|
|
|
|
}
|