From fec37dff9a8ff239fd78ec688e9d6d8497564542 Mon Sep 17 00:00:00 2001 From: mdr0id Date: Tue, 16 Jul 2019 19:14:58 -0700 Subject: [PATCH] Initial reorg fix --- cmd/ingest/main.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/ingest/main.go b/cmd/ingest/main.go index a1a9cca..8481781 100644 --- a/cmd/ingest/main.go +++ b/cmd/ingest/main.go @@ -128,9 +128,17 @@ func main() { } timeout_count := 0 + reorg_count := -1 + hash := "" + phash := "" // Start listening for new blocks for { + if reorg_count > 0 { + reorg_count = -1 + height -= 10 + } block, err := getBlock(rpcClient, height) + if err != nil { log.WithFields(logrus.Fields{ "height": height, @@ -146,11 +154,27 @@ func main() { } if block != nil { handleBlock(db, block) - height++ if timeout_count > 0 { timeout_count-- } - //TODO store block current/prev hash for formal reorg + phash = hex.EncodeToString(block.GetPrevHash()) + //check for reorgs once we have inital block hash from startup + if hash != phash && reorg_count != -1 { + reorg_count++ + log.WithFields(logrus.Fields{ + "height": height, + "hash": hash, + "phash": phash, + "reorg": reorg_count, + }).Warn("REORG") + } else { + hash = hex.EncodeToString(block.GetDisplayHash()) + } + if reorg_count == -1 { + hash = hex.EncodeToString(block.GetDisplayHash()) + reorg_count =0 + } + height++ } else { //TODO implement blocknotify to minimize polling on corner cases time.Sleep(60 * time.Second) @@ -204,7 +228,6 @@ func getBlock(rpcClient *rpcclient.Client, height int) (*parser.Block, error) { func handleBlock(db *sql.DB, block *parser.Block) { prevBlockHash := hex.EncodeToString(block.GetPrevHash()) blockHash := hex.EncodeToString(block.GetEncodableHash()) - //blockHash := hex.EncodeToString(block.GetDisplayHash()) marshaledBlock, _ := proto.Marshal(block.ToCompact()) err := storage.StoreBlock(