From f9c5505e9f66e9ecf421811abbbae0afe300c570 Mon Sep 17 00:00:00 2001
From: eupn <36292692+eupn@users.noreply.github.com>
Date: Sat, 25 May 2019 02:44:28 +0700
Subject: [PATCH] Report reorg depth in block status (#2839)
* Calculate reorg depth in BlockStatus::Reorg enum member
* rustfmt
* Fix reorg height calculation and implement reorg test
* rustfmt
* Report reorg depth in webhook payload
* Add optional depth field to the block webhook JSON reply
---
chain/src/chain.rs | 5 +-
chain/src/types.rs | 2 +-
chain/tests/mine_simple_chain.rs | 109 +++++++++++++++++++++++++++++++
servers/src/common/adapters.rs | 9 ++-
servers/src/common/hooks.rs | 32 ++++++---
5 files changed, 144 insertions(+), 13 deletions(-)
diff --git a/chain/src/chain.rs b/chain/src/chain.rs
index 5d359b513..ca5311595 100644
--- a/chain/src/chain.rs
+++ b/chain/src/chain.rs
@@ -254,15 +254,18 @@ impl Chain {
let is_more_work = head.is_some();
let mut is_next_block = false;
+ let mut reorg_depth = None;
if let Some(head) = head {
if head.prev_block_h == prev_head.last_block_h {
is_next_block = true;
+ } else {
+ reorg_depth = Some(prev_head.height.saturating_sub(head.height) + 1);
}
}
match (is_more_work, is_next_block) {
(true, true) => BlockStatus::Next,
- (true, false) => BlockStatus::Reorg,
+ (true, false) => BlockStatus::Reorg(reorg_depth.unwrap_or(0)),
(false, _) => BlockStatus::Fork,
}
}
diff --git a/chain/src/types.rs b/chain/src/types.rs
index 27895e7c4..b3f7b782b 100644
--- a/chain/src/types.rs
+++ b/chain/src/types.rs
@@ -169,5 +169,5 @@ pub enum BlockStatus {
Fork,
/// Block updates the chain head via a (potentially disruptive) "reorg".
/// Previous block was not our previous chain head.
- Reorg,
+ Reorg(u64),
}
diff --git a/chain/tests/mine_simple_chain.rs b/chain/tests/mine_simple_chain.rs
index 80da3b0ef..a0d31f522 100644
--- a/chain/tests/mine_simple_chain.rs
+++ b/chain/tests/mine_simple_chain.rs
@@ -26,9 +26,11 @@ use self::keychain::{ExtKeychain, ExtKeychainPath, Keychain};
use self::util::{RwLock, StopState};
use chrono::Duration;
use grin_chain as chain;
+use grin_chain::{BlockStatus, ChainAdapter, Options};
use grin_core as core;
use grin_keychain as keychain;
use grin_util as util;
+use std::cell::RefCell;
use std::fs;
use std::sync::Arc;
@@ -51,6 +53,41 @@ fn setup(dir_name: &str, genesis: Block) -> Chain {
.unwrap()
}
+/// Adapter to retrieve last status
+pub struct StatusAdapter {
+ pub last_status: RwLock