Skip to content

Commit

Permalink
Scale auction bid and batches for split (#1296)
Browse files Browse the repository at this point in the history
* Scale auction bid and batches for split

* Change assertions, add error logs

* Fix args

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored May 27, 2022
1 parent 92bdad8 commit a7f17c4
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 132 deletions.
13 changes: 9 additions & 4 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,11 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
for (const auto& kv : obj.balances) {
const DCT_ID& tokenId = kv.first;

if (Params().NetworkIDString() == CBaseChainParams::MAIN && height >= static_cast<uint32_t>(consensus.FortCanningCrunchHeight) &&
mnview.GetLoanTokenByID(tokenId)) {
return Res::Err("Loan tokens cannot be minted");
}

auto token = mnview.GetToken(tokenId);
if (!token) {
return Res::Err("token %s does not exist!", tokenId.ToString());
Expand Down Expand Up @@ -3402,14 +3407,14 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
if (!data)
return Res::Err("No auction data to vault %s", obj.vaultId.GetHex());

auto batch = mnview.GetAuctionBatch(obj.vaultId, obj.index);
auto batch = mnview.GetAuctionBatch({obj.vaultId, obj.index});
if (!batch)
return Res::Err("No batch to vault/index %s/%d", obj.vaultId.GetHex(), obj.index);

if (obj.amount.nTokenId != batch->loanAmount.nTokenId)
return Res::Err("Bid token does not match auction one");

auto bid = mnview.GetAuctionBid(obj.vaultId, obj.index);
auto bid = mnview.GetAuctionBid({obj.vaultId, obj.index});
if (!bid) {
auto amount = MultiplyAmounts(batch->loanAmount.nValue, COIN + data->liquidationPenalty);
if (amount > obj.amount.nValue)
Expand All @@ -3434,7 +3439,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
//check balance
CalculateOwnerRewards(obj.from);
res = mnview.SubBalance(obj.from, obj.amount);
return !res ? res : mnview.StoreAuctionBid(obj.vaultId, obj.index, {obj.from, obj.amount});
return !res ? res : mnview.StoreAuctionBid({obj.vaultId, obj.index}, {obj.from, obj.amount});
}

Res operator()(const CCustomTxMessageNone&) const {
Expand Down Expand Up @@ -3689,7 +3694,7 @@ class CCustomTxRevertVisitor : public CCustomTxVisitor
}

Res operator()(const CAuctionBidMessage& obj) const {
if (auto bid = mnview.GetAuctionBid(obj.vaultId, obj.index))
if (auto bid = mnview.GetAuctionBid({obj.vaultId, obj.index}))
EraseHistory(bid->first);

return EraseHistory(obj.from);
Expand Down
8 changes: 4 additions & 4 deletions src/masternodes/rpc_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,12 @@ UniValue minttokens(const JSONRPCRequest& request) {
if (!token) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Token %s does not exist!", kv.first.ToString()));
}
auto& tokenImpl = static_cast<CTokenImplementation const& >(*token);
const Coin& authCoin = ::ChainstateActive().CoinsTip().AccessCoin(COutPoint(tokenImpl.creationTx, 1)); // always n=1 output
if (tokenImpl.IsDAT()) {
if (token->IsDAT()) {
needFoundersAuth = true;
} else {
const Coin& authCoin = ::ChainstateActive().CoinsTip().AccessCoin(COutPoint(token->creationTx, 1)); // always n=1 output
auths.insert(authCoin.out.scriptPubKey);
}
auths.insert(authCoin.out.scriptPubKey);
}
}
rawTx.vin = GetAuthInputsSmart(pwallet, rawTx.nVersion, auths, needFoundersAuth, optAuthTx, txInputs);
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ namespace {
UniValue batchArray{UniValue::VARR};
for (uint32_t i = 0; i < batchCount; i++) {
UniValue batchObj{UniValue::VOBJ};
auto batch = pcustomcsview->GetAuctionBatch(vaultId, i);
auto batch = pcustomcsview->GetAuctionBatch({vaultId, i});
batchObj.pushKV("index", int(i));
batchObj.pushKV("collaterals", AmountsToJSON(batch->collaterals.balances));
batchObj.pushKV("loan", tokenAmountString(batch->loanAmount));
if (auto bid = pcustomcsview->GetAuctionBid(vaultId, i)) {
if (auto bid = pcustomcsview->GetAuctionBid({vaultId, i})) {
UniValue bidObj{UniValue::VOBJ};
bidObj.pushKV("owner", ScriptToString(bid->first));
bidObj.pushKV("amount", tokenAmountString(bid->second));
Expand Down
38 changes: 24 additions & 14 deletions src/masternodes/vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ Res CVaultView::EraseAuction(const CVaultId& vaultId, uint32_t height)
if (it.Key().vaultId == vaultId) {
CAuctionData data = it.Value();
for (uint32_t i = 0; i < data.batchCount; i++) {
EraseAuctionBid(vaultId, i);
EraseAuctionBatch(vaultId, i);
EraseAuctionBid({vaultId, i});
EraseAuctionBatch({vaultId, i});
}
EraseBy<AuctionHeightKey>(it.Key());
return Res::Ok();
Expand All @@ -140,21 +140,26 @@ boost::optional<CAuctionData> CVaultView::GetAuction(const CVaultId& vaultId, ui
return {};
}

Res CVaultView::StoreAuctionBatch(const CVaultId& vaultId, uint32_t id, const CAuctionBatch& batch)
Res CVaultView::StoreAuctionBatch(const AuctionStoreKey& key, const CAuctionBatch& batch)
{
WriteBy<AuctionBatchKey>(std::make_pair(vaultId, id), batch);
WriteBy<AuctionBatchKey>(key, batch);
return Res::Ok();
}

Res CVaultView::EraseAuctionBatch(const CVaultId& vaultId, uint32_t id)
Res CVaultView::EraseAuctionBatch(const AuctionStoreKey& key)
{
EraseBy<AuctionBatchKey>(std::make_pair(vaultId, id));
EraseBy<AuctionBatchKey>(key);
return Res::Ok();
}

boost::optional<CAuctionBatch> CVaultView::GetAuctionBatch(const CVaultId& vaultId, uint32_t id)
boost::optional<CAuctionBatch> CVaultView::GetAuctionBatch(const AuctionStoreKey& key)
{
return ReadBy<AuctionBatchKey, CAuctionBatch>(std::make_pair(vaultId, id));
return ReadBy<AuctionBatchKey, CAuctionBatch>(key);
}

void CVaultView::ForEachAuctionBatch(std::function<bool(const AuctionStoreKey&, const CAuctionBatch&)> callback)
{
ForEach<AuctionBatchKey, AuctionStoreKey, CAuctionBatch>(callback);
}

void CVaultView::ForEachVaultAuction(std::function<bool(const CVaultId&, const CAuctionData&)> callback, uint32_t height, const CVaultId& vaultId)
Expand All @@ -165,19 +170,24 @@ void CVaultView::ForEachVaultAuction(std::function<bool(const CVaultId&, const C
}, CAuctionKey{vaultId, height});
}

Res CVaultView::StoreAuctionBid(const CVaultId& vaultId, uint32_t id, COwnerTokenAmount amount)
Res CVaultView::StoreAuctionBid(const AuctionStoreKey& key, COwnerTokenAmount amount)
{
WriteBy<AuctionBidKey>(std::make_pair(vaultId, id), amount);
WriteBy<AuctionBidKey>(key, amount);
return Res::Ok();
}

Res CVaultView::EraseAuctionBid(const CVaultId& vaultId, uint32_t id)
Res CVaultView::EraseAuctionBid(const AuctionStoreKey& key)
{
EraseBy<AuctionBidKey>(std::make_pair(vaultId, id));
EraseBy<AuctionBidKey>(key);
return Res::Ok();
}

boost::optional<CVaultView::COwnerTokenAmount> CVaultView::GetAuctionBid(const CVaultId& vaultId, uint32_t id)
boost::optional<CVaultView::COwnerTokenAmount> CVaultView::GetAuctionBid(const AuctionStoreKey& key)
{
return ReadBy<AuctionBidKey, COwnerTokenAmount>(key);
}

void CVaultView::ForEachAuctionBid(std::function<bool(const AuctionStoreKey& key, const COwnerTokenAmount& amount)> callback)
{
return ReadBy<AuctionBidKey, COwnerTokenAmount>(std::make_pair(vaultId, id));
ForEach<AuctionBidKey, AuctionStoreKey, COwnerTokenAmount>(callback);
}
18 changes: 11 additions & 7 deletions src/masternodes/vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ struct CAuctionBatch {
class CVaultView : public virtual CStorageView
{
public:
using COwnerTokenAmount = std::pair<CScript, CTokenAmount>;
using AuctionStoreKey = std::pair<CVaultId, uint32_t>;

Res StoreVault(const CVaultId&, const CVaultData&);
Res EraseVault(const CVaultId&);
boost::optional<CVaultData> GetVault(const CVaultId&) const;
Expand All @@ -167,15 +170,16 @@ class CVaultView : public virtual CStorageView
Res StoreAuction(const CVaultId& vaultId, const CAuctionData& data);
Res EraseAuction(const CVaultId& vaultId, uint32_t height);
boost::optional<CAuctionData> GetAuction(const CVaultId& vaultId, uint32_t height);
Res StoreAuctionBatch(const CVaultId& vaultId, uint32_t id, const CAuctionBatch& batch);
Res EraseAuctionBatch(const CVaultId& vaultId, uint32_t id);
boost::optional<CAuctionBatch> GetAuctionBatch(const CVaultId& vaultId, uint32_t id);
Res StoreAuctionBatch(const AuctionStoreKey& key, const CAuctionBatch& batch);
Res EraseAuctionBatch(const AuctionStoreKey& key);
boost::optional<CAuctionBatch> GetAuctionBatch(const AuctionStoreKey& vaultId);
void ForEachVaultAuction(std::function<bool(const CVaultId&, const CAuctionData&)> callback, uint32_t height, const CVaultId& vaultId = {});
void ForEachAuctionBatch(std::function<bool(const AuctionStoreKey&, const CAuctionBatch&)> callback);

using COwnerTokenAmount = std::pair<CScript, CTokenAmount>;
Res StoreAuctionBid(const CVaultId& vaultId, uint32_t id, COwnerTokenAmount amount);
Res EraseAuctionBid(const CVaultId& vaultId, uint32_t id);
boost::optional<COwnerTokenAmount> GetAuctionBid(const CVaultId& vaultId, uint32_t id);
Res StoreAuctionBid(const AuctionStoreKey& key, COwnerTokenAmount amount);
Res EraseAuctionBid(const AuctionStoreKey& key);
boost::optional<COwnerTokenAmount> GetAuctionBid(const AuctionStoreKey& key);
void ForEachAuctionBid(std::function<bool(const AuctionStoreKey& key, const COwnerTokenAmount& amount)> callback);

struct VaultKey { static constexpr uint8_t prefix() { return 0x20; } };
struct OwnerVaultKey { static constexpr uint8_t prefix() { return 0x21; } };
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/vaulthistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void CVaultHistoryView::WriteVaultState(CCustomCSView& mnview, const CBlockIndex
std::vector<CAuctionBatch> batches;
if (auto data = mnview.GetAuction(vaultID, pindex.nHeight)) {
for (uint32_t i{0}; i < data->batchCount; ++i) {
if (auto batch = mnview.GetAuctionBatch(vaultID, i)) {
if (auto batch = mnview.GetAuctionBatch({vaultID, i})) {
batches.push_back(*batch);
}
}
Expand Down
Loading

0 comments on commit a7f17c4

Please sign in to comment.