Skip to content

Commit

Permalink
update asset (#366)
Browse files Browse the repository at this point in the history
Co-authored-by: nandofw <[email protected]>
  • Loading branch information
nandofw and nandofw authored Mar 18, 2024
1 parent 7ae49be commit 9b12eb6
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 406 deletions.
4 changes: 2 additions & 2 deletions src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CAssetMetaData::CAssetMetaData(const std::string txid, const CNewAssetTx assetTx
assetId = txid;
circulatingSupply = 0;
mintCount = 0;
if (assetTx.nVersion == 2 && !assetTx.isRoot){
if (!assetTx.isRoot){
CAssetMetaData rootAsset;
passetsCache->GetAssetMetaData(assetTx.rootId, rootAsset);
name = rootAsset.name + "|" + assetTx.name;
Expand Down Expand Up @@ -504,7 +504,7 @@ bool CAssetsCache::Flush() {

void AddAssets(const CTransaction &tx, int nHeight, CAssetsCache *assetCache,
std::pair <std::string, CBlockAssetUndo> *undoAssetData) {
if (Params().IsAssetsActive(::ChainActive().Tip()) && assetCache) {
if (Updates().IsAssetsActive(::ChainActive().Tip()) && assetCache) {
if (tx.nType == TRANSACTION_NEW_ASSET) {
CNewAssetTx assetTx;
if (GetTxPayload(tx, assetTx)) {
Expand Down
27 changes: 10 additions & 17 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ class CMainParams : public CChainParams {
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
consensus.smartnodePaymentFixedBlock = 6800;
consensus.nFutureForkBlock = 420420;
consensus.nAssetsForkBlock = 9999999;
consensus.nRootAssetsForkBlock = consensus.nAssetsForkBlock;

updateManager.Add
( // V17 voting blocks 419328-427391 in mainnet, 4032 voting, 4032 grace period, active at 427392
Expand Down Expand Up @@ -371,17 +369,16 @@ class CTestNetParams : public CChainParams {
consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
consensus.nFutureForkBlock = 1000;
consensus.nAssetsForkBlock = 411300;
consensus.nRootAssetsForkBlock = 9999999;

updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
// updateManager.Add
// (
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 7, 100, 100000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(95, 95, 1))
// );
//updateManager.Add
//(
//bit 1, 720 block/round, voting start at block 10080, 7 rounds to lock-in and 7 rounds off grace period
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 1, 720, 10080, 7, 720, 7, false, VoteThreshold(85, 85, 1), VoteThreshold(0, 0, 1))
//);

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x0"); // 0
Expand Down Expand Up @@ -523,16 +520,14 @@ class CDevNetParams : public CChainParams {
consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
consensus.nFutureForkBlock = 1;
consensus.nAssetsForkBlock = 1;
consensus.nRootAssetsForkBlock = 1;

updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
updateManager.Add
(
Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 1, 100, 2000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(0, 0, 1))
Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 1, 10, 100, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(0, 0, 1))
);

// The best chain should have at least this much work.
Expand Down Expand Up @@ -694,17 +689,15 @@ class CRegTestParams : public CChainParams {
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
consensus.nFutureForkBlock = 1;
consensus.nAssetsForkBlock = 1;
consensus.nRootAssetsForkBlock = 1;

updateManager.Add
(
Update(EUpdate::DEPLOYMENT_V17, std::string("v17"), 0, 10, 0, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);
// updateManager.Add
// (
// Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 7, 100, 100000, 5, 10, 5, false, VoteThreshold(85, 85, 1), VoteThreshold(95, 95, 1))
// );
updateManager.Add
(
Update(EUpdate::ROUND_VOTING, std::string("Round Voting"), 1, 10, 100, 10, 100, 10, false, VoteThreshold(95, 95, 5), VoteThreshold(0, 0, 1))
);

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x00");
Expand Down
9 changes: 0 additions & 9 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ class CChainParams {
return height >= GetConsensus().nFutureForkBlock;
};

bool IsAssetsActive(CBlockIndex *index) const {
int height = index == nullptr ? 0 : index->nHeight;
return height >= GetConsensus().nAssetsForkBlock;
};

bool IsRootAssetsActive(CBlockIndex *index) const {
int height = index == nullptr ? 0 : index->nHeight;
return height >= GetConsensus().nRootAssetsForkBlock;
};
const std::vector <std::string> &SporkAddresses() const { return vSporkAddresses; }

int MinSporkKeys() const { return nMinSporkKeys; }
Expand Down
3 changes: 0 additions & 3 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ namespace Consensus {
SmartnodeCollaterals nCollaterals;
int smartnodePaymentFixedBlock;
int nFutureForkBlock;
int nAssetsForkBlock;
int nRootAssetsForkBlock;

};
} // namespace Consensus

Expand Down
6 changes: 3 additions & 3 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ checkSpecialTxFee(const CTransaction &tx, CAmount &nFeeTotal, CAmount &specialTx
case TRANSACTION_NEW_ASSET: {
CNewAssetTx asset;
if (GetTxPayload(tx.vExtraPayload, asset)) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return false;
}
bool assetsEnabled = sporkManager.IsSporkActive(SPORK_22_SPECIAL_TX_FEE);
Expand All @@ -65,7 +65,7 @@ checkSpecialTxFee(const CTransaction &tx, CAmount &nFeeTotal, CAmount &specialTx
case TRANSACTION_UPDATE_ASSET: {
CUpdateAssetTx asset;
if (GetTxPayload(tx.vExtraPayload, asset)) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return false;
}
bool assetsEnabled = sporkManager.IsSporkActive(SPORK_22_SPECIAL_TX_FEE);
Expand All @@ -80,7 +80,7 @@ checkSpecialTxFee(const CTransaction &tx, CAmount &nFeeTotal, CAmount &specialTx
case TRANSACTION_MINT_ASSET: {
CMintAssetTx asset;
if (GetTxPayload(tx.vExtraPayload, asset)) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return false;
}
bool assetsEnabled = sporkManager.IsSporkActive(SPORK_22_SPECIAL_TX_FEE);
Expand Down
82 changes: 16 additions & 66 deletions src/evo/providertx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ inline bool checkNewUniqueAsset(CNewAssetTx &assetTx, CValidationState &state) {

bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CValidationState &state,
CAssetsCache *assetsCache) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return state.DoS(100, false, REJECT_INVALID, "assets-not-enabled");
}

Expand All @@ -141,14 +141,6 @@ bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVal
return state.DoS(100, false, REJECT_INVALID, "bad-assets-version");
}

if (Params().IsRootAssetsActive(::ChainActive().Tip()) && assetTx.nVersion != 2) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
} else {
if (!Params().IsRootAssetsActive(::ChainActive().Tip()) && assetTx.nVersion != 1) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
}
}

//validate asset name
if (!IsAssetNameValid(assetTx.name, assetTx.isRoot)) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-name");
Expand All @@ -157,7 +149,7 @@ bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVal
//Check if a asset already exist with give name
std::string assetId = assetTx.name;
CAssetMetaData rootAsset;
if (assetTx.nVersion == 2 && !assetTx.isRoot) {
if (!assetTx.isRoot) { //sub asset
if (!assetsCache->GetAssetMetaData(assetTx.rootId, rootAsset)){
return state.DoS(100, false, REJECT_INVALID, "bad-assets-root-id");
}
Expand All @@ -167,7 +159,7 @@ bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVal
return state.DoS(100, false, REJECT_INVALID, "bad-assets-dup-name");
}
}
} else { //v1 or ROOT asset
} else { //ROOT asset
if (assetsCache->GetAssetId(assetTx.name, assetId)) {
if (assetsCache->CheckIfAssetExists(assetId)) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-dup-name");
Expand All @@ -176,7 +168,7 @@ bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVal
}

//check if the root asset is valid
if (assetTx.nVersion == 2 && !assetTx.isRoot) {
if (!assetTx.isRoot) {
if (!rootAsset.isRoot) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-root-id");
}
Expand Down Expand Up @@ -226,25 +218,9 @@ bool CheckNewAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVal
return true;
}

inline bool checkAssetFeesPayment(const CTransaction &tx, CValidationState &state, const CCoinsViewCache &view,
CAssetMetaData asset) {
for (auto in: tx.vin) {
const Coin &coin = view.AccessCoin(in.prevout);
if (coin.IsSpent())
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-input");
CTxDestination dest;
ExtractDestination(coin.out.scriptPubKey, dest);
if (EncodeDestination(dest) != EncodeDestination(asset.ownerAddress)) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-input");
}
}

return true;
}

bool CheckUpdateAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CValidationState &state,
const CCoinsViewCache &view, CAssetsCache *assetsCache) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return state.DoS(100, false, REJECT_INVALID, "assets-not-enabled");
}

Expand All @@ -270,23 +246,11 @@ bool CheckUpdateAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, C
if (!asset.updatable) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-not-updateable");
}

if (Params().IsRootAssetsActive(::ChainActive().Tip())) {
if (assetTx.nVersion != 2) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
}
//check the asset owner signature
std::string strError;
if (!CMessageSigner::VerifyMessage(asset.ownerAddress, assetTx.vchSig, assetTx.MakeSignString(assetsCache), strError)) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-sig", false, strError);
}
} else {
if (assetTx.nVersion != 1) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
}
//Check if fees is paid by the owner address
if (!checkAssetFeesPayment(tx, state, view, asset))
return false;

//check the asset owner signature
std::string strError;
if (!CMessageSigner::VerifyMessage(asset.ownerAddress, assetTx.vchSig, assetTx.MakeSignString(assetsCache), strError)) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-sig", false, strError);
}

if (assetTx.ownerAddress.IsNull()) {
Expand Down Expand Up @@ -379,7 +343,7 @@ inline bool checkAssetMintAmount(const CTransaction &tx, CValidationState &state

bool CheckMintAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CValidationState &state,
const CCoinsViewCache &view, CAssetsCache *assetsCache) {
if (!Params().IsAssetsActive(::ChainActive().Tip())) {
if (!Updates().IsAssetsActive(::ChainActive().Tip())) {
return state.DoS(100, false, REJECT_INVALID, "assets-not-enabled");
}

Expand All @@ -402,25 +366,12 @@ bool CheckMintAssetTx(const CTransaction &tx, const CBlockIndex *pindexPrev, CVa
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-asset-id");
}

if (asset.type == 0 || asset.isUnique) { // manual mint or unique
if (Params().IsRootAssetsActive(::ChainActive().Tip())) {
if (assetTx.nVersion != 2) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
}
//check the asset owner signature
std::string strError;
if (!CMessageSigner::VerifyMessage(asset.ownerAddress, assetTx.vchSig, assetTx.MakeSignString(assetsCache), strError)) {
return state.DoS(100, false, REJECT_INVALID, "bad-mint-assets-sig", false, strError);
}
} else {
if (assetTx.nVersion != 1) {
return state.DoS(100, false, REJECT_INVALID, "bad-assets-invalid-version");
}
//Check if fees is paid by the owner address
if (!checkAssetFeesPayment(tx, state, view, asset))
return false;
if (asset.type == 0 || asset.isUnique) { // manual mint or unique
//check the asset owner signature
std::string strError;
if (!CMessageSigner::VerifyMessage(asset.ownerAddress, assetTx.vchSig, assetTx.MakeSignString(assetsCache), strError)) {
return state.DoS(100, false, REJECT_INVALID, "bad-mint-assets-sig", false, strError);
}

if (!checkAssetMintAmount(tx, state, asset))
return false;

Expand Down Expand Up @@ -867,6 +818,5 @@ std::string CMintAssetTx::MakeSignString(CAssetsCache *assetsCache) const {

// ... and also the full hash of the payload as a protection agains malleability and replays
s += ::SerializeHash(*this).ToString();

return s;
}
34 changes: 14 additions & 20 deletions src/evo/providertx.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class CFutureTx {

class CNewAssetTx {
public:
static const uint16_t CURRENT_VERSION = 2;
static const uint16_t CURRENT_VERSION = 1;

uint16_t nVersion{CURRENT_VERSION}; // message version
std::string name;
Expand Down Expand Up @@ -285,14 +285,12 @@ class CNewAssetTx {
READWRITE(obj.nVersion, obj.name, obj.updatable, obj.isUnique, obj.maxMintCount,
obj.decimalPoint, obj.referenceHash, obj.fee, obj.type, obj.targetAddress,
obj.issueFrequency, obj.amount, obj.ownerAddress, obj.collateralAddress);
if(obj.nVersion == 2 ) { //testnet use v1 and v2, mainnet v2 only
READWRITE(obj.isRoot);
if (!obj.isRoot) {
//sub asset: serialise the root id and owner signature
READWRITE(obj.rootId);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
READWRITE(obj.isRoot);
if (!obj.isRoot) {
//sub asset: serialise the root id and owner signature
READWRITE(obj.rootId);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}
READWRITE(obj.exChainType, obj.externalPayoutScript, obj.externalTxid,
Expand All @@ -309,7 +307,7 @@ class CNewAssetTx {
obj.pushKV("version", nVersion);
obj.pushKV("name", name);
obj.pushKV("isRoot", isRoot);
if (!isRoot && nVersion == 2)
if (!isRoot)
obj.pushKV("rootId", rootId);
obj.pushKV("isUnique", isUnique);
obj.pushKV("maxMintCount", maxMintCount);
Expand Down Expand Up @@ -343,7 +341,7 @@ class CNewAssetTx {

class CUpdateAssetTx {
public:
static const uint16_t CURRENT_VERSION = 2;
static const uint16_t CURRENT_VERSION = 1;

uint16_t nVersion{CURRENT_VERSION}; // message version
std::string assetId;
Expand Down Expand Up @@ -374,10 +372,8 @@ class CUpdateAssetTx {
obj.type, obj.targetAddress, obj.issueFrequency, obj.maxMintCount, obj.amount,
obj.ownerAddress, obj.collateralAddress, obj.exChainType, obj.externalPayoutScript,
obj.externalTxid, obj.externalConfirmations, obj.inputsHash);
if(obj.nVersion == 2 ) { //testnet use v1 and v2, mainnet v2 only
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}

Expand Down Expand Up @@ -419,7 +415,7 @@ class CUpdateAssetTx {

class CMintAssetTx {
public:
static const uint16_t CURRENT_VERSION = 2;
static const uint16_t CURRENT_VERSION = 1;

uint16_t nVersion{CURRENT_VERSION}; // message version
std::string assetId;
Expand All @@ -433,10 +429,8 @@ class CMintAssetTx {
)
{
READWRITE(obj.nVersion, obj.assetId, obj.fee, obj.inputsHash);
if(obj.nVersion == 2 ) { //testnet use v1 and v2, mainnet v2 only
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}

Expand Down
Loading

0 comments on commit 9b12eb6

Please sign in to comment.