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

#ifndef BITCOIN_DEPLOYMENTSTATUS_H
#define BITCOIN_DEPLOYMENTSTATUS_H

#include <chain.h>
#include <versionbits.h>

#include <limits>

/** Determine if a deployment is active for the next block */
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache)
{
    assert(Consensus::ValidDeployment(dep));
    return (pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1) >= params.DeploymentHeight(dep);
}

inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
{
    assert(Consensus::ValidDeployment(dep));
    if (ThresholdState::ACTIVE != versionbitscache.State(pindexPrev, params, dep)) return false;

    const auto& deployment = params.vDeployments[dep];
    // Permanent deployment (never expires)
    if (deployment.active_duration == std::numeric_limits<int>::max()) return true;

    const int activation_height = versionbitscache.StateSinceHeight(pindexPrev, params, dep);
    const int height = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
    return height < activation_height + deployment.active_duration;
}

/** Determine if a deployment is active for this block */
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache)
{
    assert(Consensus::ValidDeployment(dep));
    return index.nHeight >= params.DeploymentHeight(dep);
}

inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
{
    assert(Consensus::ValidDeployment(dep));
    return DeploymentActiveAfter(index.pprev, params, dep, versionbitscache);
}

/** Determine if a deployment is enabled (can ever be active) */
inline bool DeploymentEnabled(const Consensus::Params& params, Consensus::BuriedDeployment dep)
{
    assert(Consensus::ValidDeployment(dep));
    return params.DeploymentHeight(dep) != std::numeric_limits<int>::max();
}

inline bool DeploymentEnabled(const Consensus::Params& params, Consensus::DeploymentPos dep)
{
    assert(Consensus::ValidDeployment(dep));
    return params.vDeployments[dep].nStartTime != Consensus::BIP9Deployment::NEVER_ACTIVE;
}

/** Determine if mandatory signaling is required for a deployment at the next block */
inline bool DeploymentMustSignalAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, ThresholdState state)
{
    assert(Consensus::ValidDeployment(dep));
    const auto& deployment = params.vDeployments[dep];
    if (deployment.max_activation_height >= std::numeric_limits<int>::max()) return false;
    if (state != ThresholdState::STARTED) return false;  // If must_signal height is reached before start time, abstain from enforcement
    const int nPeriod = params.nMinerConfirmationWindow;
    const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
    return nHeight >= deployment.max_activation_height - (2 * nPeriod)
        && nHeight < deployment.max_activation_height - nPeriod;
}

#endif // BITCOIN_DEPLOYMENTSTATUS_H
