2016-02-07 06:15:35 +03:00
|
|
|
package uspv
|
|
|
|
|
|
|
|
import (
|
2016-02-14 01:03:11 +03:00
|
|
|
"bytes"
|
2016-02-07 06:15:35 +03:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-02-08 06:52:45 +03:00
|
|
|
"sync"
|
2016-02-07 06:15:35 +03:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockRootOK checks that all the txs in the block match the merkle root.
|
|
|
|
// Only checks merkle root; it doesn't look at txs themselves.
|
|
|
|
func BlockRootOK(blk wire.MsgBlock) bool {
|
|
|
|
var shas []*wire.ShaHash
|
|
|
|
for _, tx := range blk.Transactions { // make slice of txids
|
|
|
|
nSha := tx.TxSha()
|
|
|
|
shas = append(shas, &nSha)
|
|
|
|
}
|
|
|
|
neededLen := int(nextPowerOfTwo(uint32(len(shas)))) // kindof ugly
|
|
|
|
for len(shas) < neededLen {
|
2016-02-07 09:48:54 +03:00
|
|
|
shas = append(shas, nil) // pad out tx slice to get the full tree base
|
2016-02-07 06:15:35 +03:00
|
|
|
}
|
2016-02-07 09:48:54 +03:00
|
|
|
for len(shas) > 1 { // calculate merkle root. Terse, eh?
|
2016-02-07 06:15:35 +03:00
|
|
|
shas = append(shas[2:], MakeMerkleParent(shas[0], shas[1]))
|
2016-02-08 06:52:45 +03:00
|
|
|
} // auto recognizes coinbase-only blocks
|
2016-02-14 01:03:11 +03:00
|
|
|
fmt.Printf("MRs calcd %s given %s\n",
|
|
|
|
shas[0].String(), blk.Header.MerkleRoot.String())
|
2016-02-07 09:48:54 +03:00
|
|
|
return blk.Header.MerkleRoot.IsEqual(shas[0])
|
2016-02-07 06:15:35 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 09:48:54 +03:00
|
|
|
// IngestBlock is like IngestMerkleBlock but aralphic
|
|
|
|
// different enough that it's better to have 2 separate functions
|
2016-02-07 06:15:35 +03:00
|
|
|
func (s *SPVCon) IngestBlock(m *wire.MsgBlock) {
|
2016-02-07 09:48:54 +03:00
|
|
|
var err error
|
2016-02-14 01:03:11 +03:00
|
|
|
var buf bytes.Buffer
|
|
|
|
m.SerializeWitness(&buf)
|
|
|
|
fmt.Printf("block hex %x\n", buf.Bytes())
|
|
|
|
for i, tx := range m.Transactions {
|
|
|
|
// if i > 0 {
|
|
|
|
fmt.Printf("wtxid: %s\n", tx.WTxSha())
|
|
|
|
fmt.Printf("txid: %s\n", tx.TxSha())
|
|
|
|
fmt.Printf("%d %s", i, TxToString(tx))
|
|
|
|
// }
|
|
|
|
}
|
2016-02-07 09:48:54 +03:00
|
|
|
ok := BlockRootOK(*m) // check block self-consistency
|
2016-02-07 06:15:35 +03:00
|
|
|
if !ok {
|
|
|
|
fmt.Printf("block %s not OK!!11\n", m.BlockSha().String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-07 09:48:54 +03:00
|
|
|
var hah HashAndHeight
|
|
|
|
select { // select here so we don't block on an unrequested mblock
|
|
|
|
case hah = <-s.blockQueue: // pop height off mblock queue
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Printf("Unrequested full block")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newBlockSha := m.Header.BlockSha()
|
|
|
|
if !hah.blockhash.IsEqual(&newBlockSha) {
|
|
|
|
log.Printf("full block out of order error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through all txs in the block, looking for matches.
|
|
|
|
// this is slow and can be sped up by doing in-ram filters client side.
|
|
|
|
// kindof a pain to implement though and it's fast enough for now.
|
2016-02-08 06:52:45 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(m.Transactions))
|
2016-02-07 09:48:54 +03:00
|
|
|
for i, tx := range m.Transactions {
|
2016-02-08 06:52:45 +03:00
|
|
|
go func() {
|
|
|
|
hits, err := s.TS.Ingest(tx, hah.height)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Incoming Tx error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hits > 0 {
|
|
|
|
log.Printf("block %d tx %d %s ingested and matches %d utxo/adrs.",
|
|
|
|
hah.height, i, tx.TxSha().String(), hits)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2016-02-07 09:48:54 +03:00
|
|
|
}
|
2016-02-08 06:52:45 +03:00
|
|
|
wg.Wait()
|
2016-02-07 09:48:54 +03:00
|
|
|
|
|
|
|
// write to db that we've sync'd to the height indicated in the
|
|
|
|
// merkle block. This isn't QUITE true since we haven't actually gotten
|
|
|
|
// the txs yet but if there are problems with the txs we should backtrack.
|
|
|
|
err = s.TS.SetDBSyncHeight(hah.height)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("full block sync error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("ingested full block %s height %d OK\n",
|
|
|
|
m.Header.BlockSha().String(), hah.height)
|
|
|
|
|
|
|
|
if hah.final { // check sync end
|
|
|
|
// don't set waitstate; instead, ask for headers again!
|
|
|
|
// this way the only thing that triggers waitstate is asking for headers,
|
|
|
|
// getting 0, calling AskForMerkBlocks(), and seeing you don't need any.
|
|
|
|
// that way you are pretty sure you're synced up.
|
|
|
|
err = s.AskForHeaders()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Merkle block error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 06:15:35 +03:00
|
|
|
return
|
|
|
|
}
|