Skip to content

Commit

Permalink
Merge remote-tracking branch 'mike/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Asherda committed Jun 5, 2023
2 parents d28f835 + c493fce commit b2ae573
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/pbaas/crosschainrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern std::string VERUS_CHAINNAME;

uint32_t PBAAS_TESTFORK2_TIME = 1684281600;
uint32_t PBAAS_TESTFORK3_TIME = 1685379600;
uint32_t PBAAS_TESTFORK4_TIME = 1685984400;
uint32_t PBAAS_TESTFORK4_TIME = 1699344000;
uint32_t PBAAS_MAINDEFI3_HEIGHT = 2553500;
uint32_t PBAAS_ENFORCE_CORRECT_EVIDENCE_TIME = 1684359650;

Expand Down
80 changes: 66 additions & 14 deletions src/pbaas/notarization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,8 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
1 :
chainActive.Height() + 1;

bool improvedMinCheck = ConnectedChains.CheckZeroViaOnlyPostLaunch(currentHeight);

CTransferDestination notaryPayee;

if (!externalSystemID.IsNull())
Expand All @@ -1161,10 +1163,22 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
CNativeHashWriter hw(hashType);

CCurrencyValueMap newPreConversionReservesIn;
CCurrencyValueMap prelaunchReserveIn;

if (!IsPreLaunch() && !IsLaunchComplete())
{
newPreConversionReservesIn = CCurrencyValueMap(currencyState.currencies, currencyState.primaryCurrencyIn);
prelaunchReserveIn = newPreConversionReservesIn;
}
else if (improvedMinCheck && IsPreLaunch())
{
prelaunchReserveIn = CCurrencyValueMap(currencyState.currencies, currencyState.reserveIn);
}

CCurrencyValueMap maxPreconvertMap = CCurrencyValueMap(destCurrency.currencies, destCurrency.maxPreconvert);
if (destCurrency.maxPreconvert.size())
{
maxPreconvertMap = CCurrencyValueMap(destCurrency.currencies, destCurrency.maxPreconvert);
}

for (int i = 0; i < exportTransfers.size(); i++)
Expand Down Expand Up @@ -1200,21 +1214,29 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
CCurrencyValueMap newTotalReserves;
if (IsPreLaunch())
{
newTotalReserves = CCurrencyValueMap(destCurrency.currencies, newNotarization.currencyState.reserves) + newReserveIn + newPreConversionReservesIn;
newTotalReserves = CCurrencyValueMap(destCurrency.currencies, newNotarization.currencyState.reserves) + newReserveIn + prelaunchReserveIn;
}
else
{
newTotalReserves = CCurrencyValueMap(destCurrency.currencies, newNotarization.currencyState.primaryCurrencyIn) + newReserveIn + newPreConversionReservesIn;
newTotalReserves = CCurrencyValueMap(destCurrency.currencies, newNotarization.currencyState.primaryCurrencyIn) + newReserveIn + prelaunchReserveIn;
}

if (destCurrency.maxPreconvert.size() && newTotalReserves > CCurrencyValueMap(destCurrency.currencies, destCurrency.maxPreconvert))
if (improvedMinCheck)
{
newTotalReserves = newTotalReserves.IntersectingValues(newReserveIn);
}

if (destCurrency.maxPreconvert.size() &&
((!improvedMinCheck && newTotalReserves > maxPreconvertMap) ||
(improvedMinCheck && (maxPreconvertMap - newTotalReserves).HasNegative())))
{
LogPrintf("%s: refunding pre-conversion over maximum\n", __func__);
reserveTransfer = reserveTransfer.GetRefundTransfer();
}
else
{
newPreConversionReservesIn += newReserveIn;
prelaunchReserveIn += newReserveIn;
}
}
}
Expand Down Expand Up @@ -1298,9 +1320,12 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS

// check our currency and any co-launch currency to determine our eligibility, as ALL
// co-launch currencies must launch for one to launch
CCurrencyValueMap coLaunchReserveIn = CCurrencyValueMap(coLaunchCurrency.currencies, coLaunchState.reserveIn);
CCurrencyValueMap coLaunchMinMap = CCurrencyValueMap(coLaunchCurrency.currencies, coLaunchCurrency.minPreconvert);
if (coLaunchState.IsRefunding() ||
!coLaunchState.ValidateConversionLimits(true) ||
CCurrencyValueMap(coLaunchCurrency.currencies, coLaunchState.reserveIn) < CCurrencyValueMap(coLaunchCurrency.currencies, coLaunchCurrency.minPreconvert) ||
(!improvedMinCheck && coLaunchReserveIn < coLaunchMinMap) ||
(improvedMinCheck && (coLaunchReserveIn - coLaunchMinMap).HasNegative()) ||
(coLaunchCurrency.IsFractional() &&
CCurrencyValueMap(coLaunchCurrency.currencies, coLaunchState.reserveIn).CanonicalMap().valueMap.size() != coLaunchCurrency.currencies.size()))
{
Expand All @@ -1319,10 +1344,10 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
minPreMap = CCurrencyValueMap(destCurrency.currencies, destCurrency.minPreconvert).CanonicalMap();
}

