2019-05-07 16:32:22 -06:00
|
|
|
// Copyright (c) 2018 The LBRY developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef CLAIMSCRIPTOP_H
|
|
|
|
#define CLAIMSCRIPTOP_H
|
|
|
|
|
|
|
|
#include "amount.h"
|
|
|
|
#include "claimtrie.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "primitives/transaction.h"
|
|
|
|
#include "script/script.h"
|
|
|
|
#include "uint256.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Claim script operation base class
|
|
|
|
*/
|
|
|
|
class CClaimScriptOp
|
|
|
|
{
|
|
|
|
public:
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual ~CClaimScriptOp() = default;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
|
|
|
* Pure virtual, OP_CLAIM_NAME handler
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual bool claimName(CClaimTrieCache& trieCache, const std::string& name,
|
|
|
|
const std::vector<unsigned char>& metadata) = 0;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
|
|
|
* Pure virtual, OP_UPDATE_CLAIM handler
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) = 0;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
|
|
|
* Pure virtual, OP_SUPPORT_CLAIM handler
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) = 0;
|
2019-05-07 16:32:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to add claim in trie
|
|
|
|
*/
|
|
|
|
class CClaimScriptAddOp : public CClaimScriptOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param[in] point pair of transaction hash and its index
|
|
|
|
* @param[in] nValue value of the claim
|
|
|
|
* @param[in] nHeight entry height of the claim
|
|
|
|
*/
|
|
|
|
CClaimScriptAddOp(const COutPoint& point, CAmount nValue, int nHeight);
|
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_CLAIM_NAME handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::claimName
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool claimName(CClaimTrieCache& trieCache, const std::string& name,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_UPDATE_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::updateClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_SUPPORT_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::supportClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual bool addClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
2019-10-28 17:15:42 -06:00
|
|
|
int takeoverHeight, const std::vector<unsigned char>& metadata);
|
2019-05-07 16:32:22 -06:00
|
|
|
const COutPoint point;
|
|
|
|
const CAmount nValue;
|
|
|
|
const int nHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to undo added claim in trie
|
|
|
|
*/
|
|
|
|
class CClaimScriptUndoAddOp : public CClaimScriptOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param[in] point pair of transaction hash and its index
|
|
|
|
* @param[in] nHeight entry height of the claim
|
|
|
|
*/
|
|
|
|
CClaimScriptUndoAddOp(const COutPoint& point, int nHeight);
|
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_CLAIM_NAME handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::claimName
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool claimName(CClaimTrieCache& trieCache, const std::string& name,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_UPDATE_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::updateClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_SUPPORT_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::supportClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
|
|
|
virtual bool undoAddClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId);
|
|
|
|
const COutPoint point;
|
|
|
|
const int nHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to spend claim from trie
|
|
|
|
*/
|
|
|
|
class CClaimScriptSpendOp : public CClaimScriptOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param[in] point pair of transaction hash and its index
|
|
|
|
* @param[in] nHeight entry height of the claim
|
|
|
|
* @param[out] nValidHeight valid height of the claim
|
|
|
|
*/
|
|
|
|
CClaimScriptSpendOp(const COutPoint& point, int nHeight, int& nValidHeight);
|
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_CLAIM_NAME handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::claimName
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool claimName(CClaimTrieCache& trieCache, const std::string& name,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_UPDATE_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::updateClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_SUPPORT_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::supportClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
|
|
|
virtual bool spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId);
|
|
|
|
const COutPoint point;
|
|
|
|
const int nHeight;
|
|
|
|
int& nValidHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to undo spent claim from trie
|
|
|
|
*/
|
|
|
|
class CClaimScriptUndoSpendOp : public CClaimScriptOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param[in] point pair of transaction hash and its index
|
|
|
|
* @param[in] nValue value of the claim
|
|
|
|
* @param[in] nHeight entry height of the claim
|
|
|
|
* @param[in] nValidHeight valid height of the claim
|
|
|
|
*/
|
|
|
|
CClaimScriptUndoSpendOp(const COutPoint& point, CAmount nValue, int nHeight, int nValidHeight);
|
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_CLAIM_NAME handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::claimName
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool claimName(CClaimTrieCache& trieCache, const std::string& name,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_UPDATE_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::updateClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
/**
|
2019-08-06 09:50:52 -05:00
|
|
|
* Implementation of OP_SUPPORT_CLAIM handler
|
2019-05-07 16:32:22 -06:00
|
|
|
* @see CClaimScriptOp::supportClaim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata) override;
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] name name of the claim
|
|
|
|
* @param[in] claimId id of the claim
|
|
|
|
*/
|
2019-10-23 14:15:37 -06:00
|
|
|
virtual bool undoSpendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId,
|
|
|
|
const std::vector<unsigned char>& metadata);
|
2019-05-07 16:32:22 -06:00
|
|
|
const COutPoint point;
|
|
|
|
const CAmount nValue;
|
|
|
|
const int nHeight;
|
|
|
|
const int nValidHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to process operation on claim
|
|
|
|
* @param[in] claimOp operation to be performed
|
|
|
|
* @param[in] trieCache trie to operate on
|
|
|
|
* @param[in] scriptPubKey claim script to be decoded
|
|
|
|
*/
|
|
|
|
bool ProcessClaim(CClaimScriptOp& claimOp, CClaimTrieCache& trieCache, const CScript& scriptPubKey);
|
|
|
|
|
2019-10-30 23:13:07 -06:00
|
|
|
typedef std::pair<std::string, uint160> spentClaimType;
|
2019-05-07 16:32:22 -06:00
|
|
|
typedef std::vector<spentClaimType> spentClaimsType;
|
|
|
|
|
2019-07-09 18:02:54 +03:00
|
|
|
struct CUpdateCacheCallbacks
|
|
|
|
{
|
|
|
|
std::function<CScript(const COutPoint& point)> findScriptKey;
|
|
|
|
std::function<void(int, int)> claimUndoHeights;
|
|
|
|
};
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
/**
|
2019-07-09 18:02:54 +03:00
|
|
|
* Function to spend claim from tie, keeping the successful list on
|
|
|
|
* @param[in] tx transaction inputs/outputs
|
2019-05-07 16:32:22 -06:00
|
|
|
* @param[in] trieCache trie to operate on
|
2019-07-09 18:02:54 +03:00
|
|
|
* @param[in] view coins cache
|
2019-05-07 16:32:22 -06:00
|
|
|
* @param[in] point pair of transaction hash and its index
|
|
|
|
* @param[in] nHeight entry height of the claim
|
2019-07-09 18:02:54 +03:00
|
|
|
* @param[out] fallback optional callbacks
|
2019-05-07 16:32:22 -06:00
|
|
|
*/
|
2019-07-09 18:02:54 +03:00
|
|
|
void UpdateCache(const CTransaction& tx, CClaimTrieCache& trieCache, const CCoinsViewCache& view, int nHeight, const CUpdateCacheCallbacks& callbacks = {});
|
2019-05-07 16:32:22 -06:00
|
|
|
|
|
|
|
#endif // CLAIMSCRIPTOP_H
|