Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RingCT Staking #1019

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
188bd83
[Refactor] SetOutputs, ArrangeOutBlinds
Zannick May 19, 2022
43d50f7
[Refactor] Parameterize CreateCoinStake.
Zannick May 19, 2022
01c1ae9
Initial RingCTStake class.
Zannick Jun 28, 2022
e67ccd2
Reduce effective weights of inputs.
Zannick Jul 16, 2022
b5366b0
RingCT: SignStakeTx
Zannick Jul 16, 2022
3c258cc
RingCT Stake: mark input as spent
Zannick Jul 16, 2022
ba6ad67
RingCT: Refactor to isolate input/output handling.
Zannick Jul 16, 2022
66f7e40
RingCT staking params and fixes
Zannick Aug 9, 2022
d9ebfd2
Fix weight calculation.
Zannick Aug 13, 2022
e588f97
More attempted fixes
Zannick Aug 15, 2022
ce5daef
checkpoint some more attempts for ringct
Zannick Aug 28, 2022
f14b4c3
RingCT stake: add reward outputs.
Zannick Oct 22, 2022
d971e6c
RingCT Stake: Sign block.
Zannick Oct 23, 2022
a4b064f
RingCT: Correctly mark pending spends.
Zannick Oct 23, 2022
3b423c6
RingCT Stake: verify block signature
Zannick Oct 23, 2022
727d150
RingCT stake: Move rewards to coinbase.
Zannick Oct 28, 2022
986c7a5
RingCT stake: accept and load the block
Zannick Oct 29, 2022
4787fba
Alter some regtest params.
Zannick Oct 29, 2022
52e904f
RingCT Staking: Make hash proof match.
Zannick Oct 29, 2022
9156bd4
RingCT Stake: Rangeproof the min. allowed weight.
Zannick Oct 30, 2022
d6af2ec
RingCT Stake: Cleanup
Zannick Oct 30, 2022
fae067a
RingCT Stake: Removed transactionsigcontext.
Zannick Oct 30, 2022
4b7e21f
RingCT Stake: change required depth.
Zannick Oct 30, 2022
037b458
Remove unneeded try-catch.
Zannick Nov 5, 2022
89822cb
DevNet RingCT staking at height 25000.
Zannick Jan 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
RingCT Stake: verify block signature
and unmark pending spends if the block is not created.
  • Loading branch information
Zannick committed Oct 30, 2022
commit 3b423c6af2a2afbe5ee2e25646311c52d5362b91
15 changes: 9 additions & 6 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include <veil/budget.h>
#include <veil/proofoffullnode/proofoffullnode.h>
#include <veil/ringct/anonwallet.h>
#include <veil/ringct/anon.h>
#include <veil/zerocoin/zchain.h>

#include <algorithm>
Expand Down Expand Up @@ -178,6 +180,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
LOCK(cs_mapblockindex);
pindexPrev = mapBlockIndex.at(hashBest);
}
std::unique_ptr<CPendingSpend> pendingRCTStake;
if (fProofOfStake && pindexPrev->nHeight + 1 >= Params().HeightPoSStart()) {
//POS block - one coinbase is null then non null coinstake
//POW block - one coinbase that is not null
Expand All @@ -192,10 +195,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
if (!pwalletMain->CreateZerocoinStake(pindexPrev, pblock->nBits, txCoinStake, nTxNewTime, nComputeTimeStart)) {
if (!pwalletMain->CreateRingCTStake(pindexPrev, pblock->nBits, txCoinStake, nTxNewTime, nComputeTimeStart))
return nullptr;
else {
// output debugging info
return nullptr;
}
pendingRCTStake = pwalletMain->GetAnonWallet()->GetPendingSpendForTx(txCoinStake.GetHash());
}

pblock->nTime = nTxNewTime;
Expand Down Expand Up @@ -539,8 +539,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
} else if (pblock->vtx[1]->IsRingCtSpend()) {
// TODO: enable_wallet guards.
COutPoint prevout;
COutputRecord record;
CKeyID keyid;
// Get keyimage from the input
if (!pwalletMain->GetAnonWallet()->IsMyAnonInput(pblock->vtx[1]->vin[0], prevout, key)) {
LogPrint(BCLog::STAKING, "Failed to get key from anon spend\n");
return nullptr;
Expand All @@ -560,6 +559,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
if (pindexPrev && pindexPrev != chainActive.Tip()) {
error("%s: stale tip.", __func__);
pblocktemplate->nFlags |= TF_STAILTIP;
if (pendingRCTStake)
pendingRCTStake->SetSuccess(true);
return std::move(pblocktemplate);
}

Expand All @@ -580,6 +581,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}

pblocktemplate->nFlags = TF_SUCCESS;
if (pendingRCTStake)
pendingRCTStake->SetSuccess(true);
return std::move(pblocktemplate);
}

