// Copyright (c) 2016-2024 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/******************************************************************************
 * Copyright © 2014-2019 The SuperNET Developers.                             *
 *                                                                            *
 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
 * the top-level directory of this distribution for the individual copyright  *
 * holder information and the developer policies on copyright and licensing.  *
 *                                                                            *
 * Unless otherwise agreed in a custom licensing agreement, no part of the    *
 * SuperNET software, including this file may be copied, modified, propagated *
 * or distributed except according to the terms contained in the LICENSE file *
 *                                                                            *
 * Removal or modification of this copyright notice is prohibited.            *
 *                                                                            *
 ******************************************************************************/
#include <assert.h>
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "script/cc.h"
#include "cc/eval.h"
#include "cc/utils.h"
#include "cc/CCinclude.h"
#include "main.h"
#include "chain.h"
#include "core_io.h"
#include "crosschain.h"

char *CClib_name();

Eval* EVAL_TEST = 0;
extern pthread_mutex_t HUSH_CC_mutex;


bool Eval::GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spends) const
{
    // NOT IMPLEMENTED
    return false;
}


bool Eval::GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const
{
    return false;
}


bool Eval::GetTxConfirmed(const uint256 &hash, CTransaction &txOut, CBlockIndex &block) const
{
    return false;
}

unsigned int Eval::GetCurrentHeight() const
{
    return chainActive.Height();
}

bool Eval::GetBlock(uint256 hash, CBlockIndex& blockIdx) const
{
    return false;
}

extern int32_t hush_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp);


int32_t Eval::GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const
{
    return hush_notaries(pubkeys, height, timestamp);
}

bool Eval::CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const
{
    return false;
}

// Get MoM from a notarization tx hash (on HUSH)
bool Eval::GetNotarizationData(const uint256 notaryHash, NotarizationData &data) const
{
    CTransaction notarizationTx;
    CBlockIndex block;
    if (!GetTxConfirmed(notaryHash, notarizationTx, block)) return false;
    if (!CheckNotaryInputs(notarizationTx, block.GetHeight(), block.nTime)) return false;
    if (!ParseNotarizationOpReturn(notarizationTx, data)) return false;
    return true;
}


uint32_t Eval::GetAssetchainsCC() const
{
    return ASSETCHAINS_CC;
}


std::string Eval::GetAssetchainsSymbol() const
{
    return std::string(SMART_CHAIN_SYMBOL);
}


/*
 * Notarization data, ie, OP_RETURN payload in notarization transactions
 */
// This is used by notarizationdb.cpp
bool ParseNotarizationOpReturn(const CTransaction &tx, NotarizationData &data)
{
    if (tx.vout.size() < 2) return false;
    std::vector<unsigned char> vdata;
    if (!GetOpReturnData(tx.vout[1].scriptPubKey, vdata)) return false;
    bool out = E_UNMARSHAL(vdata, ss >> data);
    return out;
}

// Misc
std::string EvalToStr(EvalCode c)
{
    FOREACH_EVAL(EVAL_GENERATE_STRING);
    char s[10];
    sprintf(s, "0x%x", c);
    return std::string(s);

}

uint256 SafeCheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
{
    if (nIndex == -1)
        return uint256();
    for (auto it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
    {
        if (nIndex & 1) {
            if (*it == hash) {
                // non canonical. hash may be equal to node but never on the right.
                return uint256();
            }
            hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
        }
        else
            hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
        nIndex >>= 1;
    }
    return hash;
}

uint256 GetMerkleRoot(const std::vector<uint256>& vLeaves)
{
    bool fMutated;
    std::vector<uint256> vMerkleTree;
    return BuildMerkleTree(&fMutated, vLeaves, vMerkleTree);
}