bool improvedMinCheck = ConnectedChains.CheckZeroViaOnlyPostLaunch(currentHeight);
if (forcedRefund ||
(minPreMap.valueMap.size() &&
((!improvedMinCheck && preConvertedMap < minPreMap) || (improvedMinCheck && (preConvertedMap - minPreMap).HasNegative()))) ||
((!improvedMinCheck && preConvertedMap < minPreMap) ||
(improvedMinCheck && (preConvertedMap - minPreMap).HasNegative()))) ||
(destCurrency.IsFractional() &&
(CCurrencyValueMap(destCurrency.currencies, newNotarization.currencyState.reserveIn) +
newPreConversionReservesIn).CanonicalMap().valueMap.size() != destCurrency.currencies.size()))
Expand Down Expand Up @@ -1445,14 +1470,36 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
{
// accumulate reserves during pre-conversions import to enforce max pre-convert
CCurrencyValueMap tempReserves;
CCurrencyValueMap cumulativeReserves;
auto currencyIdxMap = tempState.GetReserveMap();
bool newCumulative = improvedMinCheck && tempState.IsFractional();
for (auto &oneCurrencyID : tempState.currencies)
{
if (rtxd.currencies.count(oneCurrencyID))
{
int64_t reservesConverted = rtxd.currencies[oneCurrencyID].nativeOutConverted;
if (reservesConverted)
int64_t reservesIn = newCumulative ?
oneCurrencyID == ASSETCHAINS_CHAINID ?
(rtxd.nativeIn - rtxd.nativeOut) :
(rtxd.currencies[oneCurrencyID].reserveIn - (rtxd.currencies[oneCurrencyID].reserveConversionFees + rtxd.currencies[oneCurrencyID].reserveOut)) :
rtxd.currencies[oneCurrencyID].nativeOutConverted;

if (newCumulative)
{
tempReserves.valueMap[oneCurrencyID] = reservesConverted;
int idx = currencyIdxMap[oneCurrencyID];
if (oneCurrencyID == ASSETCHAINS_CHAINID)
{
tempState.primaryCurrencyIn[idx] =
(this->currencyState.primaryCurrencyIn[idx] + rtxd.nativeIn + tempState.reserveOut[idx]) - rtxd.nativeOut;
}
else
{
tempState.primaryCurrencyIn[idx] = this->currencyState.primaryCurrencyIn[idx] + reservesIn;
}
}

if (reservesIn)
{
tempReserves.valueMap[oneCurrencyID] = reservesIn;
}
}
}
Expand All @@ -1461,7 +1508,10 @@ bool CPBaaSNotarization::NextNotarizationInfo(const CCurrencyDefinition &sourceS
// reverting supply and reserves, we end up with what we started, after prelaunch and
// before all post launch functions are complete, we use primaryCurrencyIn to accumulate
// reserves to enforce maxPreconvert
tempState.primaryCurrencyIn = tempState.AddVectors(this->currencyState.primaryCurrencyIn, tempReserves.AsCurrencyVector(tempState.currencies));
if (!newCumulative)
{
tempState.primaryCurrencyIn = tempState.AddVectors(this->currencyState.primaryCurrencyIn, tempReserves.AsCurrencyVector(tempState.currencies));
}
tempState.reserveOut =
tempState.AddVectors(tempState.reserveOut,
(CCurrencyValueMap(tempState.currencies, tempState.reserveIn) * -1).AsCurrencyVector(tempState.currencies));
Expand Down Expand Up @@ -10254,10 +10304,12 @@ bool PreCheckFinalizeNotarization(const CTransaction &tx, int32_t outNum, CValid
return state.Error("Invalid notarization output for finalization");
}

if (!(!txBlockHash.IsNull() &&
(notaTxBlockIt = mapBlockIndex.find(txBlockHash)) != mapBlockIndex.end() &&
chainActive.Contains(notaTxBlockIt->second)) &&
(!PBAAS_TESTMODE || chainActive[height - 1]->nTime >= PBAAS_TESTFORK4_TIME))
if ((!PBAAS_TESTMODE || chainActive[height - 1]->nTime >= PBAAS_TESTFORK4_TIME) &&
currentFinalization.IsConfirmed() &&
height != 1 &&
!(!txBlockHash.IsNull() &&
(notaTxBlockIt = mapBlockIndex.find(txBlockHash)) != mapBlockIndex.end() &&
chainActive.Contains(notaTxBlockIt->second)))
{
return state.Error("Uncommitted notarization output is invalid for finalization");
}
Expand Down
53 changes: 47 additions & 6 deletions src/pbaas/pbaas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2797,6 +2797,7 @@ bool ValidateReserveDeposit(struct CCcontract_info *cp, Eval* eval, const CTrans
}

bool isClearLaunch = ccxSource.IsClearLaunch();
std::vector<CTxOut> vOutputs;

if (ConnectedChains.CheckZeroViaOnlyPostLaunch(nHeight) &&
isClearLaunch &&
Expand Down Expand Up @@ -2829,9 +2830,43 @@ bool ValidateReserveDeposit(struct CCcontract_info *cp, Eval* eval, const CTrans
priorNotar.currencyID == mainImport.importCurrencyID)
{
checkState = priorNotar.currencyState;

checkState.SetPrelaunch(false);
validNotarization = true;
break;
// clear launch export is not clear launch import
checkState.SetLaunchClear(false);
CCoinbaseCurrencyState pricingState;
CCurrencyValueMap dummyCurrency, dummyCurrencyUsed, dummyCurrencyOut;

if (rtxd.AddReserveTransferImportOutputs(sourceSysDef,
destSysDef,
destCurDef,
checkState,
reserveTransfers,
nHeight,
vOutputs,
dummyCurrency,
dummyCurrencyUsed,
dummyCurrencyOut,
&pricingState,
ccxSource.exporter,
importNotarization.proposer,
EntropyHashFromHeight(CBlockIndex::BlockEntropyKey(), importNotarization.notarizationHeight, destCurDef.GetID())))
{
checkState.conversionPrice = pricingState.conversionPrice;
checkState.viaConversionPrice = pricingState.viaConversionPrice;
validNotarization = true;
vOutputs.clear();
rtxd = CReserveTransactionDescriptor();
break;
}
else
{
if (LogAcceptCategory("defi"))
{
LogPrintf("%s: Invalid currency state for import: %s\n", __func__, checkState.ToUniValue().write(1,2).c_str());
}
return eval->Error(std::string(__func__) + ": invalid prior notarization for clear launch import: " + mainImport.ToUniValue().write(1,2));
}
}
}
if (!validNotarization)
Expand All @@ -2842,7 +2877,7 @@ bool ValidateReserveDeposit(struct CCcontract_info *cp, Eval* eval, const CTrans
}
return eval->Error(std::string(__func__) + ": invalid prior notarization for clear launch import: " + mainImport.ToUniValue().write(1,2));
}
checkState.SetLaunchClear(false);
//checkState.SetLaunchClear(false);
}
else
{
Expand All @@ -2856,7 +2891,6 @@ bool ValidateReserveDeposit(struct CCcontract_info *cp, Eval* eval, const CTrans
}
}

