mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-13 11:09:23 +01:00
spv sync works but more to add
This commit is contained in:
parent
2792554c41
commit
29a8e81029
4 changed files with 85 additions and 13 deletions
|
@ -0,0 +1,8 @@
|
||||||
|
# lnd - lightning network daemon
|
||||||
|
|
||||||
|
This repo is preliminary work on a lightning network peer to peer node and wallet.
|
||||||
|
|
||||||
|
It currently is being designed for testnet-L, a test network where all* txids are normalized. This fixes malleability but isn't something that can be cleanly done to the existing bitcoin network.
|
||||||
|
|
||||||
|
It is not yet functional, but we hope to have a proof of concept on testnet-L soon.
|
||||||
|
|
|
@ -41,9 +41,11 @@ type SPVCon struct {
|
||||||
|
|
||||||
WBytes uint64 // total bytes written
|
WBytes uint64 // total bytes written
|
||||||
RBytes uint64 // total bytes read
|
RBytes uint64 // total bytes read
|
||||||
|
|
||||||
|
TS *TxStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SPVCon) Open(remoteNode string, hfn string) error {
|
func (s *SPVCon) Open(remoteNode string, hfn string, inTs *TxStore) error {
|
||||||
// open header file
|
// open header file
|
||||||
err := s.openHeaderFile(headerFileName)
|
err := s.openHeaderFile(headerFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,6 +61,8 @@ func (s *SPVCon) Open(remoteNode string, hfn string) error {
|
||||||
s.localVersion = VERSION
|
s.localVersion = VERSION
|
||||||
s.netType = NETVERSION
|
s.netType = NETVERSION
|
||||||
|
|
||||||
|
s.TS = inTs
|
||||||
|
|
||||||
myMsgVer, err := wire.NewMsgVersionFromConn(s.con, 0, 0)
|
myMsgVer, err := wire.NewMsgVersionFromConn(s.con, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -270,6 +274,32 @@ func (s *SPVCon) IngestHeaders(m *wire.MsgHeaders) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SPVCon) AskForMerkBlocks(current, last uint32) error {
|
||||||
|
var hdr wire.BlockHeader
|
||||||
|
_, err := s.headerFile.Seek(int64(current*80), os.SEEK_SET)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for current < last {
|
||||||
|
err = hdr.Deserialize(s.headerFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
current++
|
||||||
|
|
||||||
|
bHash := hdr.BlockSha()
|
||||||
|
iv1 := wire.NewInvVect(wire.InvTypeFilteredBlock, &bHash)
|
||||||
|
gdataMsg := wire.NewMsgGetData()
|
||||||
|
err = gdataMsg.AddInvVect(iv1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.outMsgQueue <- gdataMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func sendMBReq(cn net.Conn, blkhash wire.ShaHash) error {
|
func sendMBReq(cn net.Conn, blkhash wire.ShaHash) error {
|
||||||
iv1 := wire.NewInvVect(wire.InvTypeFilteredBlock, &blkhash)
|
iv1 := wire.NewInvVect(wire.InvTypeFilteredBlock, &blkhash)
|
||||||
gdataB := wire.NewMsgGetData()
|
gdataB := wire.NewMsgGetData()
|
||||||
|
|
|
@ -31,17 +31,25 @@ func (s *SPVCon) incomingMessageHandler() {
|
||||||
case *wire.MsgPong:
|
case *wire.MsgPong:
|
||||||
log.Printf("Got a pong response. OK.\n")
|
log.Printf("Got a pong response. OK.\n")
|
||||||
case *wire.MsgMerkleBlock:
|
case *wire.MsgMerkleBlock:
|
||||||
log.Printf("Got merkle block message. Will verify.\n")
|
|
||||||
fmt.Printf("%d flag bytes, %d txs, %d hashes",
|
// log.Printf("Got merkle block message. Will verify.\n")
|
||||||
len(m.Flags), m.Transactions, len(m.Hashes))
|
// fmt.Printf("%d flag bytes, %d txs, %d hashes",
|
||||||
|
// len(m.Flags), m.Transactions, len(m.Hashes))
|
||||||
txids, err := checkMBlock(m)
|
txids, err := checkMBlock(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Merkle block error: %s\n", err.Error())
|
log.Printf("Merkle block error: %s\n", err.Error())
|
||||||
return
|
return
|
||||||
// continue
|
// continue
|
||||||
}
|
}
|
||||||
fmt.Printf(" = got %d txs from block %s\n",
|
fmt.Printf(" got %d txs ", len(txids))
|
||||||
len(txids), m.Header.BlockSha().String())
|
// fmt.Printf(" = got %d txs from block %s\n",
|
||||||
|
// len(txids), m.Header.BlockSha().String())
|
||||||
|
for _, txid := range txids {
|
||||||
|
err := s.TS.AddTxid(txid)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Txid store error: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
// nextReq <- true
|
// nextReq <- true
|
||||||
|
|
||||||
case *wire.MsgHeaders:
|
case *wire.MsgHeaders:
|
||||||
|
@ -54,8 +62,11 @@ func (s *SPVCon) incomingMessageHandler() {
|
||||||
s.AskForHeaders()
|
s.AskForHeaders()
|
||||||
}
|
}
|
||||||
case *wire.MsgTx:
|
case *wire.MsgTx:
|
||||||
|
err := s.TS.IngestTx(m)
|
||||||
log.Printf("Got tx %s\n", m.TxSha().String())
|
if err != nil {
|
||||||
|
log.Printf("Incoming Tx error: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
// log.Printf("Got tx %s\n", m.TxSha().String())
|
||||||
default:
|
default:
|
||||||
log.Printf("Got unknown message type %s\n", m.Command())
|
log.Printf("Got unknown message type %s\n", m.Command())
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type TxStore struct {
|
type TxStore struct {
|
||||||
KnownTxids []wire.ShaHash
|
KnownTxids []*wire.ShaHash
|
||||||
Utxos []Utxo // stacks on stacks
|
Utxos []Utxo // stacks on stacks
|
||||||
Sum int64 // racks on racks
|
Sum int64 // racks on racks
|
||||||
Adrs []MyAdr // endeavouring to acquire capital
|
Adrs []MyAdr // endeavouring to acquire capital
|
||||||
|
@ -19,9 +19,10 @@ type TxStore struct {
|
||||||
|
|
||||||
type Utxo struct { // cash money.
|
type Utxo struct { // cash money.
|
||||||
// combo of outpoint and txout which has all the info needed to spend
|
// combo of outpoint and txout which has all the info needed to spend
|
||||||
Op wire.OutPoint
|
Op wire.OutPoint
|
||||||
Txo wire.TxOut
|
Txo wire.TxOut
|
||||||
KeyIdx uint32 // index for private key needed to sign / spend
|
AtHeight uint32 // block height where this tx was confirmed, 0 for unconf
|
||||||
|
KeyIdx uint32 // index for private key needed to sign / spend
|
||||||
}
|
}
|
||||||
|
|
||||||
type MyAdr struct { // an address I have the private key for
|
type MyAdr struct { // an address I have the private key for
|
||||||
|
@ -38,6 +39,15 @@ func (t *TxStore) AddAdr(a btcutil.Address, kidx uint32) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add txid of interest
|
||||||
|
func (t *TxStore) AddTxid(txid *wire.ShaHash) error {
|
||||||
|
if txid == nil {
|
||||||
|
return fmt.Errorf("tried to add nil txid")
|
||||||
|
}
|
||||||
|
t.KnownTxids = append(t.KnownTxids, txid)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ... or I'm gonna fade away
|
// ... or I'm gonna fade away
|
||||||
func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
|
func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
|
||||||
if len(t.Adrs) == 0 {
|
if len(t.Adrs) == 0 {
|
||||||
|
@ -52,6 +62,18 @@ func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
|
||||||
|
|
||||||
// Ingest a tx into wallet, dealing with both gains and losses
|
// Ingest a tx into wallet, dealing with both gains and losses
|
||||||
func (t *TxStore) IngestTx(tx *wire.MsgTx) error {
|
func (t *TxStore) IngestTx(tx *wire.MsgTx) error {
|
||||||
|
var match bool
|
||||||
|
inTxid := tx.TxSha()
|
||||||
|
for _, ktxid := range t.KnownTxids {
|
||||||
|
if inTxid.IsEqual(ktxid) {
|
||||||
|
match = true
|
||||||
|
break // found tx match,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
return fmt.Errorf("we don't care about tx %s", inTxid.String())
|
||||||
|
}
|
||||||
|
|
||||||
err := t.AbsorbTx(tx)
|
err := t.AbsorbTx(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -60,6 +82,7 @@ func (t *TxStore) IngestTx(tx *wire.MsgTx) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// fmt.Printf("ingested tx %s total amt %d\n", inTxid.String(), t.Sum)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +110,7 @@ func (t *TxStore) AbsorbTx(tx *wire.MsgTx) error {
|
||||||
newop.Index = uint32(i)
|
newop.Index = uint32(i)
|
||||||
newu.Op = newop
|
newu.Op = newop
|
||||||
|
|
||||||
t.Utxos = append(t.Utxos)
|
t.Utxos = append(t.Utxos, newu)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue