2016-01-20 07:02:18 +03:00
|
|
|
package uspv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
2016-02-03 04:14:13 +03:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
|
|
|
|
2016-01-31 12:05:31 +03:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
|
2016-01-20 12:23:47 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2016-01-21 12:04:45 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2016-01-22 12:41:08 +03:00
|
|
|
"github.com/btcsuite/btcutil/hdkeychain"
|
2016-01-20 12:23:47 +03:00
|
|
|
|
2016-01-20 07:02:18 +03:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-01-21 12:04:45 +03:00
|
|
|
BKTUtxos = []byte("DuffelBag") // leave the rest to collect interest
|
2016-01-27 12:24:16 +03:00
|
|
|
BKTStxos = []byte("SpentTxs") // for bookkeeping
|
|
|
|
BKTTxns = []byte("Txns") // all txs we care about, for replays
|
2016-01-21 12:04:45 +03:00
|
|
|
BKTState = []byte("MiscState") // last state of DB
|
2016-01-29 06:35:49 +03:00
|
|
|
// these are in the state bucket
|
2016-02-19 12:24:23 +03:00
|
|
|
KEYNumKeys = []byte("NumKeys") // number of p2pkh keys used
|
2016-01-29 06:35:49 +03:00
|
|
|
KEYTipHeight = []byte("TipHeight") // height synced to
|
2016-01-20 07:02:18 +03:00
|
|
|
)
|
|
|
|
|
2016-01-21 08:08:05 +03:00
|
|
|
func (ts *TxStore) OpenDB(filename string) error {
|
|
|
|
var err error
|
2016-02-01 02:02:38 +03:00
|
|
|
var numKeys uint32
|
2016-01-21 08:08:05 +03:00
|
|
|
ts.StateDB, err = bolt.Open(filename, 0644, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// create buckets if they're not already there
|
2016-02-01 02:02:38 +03:00
|
|
|
err = ts.StateDB.Update(func(btx *bolt.Tx) error {
|
2016-01-27 12:24:16 +03:00
|
|
|
_, err = btx.CreateBucketIfNotExists(BKTUtxos)
|
2016-01-21 08:08:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-27 12:24:16 +03:00
|
|
|
_, err = btx.CreateBucketIfNotExists(BKTStxos)
|
2016-01-21 08:08:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-27 12:24:16 +03:00
|
|
|
_, err = btx.CreateBucketIfNotExists(BKTTxns)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-01 02:02:38 +03:00
|
|
|
sta, err := btx.CreateBucketIfNotExists(BKTState)
|
2016-01-21 12:04:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-01 02:02:38 +03:00
|
|
|
|
2016-02-19 12:24:23 +03:00
|
|
|
numKeysBytes := sta.Get(KEYNumKeys)
|
2016-02-01 02:02:38 +03:00
|
|
|
if numKeysBytes != nil { // NumKeys exists, read into uint32
|
|
|
|
buf := bytes.NewBuffer(numKeysBytes)
|
|
|
|
err := binary.Read(buf, binary.BigEndian, &numKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("db says %d keys\n", numKeys)
|
|
|
|
} else { // no adrs yet, make it 1 (why...?)
|
|
|
|
numKeys = 1
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, numKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-19 12:24:23 +03:00
|
|
|
err = sta.Put(KEYNumKeys, buf.Bytes())
|
2016-02-01 02:02:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-01-21 08:08:05 +03:00
|
|
|
return nil
|
|
|
|
})
|
2016-02-01 02:02:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ts.PopulateAdrs(numKeys)
|
2016-01-21 08:08:05 +03:00
|
|
|
}
|
|
|
|
|
2016-01-21 12:04:45 +03:00
|
|
|
// NewAdr creates a new, never before seen address, and increments the
|
|
|
|
// DB counter as well as putting it in the ram Adrs store, and returns it
|
2016-02-19 12:24:23 +03:00
|
|
|
func (ts *TxStore) NewAdr() (btcutil.Address, error) {
|
2016-01-21 12:04:45 +03:00
|
|
|
if ts.Param == nil {
|
2016-02-19 11:32:22 +03:00
|
|
|
return nil, fmt.Errorf("NewAdr error: nil param")
|
2016-01-21 12:04:45 +03:00
|
|
|
}
|
|
|
|
|
2016-02-19 11:32:22 +03:00
|
|
|
priv := new(hdkeychain.ExtendedKey)
|
|
|
|
var err error
|
|
|
|
var n uint32
|
|
|
|
var nAdr btcutil.Address
|
|
|
|
|
2016-02-19 12:24:23 +03:00
|
|
|
n = uint32(len(ts.Adrs))
|
|
|
|
priv, err = ts.rootPrivKey.Child(n + hdkeychain.HardenedKeyStart)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nAdr, err = priv.Address(ts.Param)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-21 12:04:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// total number of keys (now +1) into 4 bytes
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, n+1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write to db file
|
2016-01-27 12:24:16 +03:00
|
|
|
err = ts.StateDB.Update(func(btx *bolt.Tx) error {
|
2016-01-29 06:35:49 +03:00
|
|
|
sta := btx.Bucket(BKTState)
|
2016-02-19 12:24:23 +03:00
|
|
|
|
|
|
|
return sta.Put(KEYNumKeys, buf.Bytes())
|
|
|
|
|
2016-01-21 12:04:45 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add in to ram.
|
2016-02-01 02:02:38 +03:00
|
|
|
var ma MyAdr
|
2016-02-19 11:32:22 +03:00
|
|
|
ma.PkhAdr = nAdr
|
2016-02-01 02:02:38 +03:00
|
|
|
ma.KeyIdx = n
|
2016-02-19 12:24:23 +03:00
|
|
|
|
|
|
|
ts.Adrs = append(ts.Adrs, ma)
|
|
|
|
|
2016-02-19 11:32:22 +03:00
|
|
|
return nAdr, nil
|
2016-01-21 12:04:45 +03:00
|
|
|
}
|
|
|
|
|
2016-02-05 12:16:45 +03:00
|
|
|
// SetDBSyncHeight sets sync height of the db, indicated the latest block
|
|
|
|
// of which it has ingested all the transactions.
|
2016-01-29 06:35:49 +03:00
|
|
|
func (ts *TxStore) SetDBSyncHeight(n int32) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_ = binary.Write(&buf, binary.BigEndian, n)
|
|
|
|
|
|
|
|
return ts.StateDB.Update(func(btx *bolt.Tx) error {
|
|
|
|
sta := btx.Bucket(BKTState)
|
|
|
|
return sta.Put(KEYTipHeight, buf.Bytes())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncHeight returns the chain height to which the db has synced
|
|
|
|
func (ts *TxStore) GetDBSyncHeight() (int32, error) {
|
|
|
|
var n int32
|
|
|
|
err := ts.StateDB.View(func(btx *bolt.Tx) error {
|
|
|
|
sta := btx.Bucket(BKTState)
|
|
|
|
if sta == nil {
|
|
|
|
return fmt.Errorf("no state")
|
|
|
|
}
|
|
|
|
t := sta.Get(KEYTipHeight)
|
|
|
|
|
|
|
|
if t == nil { // no height written, so 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read 4 byte tip height to n
|
|
|
|
err := binary.Read(bytes.NewBuffer(t), binary.BigEndian, &n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:16:45 +03:00
|
|
|
// GetAllUtxos returns a slice of all utxos known to the db. empty slice is OK.
|
|
|
|
func (ts *TxStore) GetAllUtxos() ([]*Utxo, error) {
|
|
|
|
var utxos []*Utxo
|
2016-01-27 12:24:16 +03:00
|
|
|
err := ts.StateDB.View(func(btx *bolt.Tx) error {
|
|
|
|
duf := btx.Bucket(BKTUtxos)
|
|
|
|
if duf == nil {
|
|
|
|
return fmt.Errorf("no duffel bag")
|
|
|
|
}
|
2016-02-05 12:16:45 +03:00
|
|
|
return duf.ForEach(func(k, v []byte) error {
|
|
|
|
// have to copy k and v here, otherwise append will crash it.
|
|
|
|
// not quite sure why but append does weird stuff I guess.
|
|
|
|
|
|
|
|
// create a new utxo
|
|
|
|
x := make([]byte, len(k)+len(v))
|
|
|
|
copy(x, k)
|
|
|
|
copy(x[len(k):], v)
|
|
|
|
newU, err := UtxoFromBytes(x)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// and add it to ram
|
|
|
|
utxos = append(utxos, &newU)
|
|
|
|
return nil
|
|
|
|
})
|
2016-01-27 12:24:16 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-05 12:16:45 +03:00
|
|
|
return nil, err
|
2016-01-27 12:24:16 +03:00
|
|
|
}
|
2016-02-05 12:16:45 +03:00
|
|
|
return utxos, nil
|
2016-01-27 12:24:16 +03:00
|
|
|
}
|
|
|
|
|
2016-02-05 12:16:45 +03:00
|
|
|
// GetAllStxos returns a slice of all stxos known to the db. empty slice is OK.
|
|
|
|
func (ts *TxStore) GetAllStxos() ([]*Stxo, error) {
|
|
|
|
// this is almost the same as GetAllUtxos but whatever, it'd be more
|
|
|
|
// complicated to make one contain the other or something
|
|
|
|
var stxos []*Stxo
|
2016-01-31 12:05:31 +03:00
|
|
|
err := ts.StateDB.View(func(btx *bolt.Tx) error {
|
2016-02-05 12:16:45 +03:00
|
|
|
old := btx.Bucket(BKTStxos)
|
|
|
|
if old == nil {
|
|
|
|
return fmt.Errorf("no old txos")
|
2016-01-31 12:05:31 +03:00
|
|
|
}
|
2016-02-05 12:16:45 +03:00
|
|
|
return old.ForEach(func(k, v []byte) error {
|
2016-01-31 12:05:31 +03:00
|
|
|
// have to copy k and v here, otherwise append will crash it.
|
|
|
|
// not quite sure why but append does weird stuff I guess.
|
|
|
|
|
2016-02-05 12:16:45 +03:00
|
|
|
// create a new stxo
|
2016-01-31 12:05:31 +03:00
|
|
|
x := make([]byte, len(k)+len(v))
|
|
|
|
copy(x, k)
|
|
|
|
copy(x[len(k):], v)
|
2016-02-05 12:16:45 +03:00
|
|
|
newS, err := StxoFromBytes(x)
|
2016-01-31 12:05:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// and add it to ram
|
2016-02-05 12:16:45 +03:00
|
|
|
stxos = append(stxos, &newS)
|
2016-01-31 12:05:31 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-05 12:16:45 +03:00
|
|
|
return stxos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTx takes a txid and returns the transaction. If we have it.
|
|
|
|
func (ts *TxStore) GetTx(txid *wire.ShaHash) (*wire.MsgTx, error) {
|
|
|
|
rtx := wire.NewMsgTx()
|
|
|
|
|
|
|
|
err := ts.StateDB.View(func(btx *bolt.Tx) error {
|
|
|
|
txns := btx.Bucket(BKTTxns)
|
|
|
|
if txns == nil {
|
|
|
|
return fmt.Errorf("no transactions in db")
|
|
|
|
}
|
|
|
|
txbytes := txns.Get(txid.Bytes())
|
|
|
|
if txbytes == nil {
|
|
|
|
return fmt.Errorf("tx %x not in db", txid.String())
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(txbytes)
|
|
|
|
return rtx.Deserialize(buf)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rtx, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:59:40 +03:00
|
|
|
// GetTx takes a txid and returns the transaction. If we have it.
|
|
|
|
func (ts *TxStore) GetAllTxs() ([]*wire.MsgTx, error) {
|
|
|
|
var rtxs []*wire.MsgTx
|
|
|
|
|
|
|
|
err := ts.StateDB.View(func(btx *bolt.Tx) error {
|
|
|
|
txns := btx.Bucket(BKTTxns)
|
|
|
|
if txns == nil {
|
|
|
|
return fmt.Errorf("no transactions in db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return txns.ForEach(func(k, v []byte) error {
|
|
|
|
tx := wire.NewMsgTx()
|
|
|
|
buf := bytes.NewBuffer(v)
|
|
|
|
err := tx.Deserialize(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rtxs = append(rtxs, tx)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rtxs, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:16:45 +03:00
|
|
|
// GetPendingInv returns an inv message containing all txs known to the
|
|
|
|
// db which are at height 0 (not known to be confirmed).
|
|
|
|
// This can be useful on startup or to rebroadcast unconfirmed txs.
|
|
|
|
func (ts *TxStore) GetPendingInv() (*wire.MsgInv, error) {
|
|
|
|
// use a map (really a set) do avoid dupes
|
|
|
|
txidMap := make(map[wire.ShaHash]struct{})
|
|
|
|
|
|
|
|
utxos, err := ts.GetAllUtxos() // get utxos from db
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stxos, err := ts.GetAllStxos() // get stxos from db
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through utxos, adding txids of anything with height 0
|
|
|
|
for _, utxo := range utxos {
|
|
|
|
if utxo.AtHeight == 0 {
|
|
|
|
txidMap[utxo.Op.Hash] = struct{}{} // adds to map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// do the same with stxos based on height at which spent
|
|
|
|
for _, stxo := range stxos {
|
|
|
|
if stxo.SpendHeight == 0 {
|
|
|
|
txidMap[stxo.SpendTxid] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
invMsg := wire.NewMsgInv()
|
|
|
|
for txid := range txidMap {
|
|
|
|
item := wire.NewInvVect(wire.InvTypeTx, &txid)
|
|
|
|
err = invMsg.AddInvVect(item)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return inv message with all txids (maybe none)
|
|
|
|
return invMsg, nil
|
2016-01-31 12:05:31 +03:00
|
|
|
}
|
|
|
|
|
2016-01-21 12:04:45 +03:00
|
|
|
// PopulateAdrs just puts a bunch of adrs in ram; it doesn't touch the DB
|
2016-01-21 08:08:05 +03:00
|
|
|
func (ts *TxStore) PopulateAdrs(lastKey uint32) error {
|
|
|
|
for k := uint32(0); k < lastKey; k++ {
|
|
|
|
|
2016-01-22 12:41:08 +03:00
|
|
|
priv, err := ts.rootPrivKey.Child(k + hdkeychain.HardenedKeyStart)
|
2016-01-21 08:08:05 +03:00
|
|
|
if err != nil {
|
2016-01-21 12:04:45 +03:00
|
|
|
return err
|
2016-01-21 08:08:05 +03:00
|
|
|
}
|
2016-01-21 12:04:45 +03:00
|
|
|
|
|
|
|
newAdr, err := priv.Address(ts.Param)
|
2016-01-21 08:08:05 +03:00
|
|
|
if err != nil {
|
2016-01-21 12:04:45 +03:00
|
|
|
return err
|
2016-01-21 08:08:05 +03:00
|
|
|
}
|
2016-02-01 02:02:38 +03:00
|
|
|
var ma MyAdr
|
|
|
|
ma.PkhAdr = newAdr
|
|
|
|
ma.KeyIdx = k
|
|
|
|
ts.Adrs = append(ts.Adrs, ma)
|
2016-01-21 08:08:05 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-21 12:04:45 +03:00
|
|
|
|
2016-01-31 12:05:31 +03:00
|
|
|
// Ingest puts a tx into the DB atomically. This can result in a
|
|
|
|
// gain, a loss, or no result. Gain or loss in satoshis is returned.
|
2016-02-05 12:16:45 +03:00
|
|
|
func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
|
2016-01-31 12:05:31 +03:00
|
|
|
var hits uint32
|
|
|
|
var err error
|
|
|
|
var nUtxoBytes [][]byte
|
|
|
|
|
2016-02-03 04:14:13 +03:00
|
|
|
// tx has been OK'd by SPV; check tx sanity
|
|
|
|
utilTx := btcutil.NewTx(tx) // convert for validation
|
2016-02-08 06:52:45 +03:00
|
|
|
// checks basic stuff like there are inputs and ouputs
|
2016-02-03 04:14:13 +03:00
|
|
|
err = blockchain.CheckTransactionSanity(utilTx)
|
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
|
|
|
// note that you can't check signatures; this is SPV.
|
|
|
|
// 0 conf SPV means pretty much nothing. Anyone can say anything.
|
|
|
|
|
2016-02-23 03:32:58 +03:00
|
|
|
spentOPs := make([][]byte, len(tx.TxIn))
|
2016-01-31 12:05:31 +03:00
|
|
|
// before entering into db, serialize all inputs of the ingested tx
|
2016-02-23 03:32:58 +03:00
|
|
|
for i, txin := range tx.TxIn {
|
2016-02-24 13:37:52 +03:00
|
|
|
spentOPs[i], err = outPointToBytes(&txin.PreviousOutPoint)
|
2016-01-31 12:05:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
|
|
|
|
// go through txouts, and then go through addresses to match
|
|
|
|
|
|
|
|
// generate PKscripts for all addresses
|
|
|
|
wPKscripts := make([][]byte, len(ts.Adrs))
|
|
|
|
aPKscripts := make([][]byte, len(ts.Adrs))
|
|
|
|
|
|
|
|
for i, adr := range ts.Adrs {
|
2016-01-31 12:05:31 +03:00
|
|
|
// iterate through all our addresses
|
2016-02-19 11:32:22 +03:00
|
|
|
// convert regular address to witness address. (split adrs later)
|
|
|
|
wa, err := btcutil.NewAddressWitnessPubKeyHash(
|
|
|
|
adr.PkhAdr.ScriptAddress(), ts.Param)
|
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:27:29 +03:00
|
|
|
wPKscripts[i], err = txscript.PayToAddrScript(wa)
|
2016-02-19 11:32:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
aPKscripts[i], err = txscript.PayToAddrScript(adr.PkhAdr)
|
2016-01-31 12:05:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
|
2016-02-25 04:27:29 +03:00
|
|
|
// iterate through all outputs of this tx, see if we gain
|
|
|
|
for i, out := range tx.TxOut {
|
|
|
|
for j, ascr := range aPKscripts {
|
2016-02-19 11:32:22 +03:00
|
|
|
|
|
|
|
// detect p2wpkh
|
|
|
|
witBool := false
|
2016-02-25 04:27:29 +03:00
|
|
|
if bytes.Equal(out.PkScript, wPKscripts[j]) {
|
2016-02-19 11:32:22 +03:00
|
|
|
witBool = true
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:27:29 +03:00
|
|
|
if bytes.Equal(out.PkScript, ascr) || witBool { // new utxo found
|
2016-02-19 11:32:22 +03:00
|
|
|
var newu Utxo // create new utxo and copy into it
|
2016-01-31 12:05:31 +03:00
|
|
|
newu.AtHeight = height
|
2016-02-25 04:27:29 +03:00
|
|
|
newu.KeyIdx = ts.Adrs[j].KeyIdx
|
2016-01-31 12:05:31 +03:00
|
|
|
newu.Value = out.Value
|
2016-02-19 11:32:22 +03:00
|
|
|
newu.IsWit = witBool // copy witness version from pkscript
|
2016-01-31 12:05:31 +03:00
|
|
|
var newop wire.OutPoint
|
|
|
|
newop.Hash = tx.TxSha()
|
|
|
|
newop.Index = uint32(i)
|
|
|
|
newu.Op = newop
|
|
|
|
b, err := newu.ToBytes()
|
|
|
|
if err != nil {
|
|
|
|
return hits, err
|
|
|
|
}
|
|
|
|
nUtxoBytes = append(nUtxoBytes, b)
|
|
|
|
hits++
|
2016-02-25 04:27:29 +03:00
|
|
|
break // txos can match only 1 script
|
2016-01-31 12:05:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ts.StateDB.Update(func(btx *bolt.Tx) error {
|
|
|
|
// get all 4 buckets
|
|
|
|
duf := btx.Bucket(BKTUtxos)
|
|
|
|
// sta := btx.Bucket(BKTState)
|
2016-02-01 02:02:38 +03:00
|
|
|
old := btx.Bucket(BKTStxos)
|
2016-02-02 06:03:01 +03:00
|
|
|
txns := btx.Bucket(BKTTxns)
|
2016-02-05 12:16:45 +03:00
|
|
|
if duf == nil || old == nil || txns == nil {
|
|
|
|
return fmt.Errorf("error: db not initialized")
|
|
|
|
}
|
2016-01-31 12:05:31 +03:00
|
|
|
|
|
|
|
// first see if we lose utxos
|
|
|
|
// iterate through duffel bag and look for matches
|
|
|
|
// this makes us lose money, which is regrettable, but we need to know.
|
2016-02-24 13:37:52 +03:00
|
|
|
// var delOPs [][]byte
|
2016-01-31 12:05:31 +03:00
|
|
|
for _, nOP := range spentOPs {
|
2016-02-24 13:37:52 +03:00
|
|
|
v := duf.Get(nOP)
|
|
|
|
if v != nil {
|
|
|
|
hits++
|
|
|
|
// do all this just to figure out value we lost
|
|
|
|
x := make([]byte, len(nOP)+len(v))
|
|
|
|
copy(x, nOP)
|
|
|
|
copy(x[len(nOP):], v)
|
|
|
|
lostTxo, err := UtxoFromBytes(x)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// after marking for deletion, save stxo to old bucket
|
|
|
|
var st Stxo // generate spent txo
|
|
|
|
st.Utxo = lostTxo // assign outpoint
|
|
|
|
st.SpendHeight = height // spent at height
|
|
|
|
st.SpendTxid = tx.TxSha() // spent by txid
|
|
|
|
stxb, err := st.ToBytes() // serialize
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-31 12:05:31 +03:00
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
err = old.Put(nOP, stxb) // write nOP:v outpoint:stxo bytes
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// store this relevant tx
|
|
|
|
sha := tx.TxSha()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
tx.SerializeWitness(&buf) // always store witness version
|
|
|
|
err = txns.Put(sha.Bytes(), buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// stash for deletion
|
|
|
|
// delOPs = append(delOPs, nOP)
|
|
|
|
}
|
2016-02-23 10:21:54 +03:00
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
|
|
|
|
//delete everything even if it doesn't exist!
|
|
|
|
for _, dOP := range spentOPs {
|
|
|
|
err = duf.Delete(dOP)
|
2016-02-23 10:21:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-23 03:32:58 +03:00
|
|
|
}
|
2016-02-23 10:21:54 +03:00
|
|
|
}
|
|
|
|
// done losing utxos, next gain utxos
|
2016-01-31 12:05:31 +03:00
|
|
|
// next add all new utxos to db, this is quick as the work is above
|
|
|
|
for _, ub := range nUtxoBytes {
|
|
|
|
err = duf.Put(ub[:36], ub[36:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return hits, err
|
|
|
|
}
|