Browse Source

Initial rewards CC

pull/4/head
jl777 6 years ago
parent
commit
194ad5b859
  1. 39
      src/cc/CCcustom.cpp
  2. 4
      src/cc/eval.cpp
  3. 4
      src/cc/eval.h
  4. 7
      src/cc/faucet.cpp
  5. 224
      src/cc/rewards.cpp
  6. 4
      src/main.cpp
  7. 6
      src/miner.cpp

39
src/cc/CCcustom.cpp

@ -28,6 +28,7 @@
CC *MakeAssetCond(CPubKey pk);
CC *MakeFaucetCond(CPubKey pk);
CC *MakeRewardsCond(CPubKey pk);
//BTCD Address: RAssetsAtGnvwgK9gVHBbAU4sVTah1hAm5
//BTCD Privkey: UvtvQVgVScXEYm4J3r4nE4nbFuGXSVM5pKec8VWXwgG9dmpWBuDh
@ -36,10 +37,14 @@ CC *MakeFaucetCond(CPubKey pk);
const char *AssetsCCaddr = "RGKRjeTBw4LYFotSDLT6RWzMHbhXri6BG6" ;//"RFYE2yL3KknWdHK6uNhvWacYsCUtwzjY3u";
char AssetsCChexstr[67] = { "02adf84e0e075cf90868bd4e3d34a03420e034719649c41f371fc70d8e33aa2702" };
uint8_t AssetsCCpriv[32] = { 0x9b, 0x17, 0x66, 0xe5, 0x82, 0x66, 0xac, 0xb6, 0xba, 0x43, 0x83, 0x74, 0xf7, 0x63, 0x11, 0x3b, 0xf0, 0xf3, 0x50, 0x6f, 0xd9, 0x6b, 0x67, 0x85, 0xf9, 0x7a, 0xf0, 0x54, 0x4d, 0xb1, 0x30, 0x77 };
const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC" ;//"RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk";
char FaucetCChexstr[67] = { "03682b255c40d0cde8faee381a1a50bbb89980ff24539cb8518e294d3a63cefe12" };
uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4b, 0xc7, 0xdd, 0x71, 0xa0, 0x39, 0xc4, 0xbe, 0x1a, 0xfe, 0xeb, 0xc2, 0x46, 0xda, 0x76, 0xf8, 0x07, 0x53, 0x3d, 0x96, 0xb4, 0xca, 0xa0, 0xe9 };
const char *RewardsCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC" ;//"RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk";
char RewardsCChexstr[67] = { "03682b255c40d0cde8faee381a1a50bbb89980ff24539cb8518e294d3a63cefe12" };
uint8_t RewardsCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4b, 0xc7, 0xdd, 0x71, 0xa0, 0x39, 0xc4, 0xbe, 0x1a, 0xfe, 0xeb, 0xc2, 0x46, 0xda, 0x76, 0xf8, 0x07, 0x53, 0x3d, 0x96, 0xb4, 0xca, 0xa0, 0xe9 };
bool IsAssetsInput(CScript const& scriptSig)
{
@ -75,6 +80,23 @@ bool IsFaucetInput(CScript const& scriptSig)
return out;
}
bool IsRewardsInput(CScript const& scriptSig)
{
CC *cond;
if (!(cond = GetCryptoCondition(scriptSig)))
return false;
// Recurse the CC tree to find asset condition
auto findEval = [&] (CC *cond, struct CCVisitor _) {
bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == EVAL_REWARDS;
// false for a match, true for continue
return r ? 0 : 1;
};
CCVisitor visitor = {findEval, (uint8_t*)"", 0, NULL};
bool out =! cc_visit(cond, visitor);
cc_free(cond);
return out;
}
CPubKey GetUnspendable(uint8_t evalcode,uint8_t *unspendablepriv)
{
static CPubKey nullpk;
@ -92,12 +114,18 @@ CPubKey GetUnspendable(uint8_t evalcode,uint8_t *unspendablepriv)
memcpy(unspendablepriv,FaucetCCpriv,32);
return(pubkey2pk(ParseHex(FaucetCChexstr)));
}
else if ( evalcode == EVAL_REWARDS )
{
if ( unspendablepriv != 0 )
memcpy(unspendablepriv,RewardsCCpriv,32);
return(pubkey2pk(ParseHex(RewardsCChexstr)));
}
else return(nullpk);
}
CC *MakeCC(uint8_t evalcode,CPubKey pk)
{
if ( evalcode == EVAL_ASSETS || evalcode == EVAL_FAUCET )
if ( evalcode == EVAL_ASSETS || evalcode == EVAL_FAUCET || evalcode == EVAL_REWARDS )
{
std::vector<CC*> pks;
pks.push_back(CCNewSecp256k1(pk));
@ -131,6 +159,15 @@ bool GetCCaddress(uint8_t evalcode,char *destaddr,CPubKey pk)
}
return(destaddr[0] != 0);
}
else if ( evalcode == EVAL_REWARDS )
{
if ( (payoutCond= MakeRewardsCond(pk)) != 0 )
{
Getscriptaddress(destaddr,CCPubKey(payoutCond));
cc_free(payoutCond);
}
return(destaddr[0] != 0);
}
fprintf(stderr,"GetCCaddress %02x is invalid evalcode\n",evalcode);
return false;
}

