mutex helps, still some sequence errors from mblocks

Seems that merkle blocks can return txs out of sequence, unlike
regular blocks which have to be in temporal/sequential order.
so maybe want to absorb ALL txs from mblock, then expel all?
This commit is contained in:
Tadge Dryja 2016-01-29 00:48:14 -08:00
parent 4cd9087f9f
commit 83dff432b1

@ -7,6 +7,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"sync"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -25,6 +26,8 @@ const (
type SPVCon struct { type SPVCon struct {
con net.Conn // the (probably tcp) connection to the node con net.Conn // the (probably tcp) connection to the node
headerMutex sync.Mutex
headerFile *os.File // file for SPV headers headerFile *os.File // file for SPV headers
//[doesn't work without fancy mutexes, nevermind, just use header file] //[doesn't work without fancy mutexes, nevermind, just use header file]
@ -182,6 +185,8 @@ func (s *SPVCon) HeightFromHeader(query wire.BlockHeader) (uint32, error) {
// happened recently. // happened recently.
// seek to last header // seek to last header
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
lastPos, err := s.headerFile.Seek(-80, os.SEEK_END) lastPos, err := s.headerFile.Seek(-80, os.SEEK_END)
if err != nil { if err != nil {
return 0, err return 0, err
@ -226,6 +231,8 @@ func (s *SPVCon) AskForTx(txid wire.ShaHash) {
// We don't have it in our header file so when we get it we do both operations: // We don't have it in our header file so when we get it we do both operations:
// appending and checking the header, and checking spv proofs // appending and checking the header, and checking spv proofs
func (s *SPVCon) AskForBlock(hsh wire.ShaHash) { func (s *SPVCon) AskForBlock(hsh wire.ShaHash) {
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
gdata := wire.NewMsgGetData() gdata := wire.NewMsgGetData()
inv := wire.NewInvVect(wire.InvTypeFilteredBlock, &hsh) inv := wire.NewInvVect(wire.InvTypeFilteredBlock, &hsh)
@ -241,9 +248,13 @@ func (s *SPVCon) AskForBlock(hsh wire.ShaHash) {
fmt.Printf("AskForBlock - %s height %d\n", hsh.String(), nextHeight) fmt.Printf("AskForBlock - %s height %d\n", hsh.String(), nextHeight)
s.mBlockQueue <- hah // push height and mroot of requested block on queue s.mBlockQueue <- hah // push height and mroot of requested block on queue
s.outMsgQueue <- gdata // push request to outbox s.outMsgQueue <- gdata // push request to outbox
return
} }
func (s *SPVCon) AskForHeaders() error { func (s *SPVCon) AskForHeaders() error {
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
var hdr wire.BlockHeader var hdr wire.BlockHeader
ghdr := wire.NewMsgGetHeaders() ghdr := wire.NewMsgGetHeaders()
ghdr.ProtocolVersion = s.localVersion ghdr.ProtocolVersion = s.localVersion
@ -319,6 +330,9 @@ func (s *SPVCon) IngestMerkleBlock(m *wire.MsgMerkleBlock) error {
// it assumes we're done and returns false. If it worked it assumes there's // it assumes we're done and returns false. If it worked it assumes there's
// more to request and returns true. // more to request and returns true.
func (s *SPVCon) IngestHeaders(m *wire.MsgHeaders) (bool, error) { func (s *SPVCon) IngestHeaders(m *wire.MsgHeaders) (bool, error) {
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
var err error var err error
// seek to last header // seek to last header
_, err = s.headerFile.Seek(-80, os.SEEK_END) _, err = s.headerFile.Seek(-80, os.SEEK_END)
@ -431,6 +445,8 @@ func (s *SPVCon) PushTx(tx *wire.MsgTx) error {
} }
func (s *SPVCon) GetNextHeaderHeight() (int32, error) { func (s *SPVCon) GetNextHeaderHeight() (int32, error) {
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
info, err := s.headerFile.Stat() // get info, err := s.headerFile.Stat() // get
if err != nil { if err != nil {
return 0, err // crash if header file disappears return 0, err // crash if header file disappears
@ -478,6 +494,9 @@ func (s *SPVCon) AskForMerkBlocks(current, last int32) error {
s.SendFilter(filt) s.SendFilter(filt)
fmt.Printf("sent filter %x\n", filt.MsgFilterLoad().Filter) fmt.Printf("sent filter %x\n", filt.MsgFilterLoad().Filter)
s.headerMutex.Lock()
defer s.headerMutex.Unlock()
_, err = s.headerFile.Seek(int64((current-1)*80), os.SEEK_SET) _, err = s.headerFile.Seek(int64((current-1)*80), os.SEEK_SET)
if err != nil { if err != nil {
return err return err