Merge branch 'jl777' into FSM

This commit is contained in:
jl777
2018-08-17 23:13:00 -11:00
9 changed files with 70 additions and 51 deletions

View File

@@ -170,7 +170,7 @@ int64_t AddFaucetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPub
std::string FaucetGet(uint64_t txfee)
{
CMutableTransaction mtx,tmpmtx; CPubKey mypk,faucetpk; int64_t inputs,CCchange=0,nValue=FAUCETSIZE; struct CCcontract_info *cp,C; std::string rawhex; int32_t i,j,len; uint8_t buf[32768]; bits256 hash;
CMutableTransaction mtx,tmpmtx; CPubKey mypk,faucetpk; int64_t inputs,CCchange=0,nValue=FAUCETSIZE; 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_FAUCET);
if ( txfee == 0 )
txfee = 10000;
@@ -184,10 +184,11 @@ std::string FaucetGet(uint64_t txfee)
mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET,CCchange,faucetpk));
mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
for (i=0; i<1000000; i++)
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_FAUCET << (uint8_t)'G' << mypk << i));
rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_FAUCET << (uint8_t)'G' << j));
if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
{
len >>= 1;

View File

@@ -241,17 +241,27 @@ bool RewardsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &t
case 'U':
//vin.0: locked funds CC vout.0 from lock
//vin.1+: funding CC vout.0 from 'F' and 'A' and 'U'
//vout.0: funding CC change
//vout.0: funding CC change or recover normal payout
//vout.1: normal output to unlock address
//vout.n-1: opreturn 'U' sbits fundingtxid
if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 )
return eval->Invalid("always should find vin.0, but didnt");
for (i=0; i<numvins; i++)
{
if ( (*cp->ismyvin)(tx.vin[i].scriptSig) == 0 )
return eval->Invalid("unexpected normal vin for unlock");
}
if ( eval->GetTxUnconfirmed(tx.vin[0].prevout.hash,vinTx,hashBlock) == 0 )
return eval->Invalid("always should find vin.0, but didnt");
else if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 )
if ( numvouts == 1 && numvins == 1 )
{
if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition() != 0 )
return eval->Invalid("unlock recover tx vout.0 is not normal output");
else if ( tx.vout[0].scriptPubKey != vinTx.vout[1].scriptPubKey )
return eval->Invalid("unlock recover tx vout.0 mismatched scriptPubKey");
else if ( tx.vout[0].nValue > vinTx.vout[0].nValue )
return eval->Invalid("unlock recover tx vout.0 mismatched amounts");
else return(true);
}
if ( vinTx.vout[0].scriptPubKey.IsPayToCryptoCondition() == 0 )
return eval->Invalid("lock tx vout.0 is normal output");
else if ( tx.vout.size() < 3 )
return eval->Invalid("unlock tx not enough vouts");
@@ -541,9 +551,9 @@ std::string RewardsAddfunding(uint64_t txfee,char *planstr,uint256 fundingtxid,i
std::string RewardsLock(uint64_t txfee,char *planstr,uint256 fundingtxid,int64_t deposit)
{
CMutableTransaction mtx; CPubKey mypk,rewardspk; CScript opret; uint64_t lockedfunds,sbits,funding,APR,minseconds,maxseconds,mindeposit; struct CCcontract_info *cp,C;
if ( deposit < 0 )
if ( deposit < txfee )
{
fprintf(stderr,"negative parameter error\n");
fprintf(stderr,"deposit amount less than txfee\n");
return("");
}
cp = CCinit(&C,EVAL_REWARDS);
@@ -614,31 +624,46 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2
return("");
}
}
if ( amount > 0) {
reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit);
if (scriptPubKey.size() > 0) {
if (reward > txfee) {
if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) > 0 ) {
if ( inputs >= (reward + 2*txfee) )
CCchange = (inputs - (reward + txfee));
fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN);
mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk));
mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey));
return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid)));
if ( amount > txfee )
{
reward = RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit);
if ( scriptPubKey.size() > 0 )
{
if ( reward > txfee )
{
if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) > 0 )
{
if ( inputs >= (reward + 2*txfee) )
CCchange = (inputs - (reward + txfee));
fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN);
mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk));
mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey));
return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid)));
}
else
{
mtx.vout.push_back(CTxOut(amount-txfee,scriptPubKey));
//CCerror = "cant find enough rewards inputs";
fprintf(stderr,"not enough rewards funds to payout %.8f, recover mode tx\n",(double)(rewards+txfee)/COIN);
return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,opret));
}
}
CCerror = "cant find enough rewards inputs";
fprintf(stderr,"%s\n", CCerror.c_str());
} else {
else
{
CCerror = strprintf("reward %.8f is <= the transaction fee", reward);
fprintf(stderr,"%s\n", CCerror.c_str());
}
} else {
}
}
else
{
CCerror = "invalid scriptPubKey";
fprintf(stderr,"%s\n", CCerror.c_str());
}
} else {
CCerror = "amount must be positive";
fprintf(stderr,"%s\n", CCerror.c_str());
}
}
else
{
CCerror = "amount must be more than txfee";
fprintf(stderr,"%s\n", CCerror.c_str());
}
fprintf(stderr,"amount %.8f -> reward %.8f\n",(double)amount/COIN,(double)reward/COIN);
return("");

View File

