From 1db43ae672bd7dad20771c071effdfc7c0af4bf0 Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Thu, 3 Jan 2019 11:55:48 +0100 Subject: [PATCH] Oracles data and format check (#8) - Sanity check of oracle data - Fix oracle format check --- src/cc/oracles.cpp | 101 ++++++++++++++++++++++++++++++++++++--- src/wallet/rpcwallet.cpp | 20 -------- 2 files changed, 94 insertions(+), 27 deletions(-) diff --git a/src/cc/oracles.cpp b/src/cc/oracles.cpp index 8bcdf3860..66c0e1b9a 100644 --- a/src/cc/oracles.cpp +++ b/src/cc/oracles.cpp @@ -416,6 +416,42 @@ int32_t oracle_format(uint256 *hashp,int64_t *valp,char *str,uint8_t fmt,uint8_t return(offset); } +int32_t oracle_parse_data_format(std::vector data,std::string format) +{ + int64_t offset=0,len=0; char fmt; + + for (int i=0; idata.size()-offset) return (0); + if (fmt=='S' || fmt=='s') + { + for (int j=offset;j127) return (0); + } + offset+=len; + } + if (offset!=data.size()) return (0); + else return (offset); +} + int64_t _correlate_price(int64_t *prices,int32_t n,int64_t price) { int32_t i,count = 0; int64_t diff,threshold = (price >> 8); @@ -729,13 +765,29 @@ int64_t LifetimeOraclesFunds(struct CCcontract_info *cp,uint256 oracletxid,CPubK std::string OracleCreate(int64_t txfee,std::string name,std::string description,std::string format) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - CPubKey mypk,Oraclespk; struct CCcontract_info *cp,C; + CPubKey mypk,Oraclespk; struct CCcontract_info *cp,C; char fmt; + cp = CCinit(&C,EVAL_ORACLES); if ( name.size() > 32 || description.size() > 4096 || format.size() > 4096 ) { - fprintf(stderr,"name.%d or description.%d is too big\n",(int32_t)name.size(),(int32_t)description.size()); + CCerror = strprintf("name.%d or description.%d is too big",(int32_t)name.size(),(int32_t)description.size()); + fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); - } + } + for(int i = 0; i < format.size(); i++) + { + fmt=format[i]; + switch (fmt) + { + case 's': case 'S': case 'd': case 'D': + case 'c': case 'C': case 't': case 'T': + case 'i': case 'I': case 'l': case 'L': + case 'h': break; + default: CCerror = strprintf("invalid format type"); + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } + } if ( txfee == 0 ) txfee = 10000; mypk = pubkey2pk(Mypubkey()); @@ -745,6 +797,8 @@ std::string OracleCreate(int64_t txfee,std::string name,std::string description, mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(Oraclespk)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeOraclesCreateOpRet('C',name,description,format))); } + CCerror = strprintf("error adding normal inputs"); + fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); } @@ -757,7 +811,8 @@ std::string OracleRegister(int64_t txfee,uint256 oracletxid,int64_t datafee) txfee = 10000; if ( datafee < txfee ) { - fprintf(stderr,"datafee must be txfee or more\n"); + CCerror = strprintf("datafee must be txfee or more"); + fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); } mypk = pubkey2pk(Mypubkey()); @@ -769,6 +824,8 @@ std::string OracleRegister(int64_t txfee,uint256 oracletxid,int64_t datafee) mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,batonpk)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeOraclesOpRet('R',oracletxid,mypk,datafee))); } + CCerror = strprintf("error adding normal inputs"); + fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); } @@ -787,13 +844,17 @@ std::string OracleSubscribe(int64_t txfee,uint256 oracletxid,CPubKey publisher,i mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeOraclesOpRet('S',oracletxid,mypk,amount))); } + CCerror = strprintf("error adding normal inputs"); + fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); } std::string OracleData(int64_t txfee,uint256 oracletxid,std::vector data) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - CScript pubKey; CPubKey mypk,batonpk; int64_t datafee,inputs,CCchange = 0; struct CCcontract_info *cp,C; uint256 batontxid; char coinaddr[64],batonaddr[64]; std::vector prevdata; + CScript pubKey; CPubKey mypk,batonpk; int64_t offset,datafee,inputs,CCchange = 0; struct CCcontract_info *cp,C; uint256 batontxid,hashBlock; + char coinaddr[64],batonaddr[64]; std::vector prevdata; CTransaction tx; std::string name,description,format; int32_t len,numvouts; + cp = CCinit(&C,EVAL_ORACLES); mypk = pubkey2pk(Mypubkey()); if ( data.size() > 8192 ) @@ -808,6 +869,30 @@ std::string OracleData(int64_t txfee,uint256 oracletxid,std::vector da fprintf(stderr,"%s\n", CCerror.c_str() ); return(""); } + if ( GetTransaction(oracletxid,tx,hashBlock,false) != 0 && (numvouts=tx.vout.size()) > 0 ) + { + if ( DecodeOraclesCreateOpRet(tx.vout[numvouts-1].scriptPubKey,name,description,format) == 'C' ) + { + if (oracle_parse_data_format(data,format)==0) + { + CCerror = strprintf("data does not match length or content format specification"); + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } + } + else + { + CCerror = strprintf("invalid oracle txid opret data"); + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } + } + else + { + CCerror = strprintf("invalid oracle txid"); + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } if ( txfee == 0 ) txfee = 10000; GetCCaddress(cp,coinaddr,mypk); @@ -831,7 +916,7 @@ std::string OracleData(int64_t txfee,uint256 oracletxid,std::vector da fprintf(stderr,"%s\n", CCerror.c_str() ); } } else { - CCerror = strprintf("couldnt add normal inputs\n"); + CCerror = strprintf("couldnt add normal inputs"); fprintf(stderr,"%s\n", CCerror.c_str() ); } return(""); @@ -855,7 +940,9 @@ UniValue OracleFormat(uint8_t *data,int32_t datalen,char *format,int32_t formatl UniValue OracleDataSamples(uint256 reforacletxid,uint256 batontxid,int32_t num) { - UniValue result(UniValue::VOBJ),a(UniValue::VARR); CTransaction tx,oracletx; uint256 hashBlock,btxid,oracletxid; CPubKey pk; std::string name,description,format; int32_t numvouts,n=0; std::vector data; char *formatstr = 0; + UniValue result(UniValue::VOBJ),a(UniValue::VARR); CTransaction tx,oracletx; uint256 hashBlock,btxid,oracletxid; + CPubKey pk; std::string name,description,format; int32_t numvouts,n=0; std::vector data; char *formatstr = 0; + result.push_back(Pair("result","success")); if ( GetTransaction(reforacletxid,oracletx,hashBlock,false) != 0 && (numvouts=oracletx.vout.size()) > 0 ) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c3faff238..f9a6a6a5e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6089,9 +6089,7 @@ UniValue oraclesdata(const UniValue& params, bool fHelp) txid = Parseuint256((char *)params[0].get_str().c_str()); data = ParseHex(params[1].get_str().c_str()); hex = OracleData(0,txid,data); - RETURN_IF_ERROR(CCerror); - if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -6127,24 +6125,6 @@ UniValue oraclescreate(const UniValue& params, bool fHelp) ERR_RESULT("oracles format must be <= 4096 characters"); return(result); } - // list of oracle valid formats from oracles.cpp -> oracle_format - const UniValue valid_formats[13] = {"s","S","d","D","c","C","t","T","i","I","l","L","h"}; - const UniValue header_type = "Ihh"; - // checking if oracle data type is valid - bool is_valid_format = false; - for ( int i = 0; i < 13; ++i ) { - if ( valid_formats[i].get_str() == format ) { - is_valid_format = true; - } - } - // additional check for special Ihh data type - if ( format == header_type.get_str() ) { - is_valid_format = true; - } - if ( !is_valid_format ) { - ERR_RESULT("oracles format not valid"); - return(result); - } hex = OracleCreate(0,name,description,format); RETURN_IF_ERROR(CCerror); if ( hex.size() > 0 )