Expand Down
65 changes: 56 additions & 9 deletions src/veil/proofofstake/blockvalidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

#include "blockvalidation.h"

#include "chainparams.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "util/system.h"
#include "validation.h"
#include "veil/ringct/rctindex.h"
#include "veil/zerocoin/zchain.h"

namespace veil {
Expand All @@ -16,19 +19,63 @@ bool ValidateBlockSignature(const CBlock& block)
if (block.IsProofOfWork())
return true;

if (block.vtx.size() < 2 || block.vtx[1]->vin.empty() || !block.vtx[1]->vin[0].IsZerocoinSpend())
if (block.vtx.size() < 2 || block.vtx[1]->vin.empty())
return error("%s: Block transaction structure is not compatible with Veil's Proof of Stake validation", __func__);

//Get the zerocoin that was staked
auto spend = TxInToZerocoinSpend(block.vtx[1]->vin[0]);
if (!spend)
return error("%s: failed to get spend from txin", __func__);
auto pubkey = spend->getPubKey();
//Get the coin that was staked
if (block.vtx[1]->vin[0].IsZerocoinSpend()) {
auto spend = TxInToZerocoinSpend(block.vtx[1]->vin[0]);
if (!spend)
return error("%s: failed to get spend from txin", __func__);
CPubKey pubkey = spend->getPubKey();

if (!pubkey.IsValid())
return error("%s: Public Key from zerocoin stake is not valid", __func__);
if (!pubkey.IsValid())
return error("%s: Public Key from zerocoin stake is not valid", __func__);

return pubkey.Verify(block.GetHash(), block.vchBlockSig);
return pubkey.Verify(block.GetHash(), block.vchBlockSig);
} else if (block.vtx[1]->vin[0].IsAnonInput()) {
if (block.nHeight < Params().HeightRingCTStaking()) {
return error("%s: RingCT staking not accepted before height %d",
__func__, Params().HeightRingCTStaking());
}
CTxIn txin = block.vtx[1]->vin[0];
const std::vector<uint8_t> &vKeyImages = txin.scriptData.stack[0];
const std::vector<uint8_t> vMI = txin.scriptWitness.stack[0];

uint32_t nInputs, nRingSize;
txin.GetAnonInfo(nInputs, nRingSize);
size_t nCols = nRingSize;
size_t nRows = nInputs + 1;

if (vKeyImages.size() != nInputs * 33)
return error("%s: bad keyimage size", __func__);

const CCmpPubKey &ki = *((CCmpPubKey *) &vKeyImages[0]);

size_t ofs = 0, nB = 0;
for (size_t i = 0; i < nCols; ++i) {
int64_t nIndex = 0;

if (0 != GetVarInt(vMI, ofs, (uint64_t &) nIndex, nB))
return false;
ofs += nB;

CAnonOutput ao;
if (!pblocktree->ReadRCTOutput(nIndex, ao))
return false;

CPubKey pubkey;
pubkey.Set(ao.pubkey.begin(), ao.pubkey.end());
if (!pubkey.IsValid())
return error("%s: public key from ringct stake is not valid");

if (pubkey.Verify(block.GetHash(), block.vchBlockSig))
return true;
}
return error("%s: No pubkeys verified for RingCT stake", __func__);
}

return error("%s: Stake transaction is not zerocoin or ringct spend", __func__);
}

}