// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/validation.h>

#include <consensus/validation.h>
#include <tinyformat.h>

std::string FormatStateMessage(const ValidationState &state) {
    if (state.IsValid()) {
        return "Valid";
    }

    const std::string debug_message = state.GetDebugMessage();
    if (!debug_message.empty()) {
        return strprintf("%s, %s (code %i)", state.GetRejectReason(),
                         debug_message, state.GetRejectCode());
    }

    return strprintf("%s (code %i)", state.GetRejectReason(),
                     state.GetRejectCode());
}

const std::string strMessageMagic = "Bitcoin Signed Message:\n";

// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/validation.h>

#include <consensus/validation.h>
#include <tinyformat.h>

/** Convert ValidationState to a human-readable message for logging */
std::string FormatStateMessage(const ValidationState &state) {
    return strprintf(
        "%s%s (code %i)", state.GetRejectReason(),
        state.GetDebugMessage().empty() ? "" : ", " + state.GetDebugMessage(),
        state.GetRejectCode());
}

const std::string strMessageMagic = "Bitcoin Signed Message:\n";

--- 
+++ 
@@ -8,12 +8,19 @@
 #include <consensus/validation.h>
 #include <tinyformat.h>
 
-/** Convert ValidationState to a human-readable message for logging */
 std::string FormatStateMessage(const ValidationState &state) {
-    return strprintf(
-        "%s%s (code %i)", state.GetRejectReason(),
-        state.GetDebugMessage().empty() ? "" : ", " + state.GetDebugMessage(),
-        state.GetRejectCode());
+    if (state.IsValid()) {
+        return "Valid";
+    }
+
+    const std::string debug_message = state.GetDebugMessage();
+    if (!debug_message.empty()) {
+        return strprintf("%s, %s (code %i)", state.GetRejectReason(),
+                         debug_message, state.GetRejectCode());
+    }
+
+    return strprintf("%s (code %i)", state.GetRejectReason(),
+                     state.GetRejectCode());
 }
 
 const std::string strMessageMagic = "Bitcoin Signed Message:\n";
Refactor FormatStateMessage() to better match Core

Summary:
This completes the backport of Core [[https://github.com/bitcoin/bitcoin/pull/16688 | PR16688]] (see my note in the summary of D6652).
We diverge slightly due to a not-yet-backported change that will remove the RejectCode.

Depends on D6860

Test Plan: `ninja check check-functional`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D6862
