Channelsopen

This commit is contained in:
jl777
2018-08-28 02:18:04 -11:00
parent 510e020cb1
commit 810f63668c
13 changed files with 122 additions and 77 deletions

View File

@@ -18,8 +18,10 @@
#define CC_CHANNELS_H
#include "CCinclude.h"
#define CHANNELS_MAXPAYMENTS 1000
bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx);
std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int32_t payment);
// CCcustom
UniValue ChannelsInfo();

View File

@@ -92,7 +92,10 @@ int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex);
CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv);
// CCutils
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len);
uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv);
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk);
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2);
CC *MakeCCcond1(uint8_t evalcode,CPubKey pk);
CC* GetCryptoCondition(CScript const& scriptSig);
bool IsCCInput(CScript const& scriptSig);
@@ -104,6 +107,7 @@ char *pubkey33_str(char *dest,uint8_t *pubkey33);
uint256 Parseuint256(char *hexstr);
CPubKey pubkey2pk(std::vector<uint8_t> pubkey);
bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk);
bool GetCCaddress1of2(struct CCcontract_info *cp,char *destaddr,CPubKey pk,CPubKey pk2);
bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,int64_t nValue);
bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t numvins,int32_t preventCCvouts,int32_t numvouts);
bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey);

View File

@@ -20,7 +20,6 @@
#include "CCinclude.h"
#define EVAL_LOTTO 0xe9
uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv);
bool LottoValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx);

View File

@@ -19,6 +19,16 @@
CCutils has low level functions that are universally useful for all contracts.
*/
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len)
{
int32_t i,j=0;
#if defined(WORDS_BIGENDIAN)
for (i=31; i>=0; i--)
dest[j++] = src[i];
#else
memcpy(dest,src,len);
#endif
}
CC *MakeCCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2)
{
@@ -48,6 +58,15 @@ CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk)
return(vout);
}
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2)
{
CTxOut vout;
CC *payoutCond = MakeCCcond1of2(evalcode,pk,pk2);
vout = CTxOut(nValue,CCPubKey(payoutCond));
cc_free(payoutCond);
return(vout);
}
CC* GetCryptoCondition(CScript const& scriptSig)
{
auto pc = scriptSig.begin();
@@ -167,6 +186,18 @@ bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk)
return(destaddr[0] != 0);
}
bool GetCCaddress1of2(struct CCcontract_info *cp,char *destaddr,CPubKey pk,CPubKey pk2)
{
CC *payoutCond;
destaddr[0] = 0;
if ( (payoutCond= MakeCCcond1of2(cp->evalcode,pk,pk2)) != 0 )
{
Getscriptaddress(destaddr,CCPubKey(payoutCond));
cc_free(payoutCond);
}
return(destaddr[0] != 0);
}
bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,int64_t nValue)
{
char destaddr[64];

View File

@@ -35,6 +35,14 @@
For efficiency we want to allow batch spend with multiple secrets to claim a single total
Second iteration:
As implementing it, some efficieny gains to be made with a slightly different approach.
Instead of separate secrets for each amount, a hashchain will be used, each releasing the same amount
To spend, the prior value in the hash chain is published, or can publish N deep. validation takes N hashes.
Also, in order to be able to track open channels, a tag is needed to be sent and better to send to a normal CC address for a pubkey to isolate the transactions for channel opens.
*/
// start of consensus code
@@ -129,6 +137,13 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &
// helper functions for rpc calls in rpcwallet.cpp
CScript EncodeChannelsOpRet(uint8_t funcid,int32_t numpayments,int32_t payment,uint256 hashchain)
{
CScript opret; uint8_t evalcode = EVAL_CHANNELS;
opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << numpayments << payment << hashchain);
return(opret);
}
int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
{
char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
@@ -157,58 +172,33 @@ int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CP
return(totalinputs);
}
std::string ChannelsGet(uint64_t txfee,int64_t nValue)
std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int32_t payment)
{
CMutableTransaction mtx,tmpmtx; CPubKey mypk,Channelspk; int64_t inputs,CCchange=0; struct CCcontract_info *cp,C; std::string rawhex; uint32_t j; int32_t i,len; uint8_t buf[32768]; bits256 hash;
cp = CCinit(&C,EVAL_CHANNELS);
if ( txfee == 0 )
txfee = 10000;
Channelspk = GetUnspendable(cp,0);
mypk = pubkey2pk(Mypubkey());
if ( (inputs= AddChannelsInputs(cp,mtx,Channelspk,nValue+txfee,60)) > 0 )
CMutableTransaction mtx; uint8_t hash[32],hashdest[32]; uint64_t funds; int32_t i; uint256 hashchain,entropy,hentropy; CPubKey mypk; CScript opret; struct CCcontract_info *cp,C;
if ( numpayments <= 0 || payment <= 0 || numpayments > CHANNELS_MAXPAYMENTS )
{
if ( inputs > nValue )
CCchange = (inputs - nValue - txfee);
if ( CCchange != 0 )
mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CCchange,Channelspk));
mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
j = rand() & 0xfffffff;
for (i=0; i<1000000; i++,j++)
{
tmpmtx = mtx;
rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_CHANNELS << (uint8_t)'G' << j));
if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
{
len >>= 1;
decode_hex(buf,len,(char *)rawhex.c_str());
hash = bits256_doublesha256(0,buf,len);
if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 )
{
fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
return(rawhex);
}
//fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
}
}
fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
CCerror = strprintf(stderr,"invalid ChannelsFund param numpayments.%d max.%d payment.%d\n",numpayments,CHANNELS_MAXPAYMENTS,payment);
fprintf(stderr,"%s\n",CCerror.c_str());
return("");
} else fprintf(stderr,"cant find Channels inputs\n");
return("");
}
std::string ChannelsFund(uint64_t txfee,int64_t funds)
{
CMutableTransaction mtx; CPubKey mypk,Channelspk; CScript opret; struct CCcontract_info *cp,C;
}
cp = CCinit(&C,EVAL_CHANNELS);
if ( txfee == 0 )
txfee = 10000;
mypk = pubkey2pk(Mypubkey());
Channelspk = GetUnspendable(cp,0);
if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
funds = numpayments * payment;
if ( AddNormalinputs(mtx,mypk,funds+2*txfee,64) > 0 )
{
mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,funds,Channelspk));
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash);
endiancpy(hash,&hentropy,32);
for (i=0; i<numpayments; i++)
{
vcalc_sha256(0,hashdest,hash,32);
memcpy(hash,hashdest,32);
}
endiancpy((uint8_t *)&hashchain,hashdest,32);
mtx.vout.push_back(MakeCC1of2vout(EVAL_CHANNELS,funds,mypk,destpub));
mtx.vout.push_back(MakeCC1(EVAL_CHANNELS,txfee,mypk));
return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeChannelsOpRet('O',numpayments,payment,hashchain)));
}
return("");
}

View File

@@ -167,17 +167,6 @@ void DiceQueue(int32_t iswin,uint64_t sbits,uint256 fundingtxid,uint256 bettxid)
} // small memory leak per DiceQueue
}
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len)
{
int32_t i,j=0;
#if defined(WORDS_BIGENDIAN)
for (i=31; i>=0; i--)
dest[j++] = src[i];
#else
memcpy(dest,src,len);
#endif
}
CPubKey DiceFundingPk(CScript scriptPubKey)
{
CPubKey pk; uint8_t *ptr,*dest; int32_t i;