Skip to content

Commit

Permalink
Merge pull request #118 from BlockMechanic/segwit
Browse files Browse the repository at this point in the history
Terminal
  • Loading branch information
BlockMechanic authored Oct 4, 2022
2 parents 9c143dc + 7835215 commit d33dc59
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 131 deletions.
4 changes: 2 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ class CTestNetParams : public CChainParams {
vFixedSeeds.clear();
vSeeds.clear();
// nodes with support for servicebits filtering should be at the top
vSeeds.emplace_back("92.60.46.26");
/*vSeeds.emplace_back("92.60.46.26");
vSeeds.emplace_back("92.60.46.27");
vSeeds.emplace_back("92.60.46.28");
vSeeds.emplace_back("92.60.46.29");
vSeeds.emplace_back("92.60.46.30");
vSeeds.emplace_back("92.60.46.31");
vSeeds.emplace_back("92.60.46.31");*/

base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
Expand Down
57 changes: 31 additions & 26 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state,
if(tx.nVersion >= TX_ELE_VERSION){
outputAssets = tx.GetValueOutMap();
}

//LogPrintf("Input assets size : %d outputs assets size : %d", inputAssets.size(), outputAssets.size());

if(tx.nVersion >= TX_ELE_VERSION && inputAssets.size() < 1)
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-input-size");

// enforce asset rules
{
CAsset subsidy_asset = GetSubsidyAsset();
CAsset subsidy_asset = GetSubsidyAsset();
//prevent asset merging
if(inputAssets.size() > 1)
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-input-asset-multiple", strprintf("found (%d) , expected 1", inputAssets.size()));
Expand All @@ -235,17 +235,22 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state,

for(auto & a : outputAssets){
CAsset asset = a.first;
bool exists = assetExists(asset);
bool exists = assetExists(asset);

//Asset exists , check for output rules
if (exists && asset != subsidy_asset) {
// check asset limited
/*
if(asset.isLimited() && inputAssets.begin()->first != asset)
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-output-asset-is-limited", strprintf("cannot convert other assets to (%s)", asset.getAssetName()));

*/
// check asset restricted
// get contract hash, retrieve contract, get issuer , compare input address to issueraddress
CContract contract = GetContractByHash(asset.contract_hash);
const CContract &contract = GetContractByHash(asset.contract_hash);
/*
if(contract.IsEmpty())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "contract-not-found", strprintf("contract retrieval failed %s", asset.contract_hash.ToString()));
*/
CTxDestination address1;
ExtractDestination(input_addresses.front(), address1);
if(asset.isRestricted()){
Expand All @@ -254,23 +259,23 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state,

if(contract.sIssuingaddress != EncodeDestination(address1))
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-issuer-mismatch", strprintf("(%s) vs (%s) only", contract.sIssuingaddress, EncodeDestination(address1)));
}
}

// check asset inflation
if(asset.isInflatable()){
// check asset inflation
if(asset.isInflatable()){
if(contract.sIssuingaddress != EncodeDestination(address1))
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inflation-issuer-mismatch", strprintf("(%s) vs (%s) only", contract.sIssuingaddress, EncodeDestination(address1)));

}
else
}
else
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-ouput-asset-not-inflatable");

}
}

//check asset creation
if(!exists) {
//Prevent stakable assets from non dev address
if(asset.isStakeable())
if(!exists) {
//Prevent stakable assets from non dev address
if(asset.isStakeable())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "new-asset-stakable");

if(inputAssets.begin()->first != subsidy_asset)
Expand All @@ -279,39 +284,39 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state,
//if(inputAssets[subsidy_asset] < 10 * COIN)
// return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-input-amount", strprintf("found (%d) (%s), min (%d) CRW", inputAssets[subsidy_asset], inputAssets.begin()->first.getShortName(), 10));

if(assetNameExists(asset.getAssetName()) || assetNameExists(asset.getShortName()))
if(assetNameExists(asset.getAssetName()) || assetNameExists(asset.getShortName()))
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-asset-name", strprintf("asset name/shortname %s / %s already in use", asset.getAssetName(), asset.getShortName()));

// if(asset.nExpiry != 0 && asset.nExpiry < tx.nTime + 84000)// TODO (Why on earth do transactions not have time ?
// return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-asset-expiry");

if(asset.nType == 2){
if(asset.isConvertable())
if(asset.isConvertable())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-unique-asset-convertable");
if(asset.isInflatable())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-unique-asset-inflatable");
}
if(asset.isInflatable())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-unique-asset-inflatable");
}

}
}
}
}
}

