fix bolt slices. db read / write works.

also switch heights from uint32 to int32, which I guess is what they use.
Makes this thing last 2 billion blocks shorter.  If we get past uint32 time.
This commit is contained in:
Tadge Dryja 2016-01-19 23:40:04 -08:00
parent 0b5daa9b2b
commit 29d0c0cc6f
3 changed files with 27 additions and 15 deletions

@ -111,6 +111,9 @@ func OpenSPV(remoteNode string, hfn string,
log.Printf("remote reports version %x (dec %d)\n",
mv.ProtocolVersion, mv.ProtocolVersion)
// set remote height
s.remoteHeight = mv.LastBlock
mva := wire.NewMsgVerAck()
n, err = wire.WriteMessageN(s.con, mva, s.localVersion, s.param.Net)
if err != nil {
@ -118,9 +121,9 @@ func OpenSPV(remoteNode string, hfn string,
}
s.WBytes += uint64(n)
s.inMsgQueue = make(chan wire.Message)
s.inMsgQueue = make(chan wire.Message, 1)
go s.incomingMessageHandler()
s.outMsgQueue = make(chan wire.Message)
s.outMsgQueue = make(chan wire.Message, 1)
go s.outgoingMessageHandler()
s.mBlockQueue = make(chan RootAndHeight, 32) // queue depth 32 is a thing
@ -353,11 +356,11 @@ func (s *SPVCon) IngestHeaders(m *wire.MsgHeaders) (bool, error) {
// look them up from the disk.
type RootAndHeight struct {
root wire.ShaHash
height uint32
height int32
}
// NewRootAndHeight saves like 2 lines.
func NewRootAndHeight(r wire.ShaHash, h uint32) (rah RootAndHeight) {
func NewRootAndHeight(r wire.ShaHash, h int32) (rah RootAndHeight) {
rah.root = r
rah.height = h
return
@ -366,8 +369,17 @@ func NewRootAndHeight(r wire.ShaHash, h uint32) (rah RootAndHeight) {
// AskForMerkBlocks requests blocks from current to last
// right now this asks for 1 block per getData message.
// Maybe it's faster to ask for many in a each message?
func (s *SPVCon) AskForMerkBlocks(current, last uint32) error {
func (s *SPVCon) AskForMerkBlocks(current, last int32) error {
var hdr wire.BlockHeader
// if last is 0, that means go as far as we can
if last == 0 {
n, err := s.headerFile.Seek(-80, os.SEEK_END)
if err != nil {
return err
}
last = int32(n / 80)
}
_, err := s.headerFile.Seek(int64(current*80), os.SEEK_SET)
if err != nil {
return err

@ -12,7 +12,7 @@ import (
)
type TxStore struct {
OKTxids map[wire.ShaHash]uint32 // known good txids and their heights
OKTxids map[wire.ShaHash]int32 // known good txids and their heights
Utxos []Utxo // stacks on stacks
Sum int64 // racks on racks
@ -22,7 +22,7 @@ type TxStore struct {
type Utxo struct { // cash money.
// combo of outpoint and txout which has all the info needed to spend
AtHeight uint32 // block height where this tx was confirmed, 0 for unconf
AtHeight int32 // block height where this tx was confirmed, 0 for unconf
KeyIdx uint32 // index for private key needed to sign / spend
Op wire.OutPoint // where
Txo wire.TxOut // what
@ -35,7 +35,7 @@ type MyAdr struct { // an address I have the private key for
func NewTxStore() TxStore {
var txs TxStore
txs.OKTxids = make(map[wire.ShaHash]uint32)
txs.OKTxids = make(map[wire.ShaHash]int32)
return txs
}
@ -49,7 +49,7 @@ func (t *TxStore) AddAdr(a btcutil.Address, kidx uint32) {
}
// add txid of interest
func (t *TxStore) AddTxid(txid *wire.ShaHash, height uint32) error {
func (t *TxStore) AddTxid(txid *wire.ShaHash, height int32) error {
if txid == nil {
return fmt.Errorf("tried to add nil txid")
}
@ -92,7 +92,7 @@ func (t *TxStore) IngestTx(tx *wire.MsgTx) error {
}
// Absorb money into wallet from a tx
func (t *TxStore) AbsorbTx(tx *wire.MsgTx, height uint32) error {
func (t *TxStore) AbsorbTx(tx *wire.MsgTx, height int32) error {
if tx == nil {
return fmt.Errorf("Tried to add nil tx")
}

@ -27,7 +27,6 @@ func (u *Utxo) SaveToDB(dbx *bolt.DB) error {
}
func (ts *TxStore) LoadUtxos() error {
var kc, vc []byte
err := ts.StateDB.View(func(tx *bolt.Tx) error {
duf := tx.Bucket(BKTUtxos)
@ -42,11 +41,12 @@ func (ts *TxStore) LoadUtxos() error {
duf.ForEach(func(k, v []byte) error {
// have to copy these here, otherwise append will crash it.
// not quite sure why but append does weird stuff I guess.
copy(kc, k)
copy(vc, v)
if spent.Get(kc) == nil { // if it's not in the spent bucket
if spent.Get(k) == nil { // if it's not in the spent bucket
// create a new utxo
newU, err := UtxoFromBytes(append(kc, vc...))
x := make([]byte, len(k)+len(v))
copy(x, k)
copy(x[len(k):], v)
newU, err := UtxoFromBytes(x)
if err != nil {
return err
}