update filters as txs come in; missed some before

This commit is contained in:
Tadge Dryja 2016-01-21 21:50:42 -08:00
parent fc100921e8
commit fbc424492d
4 changed files with 41 additions and 8 deletions

@ -164,6 +164,7 @@ func (s *SPVCon) PongBack(nonce uint64) {
func (s *SPVCon) SendFilter(f *bloom.Filter) {
s.outMsgQueue <- f.MsgFilterLoad()
return
}
@ -374,12 +375,35 @@ func (s *SPVCon) AskForMerkBlocks(current, last int32) error {
last = int32(n / 80)
}
_, err := s.headerFile.Seek(int64(current*80), os.SEEK_SET)
// track number of utxos
track := len(s.TS.Utxos)
// create initial filter
filt, err := s.TS.GimmeFilter()
if err != nil {
return err
}
// send filter
s.SendFilter(filt)
fmt.Printf("sent filter %x\n", filt.MsgFilterLoad().Filter)
_, err = s.headerFile.Seek(int64(current*80), os.SEEK_SET)
if err != nil {
return err
}
// loop through all heights where we want merkleblocks.
for current < last {
// check if we need to update filter... every 5 new inputs...?
if track+4 < len(s.TS.Utxos) {
track = len(s.TS.Utxos)
filt, err := s.TS.GimmeFilter()
if err != nil {
return err
}
s.SendFilter(filt)
fmt.Printf("sent filter %x\n", filt.MsgFilterLoad().Filter)
}
// load header from file
err = hdr.Deserialize(s.headerFile)
if err != nil {
@ -400,6 +424,5 @@ func (s *SPVCon) AskForMerkBlocks(current, last int32) error {
s.mBlockQueue <- rah // push height and mroot of requested block on queue
current++
}
return nil
}

@ -1,7 +1,6 @@
package uspv
import (
"fmt"
"log"
"github.com/btcsuite/btcd/wire"
@ -40,7 +39,7 @@ func (s *SPVCon) incomingMessageHandler() {
log.Printf("Merkle block error: %s\n", err.Error())
return
}
fmt.Printf(" got %d txs ", len(txids))
// fmt.Printf(" got %d txs ", len(txids))
// fmt.Printf(" = got %d txs from block %s\n",
// len(txids), m.Header.BlockSha().String())
rah := <-s.mBlockQueue // pop height off mblock queue
@ -70,7 +69,12 @@ func (s *SPVCon) incomingMessageHandler() {
if err != nil {
log.Printf("Incoming Tx error: %s\n", err.Error())
}
// log.Printf("Got tx %s\n", m.TxSha().String())
// log.Printf("Got tx %s\n", m.TxSha().String())
case *wire.MsgReject:
log.Printf("Rejected! cmd: %s code: %s tx: %s reason: %s",
m.Cmd, m.Code.String(), m.Hash.String(), m.Reason)
default:
log.Printf("Got unknown message type %s\n", m.Command())
}

@ -78,10 +78,17 @@ func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
if len(t.Adrs) == 0 {
return nil, fmt.Errorf("no addresses to filter for")
}
f := bloom.NewFilter(uint32(len(t.Adrs)), 0, 0.001, wire.BloomUpdateNone)
// add addresses to look for incoming
elem := uint32(len(t.Adrs) + len(t.Utxos))
f := bloom.NewFilter(elem, 0, 0.001, wire.BloomUpdateAll)
for _, a := range t.Adrs {
f.Add(a.PkhAdr.ScriptAddress())
}
// add txids of utxos to look for outgoing
for _, u := range t.Utxos {
f.AddOutPoint(&u.Op)
}
return f, nil
}
@ -137,7 +144,6 @@ func (t *TxStore) AbsorbTx(tx *wire.MsgTx, height int32) error {
if err != nil {
return err
}
t.Sum += newu.Value
t.Utxos = append(t.Utxos, newu)
break
}

@ -185,8 +185,8 @@ func (ts *TxStore) LoadFromDB() error {
return err
}
// and add it to ram
ts.Sum += newU.Value
ts.Utxos = append(ts.Utxos, newU)
ts.Sum += newU.Value
} else {
fmt.Printf("had utxo %x but spent by tx %x...\n",
k, stx[:8])