2015-12-01 19:44:58 +01:00
|
|
|
// Copyright (c) 2015 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-11-04 18:22:13 +01:00
|
|
|
|
|
|
|
package votingpool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-23 17:07:12 +01:00
|
|
|
"reflect"
|
2014-11-04 18:22:13 +01:00
|
|
|
"testing"
|
|
|
|
|
2017-06-06 02:54:35 +02:00
|
|
|
"github.com/roasbeef/btcwallet/walletdb"
|
2014-11-04 18:22:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPutUsedAddrHash(t *testing.T) {
|
2017-01-19 21:24:57 +01:00
|
|
|
tearDown, db, pool := TstCreatePool(t)
|
2014-11-04 18:22:13 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
dummyHash := bytes.Repeat([]byte{0x09}, 10)
|
2017-01-19 21:24:57 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, _ := TstRWNamespaces(tx)
|
|
|
|
return putUsedAddrHash(ns, pool.ID, 0, 0, 0, dummyHash)
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var storedHash []byte
|
2017-01-19 21:24:57 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns, _ := TstRNamespaces(tx)
|
|
|
|
storedHash = getUsedAddrHash(ns, pool.ID, 0, 0, 0)
|
|
|
|
return nil
|
|
|
|
})
|
2014-11-04 18:22:13 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(storedHash, dummyHash) {
|
|
|
|
t.Fatalf("Wrong stored hash; got %x, want %x", storedHash, dummyHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMaxUsedIdx(t *testing.T) {
|
2017-01-19 21:24:57 +01:00
|
|
|
tearDown, db, pool := TstCreatePool(t)
|
2014-11-04 18:22:13 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, _ := TstRWNamespaces(tx)
|
|
|
|
for i, idx := range []int{0, 7, 9, 3001, 41, 500, 6} {
|
|
|
|
dummyHash := bytes.Repeat([]byte{byte(i)}, 10)
|
|
|
|
err := putUsedAddrHash(ns, pool.ID, 0, 0, Index(idx), dummyHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-04 18:22:13 +01:00
|
|
|
}
|
2017-01-19 21:24:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
|
|
|
|
var maxIdx Index
|
2017-01-19 21:24:57 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns, _ := TstRNamespaces(tx)
|
|
|
|
var err error
|
|
|
|
maxIdx, err = getMaxUsedIdx(ns, pool.ID, 0, 0)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-04 18:22:13 +01:00
|
|
|
if maxIdx != Index(3001) {
|
|
|
|
t.Fatalf("Wrong max idx; got %d, want %d", maxIdx, Index(3001))
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 17:07:12 +01:00
|
|
|
|
|
|
|
func TestWithdrawalSerialization(t *testing.T) {
|
2017-01-19 21:24:57 +01:00
|
|
|
tearDown, db, pool := TstCreatePool(t)
|
2015-02-23 17:07:12 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
2017-01-19 21:24:57 +01:00
|
|
|
dbtx, err := db.BeginReadWriteTx()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dbtx.Commit()
|
|
|
|
ns, addrmgrNs := TstRWNamespaces(dbtx)
|
|
|
|
|
2015-02-23 17:07:12 +01:00
|
|
|
roundID := uint32(0)
|
2017-01-19 21:24:57 +01:00
|
|
|
wi := createAndFulfillWithdrawalRequests(t, dbtx, pool, roundID)
|
2015-02-23 17:07:12 +01:00
|
|
|
|
|
|
|
serialized, err := serializeWithdrawal(wi.requests, wi.startAddress, wi.lastSeriesID,
|
|
|
|
wi.changeStart, wi.dustThreshold, wi.status)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wInfo *withdrawalInfo
|
2017-01-19 21:24:57 +01:00
|
|
|
TstRunWithManagerUnlocked(t, pool.Manager(), addrmgrNs, func() {
|
|
|
|
wInfo, err = deserializeWithdrawal(pool, ns, addrmgrNs, serialized)
|
2015-02-23 17:07:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wInfo.startAddress, wi.startAddress) {
|
|
|
|
t.Fatalf("Wrong startAddr; got %v, want %v", wInfo.startAddress, wi.startAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wInfo.changeStart, wi.changeStart) {
|
|
|
|
t.Fatalf("Wrong changeStart; got %v, want %v", wInfo.changeStart, wi.changeStart)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wInfo.lastSeriesID != wi.lastSeriesID {
|
|
|
|
t.Fatalf("Wrong LastSeriesID; got %d, want %d", wInfo.lastSeriesID, wi.lastSeriesID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wInfo.dustThreshold != wi.dustThreshold {
|
|
|
|
t.Fatalf("Wrong DustThreshold; got %d, want %d", wInfo.dustThreshold, wi.dustThreshold)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wInfo.requests, wi.requests) {
|
|
|
|
t.Fatalf("Wrong output requests; got %v, want %v", wInfo.requests, wi.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
TstCheckWithdrawalStatusMatches(t, wInfo.status, wi.status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutAndGetWithdrawal(t *testing.T) {
|
2017-01-19 21:24:57 +01:00
|
|
|
tearDown, db, _ := TstCreatePool(t)
|
2015-02-23 17:07:12 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
serialized := bytes.Repeat([]byte{1}, 10)
|
|
|
|
poolID := []byte{0x00}
|
|
|
|
roundID := uint32(0)
|
2017-01-19 21:24:57 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, _ := TstRWNamespaces(tx)
|
|
|
|
return putWithdrawal(ns, poolID, roundID, serialized)
|
|
|
|
})
|
2015-02-23 17:07:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var retrieved []byte
|
2017-01-19 21:24:57 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns, _ := TstRNamespaces(tx)
|
|
|
|
retrieved = getWithdrawal(ns, poolID, roundID)
|
|
|
|
return nil
|
|
|
|
})
|
2015-02-23 17:07:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(retrieved, serialized) {
|
|
|
|
t.Fatalf("Wrong value retrieved from DB; got %x, want %x", retrieved, serialized)
|
|
|
|
}
|
|
|
|
}
|