std::vector<CTxOut> vOutputs;
CCurrencyValueMap importedCurrency, gatewayCurrencyUsed, spentCurrencyOut;

if (!rtxd.AddReserveTransferImportOutputs(sourceSysDef,
Expand Down Expand Up @@ -5620,8 +5654,8 @@ bool CConnectedChains::CheckZeroViaOnlyPostLaunch(uint32_t height) const
{
if (IsVerusActive())
{
if ((PBAAS_TESTMODE && height > 61982) ||
(!PBAAS_TESTMODE && height > 2570264))
if ((PBAAS_TESTMODE && height > 62378) ||
(!PBAAS_TESTMODE && height > 2573055))
{
return true;
}
Expand Down Expand Up @@ -7410,6 +7444,13 @@ bool CConnectedChains::CreateLatestImports(const CCurrencyDefinition &sourceSyst

// pay the fee out to the miner
CReserveTransactionDescriptor rtxd(tb.mtx, view, nHeight + 1);
if (!rtxd.IsValid())
{
printf("%s: Created invalid import transaction for currency %s\n", __func__, EncodeDestination(CIdentityID(ccx.destCurrencyID)).c_str());
LogPrintf("%s: Created invalid import transaction for currency %s\n", __func__, EncodeDestination(CIdentityID(ccx.destCurrencyID)).c_str());
return false;
}

tb.SetFee(rtxd.nativeIn - rtxd.nativeOut);

CCurrencyValueMap intersectMap;
Expand Down
Loading

0 comments on commit b2ae573

Please sign in to comment.