2013-08-22 19:14:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
"code.google.com/p/go.crypto/ripemd160"
|
|
|
|
"encoding/binary"
|
2013-09-09 20:14:57 +02:00
|
|
|
"errors"
|
2013-08-22 19:14:21 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2013-08-26 19:34:18 +02:00
|
|
|
// Byte headers prepending received and sent serialized transactions.
|
2013-08-22 19:14:21 +02:00
|
|
|
const (
|
|
|
|
RecvTxHeader byte = iota
|
|
|
|
SendTxHeader
|
|
|
|
)
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// UtxoStore is a type used for holding all Utxo structures for all
|
|
|
|
// addresses in a wallet.
|
|
|
|
type UtxoStore []*Utxo
|
2013-08-22 19:14:21 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Utxo is a type storing information about a single unspent
|
|
|
|
// transaction output.
|
2013-08-22 19:14:21 +02:00
|
|
|
type Utxo struct {
|
2013-10-11 00:44:44 +02:00
|
|
|
AddrHash [ripemd160.Size]byte
|
2013-09-09 20:14:57 +02:00
|
|
|
Out OutPoint
|
|
|
|
Subscript PkScript
|
|
|
|
Amt uint64 // Measured in Satoshis
|
|
|
|
Height int64
|
|
|
|
BlockHash btcwire.ShaHash
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// OutPoint is a btcwire.OutPoint with custom methods for serialization.
|
2013-09-04 22:16:20 +02:00
|
|
|
type OutPoint btcwire.OutPoint
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// PkScript is a custom type with methods to serialize pubkey scripts
|
|
|
|
// of variable length.
|
|
|
|
type PkScript []byte
|
2013-09-09 19:31:37 +02:00
|
|
|
|
2013-08-22 19:14:21 +02:00
|
|
|
// TxStore is a slice holding RecvTx and SendTx pointers.
|
|
|
|
type TxStore []interface{}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// RecvTx is a type storing information about a transaction that was
|
|
|
|
// received by an address in a wallet.
|
2013-08-22 19:14:21 +02:00
|
|
|
type RecvTx struct {
|
|
|
|
TxHash btcwire.ShaHash
|
2013-09-09 20:14:57 +02:00
|
|
|
BlockHash btcwire.ShaHash
|
|
|
|
Height int64
|
2013-09-09 19:31:37 +02:00
|
|
|
Amt uint64 // Measured in Satoshis
|
2013-08-22 19:14:21 +02:00
|
|
|
SenderAddr [ripemd160.Size]byte
|
|
|
|
ReceiverAddr [ripemd160.Size]byte
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// SendTx is a type storing information about a transaction that was
|
|
|
|
// sent by an address in a wallet.
|
2013-08-22 19:14:21 +02:00
|
|
|
type SendTx struct {
|
|
|
|
TxHash btcwire.ShaHash
|
2013-09-09 20:14:57 +02:00
|
|
|
BlockHash btcwire.ShaHash
|
|
|
|
Height int64
|
|
|
|
Fee uint64 // Measured in Satoshis
|
2013-08-22 19:14:21 +02:00
|
|
|
SenderAddr [ripemd160.Size]byte
|
|
|
|
ReceiverAddrs []struct {
|
|
|
|
Addr [ripemd160.Size]byte
|
2013-09-09 19:31:37 +02:00
|
|
|
Amt uint64 // Measured in Satoshis
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to use binaryRead and binaryWrite instead of binary.Read
|
|
|
|
// and binary.Write because those from the binary package do not return
|
|
|
|
// the number of bytes actually written or read. We need to return
|
|
|
|
// this value to correctly support the io.ReaderFrom and io.WriterTo
|
|
|
|
// interfaces.
|
|
|
|
func binaryRead(r io.Reader, order binary.ByteOrder, data interface{}) (n int64, err error) {
|
|
|
|
var read int
|
|
|
|
buf := make([]byte, binary.Size(data))
|
|
|
|
if read, err = r.Read(buf); err != nil {
|
|
|
|
return int64(read), err
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
if read < binary.Size(data) {
|
|
|
|
return int64(read), io.EOF
|
|
|
|
}
|
2013-08-22 19:14:21 +02:00
|
|
|
return int64(read), binary.Read(bytes.NewBuffer(buf), order, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See comment for binaryRead().
|
|
|
|
func binaryWrite(w io.Writer, order binary.ByteOrder, data interface{}) (n int64, err error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err = binary.Write(&buf, order, data); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
written, err := w.Write(buf.Bytes())
|
|
|
|
return int64(written), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. Utxo structs are
|
|
|
|
// read in from r until an io.EOF is reached. If an io.EOF is reached
|
|
|
|
// before a Utxo is finished being read, err will be non-nil.
|
|
|
|
func (u *UtxoStore) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
var read int64
|
|
|
|
for {
|
|
|
|
// Read Utxo
|
|
|
|
utxo := new(Utxo)
|
|
|
|
read, err = utxo.ReadFrom(r)
|
|
|
|
if err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
if read == 0 && err == io.EOF {
|
|
|
|
return n, nil
|
|
|
|
}
|
2013-08-22 19:14:21 +02:00
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
2013-09-09 20:14:57 +02:00
|
|
|
*u = append(*u, utxo)
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo satisifies the io.WriterTo interface. Each Utxo is written
|
|
|
|
// to w, prepended by a single byte header to distinguish between
|
|
|
|
// confirmed and unconfirmed outputs.
|
|
|
|
func (u *UtxoStore) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
var written int64
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, utxo := range *u {
|
2013-08-22 19:14:21 +02:00
|
|
|
// Write Utxo
|
|
|
|
written, err = utxo.WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback removes all utxos from and after the block specified
|
|
|
|
// by a block height and hash.
|
|
|
|
//
|
|
|
|
// Correct results rely on u being sorted by block height in
|
|
|
|
// increasing order.
|
|
|
|
func (u *UtxoStore) Rollback(height int64, hash *btcwire.ShaHash) (modified bool) {
|
|
|
|
s := *u
|
|
|
|
|
|
|
|
// endlen specifies the final length of the rolled-back UtxoStore.
|
|
|
|
// Past endlen, array elements are nilled. We do this instead of
|
|
|
|
// just reslicing with a shorter length to avoid leaving elements
|
|
|
|
// in the underlying array so they can be garbage collected.
|
|
|
|
endlen := len(s)
|
|
|
|
defer func() {
|
|
|
|
modified = endlen != len(s)
|
|
|
|
for i := endlen; i < len(s); i++ {
|
|
|
|
s[i] = nil
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
*u = s[:endlen]
|
|
|
|
return
|
|
|
|
}()
|
2013-08-22 19:14:21 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
|
|
if height > s[i].Height {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if height == s[i].Height && *hash == s[i].BlockHash {
|
|
|
|
endlen = i
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. A Utxo is read
|
|
|
|
// from r with the format:
|
|
|
|
//
|
2013-10-11 00:44:44 +02:00
|
|
|
// [AddrHash (20 bytes), Out (36 bytes), Subscript (varies), Amt (8 bytes), Height (8 bytes), BlockHash (32 bytes)]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is read little endian.
|
|
|
|
func (u *Utxo) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
2013-10-11 00:44:44 +02:00
|
|
|
&u.AddrHash,
|
2013-09-04 22:16:20 +02:00
|
|
|
&u.Out,
|
2013-09-09 19:31:37 +02:00
|
|
|
&u.Subscript,
|
2013-08-22 19:14:21 +02:00
|
|
|
&u.Amt,
|
|
|
|
&u.Height,
|
2013-09-09 20:14:57 +02:00
|
|
|
&u.BlockHash,
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
var read int64
|
|
|
|
for _, data := range datas {
|
2013-09-04 22:16:20 +02:00
|
|
|
if rf, ok := data.(io.ReaderFrom); ok {
|
|
|
|
read, err = rf.ReadFrom(r)
|
|
|
|
} else {
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, data)
|
|
|
|
}
|
2013-08-22 19:14:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo satisifies the io.WriterTo interface. A Utxo is written to
|
|
|
|
// w in the format:
|
|
|
|
//
|
2013-10-11 00:44:44 +02:00
|
|
|
// [AddrHash (20 bytes), Out (36 bytes), Subscript (varies), Amt (8 bytes), Height (8 bytes), BlockHash (32 bytes)]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is written little endian.
|
|
|
|
func (u *Utxo) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
2013-10-11 00:44:44 +02:00
|
|
|
&u.AddrHash,
|
2013-09-04 22:16:20 +02:00
|
|
|
&u.Out,
|
2013-09-09 19:31:37 +02:00
|
|
|
&u.Subscript,
|
2013-08-22 19:14:21 +02:00
|
|
|
&u.Amt,
|
|
|
|
&u.Height,
|
2013-09-09 20:14:57 +02:00
|
|
|
&u.BlockHash,
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
var written int64
|
2013-09-04 22:16:20 +02:00
|
|
|
for _, data := range datas {
|
|
|
|
if wt, ok := data.(io.WriterTo); ok {
|
|
|
|
written, err = wt.WriteTo(w)
|
|
|
|
} else {
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 19:31:37 +02:00
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. An OutPoint is read
|
2013-09-04 22:16:20 +02:00
|
|
|
// from r with the format:
|
|
|
|
//
|
|
|
|
// [Hash (32 bytes), Index (4 bytes)]
|
|
|
|
//
|
|
|
|
// Each field is read little endian.
|
|
|
|
func (o *OutPoint) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
|
|
|
&o.Hash,
|
|
|
|
&o.Index,
|
|
|
|
}
|
|
|
|
var read int64
|
|
|
|
for _, data := range datas {
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 19:31:37 +02:00
|
|
|
// WriteTo satisifies the io.WriterTo interface. An OutPoint is written
|
2013-09-04 22:16:20 +02:00
|
|
|
// to w in the format:
|
|
|
|
//
|
|
|
|
// [Hash (32 bytes), Index (4 bytes)]
|
|
|
|
//
|
|
|
|
// Each field is written little endian.
|
|
|
|
func (o *OutPoint) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
|
|
|
&o.Hash,
|
|
|
|
&o.Index,
|
|
|
|
}
|
|
|
|
var written int64
|
2013-08-22 19:14:21 +02:00
|
|
|
for _, data := range datas {
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. A PkScript is read
|
2013-09-09 19:31:37 +02:00
|
|
|
// from r with the format:
|
|
|
|
//
|
|
|
|
// [Length (4 byte unsigned integer), ScriptBytes (Length bytes)]
|
|
|
|
//
|
|
|
|
// Length is read little endian.
|
2013-09-09 20:14:57 +02:00
|
|
|
func (s *PkScript) ReadFrom(r io.Reader) (n int64, err error) {
|
2013-09-09 19:31:37 +02:00
|
|
|
var scriptlen uint32
|
|
|
|
var read int64
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, &scriptlen)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
|
|
|
|
scriptbuf := new(bytes.Buffer)
|
|
|
|
read, err = scriptbuf.ReadFrom(io.LimitReader(r, int64(scriptlen)))
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
*s = scriptbuf.Bytes()
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// WriteTo satisifies the io.WriterTo interface. A PkScript is written
|
2013-09-09 19:31:37 +02:00
|
|
|
// to w in the format:
|
|
|
|
//
|
|
|
|
// [Length (4 byte unsigned integer), ScriptBytes (Length bytes)]
|
|
|
|
//
|
|
|
|
// Length is written little endian.
|
2013-09-09 20:14:57 +02:00
|
|
|
func (s *PkScript) WriteTo(w io.Writer) (n int64, err error) {
|
2013-09-09 19:31:37 +02:00
|
|
|
var written int64
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, uint32(len(*s)))
|
|
|
|
if err != nil {
|
|
|
|
return n + written, nil
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
|
|
|
|
written, err = bytes.NewBuffer(*s).WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, nil
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-08-22 19:14:21 +02:00
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. A TxStore is read
|
|
|
|
// in from r with the format:
|
|
|
|
//
|
|
|
|
// [[TxHeader (1 byte), Tx (varies in size)]...]
|
|
|
|
func (txs *TxStore) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
store := []interface{}{}
|
|
|
|
defer func() {
|
|
|
|
*txs = store
|
|
|
|
}()
|
|
|
|
var read int64
|
|
|
|
for {
|
|
|
|
// Read header
|
|
|
|
var header byte
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, &header)
|
|
|
|
if err != nil {
|
|
|
|
// io.EOF is not an error here.
|
|
|
|
if err == io.EOF {
|
|
|
|
return n + read, nil
|
|
|
|
}
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
|
|
|
|
var tx io.ReaderFrom
|
|
|
|
switch header {
|
|
|
|
case RecvTxHeader:
|
|
|
|
tx = new(RecvTx)
|
|
|
|
case SendTxHeader:
|
|
|
|
tx = new(SendTx)
|
|
|
|
default:
|
2013-09-09 20:14:57 +02:00
|
|
|
return n, fmt.Errorf("unknown Tx header")
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read tx
|
|
|
|
read, err = tx.ReadFrom(r)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
2013-08-26 19:34:18 +02:00
|
|
|
|
|
|
|
store = append(store, tx)
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo satisifies the io.WriterTo interface. A TxStore is written
|
|
|
|
// to w in the format:
|
|
|
|
//
|
|
|
|
// [[TxHeader (1 byte), Tx (varies in size)]...]
|
|
|
|
func (txs *TxStore) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
store := ([]interface{})(*txs)
|
|
|
|
var written int64
|
|
|
|
for _, tx := range store {
|
|
|
|
switch tx.(type) {
|
|
|
|
case *RecvTx:
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, RecvTxHeader)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
case *SendTx:
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, SendTxHeader)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
default:
|
2013-09-09 20:14:57 +02:00
|
|
|
return n, fmt.Errorf("unknown type in TxStore")
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
wt := tx.(io.WriterTo)
|
|
|
|
written, err = wt.WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Rollback removes all txs from and after the block specified by a
|
|
|
|
// block height and hash.
|
|
|
|
//
|
|
|
|
// Correct results rely on txs being sorted by block height in
|
|
|
|
// increasing order.
|
|
|
|
func (txs *TxStore) Rollback(height int64, hash *btcwire.ShaHash) (modified bool) {
|
|
|
|
s := ([]interface{})(*txs)
|
|
|
|
|
|
|
|
// endlen specifies the final length of the rolled-back TxStore.
|
|
|
|
// Past endlen, array elements are nilled. We do this instead of
|
|
|
|
// just reslicing with a shorter length to avoid leaving elements
|
|
|
|
// in the underlying array so they can be garbage collected.
|
|
|
|
endlen := len(s)
|
|
|
|
defer func() {
|
|
|
|
modified = endlen != len(s)
|
|
|
|
for i := endlen; i < len(s); i++ {
|
|
|
|
s[i] = nil
|
|
|
|
}
|
|
|
|
*txs = s[:endlen]
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
|
|
var txheight int64
|
|
|
|
var txhash *btcwire.ShaHash
|
|
|
|
switch s[i].(type) {
|
|
|
|
case *RecvTx:
|
|
|
|
tx := s[i].(*RecvTx)
|
|
|
|
if height > tx.Height {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
txheight = tx.Height
|
|
|
|
txhash = &tx.BlockHash
|
|
|
|
case *SendTx:
|
|
|
|
tx := s[i].(*SendTx)
|
|
|
|
if height > tx.Height {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
txheight = tx.Height
|
|
|
|
txhash = &tx.BlockHash
|
|
|
|
}
|
|
|
|
if height == txheight && *hash == *txhash {
|
|
|
|
endlen = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-22 19:14:21 +02:00
|
|
|
// ReadFrom satisifies the io.ReaderFrom interface. A RecTx is read
|
|
|
|
// in from r with the format:
|
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// [TxHash (32 bytes), BlockHash (32 bytes), Height (8 bytes), Amt (8 bytes), SenderAddr (20 bytes), ReceiverAddr (20 bytes)]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is read little endian.
|
|
|
|
func (tx *RecvTx) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
|
|
|
&tx.TxHash,
|
2013-09-09 20:14:57 +02:00
|
|
|
&tx.BlockHash,
|
|
|
|
&tx.Height,
|
2013-08-22 19:14:21 +02:00
|
|
|
&tx.Amt,
|
|
|
|
&tx.SenderAddr,
|
|
|
|
&tx.ReceiverAddr,
|
|
|
|
}
|
|
|
|
var read int64
|
|
|
|
for _, data := range datas {
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo satisifies the io.WriterTo interface. A RecvTx is written to
|
|
|
|
// w in the format:
|
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// [TxHash (32 bytes), BlockHash (32 bytes), Height (8 bytes), Amt (8 bytes), SenderAddr (20 bytes), ReceiverAddr (20 bytes)]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is written little endian.
|
|
|
|
func (tx *RecvTx) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
datas := []interface{}{
|
|
|
|
&tx.TxHash,
|
2013-09-09 20:14:57 +02:00
|
|
|
&tx.BlockHash,
|
|
|
|
&tx.Height,
|
2013-08-22 19:14:21 +02:00
|
|
|
&tx.Amt,
|
|
|
|
&tx.SenderAddr,
|
|
|
|
&tx.ReceiverAddr,
|
|
|
|
}
|
|
|
|
var written int64
|
|
|
|
for _, data := range datas {
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom satisifies the io.WriterTo interface. A SendTx is read
|
|
|
|
// from r with the format:
|
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// [TxHash (32 bytes), Height (8 bytes), Fee (8 bytes), SenderAddr (20 bytes), len(ReceiverAddrs) (4 bytes), ReceiverAddrs[Addr (20 bytes), Amt (8 bytes)]...]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is read little endian.
|
|
|
|
func (tx *SendTx) ReadFrom(r io.Reader) (n int64, err error) {
|
|
|
|
var nReceivers uint32
|
|
|
|
datas := []interface{}{
|
|
|
|
&tx.TxHash,
|
2013-09-09 20:14:57 +02:00
|
|
|
&tx.Height,
|
2013-08-27 18:39:45 +02:00
|
|
|
&tx.Fee,
|
2013-08-22 19:14:21 +02:00
|
|
|
&tx.SenderAddr,
|
|
|
|
&nReceivers,
|
|
|
|
}
|
|
|
|
var read int64
|
|
|
|
for _, data := range datas {
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
}
|
|
|
|
if nReceivers == 0 {
|
|
|
|
// XXX: Is this valid? Entire output is a fee for the miner?
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.ReceiverAddrs = make([]struct {
|
|
|
|
Addr [ripemd160.Size]byte
|
2013-09-09 19:31:37 +02:00
|
|
|
Amt uint64
|
2013-08-22 19:14:21 +02:00
|
|
|
},
|
|
|
|
nReceivers)
|
|
|
|
for i := uint32(0); i < nReceivers; i++ {
|
|
|
|
datas := []interface{}{
|
|
|
|
&tx.ReceiverAddrs[i].Addr,
|
|
|
|
&tx.ReceiverAddrs[i].Amt,
|
|
|
|
}
|
|
|
|
for _, data := range datas {
|
|
|
|
read, err = binaryRead(r, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + read, err
|
|
|
|
}
|
|
|
|
n += read
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// WriteTo satisifies the io.WriterTo interface. A SendTx is written to
|
2013-08-22 19:14:21 +02:00
|
|
|
// w in the format:
|
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// [TxHash (32 bytes), Height (8 bytes), Fee (8 bytes), SenderAddr (20 bytes), len(ReceiverAddrs) (4 bytes), ReceiverAddrs[Addr (20 bytes), Amt (8 bytes)]...]
|
2013-08-22 19:14:21 +02:00
|
|
|
//
|
|
|
|
// Each field is written little endian.
|
|
|
|
func (tx *SendTx) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
nReceivers := uint32(len(tx.ReceiverAddrs))
|
|
|
|
if int64(nReceivers) != int64(len(tx.ReceiverAddrs)) {
|
2013-09-09 20:14:57 +02:00
|
|
|
return n, errors.New("too many receiving addresses")
|
2013-08-22 19:14:21 +02:00
|
|
|
}
|
|
|
|
datas := []interface{}{
|
|
|
|
&tx.TxHash,
|
2013-09-09 20:14:57 +02:00
|
|
|
&tx.Height,
|
2013-08-27 18:39:45 +02:00
|
|
|
&tx.Fee,
|
2013-08-22 19:14:21 +02:00
|
|
|
&tx.SenderAddr,
|
|
|
|
nReceivers,
|
|
|
|
}
|
|
|
|
var written int64
|
|
|
|
for _, data := range datas {
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
for i := range tx.ReceiverAddrs {
|
2013-08-22 19:14:21 +02:00
|
|
|
datas := []interface{}{
|
|
|
|
&tx.ReceiverAddrs[i].Addr,
|
|
|
|
&tx.ReceiverAddrs[i].Amt,
|
|
|
|
}
|
|
|
|
for _, data := range datas {
|
|
|
|
written, err = binaryWrite(w, binary.LittleEndian, data)
|
|
|
|
if err != nil {
|
|
|
|
return n + written, err
|
|
|
|
}
|
|
|
|
n += written
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|