if(tx.nVersion >= TX_ELE_VERSION){
if(outputAssets.size() == 1)
if(inputAssets.begin()->second < outputAssets.begin()->second)
if(inputAssets.begin()->second < outputAssets.begin()->second)
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-in-below-out", strprintf("value in (%s) < value out (%s)", FormatMoney(inputAssets.begin()->second), FormatMoney(outputAssets.begin()->second)));

// Tally transaction fees
CAmountMap txfee_aux = inputAssets - outputAssets;

//identify the fee asset
CAmount mfee = txfee_aux[inputAssets.begin()->first];
if (!MoneyRange(mfee)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-fee-out-of-range");
}
txfee = mfee;
}
else{
}
else{
const CAmount value_out = tx.GetValueOut();
if (nValueIn < value_out)
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-in-below-out",
Expand Down
9 changes: 9 additions & 0 deletions src/contractdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,12 @@ bool ExistsContract(const std::string& name){
}
return false;
}

std::vector<CContract> GetAllContracts(){
std::vector<CContract> tmp;

for(auto const& x : pcontractCache->GetItemsMap())
tmp.push_back(x.second->second.contract);

return tmp;
}
2 changes: 1 addition & 1 deletion src/contractdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ extern std::unique_ptr<CContractDB> pcontractdb;
extern CLRUCache<std::string, CContractData> *pcontractCache;
bool ExistsContract(const std::string& name);
void DumpContracts();

std::vector<CContract> GetAllContracts();
#endif // CROWN_ID_DB_H
4 changes: 2 additions & 2 deletions src/core_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::string SighashToStr(unsigned char sighash_type);
void ScriptPubKeyToUniv(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
void ScriptToUniv(const CScript& script, UniValue& out, bool include_address);
void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex = true, int serialize_flags = 0);
void ContractToUniv(CContract *s , UniValue &entry);
void AssetToUniv(CAsset& asset, UniValue &entry);
void ContractToUniv(const CContract &s , UniValue &entry);
void AssetToUniv(const CAsset& asset, UniValue &entry);

#endif // CROWN_CORE_IO_H
16 changes: 13 additions & 3 deletions src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,19 @@ static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
}
}
// Check output scripts
for (unsigned int i = 0; i < tx.vout.size(); i++) {
if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
return false;
if(tx.nVersion >= TX_ELE_VERSION ){
for (unsigned int i = 0; i < tx.vpout.size(); i++) {
if (!tx.vpout[i].scriptPubKey.HasValidOps() || tx.vpout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
return false;
}
}
}
else
{
for (unsigned int i = 0; i < tx.vout.size(); i++) {
if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
return false;
}
}
}

Expand Down
108 changes: 61 additions & 47 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,15 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey,
out.pushKV("addresses", a);
}

void ContractToUniv(CContract *s , UniValue &entry){
if(!s->IsEmpty()){
entry.pushKV("url", s->contract_url);
entry.pushKV("name", s->asset_name);
entry.pushKV("symbol", s->asset_symbol);
entry.pushKV("issuing address", s->sIssuingaddress);
entry.pushKV("description", s->description);
entry.pushKV("website", s->website_url);
entry.pushKV("script", HexStr(s->scriptcode));
entry.pushKV("signature", HexStr(s->vchContractSig));
}
void ContractToUniv(const CContract &s , UniValue &entry){
entry.pushKV("url", s.contract_url);
entry.pushKV("name", s.asset_name);
entry.pushKV("symbol", s.asset_symbol);
entry.pushKV("issuing address", s.sIssuingaddress);
entry.pushKV("description", s.description);
entry.pushKV("website", s.website_url);
entry.pushKV("script", HexStr(s.scriptcode));
entry.pushKV("signature", HexStr(s.vchContractSig));
}

void DataToJSON(const CTxDataBase *baseOut, UniValue &entry)
Expand All @@ -210,7 +208,7 @@ void DataToJSON(const CTxDataBase *baseOut, UniValue &entry)
case OUTPUT_CONTRACT:{
entry.pushKV("type", "contract");
CContract *s = (CContract*) baseOut;
ContractToUniv(s, entry);
ContractToUniv(*s, entry);
break;
}
default:
Expand All @@ -219,30 +217,30 @@ void DataToJSON(const CTxDataBase *baseOut, UniValue &entry)
}
};

