From 7f478d79f6ee10847c4bcc1a1c0bca8e7f440632 Mon Sep 17 00:00:00 2001
From: Ignotus Peverell <igno.peverell@protonmail.com>
Date: Thu, 1 Mar 2018 03:09:10 +0000
Subject: [PATCH] Proper error when trying to rewind to empty genesis

---
 chain/src/sumtree.rs | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/chain/src/sumtree.rs b/chain/src/sumtree.rs
index 37e781d6f..d3eb6041b 100644
--- a/chain/src/sumtree.rs
+++ b/chain/src/sumtree.rs
@@ -682,28 +682,27 @@ fn indexes_at(block: &Block, commit_index: &ChainStore) -> Result<(u64, u64), Er
 
 	// use last regular output if we have any, otherwise last coinbase output
 	let last_output = if last_regular_output.is_some() {
-		last_regular_output
+		last_regular_output.unwrap()
 	} else if last_coinbase_output.is_some() {
-		last_coinbase_output
+		last_coinbase_output.unwrap()
 	} else {
-		None
+		return Err(Error::Other("can't get index in an empty block".to_owned()))
 	};
 
-	let out_idx = match last_output {
-		Some(output) => commit_index.get_output_pos(&output.commitment())
-			.map_err(|e| {
-				Error::StoreErr(e, format!("missing output pos for known block"))
-			})?,
-		None => 0,
-	};
+	let out_idx = commit_index
+		.get_output_pos(&last_output.commitment())
+		.map_err(|e| Error::StoreErr(e, format!("missing output pos for block")))?;
 
 	let kern_idx = match block.kernels.last() {
 		Some(kernel) => commit_index.get_kernel_pos(&kernel.excess)
 			.map_err(|e| {
-				Error::StoreErr(e, format!("missing kernel pos for known block"))
+				Error::StoreErr(e, format!("missing kernel pos for block"))
 			})?,
-		None => 0,
+		None => {
+			return Err(Error::Other("can't get index in an empty block".to_owned()));
+		}
 	};
+
 	Ok((out_idx, kern_idx))
 }