2013-08-26 16:48:42 +02:00
|
|
|
/*
|
2014-01-09 20:12:20 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-08-26 16:48:42 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-08-26 19:34:18 +02:00
|
|
|
"code.google.com/p/go.crypto/ripemd160"
|
2013-08-26 16:48:42 +02:00
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"io"
|
2013-08-26 19:34:18 +02:00
|
|
|
"reflect"
|
2013-08-26 16:48:42 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-08-26 19:34:18 +02:00
|
|
|
recvtx = &RecvTx{
|
2013-11-22 19:42:25 +01:00
|
|
|
TxID: [btcwire.HashSize]byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
|
|
|
},
|
2014-01-06 19:35:07 +01:00
|
|
|
TxOutIdx: 0,
|
2013-11-22 19:42:25 +01:00
|
|
|
BlockHash: [btcwire.HashSize]byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
2013-11-22 19:42:25 +01:00
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
2013-08-26 19:34:18 +02:00
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
BlockHeight: 69,
|
|
|
|
Amount: 69,
|
|
|
|
ReceiverHash: []byte{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19,
|
2013-08-26 19:34:18 +02:00
|
|
|
},
|
|
|
|
}
|
2013-08-26 19:35:29 +02:00
|
|
|
|
2013-08-26 19:34:18 +02:00
|
|
|
sendtx = &SendTx{
|
2013-11-22 19:42:25 +01:00
|
|
|
TxID: [btcwire.HashSize]byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
Time: 12345,
|
|
|
|
BlockHash: [btcwire.HashSize]byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
2013-11-22 19:42:25 +01:00
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
2013-08-26 19:34:18 +02:00
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
BlockHeight: 69,
|
|
|
|
BlockTime: 54321,
|
|
|
|
BlockIndex: 3,
|
|
|
|
Receivers: []Pair{
|
|
|
|
Pair{
|
|
|
|
PubkeyHash: []byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
|
|
|
|
34, 35, 36, 37, 38, 39,
|
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
Amount: 69,
|
2013-08-26 19:34:18 +02:00
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
Pair{
|
|
|
|
PubkeyHash: []byte{
|
2013-08-26 19:34:18 +02:00
|
|
|
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
|
|
|
|
54, 55, 56, 57, 58, 59,
|
|
|
|
},
|
2013-11-22 19:42:25 +01:00
|
|
|
Amount: 96,
|
2013-08-26 19:34:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2013-08-26 16:48:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUtxoWriteRead(t *testing.T) {
|
|
|
|
utxo1 := &Utxo{
|
2013-10-22 16:03:11 +02:00
|
|
|
AddrHash: [ripemd160.Size]byte{
|
2013-09-04 15:32:14 +02:00
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19,
|
|
|
|
},
|
2013-09-04 22:16:20 +02:00
|
|
|
Out: OutPoint{
|
|
|
|
Hash: [btcwire.HashSize]byte{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
|
|
|
},
|
|
|
|
Index: 1,
|
2013-08-26 16:48:42 +02:00
|
|
|
},
|
2013-09-09 19:31:37 +02:00
|
|
|
Subscript: []byte{},
|
2013-09-09 20:14:57 +02:00
|
|
|
Amt: 69,
|
|
|
|
Height: 1337,
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
bufWriter := &bytes.Buffer{}
|
2013-09-09 19:31:37 +02:00
|
|
|
written, err := utxo1.WriteTo(bufWriter)
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
utxoBytes := bufWriter.Bytes()
|
|
|
|
|
|
|
|
utxo2 := new(Utxo)
|
2013-09-09 19:31:37 +02:00
|
|
|
read, err := utxo2.ReadFrom(bytes.NewBuffer(utxoBytes))
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-09-09 19:31:37 +02:00
|
|
|
if written != read {
|
|
|
|
t.Error("Reading and Writing Utxo: Size Mismatch")
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 19:34:18 +02:00
|
|
|
if !reflect.DeepEqual(utxo1, utxo2) {
|
2013-08-26 16:48:42 +02:00
|
|
|
spew.Dump(utxo1, utxo2)
|
|
|
|
t.Error("Utxos do not match.")
|
|
|
|
}
|
|
|
|
|
|
|
|
truncatedReadBuf := bytes.NewBuffer(utxoBytes)
|
|
|
|
truncatedReadBuf.Truncate(btcwire.HashSize)
|
|
|
|
utxo3 := new(Utxo)
|
2013-09-09 19:31:37 +02:00
|
|
|
n, err := utxo3.ReadFrom(truncatedReadBuf)
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != io.EOF {
|
|
|
|
t.Error("Expected err = io.EOF reading from truncated buffer.")
|
|
|
|
}
|
|
|
|
if n != btcwire.HashSize {
|
|
|
|
t.Error("Incorrect number of bytes read from truncated buffer.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUtxoStoreWriteRead(t *testing.T) {
|
|
|
|
store1 := new(UtxoStore)
|
2013-09-09 20:14:57 +02:00
|
|
|
for i := 0; i < 20; i++ {
|
2013-08-26 16:48:42 +02:00
|
|
|
utxo := new(Utxo)
|
2013-09-09 20:14:57 +02:00
|
|
|
for j := range utxo.Out.Hash[:] {
|
|
|
|
utxo.Out.Hash[j] = byte(i + 1)
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
utxo.Out.Index = uint32(i + 2)
|
2013-09-09 19:31:37 +02:00
|
|
|
utxo.Subscript = []byte{}
|
2013-09-09 20:14:57 +02:00
|
|
|
utxo.Amt = uint64(i + 3)
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
utxo.Height = int32(i + 4)
|
2013-11-22 19:42:25 +01:00
|
|
|
utxo.BlockHash = [btcwire.HashSize]byte{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
|
|
|
|
30, 31,
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
*store1 = append(*store1, utxo)
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bufWriter := &bytes.Buffer{}
|
2013-11-22 19:42:25 +01:00
|
|
|
nWritten, err := store1.WriteTo(bufWriter)
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-11-22 19:42:25 +01:00
|
|
|
if nWritten != int64(bufWriter.Len()) {
|
|
|
|
t.Errorf("Wrote %v bytes but write buffer has %v bytes.", nWritten, bufWriter.Len())
|
|
|
|
}
|
2013-08-26 16:48:42 +02:00
|
|
|
|
|
|
|
storeBytes := bufWriter.Bytes()
|
2013-11-22 19:42:25 +01:00
|
|
|
bufReader := bytes.NewBuffer(storeBytes)
|
|
|
|
if nWritten != int64(bufReader.Len()) {
|
|
|
|
t.Errorf("Wrote %v bytes but read buffer has %v bytes.", nWritten, bufReader.Len())
|
|
|
|
}
|
2013-08-26 16:48:42 +02:00
|
|
|
|
|
|
|
store2 := new(UtxoStore)
|
2013-11-22 19:42:25 +01:00
|
|
|
nRead, err := store2.ReadFrom(bufReader)
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-11-22 19:42:25 +01:00
|
|
|
if nWritten != nRead {
|
|
|
|
t.Errorf("Bytes written (%v) does not match bytes read (%v).", nWritten, nRead)
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 19:34:18 +02:00
|
|
|
if !reflect.DeepEqual(store1, store2) {
|
2013-09-09 19:31:37 +02:00
|
|
|
spew.Dump(store1, store2)
|
2013-08-26 19:34:18 +02:00
|
|
|
t.Error("Stores do not match.")
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
truncatedLen := 101
|
2013-09-09 20:14:57 +02:00
|
|
|
truncatedReadBuf := bytes.NewBuffer(storeBytes[:truncatedLen])
|
2013-08-26 16:48:42 +02:00
|
|
|
store3 := new(UtxoStore)
|
2013-11-22 19:42:25 +01:00
|
|
|
n, err := store3.ReadFrom(truncatedReadBuf)
|
2013-08-26 16:48:42 +02:00
|
|
|
if err != io.EOF {
|
2013-09-09 20:14:57 +02:00
|
|
|
t.Errorf("Expected err = io.EOF reading from truncated buffer, got: %v", err)
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
if int(n) != truncatedLen {
|
2013-11-22 19:42:25 +01:00
|
|
|
t.Errorf("Incorrect number of bytes (%v) read from truncated buffer (len %v).", n, truncatedLen)
|
2013-08-26 16:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-26 19:34:18 +02:00
|
|
|
|
|
|
|
func TestRecvTxWriteRead(t *testing.T) {
|
|
|
|
bufWriter := &bytes.Buffer{}
|
|
|
|
n, err := recvtx.WriteTo(bufWriter)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
txBytes := bufWriter.Bytes()
|
|
|
|
|
|
|
|
tx := new(RecvTx)
|
|
|
|
n, err = tx.ReadFrom(bytes.NewBuffer(txBytes))
|
|
|
|
if err != nil {
|
2014-01-06 19:35:07 +01:00
|
|
|
t.Errorf("Read %v bytes before erroring with: %v", n, err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(recvtx, tx) {
|
|
|
|
t.Error("Txs do not match.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
truncatedReadBuf := bytes.NewBuffer(txBytes)
|
|
|
|
truncatedReadBuf.Truncate(btcwire.HashSize)
|
|
|
|
n, err = tx.ReadFrom(truncatedReadBuf)
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Error("Expected err = io.EOF reading from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
if n != btcwire.HashSize {
|
|
|
|
t.Error("Incorrect number of bytes read from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendTxWriteRead(t *testing.T) {
|
|
|
|
bufWriter := &bytes.Buffer{}
|
|
|
|
n1, err := sendtx.WriteTo(bufWriter)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
txBytes := bufWriter.Bytes()
|
|
|
|
|
|
|
|
tx := new(SendTx)
|
|
|
|
n2, err := tx.ReadFrom(bytes.NewBuffer(txBytes))
|
|
|
|
if err != nil {
|
2014-01-06 19:35:07 +01:00
|
|
|
t.Errorf("Read %v bytes before erroring with: %v", n2, err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
if n1 != n2 {
|
2013-11-22 19:42:25 +01:00
|
|
|
t.Errorf("Number of bytes written and read mismatch, %d != %d",
|
|
|
|
n1, n2)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(sendtx, tx) {
|
|
|
|
t.Error("Txs do not match.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
truncatedReadBuf := bytes.NewBuffer(txBytes)
|
|
|
|
truncatedReadBuf.Truncate(btcwire.HashSize)
|
|
|
|
n, err := tx.ReadFrom(truncatedReadBuf)
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Error("Expected err = io.EOF reading from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
if n != btcwire.HashSize {
|
|
|
|
t.Error("Incorrect number of bytes read from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxStoreWriteRead(t *testing.T) {
|
|
|
|
s := []interface{}{recvtx, sendtx}
|
|
|
|
store := TxStore(s)
|
|
|
|
|
|
|
|
bufWriter := &bytes.Buffer{}
|
|
|
|
n1, err := store.WriteTo(bufWriter)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
txsBytes := bufWriter.Bytes()
|
|
|
|
|
|
|
|
txs := TxStore{}
|
|
|
|
n2, err := txs.ReadFrom(bytes.NewBuffer(txsBytes))
|
|
|
|
if err != nil {
|
2014-01-06 19:35:07 +01:00
|
|
|
t.Errorf("Read %v bytes before erroring with: %v", n2, err)
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
if n1 != n2 {
|
|
|
|
t.Error("Number of bytes written and read mismatch.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(store, txs) {
|
|
|
|
spew.Dump(store, txs)
|
|
|
|
t.Error("TxStores do not match.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
truncatedReadBuf := bytes.NewBuffer(txsBytes)
|
|
|
|
truncatedReadBuf.Truncate(50)
|
|
|
|
n, err := txs.ReadFrom(truncatedReadBuf)
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Error("Expected err = io.EOF reading from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
if n != 50 {
|
|
|
|
t.Error("Incorrect number of bytes read from truncated buffer.")
|
2013-09-04 22:16:20 +02:00
|
|
|
return
|
2013-08-26 19:34:18 +02:00
|
|
|
}
|
|
|
|
}
|