void AssetToUniv(CAsset& asset, UniValue &entry){
if(!asset.IsEmpty()){
entry.pushKV("version", (int)asset.nVersion);
uint32_t type = asset.GetType();

entry.pushKV("type", AssetTypeToString(type));
entry.pushKV("name", asset.getAssetName());
entry.pushKV("symbol", asset.getShortName());
entry.pushKV("id", asset.GetHex());
//if (asset.contract_hash != uint256()){
// CContract contract = GetContract(asset.getAssetName());
// UniValue a(UniValue::VOBJ);
// ContractToUniv(&contract, a);
// entry.pushKV("contract", a);
//}
entry.pushKV("contract_hash", asset.contract_hash.GetHex());
entry.pushKV("expiry", (int64_t)asset.GetExpiry());
entry.pushKV("transferable", asset.isTransferable() ? "yes" : "no");
entry.pushKV("convertable", asset.isConvertable() ? "yes" : "no");
entry.pushKV("limited", asset.isLimited() ? "yes" : "no");
entry.pushKV("restricted", asset.isRestricted() ? "yes" : "no");
entry.pushKV("stakeable", asset.isStakeable() ? "yes" : "no");
entry.pushKV("inflation", asset.isInflatable() ? "yes" : "no");
}
void AssetToUniv(const CAsset& asset, UniValue &entry){

entry.pushKV("version", (int)asset.nVersion);
uint32_t type = asset.GetType();

entry.pushKV("type", AssetTypeToString(type));
entry.pushKV("name", asset.getAssetName());
entry.pushKV("symbol", asset.getShortName());
entry.pushKV("id", asset.GetHex());
//if (asset.contract_hash != uint256()){
// CContract contract = GetContract(asset.getAssetName());
// UniValue a(UniValue::VOBJ);
// ContractToUniv(&contract, a);
// entry.pushKV("contract", a);
//}
entry.pushKV("contract_hash", asset.contract_hash.GetHex());
entry.pushKV("expiry", (int64_t)asset.GetExpiry());
entry.pushKV("transferable", asset.isTransferable() ? "yes" : "no");
entry.pushKV("convertable", asset.isConvertable() ? "yes" : "no");
entry.pushKV("limited", asset.isLimited() ? "yes" : "no");
entry.pushKV("restricted", asset.isRestricted() ? "yes" : "no");
entry.pushKV("stakeable", asset.isStakeable() ? "yes" : "no");
entry.pushKV("inflation", asset.isInflatable() ? "yes" : "no");

}

void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags)
Expand Down Expand Up @@ -299,18 +297,34 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
entry.pushKV("data", vdata);

UniValue vout(UniValue::VARR);
for (unsigned int k = 0; k < (tx.nVersion >= TX_ELE_VERSION ? tx.vpout.size() : tx.vout.size()) ; k++) {
CTxOutAsset txout = (tx.nVersion >= TX_ELE_VERSION ? tx.vpout[k] : tx.vout[k]);
UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(txout.nValue));
out.pushKV("asset", txout.nAsset.ToString());
out.pushKV("n", (int64_t)k);

UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
out.pushKV("scriptPubKey", o);
vout.push_back(out);

if(tx.nVersion >= TX_ELE_VERSION){
for (unsigned int k = 0; k < tx.vpout.size() ; k++) {
UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(tx.vpout[k].nValue));
UniValue p(UniValue::VOBJ);
AssetToUniv(tx.vpout[k].nAsset, p);
out.pushKV("asset", p);
out.pushKV("n", (int64_t)k);
UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(tx.vpout[k].scriptPubKey, o, true);
out.pushKV("scriptPubKey", o);
vout.push_back(out);
}
}
else
{
for (unsigned int k = 0; k < tx.vout.size() ; k++) {
UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(tx.vout[k].nValue));
out.pushKV("n", (int64_t)k);
UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(tx.vout[k].scriptPubKey, o, true);
out.pushKV("scriptPubKey", o);
vout.push_back(out);
}
}

entry.pushKV((tx.nVersion >= TX_ELE_VERSION ? "vpout" : "vout"), vout);

if (!tx.extraPayload.empty()) {
Expand Down
Loading

0 comments on commit d33dc59

Please sign in to comment.