lnd.xprv/uspv/msghandler.go

98 lines
2.6 KiB
Go
Raw Normal View History

2016-01-15 06:56:25 +03:00
package uspv
import (
"fmt"
"log"
"github.com/btcsuite/btcd/wire"
)
2016-01-15 10:08:37 +03:00
func (s *SPVCon) incomingMessageHandler() {
2016-01-15 06:56:25 +03:00
for {
2016-01-19 10:43:41 +03:00
n, xm, _, err := wire.ReadMessageN(s.con, s.localVersion, s.param.Net)
2016-01-15 06:56:25 +03:00
if err != nil {
log.Printf("ReadMessageN error. Disconnecting: %s\n", err.Error())
return
}
2016-01-15 10:08:37 +03:00
s.RBytes += uint64(n)
2016-01-15 06:56:25 +03:00
// log.Printf("Got %d byte %s message\n", n, xm.Command())
switch m := xm.(type) {
case *wire.MsgVersion:
log.Printf("Got version message. Agent %s, version %d, at height %d\n",
m.UserAgent, m.ProtocolVersion, m.LastBlock)
2016-01-15 10:08:37 +03:00
s.remoteVersion = uint32(m.ProtocolVersion) // weird cast! bug?
2016-01-15 06:56:25 +03:00
case *wire.MsgVerAck:
log.Printf("Got verack. Whatever.\n")
case *wire.MsgAddr:
log.Printf("got %d addresses.\n", len(m.AddrList))
case *wire.MsgPing:
log.Printf("Got a ping message. We should pong back or they will kick us off.")
2016-01-15 10:08:37 +03:00
s.PongBack(m.Nonce)
2016-01-15 06:56:25 +03:00
case *wire.MsgPong:
log.Printf("Got a pong response. OK.\n")
case *wire.MsgMerkleBlock:
2016-01-15 13:40:56 +03:00
// log.Printf("Got merkle block message. Will verify.\n")
// fmt.Printf("%d flag bytes, %d txs, %d hashes",
// len(m.Flags), m.Transactions, len(m.Hashes))
2016-01-15 06:56:25 +03:00
txids, err := checkMBlock(m)
if err != nil {
log.Printf("Merkle block error: %s\n", err.Error())
return
}
2016-01-15 13:40:56 +03:00
fmt.Printf(" got %d txs ", len(txids))
// fmt.Printf(" = got %d txs from block %s\n",
// len(txids), m.Header.BlockSha().String())
var height uint32
if len(txids) > 0 {
// make sure block is in our store before adding txs
height, err = s.HeightFromHeader(m.Header)
if err != nil {
log.Printf("Merkle block height error: %s\n", err.Error())
continue
}
}
2016-01-15 13:40:56 +03:00
for _, txid := range txids {
err := s.TS.AddTxid(txid, height)
2016-01-15 13:40:56 +03:00
if err != nil {
log.Printf("Txid store error: %s\n", err.Error())
}
}
2016-01-15 06:56:25 +03:00
// nextReq <- true
2016-01-15 10:08:37 +03:00
case *wire.MsgHeaders:
moar, err := s.IngestHeaders(m)
if err != nil {
log.Printf("Header error: %s\n", err.Error())
return
}
if moar {
s.AskForHeaders()
}
2016-01-15 06:56:25 +03:00
case *wire.MsgTx:
2016-01-15 13:40:56 +03:00
err := s.TS.IngestTx(m)
if err != nil {
log.Printf("Incoming Tx error: %s\n", err.Error())
}
// log.Printf("Got tx %s\n", m.TxSha().String())
2016-01-15 06:56:25 +03:00
default:
log.Printf("Got unknown message type %s\n", m.Command())
}
}
return
}
// this one seems kindof pointless? could get ridf of it and let
// functions call WriteMessageN themselves...
2016-01-15 10:08:37 +03:00
func (s *SPVCon) outgoingMessageHandler() {
2016-01-15 06:56:25 +03:00
for {
2016-01-15 10:08:37 +03:00
msg := <-s.outMsgQueue
2016-01-19 10:43:41 +03:00
n, err := wire.WriteMessageN(s.con, msg, s.localVersion, s.param.Net)
2016-01-15 06:56:25 +03:00
if err != nil {
log.Printf("Write message error: %s", err.Error())
}
2016-01-15 10:08:37 +03:00
s.WBytes += uint64(n)
2016-01-15 06:56:25 +03:00
}
return
}