From bcf8d7c97ea5b489296d24fc27af9ae2897e6742 Mon Sep 17 00:00:00 2001 From: Marco Peereboom Date: Tue, 11 Jun 2024 10:10:20 +0100 Subject: [PATCH] Ok this seems to work --- database/tbcd/level/level.go | 20 ++++++++++++++++---- service/tbc/tbc.go | 2 +- service/tbc/tbcfork_test.go | 7 +++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/database/tbcd/level/level.go b/database/tbcd/level/level.go index 6dee8a20..eeea32c2 100644 --- a/database/tbcd/level/level.go +++ b/database/tbcd/level/level.go @@ -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 ) @@ -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) @@ -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") diff --git a/service/tbc/tbc.go b/service/tbc/tbc.go index c8e74442..97393670 100644 --- a/service/tbc/tbc.go +++ b/service/tbc/tbc.go @@ -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 diff --git a/service/tbc/tbcfork_test.go b/service/tbc/tbcfork_test.go index c134ab44..55364d12 100644 --- a/service/tbc/tbcfork_test.go +++ b/service/tbc/tbcfork_test.go @@ -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.