From 64449ba8197bccc800ba627a72e35a018c36a99e Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 20 Jan 2017 22:39:49 +0200 Subject: [PATCH] test --- src/komodo_gateway.h | 40 ++++++++++++++++++++++++++++------------ src/komodo_structs.h | 4 ++++ src/rpcblockchain.cpp | 4 ++-- src/wallet/rpcwallet.cpp | 7 ++++--- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a048a597b..d7a7c4a39 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -671,19 +671,32 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above return(0); } -int32_t komodo_kvsearch(uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) +int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) { - struct komodo_kv *ptr; int32_t retval = -1; + struct komodo_kv *ptr; int32_t duration,retval = -1; *heightp = -1; *flagsp = 0; portable_mutex_lock(&KOMODO_KV_mutex); HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); if ( ptr != 0 ) { - *heightp = ptr->height; - *flagsp = ptr->flags; - if ( (retval= ptr->valuesize) != 0 ) - memcpy(value,ptr->value,retval); + duration = ((ptr->flags >> 2) + 1) * KOMODO_KVDURATION; + if ( current_height > (ptr->height + duration) ) + { + HASH_DELETE(hh,KOMODO_KV,ptr); + if ( ptr->value != 0 ) + free(ptr->value); + if ( ptr->key != 0 ) + free(ptr->key); + free(ptr); + } + else + { + *heightp = ptr->height; + *flagsp = ptr->flags; + if ( (retval= ptr->valuesize) != 0 ) + memcpy(value,ptr->value,retval); + } } portable_mutex_unlock(&KOMODO_KV_mutex); return(retval); @@ -730,14 +743,17 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 memcpy(ptr->key,key,keylen); HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); } - if ( ptr->value != 0 ) - free(ptr->value), ptr->value = 0; - if ( (ptr->valuesize= valuesize) != 0 ) + else if ( (ptr->flags & KOMODO_KVPROTECTED) == 0 ) { - ptr->value = (uint8_t *)calloc(1,valuesize); - memcpy(ptr->value,value,valuesize); + if ( ptr->value != 0 ) + free(ptr->value), ptr->value = 0; + if ( (ptr->valuesize= valuesize) != 0 ) + { + ptr->value = (uint8_t *)calloc(1,valuesize); + memcpy(ptr->value,value,valuesize); + } + ptr->height = kmdheight; } - ptr->height = kmdheight; portable_mutex_unlock(&KOMODO_KV_mutex); } else printf("opretlen.%d mismatch keylen.%d valuesize.%d\n",opretlen,keylen,valuesize); } diff --git a/src/komodo_structs.h b/src/komodo_structs.h index 73194ab8e..672516ee3 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -37,6 +37,10 @@ #define KOMODO_OPRETURN_WITHDRAW 'W' // assetchain #define KOMODO_OPRETURN_REDEEMED 'X' +#define KOMODO_KVPROTECTED 1 +#define KOMODO_KVBINARY 2 +#define KOMODO_KVDURATION 1440 + struct komodo_kv { UT_hash_handle hh; uint8_t *key,*value; int32_t height; uint32_t flags; uint16_t keylen,valuesize; }; struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; }; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 9c01e3b9b..3fa60b80e 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -406,7 +406,7 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); uint32_t komodo_interest_args(int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n); int32_t komodo_minerids(uint8_t *minerids,int32_t height); -int32_t komodo_kvsearch(uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); +int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); Value kvsearch(const Array& params, bool fHelp) { @@ -423,7 +423,7 @@ Value kvsearch(const Array& params, bool fHelp) if ( keylen < sizeof(key) ) { memcpy(key,params[0].get_str().c_str(),keylen); - if ( (valuesize= komodo_kvsearch(&flags,&height,value,key,keylen)) >= 0 ) + if ( (valuesize= komodo_kvsearch(chainActive.Tip()->nHeight,&flags,&height,value,key,keylen)) >= 0 ) { std::string val; char *valuestr; val.resize(valuesize); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 81f598447..141729f6a 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -475,6 +475,7 @@ Value sendtoaddress(const Array& params, bool fHelp) return wtx.GetHash().GetHex(); } +#define KOMODO_KVPROTECTED 1 #define IGUANA_MAXSCRIPTSIZE 10001 uint64_t PAX_fiatdest(uint64_t *seedp,int32_t tokomodo,char *destaddr,uint8_t pubkey37[37],char *coinaddr,int32_t height,char *base,int64_t fiatoshis); int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen); @@ -484,7 +485,7 @@ int32_t komodo_is_issuer(); int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp); int32_t komodo_isrealtime(int32_t *kmdheightp); int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base); -int32_t komodo_kvsearch(uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); +int32_t komodo_kvsearch(int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); Value paxdeposit(const Array& params, bool fHelp) { @@ -574,7 +575,7 @@ Value kvupdate(const Array& params, bool fHelp) if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) { key = (uint8_t *)params[0].get_str().c_str(); - if ( (valuesize= komodo_kvsearch(&flags,&height,&keyvalue[keylen],key,keylen)) >= 0 && (flags & 1) != 0 ) + if ( (valuesize= komodo_kvsearch(chainActive.Tip()->nHeight,&flags,&height,&keyvalue[keylen],key,keylen)) >= 0 && (flags & KOMODO_KVPROTECTED) != 0 ) { ret.push_back(Pair("error",(char *)"cant modify write once key")); return ret; @@ -604,7 +605,7 @@ Value kvupdate(const Array& params, bool fHelp) // printf("%02x",opretbuf[i]); //printf(" opretbuf keylen.%d valuesize.%d height.%d (%02x %02x %02x)\n",*(uint16_t *)&keyvalue[0],*(uint16_t *)&keyvalue[2],*(uint32_t *)&keyvalue[4],keyvalue[8],keyvalue[9],keyvalue[10]); EnsureWalletIsUnlocked(); - if ( (fee= opretlen * opretlen / keylen) < 100000 ) + if ( (fee= ((flags>>2)+1)*(opretlen * opretlen / keylen)) < 100000 ) fee = 100000; CBitcoinAddress destaddress(CRYPTO777_KMDADDR); if (!destaddress.IsValid())