@@ -29,11 +29,7 @@
#include "src/json_rpc.c"
#include <cJSON.h>
#ifdef __LP64__
#include <stdlib.h>
#else
#include <malloc.h> // Index into CTransaction.vjoinsplit
#endif
struct CCType *CCTypeRegistry[] = {
@@ -199,7 +195,7 @@ CC *fulfillmentToCC(Fulfillment_t *ffill) {
CC *cc_readFulfillmentBinary(const unsigned char *ffill_bin, size_t ffill_bin_len) {
CC *cond = 0;
unsigned char *buf = malloc(ffill_bin_len);
unsigned char *buf = calloc(1,ffill_bin_len);
Fulfillment_t *ffill = 0;
asn_dec_rval_t rval = ber_decode(0, &asn_DEF_Fulfillment, (void **)&ffill, ffill_bin, ffill_bin_len);
if (rval.code != RC_OK) {

View File

@@ -62,7 +62,7 @@ static int ed25519Sign(CC *cond, CCVisitor visitor) {
if (cond->type->typeId != CC_Ed25519Type.typeId) return 1;
CCEd25519SigningData *signing = (CCEd25519SigningData*) visitor.context;
if (0 != memcmp(cond->publicKey, signing->pk, 32)) return 1;
if (!cond->signature) cond->signature = malloc(64);
if (!cond->signature) cond->signature = calloc(1,64);
ed25519_sign(cond->signature, visitor.msg, visitor.msgLength,
signing->pk, signing->skpk);
signing->nSigned++;
@@ -141,9 +141,9 @@ static void ed25519ToJSON(const CC *cond, cJSON *params) {
static CC *ed25519FromFulfillment(const Fulfillment_t *ffill) {
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = malloc(32);
cond->publicKey = calloc(1,32);
memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32);
cond->signature = malloc(64);
cond->signature = calloc(1,64);
memcpy(cond->signature, ffill->choice.ed25519Sha256.signature.buf, 64);
return cond;
}

View File

@@ -68,7 +68,7 @@ static CC *evalFromFulfillment(const Fulfillment_t *ffill) {
OCTET_STRING_t octets = eval->code;
cond->codeLength = octets.size;
cond->code = malloc(octets.size);
cond->code = calloc(1,octets.size);
memcpy(cond->code, octets.buf, octets.size);
return cond;

View File

@@ -56,8 +56,8 @@ typedef struct CCType {
/*
* Globals
*/
struct CCType *CCTypeRegistry[32];
int CCTypeRegistryLength;
extern struct CCType *CCTypeRegistry[16];
extern int CCTypeRegistryLength;
/*

View File

@@ -17,11 +17,8 @@
#include "internal.h"
#include <cJSON.h>
#ifdef __LP64__
#include <stdlib.h>
#else
#include <malloc.h> // Index into CTransaction.vjoinsplit
#endif
static cJSON *jsonCondition(CC *cond) {
cJSON *root = cJSON_CreateObject();

View File

@@ -26,7 +26,7 @@ struct CCType CC_PrefixType;
static int prefixVisitChildren(CC *cond, CCVisitor visitor) {
size_t prefixedLength = cond->prefixLength + visitor.msgLength;
unsigned char *prefixed = malloc(prefixedLength);
unsigned char *prefixed = calloc(1,prefixedLength);
memcpy(prefixed, cond->prefix, cond->prefixLength);
memcpy(prefixed + cond->prefixLength, visitor.msg, visitor.msgLength);
visitor.msg = prefixed;

View File

@@ -39,7 +39,7 @@ static int mod_table[] = {0, 2, 1};
void build_decoding_table() {
decoding_table = malloc(256);
decoding_table = calloc(1,256);
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char) encoding_table[i]] = i;
}
@@ -49,7 +49,7 @@ unsigned char *base64_encode(const unsigned char *data, size_t input_length) {
size_t output_length = 4 * ((input_length + 2) / 3);
unsigned char *encoded_data = malloc(output_length + 1);
unsigned char *encoded_data = calloc(1,output_length + 1);
if (encoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
@@ -90,7 +90,7 @@ unsigned char *base64_decode(const unsigned char *data_,
size_t input_length = strlen(data_);
int rem = input_length % 4;
unsigned char *data = malloc(input_length + (4-rem));
unsigned char *data = calloc(1,input_length + (4-rem));
strcpy(data, data_);
// for unpadded b64
@@ -111,7 +111,7 @@ unsigned char *base64_decode(const unsigned char *data_,
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = malloc(*output_length);
unsigned char *decoded_data = calloc(1,*output_length);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
@@ -217,7 +217,7 @@ unsigned char *hashFingerprintContents(asn_TYPE_descriptor_t *asnType, void *fp)
fprintf(stderr, "Encoding fingerprint failed\n");
return 0;
}
unsigned char *hash = malloc(32);
unsigned char *hash = calloc(1,32);
sha256(buf, rc.encoded, hash);
return hash;
}
@@ -225,7 +225,7 @@ unsigned char *hashFingerprintContents(asn_TYPE_descriptor_t *asnType, void *fp)
char* cc_hex_encode(const uint8_t *bin, size_t len)
{
char* hex = malloc(len*2+1);
char* hex = calloc(1,len*2+1);
if (bin == NULL) return hex;
char map[16] = "0123456789ABCDEF";
for (int i=0; i<len; i++) {