4
src/cc/eval.cpp

@ -78,6 +78,10 @@ bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
case EVAL_FAUCET:
return ProcessFaucet(this, vparams, txTo, nIn);
break;
case EVAL_REWARDS:
return ProcessRewards(this, vparams, txTo, nIn);
break;
}
return Invalid("invalid-code, dont forget to add EVAL_NEWCC to Eval::Dispatch");
}

4
src/cc/eval.h

@ -40,7 +40,8 @@
EVAL(EVAL_IMPORTPAYOUT, 0xe1) \
EVAL(EVAL_IMPORTCOIN, 0xe2) \
EVAL(EVAL_ASSETS, 0xe3) \
EVAL(EVAL_FAUCET, 0xe4)
EVAL(EVAL_FAUCET, 0xe4) \
EVAL(EVAL_REWARDS, 0xe5)
typedef uint8_t EvalCode;
@ -269,6 +270,7 @@ typedef std::pair<uint256,MerkleBranch> TxProof;
uint256 GetMerkleRoot(const std::vector<uint256>& vLeaves);
bool ProcessAssets(Eval* eval, std::vector<uint8_t> paramsNull, const CTransaction &tx, unsigned int nIn);
bool ProcessFaucet(Eval* eval, std::vector<uint8_t> paramsNull, const CTransaction &tx, unsigned int nIn);
bool ProcessRewards(Eval* eval, std::vector<uint8_t> paramsNull, const CTransaction &tx, unsigned int nIn);
#endif /* CC_EVAL_H */

7
src/cc/faucet.cpp

