add shell spv testing mode

To try uspv, do ./lnd -spv
The remote node is hardcoded in shell.go.  If you aren't
running a full node on localhost, specify where to connect to.
Nearby nodes will be much faster but random testnet nodes on the
internet should also work.
This commit is contained in:
Tadge Dryja committed 2016-02-02 23:37:29 -08:00
1 parent 2f9fc87636
commit 624e776987
7 files changed
+358 -20

No files matched your search

+12 -14
View File
@@ -43,8 +43,7 @@ type SPVCon struct {
WBytes uint64 // total bytes written
RBytes uint64 // total bytes read
TS *TxStore // transaction store to write to
param *chaincfg.Params // network parameters (testnet3, testnetL)
TS *TxStore // transaction store to write to
// mBlockQueue is for keeping track of what height we've requested.
mBlockQueue chan HashAndHeight
@@ -56,16 +55,17 @@ type SPVCon struct {
inWaitState chan bool
}
func OpenSPV(remoteNode string, hfn, tsfn string,
func OpenSPV(remoteNode string, hfn, dbfn string,
inTs *TxStore, p *chaincfg.Params) (SPVCon, error) {
// create new SPVCon
var s SPVCon
// assign network parameters to SPVCon
s.param = p
// I should really merge SPVCon and TxStore, they're basically the same
inTs.Param = p
s.TS = inTs // copy pointer of txstore into spvcon
// open header file
err := s.openHeaderFile(headerFileName)
err := s.openHeaderFile(hfn)
if err != nil {
return s, err
}
@@ -80,12 +80,10 @@ func OpenSPV(remoteNode string, hfn, tsfn string,
s.localVersion = VERSION
// transaction store for this SPV connection
inTs.Param = p
err = inTs.OpenDB(tsfn)
err = inTs.OpenDB(dbfn)
if err != nil {
return s, err
}
s.TS = inTs // copy pointer of txstore into spvcon
myMsgVer, err := wire.NewMsgVersionFromConn(s.con, 0, 0)
if err != nil {
@@ -99,7 +97,7 @@ func OpenSPV(remoteNode string, hfn, tsfn string,
myMsgVer.AddService(wire.SFNodeBloom)
// this actually sends
n, err := wire.WriteMessageN(s.con, myMsgVer, s.localVersion, s.param.Net)
n, err := wire.WriteMessageN(s.con, myMsgVer, s.localVersion, s.TS.Param.Net)
if err != nil {
return s, err
}
@@ -107,7 +105,7 @@ func OpenSPV(remoteNode string, hfn, tsfn string,
log.Printf("wrote %d byte version message to %s\n",
n, s.con.RemoteAddr().String())
n, m, b, err := wire.ReadMessageN(s.con, s.localVersion, s.param.Net)
n, m, b, err := wire.ReadMessageN(s.con, s.localVersion, s.TS.Param.Net)
if err != nil {
return s, err
}
@@ -126,7 +124,7 @@ func OpenSPV(remoteNode string, hfn, tsfn string,
s.remoteHeight = mv.LastBlock
mva := wire.NewMsgVerAck()
n, err = wire.WriteMessageN(s.con, mva, s.localVersion, s.param.Net)
n, err = wire.WriteMessageN(s.con, mva, s.localVersion, s.TS.Param.Net)
if err != nil {
return s, err
}
@@ -149,7 +147,7 @@ func (s *SPVCon) openHeaderFile(hfn string) error {
if err != nil {
if os.IsNotExist(err) {
var b bytes.Buffer
err = s.param.GenesisBlock.Header.Serialize(&b)
err = s.TS.Param.GenesisBlock.Header.Serialize(&b)
if err != nil {
return err
}
@@ -405,7 +403,7 @@ func (s *SPVCon) IngestHeaders(m *wire.MsgHeaders) (bool, error) {
// advance chain tip
tip++
// check last header
worked := CheckHeader(s.headerFile, tip, s.param)
worked := CheckHeader(s.headerFile, tip, s.TS.Param)
if !worked {
if endPos < 8080 {
// jeez I give up, back to genesis
+2 -2
View File
@@ -10,7 +10,7 @@ import (
func (s *SPVCon) incomingMessageHandler() {
for {
n, xm, _, err := wire.ReadMessageN(s.con, s.localVersion, s.param.Net)
n, xm, _, err := wire.ReadMessageN(s.con, s.localVersion, s.TS.Param.Net)
if err != nil {
log.Printf("ReadMessageN error. Disconnecting: %s\n", err.Error())
return
@@ -64,7 +64,7 @@ func (s *SPVCon) incomingMessageHandler() {
func (s *SPVCon) outgoingMessageHandler() {
for {
msg := <-s.outMsgQueue
n, err := wire.WriteMessageN(s.con, msg, s.localVersion, s.param.Net)
n, err := wire.WriteMessageN(s.con, msg, s.localVersion, s.TS.Param.Net)
if err != nil {
log.Printf("Write message error: %s", err.Error())
}
+4 -2
View File
@@ -20,7 +20,8 @@ type TxStore struct {
Adrs []MyAdr // endeavouring to acquire capital
StateDB *bolt.DB // place to write all this down
// this is redundant with the SPVCon param... ugly and should be taken out
// Params live here, not SCon
Param *chaincfg.Params // network parameters (testnet3, testnetL)
// From here, comes everything. It's a secret to everybody.
@@ -52,9 +53,10 @@ type MyAdr struct { // an address I have the private key for
// inside the Adrs slice, right? leave for now
}
func NewTxStore(rootkey *hdkeychain.ExtendedKey) TxStore {
func NewTxStore(rootkey *hdkeychain.ExtendedKey, p *chaincfg.Params) TxStore {
var txs TxStore
txs.rootPrivKey = rootkey
txs.Param = p
txs.OKTxids = make(map[wire.ShaHash]int32)
return txs
}