Skip to content

Commit

Permalink
Ok this seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jun 11, 2024
1 parent 3de9272 commit bcf8d7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
20 changes: 16 additions & 4 deletions database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
logLevel = "INFO"
verbose = false

bhsLastKey = "last"
bhsLastKey = "last" // XXX rename best

minPeersRequired = 64 // minimum number of peers in good map before cache is purged
)
Expand Down Expand Up @@ -462,11 +462,21 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs [][80]byte) (tbcd.Inse
}
return tbcd.ITInvalid, nil, fmt.Errorf("best block header: %v", err)
}
bestBH := decodeBlockHeader(bbh)
bestBH := decodeBlockHeader(bbh) // XXX shouldn't this return chainhashes?

// Fork is set to true if the first blockheader does not connect to the
// canonical blockheader.
fork := !bytes.Equal(wbh.PrevBlock[:], bestBH.Hash[:])
if fork {
b, _ := chainhash.NewHash(bestBH.Hash[:])
log.Infof("=== FORK ===")
log.Infof("blockheader hash: %v", wbh.BlockHash())
log.Infof("previous hash : %v", wbh.PrevBlock)
log.Infof("previous height : %v", pbh.Height)
log.Infof("best hash : %v", b)
log.Infof("best height : %v", bestBH.Height)
log.Infof("--- FORK ---")
}

// Insert missing blocks and block headers
hhBatch := new(leveldb.Batch)
Expand Down Expand Up @@ -536,11 +546,13 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs [][80]byte) (tbcd.Inse
// log.Infof("%v", spew.Sdump(firstHash))
// Extend current best tip
bhsBatch.Put([]byte(bhsLastKey), lastRecord)
// pick the right return value based on ancestorA
if fork { // bytes.Equal(firstHash[:], bestBH.Hash[:]) {
// pick the right return value based on ancestor
if fork {
it = tbcd.ITChainFork
log.Infof("FORK FORK FORK")
} else {
it = tbcd.ITChainExtend
log.Infof("EXTENDED EXTENDED EXTENDED")
}
default:
panic("impossible cmp")
Expand Down
2 changes: 1 addition & 1 deletion service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ func (s *Server) handleBlock(ctx context.Context, p *peer, msg *wire.MsgBlock) {
}

// Whatever happens, delete from cache and potentially try again
// log.Infof("inserted block at height %d, parent hash %s", height, block.MsgBlock().Header.PrevBlock)
log.Infof("inserted block at height %d, parent hash %s", height, block.MsgBlock().Header.PrevBlock)
var (
printStats bool
blocksSize uint64
Expand Down
7 changes: 7 additions & 0 deletions service/tbc/tbcfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ func TestFork(t *testing.T) {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)

log.Infof("did we fork?")

// Dump best chain
if err = n.dumpChain(n.Best()[0]); err != nil {
t.Fatal(err)
}
}

// XXX this needs to actually test stuff. RN it is visual only.
Expand Down

0 comments on commit bcf8d7c

Please sign in to comment.