@ -119,18 +119,21 @@ bool FaucetValidate(Eval* eval,const CTransaction &tx)
return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));
}
}
fprintf(stderr,"faucet validated\n");
return(true);
}
bool ProcessFaucet(Eval* eval, std::vector<uint8_t> paramsNull,const CTransaction &ctx, unsigned int nIn)
{
fprintf(stderr,"start faucet validate\n");
if ( paramsNull.size() != 0 ) // Don't expect params
return eval->Invalid("Cannot have params");
else if ( ctx.vout.size() == 0 )
return eval->Invalid("no-vouts");
if ( FaucetValidate(eval,ctx) != 0 )
return(true);
else return(false);
fprintf(stderr,"faucet validate failed\n");
return(false);
}
uint64_t AddFaucetInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs)
@ -166,7 +169,7 @@ std::string FaucetFund(uint64_t txfee,uint64_t funds)
txfee = 10000;
mypk = pubkey2pk(Mypubkey());
faucetpk = GetUnspendable(EVAL_FAUCET,0);
if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 )
if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
{
mtx.vout.push_back(MakeFaucetVout(funds,faucetpk));
return(FinalizeCCTx(EVAL_FAUCET,mtx,mypk,txfee,opret));

224
src/cc/rewards.cpp

@ -0,0 +1,224 @@
/******************************************************************************
* Copyright © 2014-2018 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "CCinclude.h"
/*
*/
#define EVAL_REWARDS 0xe5
extern const char *RewardsCCaddr;
extern char RewardsCChexstr[67];
uint64_t RewardsCalc(uint64_t claim,uint256 txid)
{
uint64_t reward = 0;
return(reward);
}
CC *MakeRewardsCond(CPubKey pk)
{
std::vector<CC*> pks; uint8_t evalcode = EVAL_REWARDS;
pks.push_back(CCNewSecp256k1(pk));
CC *rewardsCC = CCNewEval(E_MARSHAL(ss << evalcode));
CC *Sig = CCNewThreshold(1, pks);
return CCNewThreshold(2, {rewardsCC, Sig});
}
CTxOut MakeRewardsVout(CAmount nValue,CPubKey pk)
{
CTxOut vout;
CC *payoutCond = MakeRewardsCond(pk);
vout = CTxOut(nValue,CCPubKey(payoutCond));
cc_free(payoutCond);
return(vout);
}
uint64_t IsRewardsvout(const CTransaction& tx,int32_t v)
{
char destaddr[64];
if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
{
if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,RewardsCCaddr) == 0 )
return(tx.vout[v].nValue);
}
return(0);
}
bool RewardsExactAmounts(Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
{
static uint256 zerohash;
CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; uint64_t inputs=0,outputs=0,assetoshis;
numvins = tx.vin.size();
numvouts = tx.vout.size();
for (i=0; i<numvins; i++)
{
if ( IsRewardsInput(tx.vin[i].scriptSig) != 0 )
{
if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
return eval->Invalid("always should find vin, but didnt");
else
{
if ( hashBlock == zerohash )
return eval->Invalid("cant rewards from mempool");
if ( (assetoshis= IsRewardsvout(vinTx,tx.vin[i].prevout.n)) != 0 )
inputs += assetoshis;
}
}
}
for (i=0; i<numvouts; i++)
{
//fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
if ( (assetoshis= IsRewardsvout(tx,i)) != 0 )
outputs += assetoshis;
}
if ( inputs != outputs+COIN+txfee )
{
fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
return eval->Invalid("mismatched inputs != outputs + COIN + txfee");
}
else return(true);
}
bool RewardsValidate(Eval* eval,const CTransaction &tx)
{
int32_t numvins,numvouts,preventCCvins,preventCCvouts,i;
numvins = tx.vin.size();
numvouts = tx.vout.size();
preventCCvins = preventCCvouts = -1;
if ( numvouts < 1 )
return eval->Invalid("no vouts");
else
{
for (i=0; i<numvins; i++)
{
if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
return eval->Invalid("illegal normal vini");
}
if ( RewardsExactAmounts(eval,tx,1,10000) == false )
return false;
else
{
preventCCvouts = 1;
if ( IsRewardsvout(tx,0) != 0 )
{
preventCCvouts++;
i = 1;
} else i = 0;
if ( tx.vout[i].nValue != COIN )
return eval->Invalid("invalid rewards output");
return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));
}
}
return(true);
}
bool ProcessRewards(Eval* eval, std::vector<uint8_t> paramsNull,const CTransaction &ctx, unsigned int nIn)
{
if ( paramsNull.size() != 0 ) // Don't expect params
return eval->Invalid("Cannot have params");
else if ( ctx.vout.size() == 0 )
return eval->Invalid("no-vouts");
if ( RewardsValidate(eval,ctx) != 0 )
return(true);
else return(false);
}
uint64_t AddRewardsInputs(CMutableTransaction &mtx,CPubKey pk,uint64_t total,int32_t maxinputs)
{
char coinaddr[64]; uint64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t n = 0;
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
GetCCaddress(EVAL_REWARDS,coinaddr,pk);
SetCCunspents(unspentOutputs,coinaddr);
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
{
txid = it->first.txhash;
if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
{
if ( (nValue= IsRewardsvout(vintx,(int32_t)it->first.index)) > 0 )
{
if ( total != 0 && maxinputs != 0 )
mtx.vin.push_back(CTxIn(txid,(int32_t)it->first.index,CScript()));
nValue = it->second.satoshis;
totalinputs += nValue;
n++;
if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
break;
}
}
}
return(totalinputs);
}
// 0.834% every 60 days, min 100, capped at 0.834%
std::string RewardsFund(uint64_t txfee,uint64_t funds,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit)
{
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret;
if ( txfee == 0 )
txfee = 10000;
mypk = pubkey2pk(Mypubkey());
rewardspk = GetUnspendable(EVAL_REWARDS,0);
if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 )
{
mtx.vout.push_back(MakeRewardsVout(funds,rewardspk));
mtx.vout.push_back(CTxOut(APR,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG));
mtx.vout.push_back(CTxOut(minseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG));
mtx.vout.push_back(CTxOut(maxseconds,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG));
mtx.vout.push_back(CTxOut(mindeposit,CScript() << ParseHex(HexStr(rewardspk)) << OP_CHECKSIG));
return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret));
}
return(0);
}
std::string RewardsLock(uint64_t txfee,uint64_t amount)
{
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret;
if ( txfee == 0 )
txfee = 10000;
rewardspk = GetUnspendable(EVAL_REWARDS,0);
mypk = pubkey2pk(Mypubkey());
if ( AddNormalinputs(mtx,mypk,amount+2*txfee,64) > 0 )
{
mtx.vout.push_back(MakeRewardsVout(amount,rewardspk));
// specify destination pubkey, funding txid
return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret create script));
} else fprintf(stderr,"cant find rewards inputs\n");
return(0);
}
std::string RewardsUnlock(uint64_t txfee)
{
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t reward,claim,inputs,CCchange=0;
if ( txfee == 0 )
txfee = 10000;
rewardspk = GetUnspendable(EVAL_REWARDS,0);
mypk = pubkey2pk(Mypubkey());
if ( (claim= AddRewardsInputs(mtx,mypk,(1LL << 30),1)) > 0 && (reward= RewardsCalc(claim,mtx.vin[0].prevout.hash)) > txfee )
{
if ( (inputs= AddRewardsInputs(mtx,mypk,reward+txfee,30)) > 0 )
{
if ( inputs > (reward+txfee) )
CCchange = (inputs - reward - txfee);
if ( CCchange != 0 )
mtx.vout.push_back(MakeRewardsVout(CCchange,rewardspk));
mtx.vout.push_back(CTxOut(claim+reward,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
return(FinalizeCCTx(EVAL_REWARDS,mtx,mypk,txfee,opret));
}
} else fprintf(stderr,"cant find rewards inputs\n");
return(0);
}

4
src/main.cpp

@ -4529,7 +4529,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo
bool checked; uint256 hash; int32_t futureblock=0;
auto verifier = libzcash::ProofVerifier::Disabled();
hash = pblock->GetHash();
//fprintf(stderr,"ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight);
fprintf(stderr,"ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight);
if ( chainActive.LastTip() != 0 )
komodo_currentheight_set(chainActive.LastTip()->nHeight);
checked = CheckBlock(&futureblock,height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier,0);
@ -4569,7 +4569,7 @@ bool ProcessNewBlock(bool from_miner,int32_t height,CValidationState &state, CNo
if (futureblock == 0 && !ActivateBestChain(state, pblock))
return error("%s: ActivateBestChain failed", __func__);
//fprintf(stderr,"finished ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight);
fprintf(stderr,"finished ProcessBlock %d\n",(int32_t)chainActive.LastTip()->nHeight);
return true;
}

6
src/miner.cpp

@ -644,7 +644,7 @@ static bool ProcessBlockFound(CBlock* pblock)
// Found a solution
{
LOCK(cs_main);
//LOCK(cs_main);
if (pblock->hashPrevBlock != chainActive.LastTip()->GetBlockHash())
{
uint256 hash; int32_t i;
@ -673,11 +673,13 @@ static bool ProcessBlockFound(CBlock* pblock)
// Track how many getdata requests this block gets
//if ( 0 )
{
fprintf(stderr,"lock cs_wallet\n");
LOCK(wallet.cs_wallet);
wallet.mapRequestCount[pblock->GetHash()] = 0;
}
#endif
fprintf(stderr,"process new block\n");
// Process this block the same as if we had received it from another node
CValidationState state;
if (!ProcessNewBlock(1,chainActive.LastTip()->nHeight+1,state, NULL, pblock, true, NULL))

Loading…
Cancel
Save