From 2e8573c0ec888a33a90abfd2a2cc72d311fde2d8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 19:59:13 -1100 Subject: [PATCH 001/115] Fix rogue compile --- src/cc/gamescc.cpp | 13 +++---------- src/cc/rogue_rpc.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 3e12a7010..4e8660c18 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -1510,15 +1510,8 @@ UniValue games_finish(uint64_t txfee,struct CCcontract_info *cp,cJSON *params,ch result.push_back(Pair("method",method)); result.push_back(Pair("mygamesaddr",mygamesaddr)); if ( strcmp(method,"bailout") == 0 ) - { funcid = 'Q'; - //mult = 10; //100000; - } - else - { - funcid = 'H'; - //mult = 20; //200000; - } + else funcid = 'H'; if ( params != 0 && (n= cJSON_GetArraySize(params)) > 0 ) { if ( n > 0 ) @@ -1570,13 +1563,13 @@ UniValue games_finish(uint64_t txfee,struct CCcontract_info *cp,cJSON *params,ch fprintf(stderr,"\ncashout %.8f extracted %s\n",(double)cashout/COIN,str); if ( funcid == 'H' && maxplayers > 1 ) { - if ( P.amulet == 0 ) + /*if ( P.amulet == 0 ) { if ( numplayers != maxplayers ) return(cclib_error(result,"numplayers != maxplayers")); else if ( games_playersalive(tmp,tmp,gametxid,maxplayers,gameheight,gametx) > 1 ) return(cclib_error(result,"highlander must be a winner or last one standing")); - } + }*/ cashout += games_buyins(gametxid,maxplayers);//numplayers * buyin; } if ( cashout > 0 ) diff --git a/src/cc/rogue_rpc.cpp b/src/cc/rogue_rpc.cpp index 4b74e21b7..52323adbe 100644 --- a/src/cc/rogue_rpc.cpp +++ b/src/cc/rogue_rpc.cpp @@ -283,7 +283,7 @@ int32_t rogue_iamregistered(int32_t maxplayers,uint256 gametxid,CTransaction tx, return(0); } -int64_t rogue_buyins(uint256 gametxid) +int64_t rogue_buyins(uint256 gametxid,int32_t maxplayers) { int32_t i,vout; uint256 spenttxid,hashBlock; CTransaction spenttx; int64_t buyins = 0; for (i=0; iamulet != 0 ) mult *= 5; dungeonlevel = P->dungeonlevel; @@ -1618,7 +1618,7 @@ bool rogue_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const C if ( funcid == 'H' ) { cashout *= 2; - cashout += rogue_buyins(gametxid); + cashout += rogue_buyins(gametxid,maxplayers); } sprintf(cashstr,"tokentx.(%c) decoded.%d ht.%d txid.%s %.8f vs vout2 %.8f",tokentx,decoded,height,txid.GetHex().c_str(),(double)cashout/COIN,(double)tx.vout[2].nValue/COIN); if ( strcmp(laststr,cashstr) != 0 ) From 58215af9b0d138c1a0b87351958ec22541ae8ee4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:48:44 -1100 Subject: [PATCH 002/115] Make 4 cc libs --- src/cc/makecclib | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cc/makecclib b/src/cc/makecclib index adac757c3..3aded32e3 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1,2 +1,8 @@ #!/bin/sh gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../libcc.so cclib.cpp + +gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp + +gcc -O3 -DBUILD_ROGUE -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o roguecc.so cclib.cpp + +gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp From 906cc5ccb4cd5e4aa9a059af87a78515ae5c683c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:49:59 -1100 Subject: [PATCH 003/115] -print --- src/cc/gamescc.cpp | 2 +- src/cc/tetris.c | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 4e8660c18..38396562e 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -546,7 +546,7 @@ void komodo_netevent(std::vector message) { if ( (rand() % 10) == 0 ) { - fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); + //fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); komodo_sendmessage(2,2,"events",message); } } diff --git a/src/cc/tetris.c b/src/cc/tetris.c index cb89c0da0..651b1018c 100644 --- a/src/cc/tetris.c +++ b/src/cc/tetris.c @@ -801,32 +801,35 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve if ( fp == 0 ) fp = fopen("events.log","wb"); rs->buffered[rs->num++] = c; - if ( sizeof(c) == 1 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",c&0xff,gametxidstr,eventid); - else if ( sizeof(c) == 2 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",c&0xffff,gametxidstr,eventid); - else if ( sizeof(c) == 4 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",c&0xffffffff,gametxidstr,eventid); - else if ( sizeof(c) == 8 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%016llx%%22,%%22%s%%22,%u]\"]",(long long)c,gametxidstr,eventid); - if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) + if ( 0 ) { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) + if ( sizeof(c) == 1 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",c&0xff,gametxidstr,eventid); + else if ( sizeof(c) == 2 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",c&0xffff,gametxidstr,eventid); + else if ( sizeof(c) == 4 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",c&0xffffffff,gametxidstr,eventid); + else if ( sizeof(c) == 8 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%016llx%%22,%%22%s%%22,%u]\"]",(long long)c,gametxidstr,eventid); + if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) { - if ( (resobj= jobj(retjson,(char *)"result")) != 0 ) + if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - retval = 0; - if ( fp != 0 ) + if ( (resobj= jobj(retjson,(char *)"result")) != 0 ) { - fprintf(fp,"%s\n",jprint(resobj,0)); - fflush(fp); + retval = 0; + if ( fp != 0 ) + { + fprintf(fp,"%s\n",jprint(resobj,0)); + fflush(fp); + } } - } - free_json(retjson); - } else fprintf(fp,"error parsing %s\n",retstr); - free(retstr); - } else fprintf(fp,"error issuing method %s\n",params); - return(retval); + free_json(retjson); + } else fprintf(fp,"error parsing %s\n",retstr); + free(retstr); + } else fprintf(fp,"error issuing method %s\n",params); + return(retval); + } else return(0); } int tetris(int argc, char **argv) From 3da4f521dc060c6a0275b4c9ff49f9aeeb74f5e8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:50:51 -1100 Subject: [PATCH 004/115] Add apps to makecclib --- src/cc/makecclib | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/makecclib b/src/cc/makecclib index 3aded32e3..d9a5f2a82 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -6,3 +6,6 @@ gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include gcc -O3 -DBUILD_ROGUE -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o roguecc.so cclib.cpp gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp + +./maketetris +make -f Makefile_rogue From 1e6630620c9db3ae2cd34cbf5fc6a721cba9b499 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:52:41 -1100 Subject: [PATCH 005/115] Fix --- src/cc/makecclib | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index d9a5f2a82..8db8514cd 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -8,4 +8,5 @@ gcc -O3 -DBUILD_ROGUE -std=c++11 -I../secp256k1/include -I../univalue/include -I gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp ./maketetris -make -f Makefile_rogue + +cd rogue; make; cd .. From 8a208a02c794571f8a65829c3c718a2626345b8b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:54:08 -1100 Subject: [PATCH 006/115] Make rogue --- src/cc/makecclib | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index 8db8514cd..e64934d31 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -8,5 +8,4 @@ gcc -O3 -DBUILD_ROGUE -std=c++11 -I../secp256k1/include -I../univalue/include -I gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp ./maketetris - -cd rogue; make; cd .. +./makerogue From 119decca6f464a9b4ecc22b8188593e4242338a9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 21:56:35 -1100 Subject: [PATCH 007/115] Fix --- src/cc/makecclib | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index e64934d31..6a1908829 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1,11 +1,12 @@ #!/bin/sh +rm *.so rogue/rogue tetris + gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../libcc.so cclib.cpp gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp - -gcc -O3 -DBUILD_ROGUE -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o roguecc.so cclib.cpp +./maketetris gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp -./maketetris +make -f Makefile_rogue ./makerogue From 329b7437640fcb17908a861525c2bb075cca5693 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 22:02:53 -1100 Subject: [PATCH 008/115] +prints --- src/cc/makecclib | 9 +++++++-- src/cc/rogue_rpc.cpp | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index 6a1908829..35b4a9d8d 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1,12 +1,17 @@ #!/bin/sh rm *.so rogue/rogue tetris +echo rogue +make -f Makefile_rogue +./makerogue + +echo sudoku/musig/dilithium gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../libcc.so cclib.cpp +echo games tetris gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp ./maketetris +echo customcc stub gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp -make -f Makefile_rogue -./makerogue diff --git a/src/cc/rogue_rpc.cpp b/src/cc/rogue_rpc.cpp index 52323adbe..5040c34d3 100644 --- a/src/cc/rogue_rpc.cpp +++ b/src/cc/rogue_rpc.cpp @@ -1341,12 +1341,12 @@ UniValue rogue_finishgame(uint64_t txfee,struct CCcontract_info *cp,cJSON *param UniValue rogue_bailout(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - return(rogue_finishgame(txfee,cp,params,"bailout")); + return(rogue_finishgame(txfee,cp,params,(char *)"bailout")); } UniValue rogue_highlander(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - return(rogue_finishgame(txfee,cp,params,"highlander")); + return(rogue_finishgame(txfee,cp,params,(char *)"highlander")); } UniValue rogue_gameinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) From 3640e3c4fcb67981ca8c440fb72a9dc2ecc5608a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 22:28:18 -1100 Subject: [PATCH 009/115] Prices example --- src/cc/gamescc.cpp | 13 +- src/cc/makecclib | 4 + src/cc/prices.c | 896 +++++++++++++++++++++++++++++++++++++++++++++ src/cc/prices.cpp | 463 +++-------------------- src/cc/prices.h | 208 +++++++++++ 5 files changed, 1159 insertions(+), 425 deletions(-) create mode 100644 src/cc/prices.c create mode 100644 src/cc/prices.h diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 38396562e..f010ac282 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -14,7 +14,11 @@ ******************************************************************************/ #include "gamescc.h" -#include "tetris.c" // replace with game code +#ifdef BUILD_PRICES +#include "prices.c" +#elif +#include "tetris.c" +#endif int32_t GAMEDATA(struct games_player *P,void *ptr); @@ -161,8 +165,11 @@ int32_t games_replay2(uint8_t *newdata,uint64_t seed,gamesevent *keystrokes,int3 } #ifndef STANDALONE - -#include "tetris.cpp" // replace with game specific functions +#ifdef BUILD_PRICES +#include "prices.cpp" +#elif +#include "tetris.cpp" +#endif void GAMEJSON(UniValue &obj,struct games_player *P); diff --git a/src/cc/makecclib b/src/cc/makecclib index 35b4a9d8d..38634aa78 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -12,6 +12,10 @@ echo games tetris gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp ./maketetris +echo games prices +gcc -O3 -DBUILD_GAMESCC -DBUILD_PRICES -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o pricescc.so cclib.cpp +./makeprices + echo customcc stub gcc -O3 -DBUILD_CUSTOMCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o customcc.so cclib.cpp diff --git a/src/cc/prices.c b/src/cc/prices.c new file mode 100644 index 000000000..651b1018c --- /dev/null +++ b/src/cc/prices.c @@ -0,0 +1,896 @@ + +#include "tetris.h" + +/* + In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. + + also, the standalone game needs to support argv of seed gametxid, along with replay args + */ + +int random_tetromino(struct games_state *rs) +{ + rs->seed = _games_rngnext(rs->seed); + return(rs->seed % NUM_TETROMINOS); +} + +int32_t tetrisdata(struct games_player *P,void *ptr) +{ + tetris_game *tg = (tetris_game *)ptr; + P->gold = tg->points; + P->dungeonlevel = tg->level; + //fprintf(stderr,"score.%d level.%d\n",tg->points,tg->level); + return(0); +} + +/***************************************************************************/ +/** https://github.com/brenns10/tetris + @file main.c + @author Stephen Brennan + @date Created Wednesday, 10 June 2015 + @brief Main program for tetris. + @copyright Copyright (c) 2015, Stephen Brennan. Released under the Revised + BSD License. See LICENSE.txt for details. + *******************************************************************************/ + + +#include // for FILE +#include // for bool +#include +#include +#include +#include +#include +#include + +#ifdef BUILD_GAMESCC +#include "rogue/cursesd.h" +#else +#include +#endif + + +#define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) +#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) + +/******************************************************************************* + Array Definitions + *******************************************************************************/ + +const tetris_location TETROMINOS[NUM_TETROMINOS][NUM_ORIENTATIONS][TETRIS] = +{ + // I + {{{1, 0}, {1, 1}, {1, 2}, {1, 3}}, + {{0, 2}, {1, 2}, {2, 2}, {3, 2}}, + {{3, 0}, {3, 1}, {3, 2}, {3, 3}}, + {{0, 1}, {1, 1}, {2, 1}, {3, 1}}}, + // J + {{{0, 0}, {1, 0}, {1, 1}, {1, 2}}, + {{0, 1}, {0, 2}, {1, 1}, {2, 1}}, + {{1, 0}, {1, 1}, {1, 2}, {2, 2}}, + {{0, 1}, {1, 1}, {2, 0}, {2, 1}}}, + // L + {{{0, 2}, {1, 0}, {1, 1}, {1, 2}}, + {{0, 1}, {1, 1}, {2, 1}, {2, 2}}, + {{1, 0}, {1, 1}, {1, 2}, {2, 0}}, + {{0, 0}, {0, 1}, {1, 1}, {2, 1}}}, + // O + {{{0, 1}, {0, 2}, {1, 1}, {1, 2}}, + {{0, 1}, {0, 2}, {1, 1}, {1, 2}}, + {{0, 1}, {0, 2}, {1, 1}, {1, 2}}, + {{0, 1}, {0, 2}, {1, 1}, {1, 2}}}, + // S + {{{0, 1}, {0, 2}, {1, 0}, {1, 1}}, + {{0, 1}, {1, 1}, {1, 2}, {2, 2}}, + {{1, 1}, {1, 2}, {2, 0}, {2, 1}}, + {{0, 0}, {1, 0}, {1, 1}, {2, 1}}}, + // T + {{{0, 1}, {1, 0}, {1, 1}, {1, 2}}, + {{0, 1}, {1, 1}, {1, 2}, {2, 1}}, + {{1, 0}, {1, 1}, {1, 2}, {2, 1}}, + {{0, 1}, {1, 0}, {1, 1}, {2, 1}}}, + // Z + {{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, + {{0, 2}, {1, 1}, {1, 2}, {2, 1}}, + {{1, 0}, {1, 1}, {2, 1}, {2, 2}}, + {{0, 1}, {1, 0}, {1, 1}, {2, 0}}}, +}; + +const int GRAVITY_LEVEL[MAX_LEVEL+1] = { + // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, + //10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 30, 28, 26, 24, 22, 20, 16, 12, 8, 4 +}; + +/******************************************************************************* + Helper Functions for Blocks + *******************************************************************************/ + +void sleep_milli(int milliseconds) +{ + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = milliseconds * 1000 * 1000; + nanosleep(&ts, NULL); +} + +/* + Return the block at the given row and column. + */ +char tg_get(tetris_game *obj, int row, int column) +{ + return obj->board[obj->cols * row + column]; +} + +/* + Set the block at the given row and column. + */ +static void tg_set(tetris_game *obj, int row, int column, char value) +{ + obj->board[obj->cols * row + column] = value; +} + +/* + Check whether a row and column are in bounds. + */ +bool tg_check(tetris_game *obj, int row, int col) +{ + return 0 <= row && row < obj->rows && 0 <= col && col < obj->cols; +} + +/* + Place a block onto the board. + */ +static void tg_put(tetris_game *obj, tetris_block block) +{ + int i; + for (i = 0; i < TETRIS; i++) { + tetris_location cell = TETROMINOS[block.typ][block.ori][i]; + tg_set(obj, block.loc.row + cell.row, block.loc.col + cell.col, + TYPE_TO_CELL(block.typ)); + } +} + +/* + Clear a block out of the board. + */ +static void tg_remove(tetris_game *obj, tetris_block block) +{ + int i; + for (i = 0; i < TETRIS; i++) { + tetris_location cell = TETROMINOS[block.typ][block.ori][i]; + tg_set(obj, block.loc.row + cell.row, block.loc.col + cell.col, TC_EMPTY); + } +} + +/* + Check if a block can be placed on the board. + */ +static bool tg_fits(tetris_game *obj, tetris_block block) +{ + int i, r, c; + for (i = 0; i < TETRIS; i++) { + tetris_location cell = TETROMINOS[block.typ][block.ori][i]; + r = block.loc.row + cell.row; + c = block.loc.col + cell.col; + if (!tg_check(obj, r, c) || TC_IS_FILLED(tg_get(obj, r, c))) { + return false; + } + } + return true; +} + +/* + Create a new falling block and populate the next falling block with a random + one. + */ +static void tg_new_falling(struct games_state *rs,tetris_game *obj) +{ + // Put in a new falling tetromino. + obj->falling = obj->next; + obj->next.typ = random_tetromino(rs); + obj->next.ori = 0; + obj->next.loc.row = 0; + obj->next.loc.col = obj->cols/2 - 2; +} + +/******************************************************************************* + Game Turn Helpers + *******************************************************************************/ + +/* + Tick gravity, and move the block down if gravity should act. + */ +static void tg_do_gravity_tick(struct games_state *rs,tetris_game *obj) +{ + obj->ticks_till_gravity--; + if (obj->ticks_till_gravity <= 0) { + tg_remove(obj, obj->falling); + obj->falling.loc.row++; + if (tg_fits(obj, obj->falling)) { + obj->ticks_till_gravity = GRAVITY_LEVEL[obj->level]; + } else { + obj->falling.loc.row--; + tg_put(obj, obj->falling); + + tg_new_falling(rs,obj); + } + tg_put(obj, obj->falling); + } +} + +/* + Move the falling tetris block left (-1) or right (+1). + */ +static void tg_move(tetris_game *obj, int direction) +{ + tg_remove(obj, obj->falling); + obj->falling.loc.col += direction; + if (!tg_fits(obj, obj->falling)) { + obj->falling.loc.col -= direction; + } + tg_put(obj, obj->falling); +} + +/* + Send the falling tetris block to the bottom. + */ +static void tg_down(struct games_state *rs,tetris_game *obj) +{ + tg_remove(obj, obj->falling); + while (tg_fits(obj, obj->falling)) { + obj->falling.loc.row++; + } + obj->falling.loc.row--; + tg_put(obj, obj->falling); + tg_new_falling(rs,obj); +} + +/* + Rotate the falling block in either direction (+/-1). + */ +static void tg_rotate(tetris_game *obj, int direction) +{ + tg_remove(obj, obj->falling); + + while (true) { + obj->falling.ori = (obj->falling.ori + direction) % NUM_ORIENTATIONS; + + // If the new orientation fits, we're done. + if (tg_fits(obj, obj->falling)) + break; + + // Otherwise, try moving left to make it fit. + obj->falling.loc.col--; + if (tg_fits(obj, obj->falling)) + break; + + // Finally, try moving right to make it fit. + obj->falling.loc.col += 2; + if (tg_fits(obj, obj->falling)) + break; + + // Put it back in its original location and try the next orientation. + obj->falling.loc.col--; + // Worst case, we come back to the original orientation and it fits, so this + // loop will terminate. + } + + tg_put(obj, obj->falling); +} + +/* + Swap the falling block with the block in the hold buffer. + */ +static void tg_hold(struct games_state *rs,tetris_game *obj) +{ + tg_remove(obj, obj->falling); + if (obj->stored.typ == -1) { + obj->stored = obj->falling; + tg_new_falling(rs,obj); + } else { + int typ = obj->falling.typ, ori = obj->falling.ori; + obj->falling.typ = obj->stored.typ; + obj->falling.ori = obj->stored.ori; + obj->stored.typ = typ; + obj->stored.ori = ori; + while (!tg_fits(obj, obj->falling)) { + obj->falling.loc.row--; + } + } + tg_put(obj, obj->falling); +} + +/* + Perform the action specified by the move. + */ +static void tg_handle_move(struct games_state *rs,tetris_game *obj, tetris_move move) +{ + switch (move) { + case TM_LEFT: + //fprintf(stderr,"LEFT "); + tg_move(obj, -1); + break; + case TM_RIGHT: + //fprintf(stderr,"RIGHT "); + tg_move(obj, 1); + break; + case TM_DROP: + tg_down(rs,obj); + break; + case TM_CLOCK: + tg_rotate(obj, 1); + break; + case TM_COUNTER: + tg_rotate(obj, -1); + break; + case TM_HOLD: + tg_hold(rs,obj); + break; + default: + // pass + break; + } +} + +/* + Return true if line i is full. + */ +static bool tg_line_full(tetris_game *obj, int i) +{ + int j; + for (j = 0; j < obj->cols; j++) { + if (TC_IS_EMPTY(tg_get(obj, i, j))) + return false; + } + return true; +} + +/* + Shift every row above r down one. + */ +static void tg_shift_lines(tetris_game *obj, int r) +{ + int i, j; + for (i = r-1; i >= 0; i--) { + for (j = 0; j < obj->cols; j++) { + tg_set(obj, i+1, j, tg_get(obj, i, j)); + tg_set(obj, i, j, TC_EMPTY); + } + } +} + +/* + Find rows that are filled, remove them, shift, and return the number of + cleared rows. + */ +static int tg_check_lines(tetris_game *obj) +{ + int i, nlines = 0; + tg_remove(obj, obj->falling); // don't want to mess up falling block + + for (i = obj->rows-1; i >= 0; i--) { + if (tg_line_full(obj, i)) { + tg_shift_lines(obj, i); + i++; // do this line over again since they're shifted + nlines++; + } + } + + tg_put(obj, obj->falling); // replace + return nlines; +} + +/* + Adjust the score for the game, given how many lines were just cleared. + */ +static void tg_adjust_score(tetris_game *obj, int lines_cleared) +{ + static int line_multiplier[] = {0, 40, 100, 300, 1200}; + obj->points += line_multiplier[lines_cleared] * (obj->level + 1); + if (lines_cleared >= obj->lines_remaining) { + obj->level = MIN(MAX_LEVEL, obj->level + 1); + lines_cleared -= obj->lines_remaining; + obj->lines_remaining = LINES_PER_LEVEL - lines_cleared; + } else { + obj->lines_remaining -= lines_cleared; + } +} + +/* + Return true if the game is over. + */ +static bool tg_game_over(tetris_game *obj) +{ + int i, j; + bool over = false; + tg_remove(obj, obj->falling); + for (i = 0; i < 2; i++) { + for (j = 0; j < obj->cols; j++) { + if (TC_IS_FILLED(tg_get(obj, i, j))) { + over = true; + } + } + } + tg_put(obj, obj->falling); + return over; +} + +/******************************************************************************* + Main Public Functions + *******************************************************************************/ + +/* + Do a single game tick: process gravity, user input, and score. Return true if + the game is still running, false if it is over. + */ +bool tg_tick(struct games_state *rs,tetris_game *obj, tetris_move move) +{ + int lines_cleared; + // Handle gravity. + tg_do_gravity_tick(rs,obj); + + // Handle input. + tg_handle_move(rs,obj, move); + + // Check for cleared lines + lines_cleared = tg_check_lines(obj); + + tg_adjust_score(obj, lines_cleared); + + // Return whether the game will continue (NOT whether it's over) + return !tg_game_over(obj); +} + +void tg_init(struct games_state *rs,tetris_game *obj, int rows, int cols) +{ + // Initialization logic + obj->rows = rows; + obj->cols = cols; + //obj->board = (char *)malloc(rows * cols); + memset(obj->board, TC_EMPTY, rows * cols); + obj->points = 0; + obj->level = 0; + obj->ticks_till_gravity = GRAVITY_LEVEL[obj->level]; + obj->lines_remaining = LINES_PER_LEVEL; + //srand(time(NULL)); + tg_new_falling(rs,obj); + tg_new_falling(rs,obj); + obj->stored.typ = -1; + obj->stored.ori = 0; + obj->stored.loc.row = 0; + obj->next.loc.col = obj->cols/2 - 2; + //printf("%d", obj->falling.loc.col); +} + +tetris_game *tg_create(struct games_state *rs,int rows, int cols) +{ + tetris_game *obj = (tetris_game *)malloc(sizeof(tetris_game) + rows*cols); + tg_init(rs,obj, rows, cols); + return obj; +} + +/*void tg_destroy(tetris_game *obj) +{ + // Cleanup logic + free(obj->board); +}*/ + +void tg_delete(tetris_game *obj) { + //tg_destroy(obj); + free(obj); +} + +/* + Load a game from a file. + +tetris_game *tg_load(FILE *f) +{ + tetris_game *obj = (tetris_game *)malloc(sizeof(tetris_game)); + if (fread(obj, sizeof(tetris_game), 1, f) != 1 ) + { + fprintf(stderr,"read game error\n"); + free(obj); + obj = 0; + } + else + { + obj->board = (char *)malloc(obj->rows * obj->cols); + if (fread(obj->board, sizeof(char), obj->rows * obj->cols, f) != obj->rows * obj->cols ) + { + fprintf(stderr,"fread error\n"); + free(obj->board); + free(obj); + obj = 0; + } + } + return obj; +}*/ + +/* + Save a game to a file. + +void tg_save(tetris_game *obj, FILE *f) +{ + if (fwrite(obj, sizeof(tetris_game), 1, f) != 1 ) + fprintf(stderr,"error writing tetrisgame\n"); + else if (fwrite(obj->board, sizeof(char), obj->rows * obj->cols, f) != obj->rows * obj->cols ) + fprintf(stderr,"error writing board\n"); +}*/ + +/* + Print a game board to a file. Really just for early debugging. + */ +void tg_print(tetris_game *obj, FILE *f) { + int i, j; + for (i = 0; i < obj->rows; i++) { + for (j = 0; j < obj->cols; j++) { + if (TC_IS_EMPTY(tg_get(obj, i, j))) { + fputs(TC_EMPTY_STR, f); + } else { + fputs(TC_BLOCK_STR, f); + } + } + fputc('\n', f); + } +} + +/* + 2 columns per cell makes the game much nicer. + */ +#define COLS_PER_CELL 2 +/* + Macro to print a cell of a specific type to a window. + */ +#define ADD_BLOCK(w,x) waddch((w),' '|A_REVERSE|COLOR_PAIR(x)); \ +waddch((w),' '|A_REVERSE|COLOR_PAIR(x)) +#define ADD_EMPTY(w) waddch((w), ' '); waddch((w), ' ') + +/* + Print the tetris board onto the ncurses window. + */ +void display_board(WINDOW *w, tetris_game *obj) +{ + int i, j; + box(w, 0, 0); + for (i = 0; i < obj->rows; i++) { + wmove(w, 1 + i, 1); + for (j = 0; j < obj->cols; j++) { + if (TC_IS_FILLED(tg_get(obj, i, j))) { + ADD_BLOCK(w,tg_get(obj, i, j)); + } else { + ADD_EMPTY(w); + } + } + } + wnoutrefresh(w); +} + +/* + Display a tetris piece in a dedicated window. + */ +void display_piece(WINDOW *w, tetris_block block) +{ + int b; + tetris_location c; + wclear(w); + box(w, 0, 0); + if (block.typ == -1) { + wnoutrefresh(w); + return; + } + for (b = 0; b < TETRIS; b++) { + c = TETROMINOS[block.typ][block.ori][b]; + wmove(w, c.row + 1, c.col * COLS_PER_CELL + 1); + ADD_BLOCK(w, TYPE_TO_CELL(block.typ)); + } + wnoutrefresh(w); +} + +/* + Display score information in a dedicated window. + */ +void display_score(WINDOW *w, tetris_game *tg) +{ + wclear(w); + box(w, 0, 0); + wprintw(w, (char *)"Score\n%d\n", tg->points); + wprintw(w, (char *)"Level\n%d\n", tg->level); + wprintw(w, (char *)"Lines\n%d\n", tg->lines_remaining); + wnoutrefresh(w); +} + +/* + Save and exit the game. + +void save(tetris_game *game, WINDOW *w) +{ + FILE *f; + + wclear(w); + box(w, 0, 0); // return the border + wmove(w, 1, 1); + wprintw(w, (char *)"Save and exit? [Y/n] "); + wrefresh(w); + timeout(-1); + if (getch() == 'n') { + timeout(0); + return; + } + f = fopen("tetris.save", "w"); + tg_save(game, f); + fclose(f); + tg_delete(game); + endwin(); + fprintf(stderr,"Game saved to \"tetris.save\".\n"); + fprintf(stderr,"Resume by passing the filename as an argument to this program.\n"); + exit(EXIT_SUCCESS); +}*/ + +/* + Do the NCURSES initialization steps for color blocks. + */ +void init_colors(void) +{ + start_color(); + //init_color(COLOR_ORANGE, 1000, 647, 0); + init_pair(TC_CELLI, COLOR_CYAN, COLOR_BLACK); + init_pair(TC_CELLJ, COLOR_BLUE, COLOR_BLACK); + init_pair(TC_CELLL, COLOR_WHITE, COLOR_BLACK); + init_pair(TC_CELLO, COLOR_YELLOW, COLOR_BLACK); + init_pair(TC_CELLS, COLOR_GREEN, COLOR_BLACK); + init_pair(TC_CELLT, COLOR_MAGENTA, COLOR_BLACK); + init_pair(TC_CELLZ, COLOR_RED, COLOR_BLACK); +} + +struct games_state globalR; +extern char Gametxidstr[]; +int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c); +gamesevent games_readevent(struct games_state *rs); + +void *gamesiterate(struct games_state *rs) +{ + uint32_t counter = 0; bool running = true; tetris_move move = TM_NONE; + gamesevent c; uint16_t skipcount=0; int32_t prevlevel; uint32_t eventid = 0; tetris_game *tg; + WINDOW *board, *next, *hold, *score; + if ( rs->guiflag != 0 || rs->sleeptime != 0 ) + { + // NCURSES initialization: + initscr(); // initialize curses + cbreak(); // pass key presses to program, but not signals + noecho(); // don't echo key presses to screen + keypad(stdscr, TRUE); // allow arrow keys + timeout(0); // no blocking on getch() + curs_set(0); // set the cursor to invisible + init_colors(); // setup tetris colors + } + tg = tg_create(rs,22, 10); + prevlevel = tg->level; + // Create windows for each section of the interface. + board = newwin(tg->rows + 2, 2 * tg->cols + 2, 0, 0); + next = newwin(6, 10, 0, 2 * (tg->cols + 1) + 1); + hold = newwin(6, 10, 7, 2 * (tg->cols + 1) + 1); + score = newwin(6, 10, 14, 2 * (tg->cols + 1 ) + 1); + while ( running != 0 ) + { + running = tg_tick(rs,tg,move); + if ( 1 && (rs->guiflag != 0 || rs->sleeptime != 0) ) + { + display_board(board,tg); + display_piece(next,tg->next); + display_piece(hold,tg->stored); + display_score(score,tg); + } + if ( rs->guiflag != 0 ) + { +#ifdef STANDALONE + sleep_milli(15); + if ( (counter++ % 10) == 0 ) + doupdate(); + c = games_readevent(rs); + if ( c <= 0x7f || skipcount == 0x3fff ) + { + if ( skipcount > 0 ) + issue_games_events(rs,Gametxidstr,eventid-skipcount,skipcount | 0x4000); + if ( c <= 0x7f ) + issue_games_events(rs,Gametxidstr,eventid,c); + if ( tg->level != prevlevel ) + { + flushkeystrokes(rs,0); + prevlevel = tg->level; + } + skipcount = 0; + } else skipcount++; +#endif + } + else + { + if ( rs->replaydone != 0 ) + break; + if ( rs->sleeptime != 0 ) + { + sleep_milli(1); + if ( (counter++ % 20) == 0 ) + doupdate(); + } + if ( skipcount == 0 ) + { + c = games_readevent(rs); + //fprintf(stderr,"%04x score.%d level.%d\n",c,tg->points,tg->level); + if ( (c & 0x4000) == 0x4000 ) + { + skipcount = (c & 0x3fff); + c = 'S'; + } + } + if ( skipcount > 0 ) + skipcount--; + } + eventid++; + switch ( c ) + { + case 'h': + move = TM_LEFT; + break; + case 'l': + move = TM_RIGHT; + break; + case 'k': + move = TM_CLOCK; + break; + case 'j': + move = TM_DROP; + break; + case 'q': + running = false; + move = TM_NONE; + break; + /*case 'p': + wclear(board); + box(board, 0, 0); + wmove(board, tg->rows/2, (tg->cols*COLS_PER_CELL-6)/2); + wprintw(board, "PAUSED"); + wrefresh(board); + timeout(-1); + getch(); + timeout(0); + move = TM_NONE; + break; + case 's': + save(tg, board); + move = TM_NONE; + break;*/ + case ' ': + move = TM_HOLD; + break; + default: + move = TM_NONE; + } + } + return(tg); +} + +#ifdef STANDALONE +/* + Main tetris game! + */ +#include "dapps/dappstd.c" + + +char *clonestr(char *str) +{ + char *clone; int32_t len; + if ( str == 0 || str[0] == 0 ) + { + printf("warning cloning nullstr.%p\n",str); +#ifdef __APPLE__ + while ( 1 ) sleep(1); +#endif + str = (char *)""; + } + len = strlen(str); + clone = (char *)calloc(1,len+16); + strcpy(clone,str); + return(clone); +} + +int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c) +{ + static FILE *fp; + char params[512],*retstr; cJSON *retjson,*resobj; int32_t retval = -1; + if ( fp == 0 ) + fp = fopen("events.log","wb"); + rs->buffered[rs->num++] = c; + if ( 0 ) + { + if ( sizeof(c) == 1 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",c&0xff,gametxidstr,eventid); + else if ( sizeof(c) == 2 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",c&0xffff,gametxidstr,eventid); + else if ( sizeof(c) == 4 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",c&0xffffffff,gametxidstr,eventid); + else if ( sizeof(c) == 8 ) + sprintf(params,"[\"events\",\"17\",\"[%%22%016llx%%22,%%22%s%%22,%u]\"]",(long long)c,gametxidstr,eventid); + if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (resobj= jobj(retjson,(char *)"result")) != 0 ) + { + retval = 0; + if ( fp != 0 ) + { + fprintf(fp,"%s\n",jprint(resobj,0)); + fflush(fp); + } + } + free_json(retjson); + } else fprintf(fp,"error parsing %s\n",retstr); + free(retstr); + } else fprintf(fp,"error issuing method %s\n",params); + return(retval); + } else return(0); +} + +int tetris(int argc, char **argv) +{ + struct games_state *rs = &globalR; + int32_t c,skipcount=0; uint32_t eventid = 0; tetris_game *tg = 0; + memset(rs,0,sizeof(*rs)); + rs->guiflag = 1; + rs->sleeptime = 1; // non-zero to allow refresh() + if ( argc >= 2 && strlen(argv[2]) == 64 ) + { +#ifdef _WIN32 +#ifdef _MSC_VER + rs->origseed = _strtoui64(argv[1], NULL, 10); +#else + rs->origseed = atol(argv[1]); // windows, but not MSVC +#endif // _MSC_VER +#else + rs->origseed = atol(argv[1]); // non-windows +#endif // _WIN32 + rs->seed = rs->origseed; + if ( argc >= 3 ) + { + strcpy(Gametxidstr,argv[2]); + fprintf(stderr,"setplayerdata %s\n",Gametxidstr); + if ( games_setplayerdata(rs,Gametxidstr) < 0 ) + { + fprintf(stderr,"invalid gametxid, or already started\n"); + return(-1); + } + } + } else rs->seed = 777; + + /* Load file if given a filename. + if (argc >= 2) { + FILE *f = fopen(argv[1], "r"); + if (f == NULL) { + perror("tetris"); + exit(EXIT_FAILURE); + } + tg = tg_load(f); + fclose(f); + } else { + // Otherwise create new game. + tg = tg_create(rs,22, 10); + }*/ + + // Game loop + tg = (tetris_game *)gamesiterate(rs); + gamesbailout(rs); + // Deinitialize NCurses + wclear(stdscr); + endwin(); + // Output ending message. + printf("Game over!\n"); + printf("You finished with %d points on level %d.\n", tg->points, tg->level); + + // Deinitialize Tetris + tg_delete(tg); + return 0; +} + +#endif + diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index c53207562..8a52dcb37 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -1,3 +1,4 @@ + /****************************************************************************** * Copyright © 2014-2019 The SuperNET Developers. * * * @@ -13,442 +14,60 @@ * * ******************************************************************************/ -#include "CCassets.h" -#include "CCPrices.h" - -/* - Prices CC would best build on top of the oracles CC, ie. to combine payments for multiple oracles and to calculate a 51% protected price feed. - - We need to assume there is an oracle for a specific price. In the event there are more than one provider, the majority need to be within correlation distance to update a pricepoint. - - int64_t OraclePrice(int32_t height,uint256 reforacletxid,char *markeraddr,char *format); - - Using the above function, a consensus price can be obtained for a datasource. - - given an oracletxid, the marketaddr and format can be extracted to be used for future calls to OraclePrice. This allows to set a starting price and that in turn allows cash settled leveraged trading! - - Funds work like with dice, ie. there is a Prices plan that traders bet against. - - PricesFunding oracletxid, margin, priceaveraging, maxleverage, funding, longtoken, shorttoken, N [pubkeys] - - PricesBet -> oracletxid start with 'L', leverage, funding, direction - funds are locked into global CC address - it can be closed at anytime by the trader for cash settlement - the house account can close it if rekt - - Implementation Notes: - In order to eliminate the need for worrying about sybil attacks, each prices plan would be able to specific pubkey(s?) for whitelisted publishers. It would be possible to have a non-whitelisted plan that would use 50% correlation between publishers. - - delta neutral balancing of riskexposure: fabs(long exposure - short exposure) - bet +B at leverage L - absval(sum(+BLi) - sum(-Bli)) - - validate: update riskexposure and it needs to be <= funds - - PricesProfits: limit withdraw to funds in excess of riskexposure - PricesFinish: payout (if winning) and update riskexposure - need long/short exposure assets - - funding -> 1of2 CC global CC address and dealer address, exposure tokens to global 1of2 assets CC address - pricebet -> user funds and exposure token to 1of2 address. - pricewin -> winnings from dealer funds, exposure token back to global address - priceloss -> exposuretoken back to global address - - exposure address, funds address - - - -*/ - -// start of consensus code - -int64_t PricesOraclePrice(int64_t &rektprice,uint64_t mode,uint256 oracletxid,std::vectorpubkeys,int32_t dir,int64_t amount,int32_t leverage) +// game specific code for daemon +void games_packitemstr(char *packitemstr,struct games_packitem *item) { - int64_t price; - // howto ensure price when block it confirms it not known - // get price from oracle + current chaintip - // normalize leveraged amount - if ( dir > 0 ) - rektprice = price * leverage / (leverage-1); - else rektprice = price * (leverage-1) / leverage; - return(price); + strcpy(packitemstr,""); } -CScript EncodePricesFundingOpRet(uint8_t funcid,CPubKey planpk,uint256 oracletxid,uint256 longtoken,uint256 shorttoken,int32_t millimargin,uint64_t mode,int32_t maxleverage,std::vector pubkeys,uint256 bettoken) +int64_t games_cashout(struct games_player *P) { - CScript opret; - fprintf(stderr,"implement EncodePricesFundingOpRet\n"); - return(opret); + int32_t dungeonlevel = P->dungeonlevel; int64_t mult=1000,cashout = 0; + cashout = (uint64_t)P->gold * mult * dungeonlevel * dungeonlevel; + return(cashout); } -uint8_t DecodePricesFundingOpRet(CScript scriptPubKey,CPubKey &planpk,uint256 &oracletxid,uint256 &longtoken,uint256 &shorttoken,int32_t &millimargin,uint64_t &mode,int32_t &maxleverage,std::vector &pubkeys,uint256 &bettoken) +void tetrisplayerjson(UniValue &obj,struct games_player *P) { - fprintf(stderr,"implement DecodePricesFundingOpRet\n"); + obj.push_back(Pair("packsize",(int64_t)P->packsize)); + obj.push_back(Pair("hitpoints",(int64_t)P->hitpoints)); + obj.push_back(Pair("strength",(int64_t)(P->strength&0xffff))); + obj.push_back(Pair("maxstrength",(int64_t)(P->strength>>16))); + obj.push_back(Pair("level",(int64_t)P->level)); + obj.push_back(Pair("experience",(int64_t)P->experience)); + obj.push_back(Pair("dungeonlevel",(int64_t)P->dungeonlevel)); +} + +int32_t disp_gamesplayer(char *str,struct games_player *P) +{ + str[0] = 0; + //if ( P->gold <= 0 )//|| P->hitpoints <= 0 || (P->strength&0xffff) <= 0 || P->level <= 0 || P->experience <= 0 || P->dungeonlevel <= 0 ) + // return(-1); + sprintf(str," <- playerdata: gold.%d hp.%d strength.%d/%d level.%d exp.%d dl.%d",P->gold,P->hitpoints,P->strength&0xffff,P->strength>>16,P->level,P->experience,P->dungeonlevel); return(0); } -bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn) +int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector payload) { - int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64]; - - return true; // TODO remove, for test dual-evals - - return eval->Invalid("no validation yet"); - std::vector > txids; - numvins = tx.vin.size(); - numvouts = tx.vout.size(); - preventCCvins = preventCCvouts = -1; - if ( numvouts < 1 ) - return eval->Invalid("no vouts"); - else + uint256 gametxid; int32_t i,len; char str[67]; uint32_t eventid = 0; + if ( (len= payload.size()) > 36 ) { - for (i=0; iInvalid("illegal normal vini"); - } - } - //fprintf(stderr,"check amounts\n"); - //if ( PricesExactAmounts(cp,eval,tx,1,10000) == false ) - { - fprintf(stderr,"Pricesget invalid amount\n"); - return false; - } - //else - { - txid = tx.GetHash(); - memcpy(hash,&txid,sizeof(hash)); - retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); - if ( retval != 0 ) - fprintf(stderr,"Pricesget validated\n"); - else fprintf(stderr,"Pricesget invalid\n"); - return(retval); - } - } -} -// end of consensus code - -// helper functions for rpc calls in rpcwallet.cpp - -int64_t AddTokensInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char *destaddr,uint256 tolenid,int64_t total,int32_t maxinputs) -{ - // add threshold check - int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t vout,n = 0; - std::vector > unspentOutputs; - SetCCunspents(unspentOutputs,destaddr); - for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) - { - txid = it->first.txhash; - vout = (int32_t)it->first.index; - // need to prevent dup - if ( GetTransaction(txid,vintx,hashBlock,false) != 0 && vout < vintx.vout.size() ) - { - // need to verify assetid - if ( (nValue= vintx.vout[vout].nValue) >= 10000 && myIsutxo_spentinmempool(ignoretxid,ignorevin,txid,vout) == 0 ) - { - if ( total != 0 && maxinputs != 0 ) - mtx.vin.push_back(CTxIn(txid,vout,CScript())); - nValue = it->second.satoshis; - totalinputs += nValue; - n++; - if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) - break; - } - } - } - return(totalinputs); + len -= 36; + for (i=0; i<32; i++) + ((uint8_t *)&gametxid)[i] = payload[len+i]; + eventid = (uint32_t)payload[len+32]; + eventid |= (uint32_t)payload[len+33] << 8; + eventid |= (uint32_t)payload[len+34] << 16; + eventid |= (uint32_t)payload[len+35] << 24; + //for (i=0; i > addressIndex; struct CCcontract_info *cp,C; uint64_t mode; int32_t margin,maxleverage; std::vectorpubkeys; uint256 txid,hashBlock,oracletxid,longtoken,shorttoken,bettoken; CPubKey planpk,pricespk; char str[65]; CTransaction vintx; - cp = CCinit(&C,EVAL_PRICES); - pricespk = GetUnspendable(cp,0); - SetCCtxids(addressIndex,cp->normaladdr); - for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) - { - txid = it->first.txhash; - if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) - { - if ( vintx.vout.size() > 0 && DecodePricesFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' ) - { - result.push_back(uint256_str(str,txid)); - } - } - } - return(result); + return(true); } -// longtoken satoshis limits long exposure -// shorttoken satoshis limits short exposure -// both must be in the 1of2 CC address with its total supply -// bettoken -std::string PricesCreateFunding(uint64_t txfee,uint256 bettoken,uint256 oracletxid,uint64_t margin,uint64_t mode,uint256 longtoken,uint256 shorttoken,int32_t maxleverage,int64_t funding,std::vector pubkeys) -{ - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - CTransaction oracletx; int64_t fullsupply,inputs,CCchange=0; uint256 hashBlock; char str[65],coinaddr[64],houseaddr[64]; CPubKey mypk,pricespk; int32_t i,N,numvouts; struct CCcontract_info *cp,C; - if ( funding < 100*COIN || maxleverage <= 0 || maxleverage > 10000 ) - { - CCerror = "invalid parameter error"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - return(""); - } - cp = CCinit(&C,EVAL_PRICES); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp,0); - if ( (N= (int32_t)pubkeys.size()) || N > 15 ) - { - fprintf(stderr,"too many pubkeys N.%d\n",N); - return(""); - } - for (i=0; i 0 ) - { - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodePricesFundingOpRet('F',mypk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken))); - } - else - { - CCerror = "cant find enough inputs"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - } - return(""); -} - -UniValue PricesInfo(uint256 fundingtxid) -{ - UniValue result(UniValue::VOBJ),a(UniValue::VARR); CPubKey pricespk,planpk; uint256 hashBlock,oracletxid,longtoken,shorttoken,bettoken; CTransaction vintx; int64_t balance,supply,exposure; uint64_t funding,mode; int32_t i,margin,maxleverage; char numstr[65],houseaddr[64],exposureaddr[64],str[65]; std::vectorpubkeys; struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_PRICES); - pricespk = GetUnspendable(cp,0); - if ( GetTransaction(fundingtxid,vintx,hashBlock,false) == 0 ) - { - fprintf(stderr,"cant find fundingtxid\n"); - ERR_RESULT("cant find fundingtxid"); - return(result); - } - if ( vintx.vout.size() > 0 && DecodePricesFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' ) - { - result.push_back(Pair("result","success")); - result.push_back(Pair("fundingtxid",uint256_str(str,fundingtxid))); - result.push_back(Pair("bettoken",uint256_str(str,bettoken))); - result.push_back(Pair("oracletxid",uint256_str(str,oracletxid))); - sprintf(numstr,"%.3f",(double)margin/1000); - result.push_back(Pair("profitmargin",numstr)); - result.push_back(Pair("maxleverage",maxleverage)); - result.push_back(Pair("mode",(int64_t)mode)); - for (i=0; ipubkeys; - if ( amount < 10000 ) - { - CCerror = "amount must be positive"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - return(""); - } - cp = CCinit(&C,EVAL_PRICES); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp,0); - GetCCaddress(cp,myaddr,mypk); - if ( GetTransaction(fundingtxid,tx,hashBlock,false) == 0 ) - { - fprintf(stderr,"cant find fundingtxid\n"); - return(""); - } - if ( tx.vout.size() > 0 && DecodePricesFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' && bettoken == refbettoken ) - { - GetCCaddress1of2(cp,houseaddr,pricespk,planpk); - if ( AddNormalinputs(mtx,mypk,2*txfee,3) > 0 ) - { - if ( (inputs= AddTokensInputs(cp,mtx,myaddr,bettoken,amount,60)) >= amount ) - { - mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,amount,pricespk,planpk)); - mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(planpk)) << OP_CHECKSIG)); - if ( inputs > amount+txfee ) - CCchange = (inputs - amount); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,mypk)); - // add addr2 - - std::vector voutTokenPubkeysEmpty; //TODO: add token vout pubkeys - return(FinalizeCCTx(0,cp,mtx,mypk,txfee, - EncodeTokenOpRet(bettoken, voutTokenPubkeysEmpty, - std::make_pair(OPRETID_ASSETSDATA, EncodeAssetOpRet('t',/*bettoken,*/zeroid, 0, Mypubkey()))))); - } - else - { - CCerror = "cant find enough bet inputs"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - } - } - else - { - CCerror = "cant find enough inputs"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - } - } - return(""); -} - -std::string PricesBet(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,int64_t amount,int32_t leverage) -{ - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - struct CCcontract_info *cp,C; CPubKey pricespk,planpk,mypk; uint256 hashBlock,oracletxid,longtoken,shorttoken,tokenid,bettoken; CTransaction tx; int64_t balance,supply,exposure,inputs,inputs2,longexposure,netexposure,shortexposure,CCchange = 0,CCchange2 = 0; uint64_t funding,mode; int32_t dir,margin,maxleverage; char houseaddr[64],myaddr[64],exposureaddr[64]; std::vectorpubkeys; - if ( amount < 0 ) - { - amount = -amount; - dir = -1; - } else dir = 1; - cp = CCinit(&C,EVAL_PRICES); - if ( txfee == 0 ) - txfee = 10000; - mypk = pubkey2pk(Mypubkey()); - pricespk = GetUnspendable(cp,0); - GetCCaddress(cp,myaddr,mypk); - if ( GetTransaction(fundingtxid,tx,hashBlock,false) == 0 ) - { - fprintf(stderr,"cant find fundingtxid\n"); - return(""); - } - if ( tx.vout.size() > 0 && DecodePricesFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' && bettoken == refbettoken ) - { - if ( leverage > maxleverage || leverage < 1 ) - { - fprintf(stderr,"illegal leverage\n"); - return(""); - } - GetCCaddress1of2(cp,houseaddr,pricespk,planpk); - GetCCaddress1of2(cp,exposureaddr,pricespk,pricespk); - if ( dir < 0 ) - tokenid = shorttoken; - else tokenid = longtoken; - exposure = leverage * amount; - longexposure = CCtoken_balance(exposureaddr,longtoken); - shortexposure = CCtoken_balance(exposureaddr,shorttoken); - netexposure = (longexposure - shortexposure + exposure*dir); - if ( netexposure < 0 ) - netexposure = -netexposure; - balance = CCtoken_balance(myaddr,bettoken) / COIN; - if ( balance < netexposure*9/10 ) // 10% extra room for dynamically closed bets in wrong direction - { - fprintf(stderr,"balance %lld < 90%% netexposure %lld, refuse bet\n",(long long)balance,(long long)netexposure); - return(""); - } - if ( AddNormalinputs(mtx,mypk,txfee,3) > 0 ) - { - if ( (inputs= AddTokensInputs(cp,mtx,houseaddr,tokenid,exposure,30)) >= exposure ) - { - if ( (inputs2= AddTokensInputs(cp,mtx,myaddr,bettoken,amount,30)) >= amount ) - { - mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,amount,pricespk,planpk)); - mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,exposure,pricespk,pricespk)); - if ( inputs > exposure+txfee ) - CCchange = (inputs - exposure); - if ( inputs2 > amount+txfee ) - CCchange2 = (inputs2 - amount); - mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,CCchange,pricespk,planpk)); - mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange2,mypk)); - // add addr2 and addr3 - //return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodePricesExtra('T',tokenid,bettoken,zeroid,dir*leverage))); - CScript opret; - return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); - } - else - { - fprintf(stderr,"cant find enough bettoken inputs\n"); - return(""); - } - } - else - { - fprintf(stderr,"cant find enough exposure inputs\n"); - return(""); - } - } - else - { - CCerror = "cant find enough inputsB"; - fprintf(stderr,"%s\n", CCerror.c_str() ); - } - } - return(""); -} - -UniValue PricesStatus(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid) -{ - UniValue result(UniValue::VOBJ); - // get height of bettxid - // get price and rekt - // get current height and price - // what about if rekt in the past? - return(result); -} - -std::string PricesFinish(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid) -{ - return(""); -} - - - diff --git a/src/cc/prices.h b/src/cc/prices.h new file mode 100644 index 000000000..ae1c46821 --- /dev/null +++ b/src/cc/prices.h @@ -0,0 +1,208 @@ + +#ifndef H_PRICES_H +#define H_PRICES_H + +/***************************************************************************/ +/** https://github.com/brenns10/tetris + @file main.c + @author Stephen Brennan + @date Created Wednesday, 10 June 2015 + @brief Main program for tetris. + @copyright Copyright (c) 2015, Stephen Brennan. Released under the Revised + BSD License. See LICENSE.txt for details. + *******************************************************************************/ + +/* + Convert a tetromino type to its corresponding cell. + */ +#define TYPE_TO_CELL(x) ((x)+1) + +/* + Strings for how you would print a tetris board. + */ +#define TC_EMPTY_STR " " +#define TC_BLOCK_STR "\u2588" + +/* + Questions about a tetris cell. + */ +#define TC_IS_EMPTY(x) ((x) == TC_EMPTY) +#define TC_IS_FILLED(x) (!TC_IS_EMPTY(x)) + +/* + How many cells in a tetromino? + */ +#define TETRIS 4 +/* + How many tetrominos? + */ +#define NUM_TETROMINOS 7 +/* + How many orientations of a tetromino? + */ +#define NUM_ORIENTATIONS 4 + +/* + Level constants. + */ +#define MAX_LEVEL 19 +#define LINES_PER_LEVEL 10 + +/* + A "cell" is a 1x1 block within a tetris board. + */ +typedef enum { + TC_EMPTY, TC_CELLI, TC_CELLJ, TC_CELLL, TC_CELLO, TC_CELLS, TC_CELLT, TC_CELLZ +} tetris_cell; + +/* + A "type" is a type/shape of a tetromino. Not including orientation. + */ +typedef enum { + TET_I, TET_J, TET_L, TET_O, TET_S, TET_T, TET_Z +} tetris_type; + +/* + A row,column pair. Negative numbers allowed, because we need them for + offsets. + */ +typedef struct { + int row; + int col; +} tetris_location; + +/* + A "block" is a struct that contains information about a tetromino. + Specifically, what type it is, what orientation it has, and where it is. + */ +typedef struct { + int typ; + int ori; + tetris_location loc; +} tetris_block; + +/* + All possible moves to give as input to the game. + */ +typedef enum { + TM_LEFT, TM_RIGHT, TM_CLOCK, TM_COUNTER, TM_DROP, TM_HOLD, TM_NONE +} tetris_move; + +/* + A game object! + */ +typedef struct { + /* + Game board stuff: + */ + int rows; + int cols; + /* + Scoring information: + */ + int points; + int level; + /* + Falling block is the one currently going down. Next block is the one that + will be falling after this one. Stored is the block that you can swap out. + */ + tetris_block falling; + tetris_block next; + tetris_block stored; + /* + Number of game ticks until the block will move down. + */ + int ticks_till_gravity; + /* + Number of lines until you advance to the next level. + */ + int lines_remaining; + char board[]; +} tetris_game; + +/* + This array stores all necessary information about the cells that are filled by + each tetromino. The first index is the type of the tetromino (i.e. shape, + e.g. I, J, Z, etc.). The next index is the orientation (0-3). The final + array contains 4 tetris_location objects, each mapping to an offset from a + point on the upper left that is the tetromino "origin". + */ +extern const tetris_location TETROMINOS[NUM_TETROMINOS][NUM_ORIENTATIONS][TETRIS]; + +/* + This array tells you how many ticks per gravity by level. Decreases as level + increases, to add difficulty. + */ +extern const int GRAVITY_LEVEL[MAX_LEVEL+1]; + +// Data structure manipulation. +void tg_init(tetris_game *obj, int rows, int cols); +tetris_game *tg_create(struct games_state *rs,int rows, int cols); +void tg_destroy(tetris_game *obj); +void tg_delete(tetris_game *obj); +tetris_game *tg_load(FILE *f); +void tg_save(tetris_game *obj, FILE *f); + +// Public methods not related to memory: +char tg_get(tetris_game *obj, int row, int col); +bool tg_check(tetris_game *obj, int row, int col); +bool tg_tick(struct games_state *rs,tetris_game *obj, tetris_move move); +void tg_print(tetris_game *obj, FILE *f); + +/****************************************************************************** + * Copyright © 2014-2019 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. * + * * + ******************************************************************************/ +#define GAMENAME "prices" // name of executable +#define GAMEMAIN prices // main program of game +#define GAMEPLAYERJSON pricesplayerjson // displays game specific json +#define GAMEDATA pricesdata // extracts data from game specific variables into games_state +#define CHAINNAME "PRICES" // -ac_name= +typedef uint64_t gamesevent; // can be 8, 16, 32, or 64 bits + +#define MAXPACK 23 +struct games_packitem +{ + int32_t type,launch,count,which,hplus,dplus,arm,flags,group; + char damage[8],hurldmg[8]; +}; + +struct games_player +{ + int32_t gold,hitpoints,strength,level,experience,packsize,dungeonlevel,amulet; + struct games_packitem gamespack[MAXPACK]; +}; + +struct games_state +{ + uint64_t seed,origseed; + char *keystrokeshex; + uint32_t needflush,replaydone; + int32_t numkeys,ind,num,guiflag,counter,sleeptime,playersize,restoring,lastnum; + FILE *logfp; + struct games_player P; + gamesevent buffered[5000],*keystrokes; + uint8_t playerdata[8192]; +}; +extern struct games_state globalR; +void *gamesiterate(struct games_state *rs); +int32_t flushkeystrokes(struct games_state *rs,int32_t waitflag); + +void games_packitemstr(char *packitemstr,struct games_packitem *item); +uint64_t _games_rngnext(uint64_t initseed); +int32_t games_replay2(uint8_t *newdata,uint64_t seed,gamesevent *keystrokes,int32_t num,struct games_player *player,int32_t sleepmillis); +gamesevent games_revendian(gamesevent revx); +int32_t disp_gamesplayer(char *str,struct games_player *P); + +#endif + From 36fe43ff5876b0f161aaed3dc7b93999264a9ef0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 22:32:25 -1100 Subject: [PATCH 010/115] Make prices --- src/cc/gamescc.cpp | 4 ++-- src/cc/makeprices | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100755 src/cc/makeprices diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index f010ac282..ef9aa157b 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -16,7 +16,7 @@ #include "gamescc.h" #ifdef BUILD_PRICES #include "prices.c" -#elif +#else #include "tetris.c" #endif @@ -167,7 +167,7 @@ int32_t games_replay2(uint8_t *newdata,uint64_t seed,gamesevent *keystrokes,int3 #ifndef STANDALONE #ifdef BUILD_PRICES #include "prices.cpp" -#elif +#else #include "tetris.cpp" #endif diff --git a/src/cc/makeprices b/src/cc/makeprices new file mode 100755 index 000000000..012b8e157 --- /dev/null +++ b/src/cc/makeprices @@ -0,0 +1,3 @@ +gcc -O3 -DBUILD_GAMESCC -DBUILD_PRICES -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o pricescc.so cclib.cpp +gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE -DBUILD_PRICES gamescc.cpp -lncurses -lcurl -o tetris + From 7b832d7124d72473ca58066589970a9c764e45d0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:00:55 -1100 Subject: [PATCH 011/115] Games dir --- src/cc/{ => games}/prices.c | 6 +- src/cc/{tetris.cpp => games/prices.cpp} | 0 src/cc/{ => games}/prices.h | 0 src/cc/{ => games}/tetris.c | 0 src/cc/games/tetris.cpp | 73 ++++ src/cc/{ => games}/tetris.h | 0 src/cc/makeprices | 3 + src/cc/prices.cpp | 462 +++++++++++++++++++++--- 8 files changed, 500 insertions(+), 44 deletions(-) rename src/cc/{ => games}/prices.c (99%) rename src/cc/{tetris.cpp => games/prices.cpp} (100%) rename src/cc/{ => games}/prices.h (100%) rename src/cc/{ => games}/tetris.c (100%) create mode 100644 src/cc/games/tetris.cpp rename src/cc/{ => games}/tetris.h (100%) diff --git a/src/cc/prices.c b/src/cc/games/prices.c similarity index 99% rename from src/cc/prices.c rename to src/cc/games/prices.c index 651b1018c..02366fbc4 100644 --- a/src/cc/prices.c +++ b/src/cc/games/prices.c @@ -1,5 +1,5 @@ -#include "tetris.h" +#include "prices.h" /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. @@ -13,7 +13,7 @@ int random_tetromino(struct games_state *rs) return(rs->seed % NUM_TETROMINOS); } -int32_t tetrisdata(struct games_player *P,void *ptr) +int32_t pricesdata(struct games_player *P,void *ptr) { tetris_game *tg = (tetris_game *)ptr; P->gold = tg->points; @@ -832,7 +832,7 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve } else return(0); } -int tetris(int argc, char **argv) +int prices(int argc, char **argv) { struct games_state *rs = &globalR; int32_t c,skipcount=0; uint32_t eventid = 0; tetris_game *tg = 0; diff --git a/src/cc/tetris.cpp b/src/cc/games/prices.cpp similarity index 100% rename from src/cc/tetris.cpp rename to src/cc/games/prices.cpp diff --git a/src/cc/prices.h b/src/cc/games/prices.h similarity index 100% rename from src/cc/prices.h rename to src/cc/games/prices.h diff --git a/src/cc/tetris.c b/src/cc/games/tetris.c similarity index 100% rename from src/cc/tetris.c rename to src/cc/games/tetris.c diff --git a/src/cc/games/tetris.cpp b/src/cc/games/tetris.cpp new file mode 100644 index 000000000..8a52dcb37 --- /dev/null +++ b/src/cc/games/tetris.cpp @@ -0,0 +1,73 @@ + +/****************************************************************************** + * Copyright © 2014-2019 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. * + * * + ******************************************************************************/ + +// game specific code for daemon +void games_packitemstr(char *packitemstr,struct games_packitem *item) +{ + strcpy(packitemstr,""); +} + +int64_t games_cashout(struct games_player *P) +{ + int32_t dungeonlevel = P->dungeonlevel; int64_t mult=1000,cashout = 0; + cashout = (uint64_t)P->gold * mult * dungeonlevel * dungeonlevel; + return(cashout); +} + +void tetrisplayerjson(UniValue &obj,struct games_player *P) +{ + obj.push_back(Pair("packsize",(int64_t)P->packsize)); + obj.push_back(Pair("hitpoints",(int64_t)P->hitpoints)); + obj.push_back(Pair("strength",(int64_t)(P->strength&0xffff))); + obj.push_back(Pair("maxstrength",(int64_t)(P->strength>>16))); + obj.push_back(Pair("level",(int64_t)P->level)); + obj.push_back(Pair("experience",(int64_t)P->experience)); + obj.push_back(Pair("dungeonlevel",(int64_t)P->dungeonlevel)); +} + +int32_t disp_gamesplayer(char *str,struct games_player *P) +{ + str[0] = 0; + //if ( P->gold <= 0 )//|| P->hitpoints <= 0 || (P->strength&0xffff) <= 0 || P->level <= 0 || P->experience <= 0 || P->dungeonlevel <= 0 ) + // return(-1); + sprintf(str," <- playerdata: gold.%d hp.%d strength.%d/%d level.%d exp.%d dl.%d",P->gold,P->hitpoints,P->strength&0xffff,P->strength>>16,P->level,P->experience,P->dungeonlevel); + return(0); +} + +int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector payload) +{ + uint256 gametxid; int32_t i,len; char str[67]; uint32_t eventid = 0; + if ( (len= payload.size()) > 36 ) + { + len -= 36; + for (i=0; i<32; i++) + ((uint8_t *)&gametxid)[i] = payload[len+i]; + eventid = (uint32_t)payload[len+32]; + eventid |= (uint32_t)payload[len+33] << 8; + eventid |= (uint32_t)payload[len+34] << 16; + eventid |= (uint32_t)payload[len+35] << 24; + //for (i=0; i oracletxid start with 'L', leverage, funding, direction + funds are locked into global CC address + it can be closed at anytime by the trader for cash settlement + the house account can close it if rekt + + Implementation Notes: + In order to eliminate the need for worrying about sybil attacks, each prices plan would be able to specific pubkey(s?) for whitelisted publishers. It would be possible to have a non-whitelisted plan that would use 50% correlation between publishers. + + delta neutral balancing of riskexposure: fabs(long exposure - short exposure) + bet +B at leverage L + absval(sum(+BLi) - sum(-Bli)) + + validate: update riskexposure and it needs to be <= funds + + PricesProfits: limit withdraw to funds in excess of riskexposure + PricesFinish: payout (if winning) and update riskexposure + need long/short exposure assets + + funding -> 1of2 CC global CC address and dealer address, exposure tokens to global 1of2 assets CC address + pricebet -> user funds and exposure token to 1of2 address. + pricewin -> winnings from dealer funds, exposure token back to global address + priceloss -> exposuretoken back to global address + + exposure address, funds address + + + +*/ + +// start of consensus code + +int64_t PricesOraclePrice(int64_t &rektprice,uint64_t mode,uint256 oracletxid,std::vectorpubkeys,int32_t dir,int64_t amount,int32_t leverage) { - strcpy(packitemstr,""); + int64_t price; + // howto ensure price when block it confirms it not known + // get price from oracle + current chaintip + // normalize leveraged amount + if ( dir > 0 ) + rektprice = price * leverage / (leverage-1); + else rektprice = price * (leverage-1) / leverage; + return(price); } -int64_t games_cashout(struct games_player *P) +CScript EncodePricesFundingOpRet(uint8_t funcid,CPubKey planpk,uint256 oracletxid,uint256 longtoken,uint256 shorttoken,int32_t millimargin,uint64_t mode,int32_t maxleverage,std::vector pubkeys,uint256 bettoken) { - int32_t dungeonlevel = P->dungeonlevel; int64_t mult=1000,cashout = 0; - cashout = (uint64_t)P->gold * mult * dungeonlevel * dungeonlevel; - return(cashout); + CScript opret; + fprintf(stderr,"implement EncodePricesFundingOpRet\n"); + return(opret); } -void tetrisplayerjson(UniValue &obj,struct games_player *P) +uint8_t DecodePricesFundingOpRet(CScript scriptPubKey,CPubKey &planpk,uint256 &oracletxid,uint256 &longtoken,uint256 &shorttoken,int32_t &millimargin,uint64_t &mode,int32_t &maxleverage,std::vector &pubkeys,uint256 &bettoken) { - obj.push_back(Pair("packsize",(int64_t)P->packsize)); - obj.push_back(Pair("hitpoints",(int64_t)P->hitpoints)); - obj.push_back(Pair("strength",(int64_t)(P->strength&0xffff))); - obj.push_back(Pair("maxstrength",(int64_t)(P->strength>>16))); - obj.push_back(Pair("level",(int64_t)P->level)); - obj.push_back(Pair("experience",(int64_t)P->experience)); - obj.push_back(Pair("dungeonlevel",(int64_t)P->dungeonlevel)); -} - -int32_t disp_gamesplayer(char *str,struct games_player *P) -{ - str[0] = 0; - //if ( P->gold <= 0 )//|| P->hitpoints <= 0 || (P->strength&0xffff) <= 0 || P->level <= 0 || P->experience <= 0 || P->dungeonlevel <= 0 ) - // return(-1); - sprintf(str," <- playerdata: gold.%d hp.%d strength.%d/%d level.%d exp.%d dl.%d",P->gold,P->hitpoints,P->strength&0xffff,P->strength>>16,P->level,P->experience,P->dungeonlevel); + fprintf(stderr,"implement DecodePricesFundingOpRet\n"); return(0); } -int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector payload) +bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn) { - uint256 gametxid; int32_t i,len; char str[67]; uint32_t eventid = 0; - if ( (len= payload.size()) > 36 ) + int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64]; + + return true; // TODO remove, for test dual-evals + + return eval->Invalid("no validation yet"); + std::vector > txids; + numvins = tx.vin.size(); + numvouts = tx.vout.size(); + preventCCvins = preventCCvouts = -1; + if ( numvouts < 1 ) + return eval->Invalid("no vouts"); + else { - len -= 36; - for (i=0; i<32; i++) - ((uint8_t *)&gametxid)[i] = payload[len+i]; - eventid = (uint32_t)payload[len+32]; - eventid |= (uint32_t)payload[len+33] << 8; - eventid |= (uint32_t)payload[len+34] << 16; - eventid |= (uint32_t)payload[len+35] << 24; - //for (i=0; iInvalid("illegal normal vini"); + } + } + //fprintf(stderr,"check amounts\n"); + //if ( PricesExactAmounts(cp,eval,tx,1,10000) == false ) + { + fprintf(stderr,"Pricesget invalid amount\n"); + return false; + } + //else + { + txid = tx.GetHash(); + memcpy(hash,&txid,sizeof(hash)); + retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); + if ( retval != 0 ) + fprintf(stderr,"Pricesget validated\n"); + else fprintf(stderr,"Pricesget invalid\n"); + return(retval); + } + } } +// end of consensus code -bool games_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx) +// helper functions for rpc calls in rpcwallet.cpp + +int64_t AddTokensInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char *destaddr,uint256 tolenid,int64_t total,int32_t maxinputs) { - return(true); + // add threshold check + int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector origpubkey; CTransaction vintx; int32_t vout,n = 0; + std::vector > unspentOutputs; + SetCCunspents(unspentOutputs,destaddr); + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + vout = (int32_t)it->first.index; + // need to prevent dup + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 && vout < vintx.vout.size() ) + { + // need to verify assetid + if ( (nValue= vintx.vout[vout].nValue) >= 10000 && myIsutxo_spentinmempool(ignoretxid,ignorevin,txid,vout) == 0 ) + { + if ( total != 0 && maxinputs != 0 ) + mtx.vin.push_back(CTxIn(txid,vout,CScript())); + nValue = it->second.satoshis; + totalinputs += nValue; + n++; + if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) + break; + } + } + } + return(totalinputs); } +UniValue PricesList() +{ + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; uint64_t mode; int32_t margin,maxleverage; std::vectorpubkeys; uint256 txid,hashBlock,oracletxid,longtoken,shorttoken,bettoken; CPubKey planpk,pricespk; char str[65]; CTransaction vintx; + cp = CCinit(&C,EVAL_PRICES); + pricespk = GetUnspendable(cp,0); + SetCCtxids(addressIndex,cp->normaladdr); + for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) + { + txid = it->first.txhash; + if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) + { + if ( vintx.vout.size() > 0 && DecodePricesFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' ) + { + result.push_back(uint256_str(str,txid)); + } + } + } + return(result); +} + +// longtoken satoshis limits long exposure +// shorttoken satoshis limits short exposure +// both must be in the 1of2 CC address with its total supply +// bettoken +std::string PricesCreateFunding(uint64_t txfee,uint256 bettoken,uint256 oracletxid,uint64_t margin,uint64_t mode,uint256 longtoken,uint256 shorttoken,int32_t maxleverage,int64_t funding,std::vector pubkeys) +{ + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + CTransaction oracletx; int64_t fullsupply,inputs,CCchange=0; uint256 hashBlock; char str[65],coinaddr[64],houseaddr[64]; CPubKey mypk,pricespk; int32_t i,N,numvouts; struct CCcontract_info *cp,C; + if ( funding < 100*COIN || maxleverage <= 0 || maxleverage > 10000 ) + { + CCerror = "invalid parameter error"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + if ( (N= (int32_t)pubkeys.size()) || N > 15 ) + { + fprintf(stderr,"too many pubkeys N.%d\n",N); + return(""); + } + for (i=0; i 0 ) + { + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(pricespk)) << OP_CHECKSIG)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodePricesFundingOpRet('F',mypk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken))); + } + else + { + CCerror = "cant find enough inputs"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + } + return(""); +} + +UniValue PricesInfo(uint256 fundingtxid) +{ + UniValue result(UniValue::VOBJ),a(UniValue::VARR); CPubKey pricespk,planpk; uint256 hashBlock,oracletxid,longtoken,shorttoken,bettoken; CTransaction vintx; int64_t balance,supply,exposure; uint64_t funding,mode; int32_t i,margin,maxleverage; char numstr[65],houseaddr[64],exposureaddr[64],str[65]; std::vectorpubkeys; struct CCcontract_info *cp,C; + cp = CCinit(&C,EVAL_PRICES); + pricespk = GetUnspendable(cp,0); + if ( GetTransaction(fundingtxid,vintx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find fundingtxid\n"); + ERR_RESULT("cant find fundingtxid"); + return(result); + } + if ( vintx.vout.size() > 0 && DecodePricesFundingOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' ) + { + result.push_back(Pair("result","success")); + result.push_back(Pair("fundingtxid",uint256_str(str,fundingtxid))); + result.push_back(Pair("bettoken",uint256_str(str,bettoken))); + result.push_back(Pair("oracletxid",uint256_str(str,oracletxid))); + sprintf(numstr,"%.3f",(double)margin/1000); + result.push_back(Pair("profitmargin",numstr)); + result.push_back(Pair("maxleverage",maxleverage)); + result.push_back(Pair("mode",(int64_t)mode)); + for (i=0; ipubkeys; + if ( amount < 10000 ) + { + CCerror = "amount must be positive"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + return(""); + } + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + GetCCaddress(cp,myaddr,mypk); + if ( GetTransaction(fundingtxid,tx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find fundingtxid\n"); + return(""); + } + if ( tx.vout.size() > 0 && DecodePricesFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' && bettoken == refbettoken ) + { + GetCCaddress1of2(cp,houseaddr,pricespk,planpk); + if ( AddNormalinputs(mtx,mypk,2*txfee,3) > 0 ) + { + if ( (inputs= AddTokensInputs(cp,mtx,myaddr,bettoken,amount,60)) >= amount ) + { + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,amount,pricespk,planpk)); + mtx.vout.push_back(CTxOut(txfee,CScript() << ParseHex(HexStr(planpk)) << OP_CHECKSIG)); + if ( inputs > amount+txfee ) + CCchange = (inputs - amount); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,mypk)); + // add addr2 + + std::vector voutTokenPubkeysEmpty; //TODO: add token vout pubkeys + return(FinalizeCCTx(0,cp,mtx,mypk,txfee, + EncodeTokenOpRet(bettoken, voutTokenPubkeysEmpty, + std::make_pair(OPRETID_ASSETSDATA, EncodeAssetOpRet('t',/*bettoken,*/zeroid, 0, Mypubkey()))))); + } + else + { + CCerror = "cant find enough bet inputs"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + } + } + else + { + CCerror = "cant find enough inputs"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + } + } + return(""); +} + +std::string PricesBet(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,int64_t amount,int32_t leverage) +{ + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + struct CCcontract_info *cp,C; CPubKey pricespk,planpk,mypk; uint256 hashBlock,oracletxid,longtoken,shorttoken,tokenid,bettoken; CTransaction tx; int64_t balance,supply,exposure,inputs,inputs2,longexposure,netexposure,shortexposure,CCchange = 0,CCchange2 = 0; uint64_t funding,mode; int32_t dir,margin,maxleverage; char houseaddr[64],myaddr[64],exposureaddr[64]; std::vectorpubkeys; + if ( amount < 0 ) + { + amount = -amount; + dir = -1; + } else dir = 1; + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + GetCCaddress(cp,myaddr,mypk); + if ( GetTransaction(fundingtxid,tx,hashBlock,false) == 0 ) + { + fprintf(stderr,"cant find fundingtxid\n"); + return(""); + } + if ( tx.vout.size() > 0 && DecodePricesFundingOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,planpk,oracletxid,longtoken,shorttoken,margin,mode,maxleverage,pubkeys,bettoken) == 'F' && bettoken == refbettoken ) + { + if ( leverage > maxleverage || leverage < 1 ) + { + fprintf(stderr,"illegal leverage\n"); + return(""); + } + GetCCaddress1of2(cp,houseaddr,pricespk,planpk); + GetCCaddress1of2(cp,exposureaddr,pricespk,pricespk); + if ( dir < 0 ) + tokenid = shorttoken; + else tokenid = longtoken; + exposure = leverage * amount; + longexposure = CCtoken_balance(exposureaddr,longtoken); + shortexposure = CCtoken_balance(exposureaddr,shorttoken); + netexposure = (longexposure - shortexposure + exposure*dir); + if ( netexposure < 0 ) + netexposure = -netexposure; + balance = CCtoken_balance(myaddr,bettoken) / COIN; + if ( balance < netexposure*9/10 ) // 10% extra room for dynamically closed bets in wrong direction + { + fprintf(stderr,"balance %lld < 90%% netexposure %lld, refuse bet\n",(long long)balance,(long long)netexposure); + return(""); + } + if ( AddNormalinputs(mtx,mypk,txfee,3) > 0 ) + { + if ( (inputs= AddTokensInputs(cp,mtx,houseaddr,tokenid,exposure,30)) >= exposure ) + { + if ( (inputs2= AddTokensInputs(cp,mtx,myaddr,bettoken,amount,30)) >= amount ) + { + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,amount,pricespk,planpk)); + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,exposure,pricespk,pricespk)); + if ( inputs > exposure+txfee ) + CCchange = (inputs - exposure); + if ( inputs2 > amount+txfee ) + CCchange2 = (inputs2 - amount); + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,CCchange,pricespk,planpk)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange2,mypk)); + // add addr2 and addr3 + //return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodePricesExtra('T',tokenid,bettoken,zeroid,dir*leverage))); + CScript opret; + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); + } + else + { + fprintf(stderr,"cant find enough bettoken inputs\n"); + return(""); + } + } + else + { + fprintf(stderr,"cant find enough exposure inputs\n"); + return(""); + } + } + else + { + CCerror = "cant find enough inputsB"; + fprintf(stderr,"%s\n", CCerror.c_str() ); + } + } + return(""); +} + +UniValue PricesStatus(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid) +{ + UniValue result(UniValue::VOBJ); + // get height of bettxid + // get price and rekt + // get current height and price + // what about if rekt in the past? + return(result); +} + +std::string PricesFinish(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid) +{ + return(""); +} + + From 8f6561951e9fd0a9ad2646214f051edc96b3a7b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:01:45 -1100 Subject: [PATCH 012/115] Games/ --- src/cc/gamescc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index ef9aa157b..6bcd17b09 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -15,9 +15,9 @@ #include "gamescc.h" #ifdef BUILD_PRICES -#include "prices.c" +#include "games/prices.c" #else -#include "tetris.c" +#include "games/tetris.c" #endif int32_t GAMEDATA(struct games_player *P,void *ptr); @@ -166,9 +166,9 @@ int32_t games_replay2(uint8_t *newdata,uint64_t seed,gamesevent *keystrokes,int3 #ifndef STANDALONE #ifdef BUILD_PRICES -#include "prices.cpp" +#include "games/prices.cpp" #else -#include "tetris.cpp" +#include "games/tetris.cpp" #endif void GAMEJSON(UniValue &obj,struct games_player *P); From f31b33355a64fb5d42b7bed6bef8f8a2b8a1d8b0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:03:15 -1100 Subject: [PATCH 013/115] Pricesplayerjson --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 8a52dcb37..cb2de54a1 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -27,7 +27,7 @@ int64_t games_cashout(struct games_player *P) return(cashout); } -void tetrisplayerjson(UniValue &obj,struct games_player *P) +void pricesplayerjson(UniValue &obj,struct games_player *P) { obj.push_back(Pair("packsize",(int64_t)P->packsize)); obj.push_back(Pair("hitpoints",(int64_t)P->hitpoints)); From 6fca707153a3dde7826bf3798085fb30f61cb8ce Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:04:32 -1100 Subject: [PATCH 014/115] (uint8_t) --- src/cc/gamescc.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 6bcd17b09..56fd13310 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -1461,13 +1461,13 @@ UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) switch ( sizeof(gamesevent) ) { case 1: - sprintf(&hexstr[i<<1],"%02x",keystrokes[i]); + sprintf(&hexstr[i<<1],"%02x",(uint8_t)keystrokes[i]); break; case 2: - sprintf(&hexstr[i<<2],"%04x",keystrokes[i]); + sprintf(&hexstr[i<<2],"%04x",(uint16_t)keystrokes[i]); break; case 4: - sprintf(&hexstr[i<<3],"%08x",keystrokes[i]); + sprintf(&hexstr[i<<3],"%08x",(uint32_t)keystrokes[i]); break; case 8: sprintf(&hexstr[i<<4],"%016llxx",(long long)keystrokes[i]); From 5f2eeac994966f0b2629204e09eb753f0f472a44 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:08:20 -1100 Subject: [PATCH 015/115] Fixes --- src/cc/makeprices | 2 +- src/cc/maketetris | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cc/makeprices b/src/cc/makeprices index dc120deff..8bb98dd68 100755 --- a/src/cc/makeprices +++ b/src/cc/makeprices @@ -1,6 +1,6 @@ gcc -O3 -DBUILD_GAMESCC -DBUILD_PRICES -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o pricescc.so cclib.cpp cd games -gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE -DBUILD_PRICES gamescc.cpp -lncurses -lcurl -o tetris +gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE -DBUILD_PRICES ../gamescc.cpp -lncurses -lcurl -o tetris cd .. diff --git a/src/cc/maketetris b/src/cc/maketetris index 9f0af354e..f11a4de9c 100755 --- a/src/cc/maketetris +++ b/src/cc/maketetris @@ -1,2 +1,4 @@ -gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE gamescc.cpp -lncurses -lcurl -o tetris +cd games +gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE ../gamescc.cpp -lncurses -lcurl -o tetris +cd .. From 7e1fc1ef0986d8564a30f9551f9865e42e28af62 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:10:16 -1100 Subject: [PATCH 016/115] (uint8_t) --- src/cc/dapps/dappstd.c | 6 +++--- src/cc/makeprices | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/dapps/dappstd.c b/src/cc/dapps/dappstd.c index 917045ed5..82366315c 100644 --- a/src/cc/dapps/dappstd.c +++ b/src/cc/dapps/dappstd.c @@ -761,11 +761,11 @@ int32_t games_progress(struct games_state *rs,int32_t waitflag,uint64_t seed,gam for (i=0; i Date: Tue, 26 Mar 2019 23:12:41 -1100 Subject: [PATCH 017/115] (uint8_t) --- src/cc/games/prices.c | 6 +++--- src/cc/games/tetris.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 02366fbc4..f35e8164b 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -804,11 +804,11 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve if ( 0 ) { if ( sizeof(c) == 1 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",c&0xff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",(uint8_t)c&0xff,gametxidstr,eventid); else if ( sizeof(c) == 2 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",c&0xffff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",(uint16_t)c&0xffff,gametxidstr,eventid); else if ( sizeof(c) == 4 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",c&0xffffffff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",(uint32_t)c&0xffffffff,gametxidstr,eventid); else if ( sizeof(c) == 8 ) sprintf(params,"[\"events\",\"17\",\"[%%22%016llx%%22,%%22%s%%22,%u]\"]",(long long)c,gametxidstr,eventid); if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) diff --git a/src/cc/games/tetris.c b/src/cc/games/tetris.c index 651b1018c..254d324a8 100644 --- a/src/cc/games/tetris.c +++ b/src/cc/games/tetris.c @@ -804,11 +804,11 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve if ( 0 ) { if ( sizeof(c) == 1 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",c&0xff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",(uint8_t)c&0xff,gametxidstr,eventid); else if ( sizeof(c) == 2 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",c&0xffff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%04x%%22,%%22%s%%22,%u]\"]",(uint16_t)c&0xffff,gametxidstr,eventid); else if ( sizeof(c) == 4 ) - sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",c&0xffffffff,gametxidstr,eventid); + sprintf(params,"[\"events\",\"17\",\"[%%22%08x%%22,%%22%s%%22,%u]\"]",(uint32_t)c&0xffffffff,gametxidstr,eventid); else if ( sizeof(c) == 8 ) sprintf(params,"[\"events\",\"17\",\"[%%22%016llx%%22,%%22%s%%22,%u]\"]",(long long)c,gametxidstr,eventid); if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) From 97460af39460d404315a4268c6689e46f948db5d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:21:34 -1100 Subject: [PATCH 018/115] Fix makecclib --- src/cc/makecclib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index 38634aa78..8179623ad 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -6,7 +6,7 @@ make -f Makefile_rogue ./makerogue echo sudoku/musig/dilithium -gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../libcc.so cclib.cpp +gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o sudokucc.so cclib.cpp echo games tetris gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp From eed57deaffa0ca4ccfde66a5ac5852459af1c2d0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:36:34 -1100 Subject: [PATCH 019/115] Btcusd --- src/cc/games/prices.c | 777 +++++------------------------------------- 1 file changed, 83 insertions(+), 694 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index f35e8164b..cb475c763 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,5 +1,6 @@ #include "prices.h" +#include /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. @@ -22,90 +23,6 @@ int32_t pricesdata(struct games_player *P,void *ptr) return(0); } -/***************************************************************************/ -/** https://github.com/brenns10/tetris - @file main.c - @author Stephen Brennan - @date Created Wednesday, 10 June 2015 - @brief Main program for tetris. - @copyright Copyright (c) 2015, Stephen Brennan. Released under the Revised - BSD License. See LICENSE.txt for details. - *******************************************************************************/ - - -#include // for FILE -#include // for bool -#include -#include -#include -#include -#include -#include - -#ifdef BUILD_GAMESCC -#include "rogue/cursesd.h" -#else -#include -#endif - - -#define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) -#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) - -/******************************************************************************* - Array Definitions - *******************************************************************************/ - -const tetris_location TETROMINOS[NUM_TETROMINOS][NUM_ORIENTATIONS][TETRIS] = -{ - // I - {{{1, 0}, {1, 1}, {1, 2}, {1, 3}}, - {{0, 2}, {1, 2}, {2, 2}, {3, 2}}, - {{3, 0}, {3, 1}, {3, 2}, {3, 3}}, - {{0, 1}, {1, 1}, {2, 1}, {3, 1}}}, - // J - {{{0, 0}, {1, 0}, {1, 1}, {1, 2}}, - {{0, 1}, {0, 2}, {1, 1}, {2, 1}}, - {{1, 0}, {1, 1}, {1, 2}, {2, 2}}, - {{0, 1}, {1, 1}, {2, 0}, {2, 1}}}, - // L - {{{0, 2}, {1, 0}, {1, 1}, {1, 2}}, - {{0, 1}, {1, 1}, {2, 1}, {2, 2}}, - {{1, 0}, {1, 1}, {1, 2}, {2, 0}}, - {{0, 0}, {0, 1}, {1, 1}, {2, 1}}}, - // O - {{{0, 1}, {0, 2}, {1, 1}, {1, 2}}, - {{0, 1}, {0, 2}, {1, 1}, {1, 2}}, - {{0, 1}, {0, 2}, {1, 1}, {1, 2}}, - {{0, 1}, {0, 2}, {1, 1}, {1, 2}}}, - // S - {{{0, 1}, {0, 2}, {1, 0}, {1, 1}}, - {{0, 1}, {1, 1}, {1, 2}, {2, 2}}, - {{1, 1}, {1, 2}, {2, 0}, {2, 1}}, - {{0, 0}, {1, 0}, {1, 1}, {2, 1}}}, - // T - {{{0, 1}, {1, 0}, {1, 1}, {1, 2}}, - {{0, 1}, {1, 1}, {1, 2}, {2, 1}}, - {{1, 0}, {1, 1}, {1, 2}, {2, 1}}, - {{0, 1}, {1, 0}, {1, 1}, {2, 1}}}, - // Z - {{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, - {{0, 2}, {1, 1}, {1, 2}, {2, 1}}, - {{1, 0}, {1, 1}, {2, 1}, {2, 2}}, - {{0, 1}, {1, 0}, {1, 1}, {2, 0}}}, -}; - -const int GRAVITY_LEVEL[MAX_LEVEL+1] = { - // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, - //10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 30, 28, 26, 24, 22, 20, 16, 12, 8, 4 -}; - -/******************************************************************************* - Helper Functions for Blocks - *******************************************************************************/ - void sleep_milli(int milliseconds) { struct timespec ts; @@ -114,578 +31,117 @@ void sleep_milli(int milliseconds) nanosleep(&ts, NULL); } -/* - Return the block at the given row and column. - */ -char tg_get(tetris_game *obj, int row, int column) +void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { - return obj->board[obj->cols * row + column]; -} - -/* - Set the block at the given row and column. - */ -static void tg_set(tetris_game *obj, int row, int column, char value) -{ - obj->board[obj->cols * row + column] = value; -} - -/* - Check whether a row and column are in bounds. - */ -bool tg_check(tetris_game *obj, int row, int col) -{ - return 0 <= row && row < obj->rows && 0 <= col && col < obj->cols; -} - -/* - Place a block onto the board. - */ -static void tg_put(tetris_game *obj, tetris_block block) -{ - int i; - for (i = 0; i < TETRIS; i++) { - tetris_location cell = TETROMINOS[block.typ][block.ori][i]; - tg_set(obj, block.loc.row + cell.row, block.loc.col + cell.col, - TYPE_TO_CELL(block.typ)); - } -} - -/* - Clear a block out of the board. - */ -static void tg_remove(tetris_game *obj, tetris_block block) -{ - int i; - for (i = 0; i < TETRIS; i++) { - tetris_location cell = TETROMINOS[block.typ][block.ori][i]; - tg_set(obj, block.loc.row + cell.row, block.loc.col + cell.col, TC_EMPTY); - } -} - -/* - Check if a block can be placed on the board. - */ -static bool tg_fits(tetris_game *obj, tetris_block block) -{ - int i, r, c; - for (i = 0; i < TETRIS; i++) { - tetris_location cell = TETROMINOS[block.typ][block.ori][i]; - r = block.loc.row + cell.row; - c = block.loc.col + cell.col; - if (!tg_check(obj, r, c) || TC_IS_FILLED(tg_get(obj, r, c))) { - return false; - } - } - return true; -} - -/* - Create a new falling block and populate the next falling block with a random - one. - */ -static void tg_new_falling(struct games_state *rs,tetris_game *obj) -{ - // Put in a new falling tetromino. - obj->falling = obj->next; - obj->next.typ = random_tetromino(rs); - obj->next.ori = 0; - obj->next.loc.row = 0; - obj->next.loc.col = obj->cols/2 - 2; -} - -/******************************************************************************* - Game Turn Helpers - *******************************************************************************/ - -/* - Tick gravity, and move the block down if gravity should act. - */ -static void tg_do_gravity_tick(struct games_state *rs,tetris_game *obj) -{ - obj->ticks_till_gravity--; - if (obj->ticks_till_gravity <= 0) { - tg_remove(obj, obj->falling); - obj->falling.loc.row++; - if (tg_fits(obj, obj->falling)) { - obj->ticks_till_gravity = GRAVITY_LEVEL[obj->level]; - } else { - obj->falling.loc.row--; - tg_put(obj, obj->falling); - - tg_new_falling(rs,obj); - } - tg_put(obj, obj->falling); - } -} - -/* - Move the falling tetris block left (-1) or right (+1). - */ -static void tg_move(tetris_game *obj, int direction) -{ - tg_remove(obj, obj->falling); - obj->falling.loc.col += direction; - if (!tg_fits(obj, obj->falling)) { - obj->falling.loc.col -= direction; - } - tg_put(obj, obj->falling); -} - -/* - Send the falling tetris block to the bottom. - */ -static void tg_down(struct games_state *rs,tetris_game *obj) -{ - tg_remove(obj, obj->falling); - while (tg_fits(obj, obj->falling)) { - obj->falling.loc.row++; - } - obj->falling.loc.row--; - tg_put(obj, obj->falling); - tg_new_falling(rs,obj); -} - -/* - Rotate the falling block in either direction (+/-1). - */ -static void tg_rotate(tetris_game *obj, int direction) -{ - tg_remove(obj, obj->falling); - - while (true) { - obj->falling.ori = (obj->falling.ori + direction) % NUM_ORIENTATIONS; - - // If the new orientation fits, we're done. - if (tg_fits(obj, obj->falling)) - break; - - // Otherwise, try moving left to make it fit. - obj->falling.loc.col--; - if (tg_fits(obj, obj->falling)) - break; - - // Finally, try moving right to make it fit. - obj->falling.loc.col += 2; - if (tg_fits(obj, obj->falling)) - break; - - // Put it back in its original location and try the next orientation. - obj->falling.loc.col--; - // Worst case, we come back to the original orientation and it fits, so this - // loop will terminate. - } - - tg_put(obj, obj->falling); -} - -/* - Swap the falling block with the block in the hold buffer. - */ -static void tg_hold(struct games_state *rs,tetris_game *obj) -{ - tg_remove(obj, obj->falling); - if (obj->stored.typ == -1) { - obj->stored = obj->falling; - tg_new_falling(rs,obj); - } else { - int typ = obj->falling.typ, ori = obj->falling.ori; - obj->falling.typ = obj->stored.typ; - obj->falling.ori = obj->stored.ori; - obj->stored.typ = typ; - obj->stored.ori = ori; - while (!tg_fits(obj, obj->falling)) { - obj->falling.loc.row--; - } - } - tg_put(obj, obj->falling); -} - -/* - Perform the action specified by the move. - */ -static void tg_handle_move(struct games_state *rs,tetris_game *obj, tetris_move move) -{ - switch (move) { - case TM_LEFT: - //fprintf(stderr,"LEFT "); - tg_move(obj, -1); - break; - case TM_RIGHT: - //fprintf(stderr,"RIGHT "); - tg_move(obj, 1); - break; - case TM_DROP: - tg_down(rs,obj); - break; - case TM_CLOCK: - tg_rotate(obj, 1); - break; - case TM_COUNTER: - tg_rotate(obj, -1); - break; - case TM_HOLD: - tg_hold(rs,obj); - break; - default: - // pass - break; - } -} - -/* - Return true if line i is full. - */ -static bool tg_line_full(tetris_game *obj, int i) -{ - int j; - for (j = 0; j < obj->cols; j++) { - if (TC_IS_EMPTY(tg_get(obj, i, j))) - return false; - } - return true; -} - -/* - Shift every row above r down one. - */ -static void tg_shift_lines(tetris_game *obj, int r) -{ - int i, j; - for (i = r-1; i >= 0; i--) { - for (j = 0; j < obj->cols; j++) { - tg_set(obj, i+1, j, tg_get(obj, i, j)); - tg_set(obj, i, j, TC_EMPTY); - } - } -} - -/* - Find rows that are filled, remove them, shift, and return the number of - cleared rows. - */ -static int tg_check_lines(tetris_game *obj) -{ - int i, nlines = 0; - tg_remove(obj, obj->falling); // don't want to mess up falling block - - for (i = obj->rows-1; i >= 0; i--) { - if (tg_line_full(obj, i)) { - tg_shift_lines(obj, i); - i++; // do this line over again since they're shifted - nlines++; - } - } - - tg_put(obj, obj->falling); // replace - return nlines; -} - -/* - Adjust the score for the game, given how many lines were just cleared. - */ -static void tg_adjust_score(tetris_game *obj, int lines_cleared) -{ - static int line_multiplier[] = {0, 40, 100, 300, 1200}; - obj->points += line_multiplier[lines_cleared] * (obj->level + 1); - if (lines_cleared >= obj->lines_remaining) { - obj->level = MIN(MAX_LEVEL, obj->level + 1); - lines_cleared -= obj->lines_remaining; - obj->lines_remaining = LINES_PER_LEVEL - lines_cleared; - } else { - obj->lines_remaining -= lines_cleared; - } -} - -/* - Return true if the game is over. - */ -static bool tg_game_over(tetris_game *obj) -{ - int i, j; - bool over = false; - tg_remove(obj, obj->falling); - for (i = 0; i < 2; i++) { - for (j = 0; j < obj->cols; j++) { - if (TC_IS_FILLED(tg_get(obj, i, j))) { - over = true; - } - } - } - tg_put(obj, obj->falling); - return over; -} - -/******************************************************************************* - Main Public Functions - *******************************************************************************/ - -/* - Do a single game tick: process gravity, user input, and score. Return true if - the game is still running, false if it is over. - */ -bool tg_tick(struct games_state *rs,tetris_game *obj, tetris_move move) -{ - int lines_cleared; - // Handle gravity. - tg_do_gravity_tick(rs,obj); - - // Handle input. - tg_handle_move(rs,obj, move); - - // Check for cleared lines - lines_cleared = tg_check_lines(obj); - - tg_adjust_score(obj, lines_cleared); - - // Return whether the game will continue (NOT whether it's over) - return !tg_game_over(obj); -} - -void tg_init(struct games_state *rs,tetris_game *obj, int rows, int cols) -{ - // Initialization logic - obj->rows = rows; - obj->cols = cols; - //obj->board = (char *)malloc(rows * cols); - memset(obj->board, TC_EMPTY, rows * cols); - obj->points = 0; - obj->level = 0; - obj->ticks_till_gravity = GRAVITY_LEVEL[obj->level]; - obj->lines_remaining = LINES_PER_LEVEL; - //srand(time(NULL)); - tg_new_falling(rs,obj); - tg_new_falling(rs,obj); - obj->stored.typ = -1; - obj->stored.ori = 0; - obj->stored.loc.row = 0; - obj->next.loc.col = obj->cols/2 - 2; - //printf("%d", obj->falling.loc.col); -} - -tetris_game *tg_create(struct games_state *rs,int rows, int cols) -{ - tetris_game *obj = (tetris_game *)malloc(sizeof(tetris_game) + rows*cols); - tg_init(rs,obj, rows, cols); - return obj; -} - -/*void tg_destroy(tetris_game *obj) -{ - // Cleanup logic - free(obj->board); -}*/ - -void tg_delete(tetris_game *obj) { - //tg_destroy(obj); - free(obj); -} - -/* - Load a game from a file. - -tetris_game *tg_load(FILE *f) -{ - tetris_game *obj = (tetris_game *)malloc(sizeof(tetris_game)); - if (fread(obj, sizeof(tetris_game), 1, f) != 1 ) + FILE *fp; + long filesize,buflen = *allocsizep; + uint8_t *buf = *bufp; + *lenp = 0; + if ( (fp= fopen(portable_path(fname),"rb")) != 0 ) { - fprintf(stderr,"read game error\n"); - free(obj); - obj = 0; - } - else - { - obj->board = (char *)malloc(obj->rows * obj->cols); - if (fread(obj->board, sizeof(char), obj->rows * obj->cols, f) != obj->rows * obj->cols ) + fseek(fp,0,SEEK_END); + filesize = ftell(fp); + if ( filesize == 0 ) { - fprintf(stderr,"fread error\n"); - free(obj->board); - free(obj); - obj = 0; + fclose(fp); + *lenp = 0; + //printf("loadfile null size.(%s)\n",fname); + return(0); } - } - return obj; -}*/ - -/* - Save a game to a file. - -void tg_save(tetris_game *obj, FILE *f) -{ - if (fwrite(obj, sizeof(tetris_game), 1, f) != 1 ) - fprintf(stderr,"error writing tetrisgame\n"); - else if (fwrite(obj->board, sizeof(char), obj->rows * obj->cols, f) != obj->rows * obj->cols ) - fprintf(stderr,"error writing board\n"); -}*/ - -/* - Print a game board to a file. Really just for early debugging. - */ -void tg_print(tetris_game *obj, FILE *f) { - int i, j; - for (i = 0; i < obj->rows; i++) { - for (j = 0; j < obj->cols; j++) { - if (TC_IS_EMPTY(tg_get(obj, i, j))) { - fputs(TC_EMPTY_STR, f); - } else { - fputs(TC_BLOCK_STR, f); - } + if ( filesize > buflen ) + { + *allocsizep = filesize; + *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); } - fputc('\n', f); - } -} - -/* - 2 columns per cell makes the game much nicer. - */ -#define COLS_PER_CELL 2 -/* - Macro to print a cell of a specific type to a window. - */ -#define ADD_BLOCK(w,x) waddch((w),' '|A_REVERSE|COLOR_PAIR(x)); \ -waddch((w),' '|A_REVERSE|COLOR_PAIR(x)) -#define ADD_EMPTY(w) waddch((w), ' '); waddch((w), ' ') - -/* - Print the tetris board onto the ncurses window. - */ -void display_board(WINDOW *w, tetris_game *obj) -{ - int i, j; - box(w, 0, 0); - for (i = 0; i < obj->rows; i++) { - wmove(w, 1 + i, 1); - for (j = 0; j < obj->cols; j++) { - if (TC_IS_FILLED(tg_get(obj, i, j))) { - ADD_BLOCK(w,tg_get(obj, i, j)); - } else { - ADD_EMPTY(w); - } + rewind(fp); + if ( buf == 0 ) + printf("Null buf ???\n"); + else + { + if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) + printf("error reading filesize.%ld\n",(long)filesize); + buf[filesize] = 0; } - } - wnoutrefresh(w); + fclose(fp); + *lenp = filesize; + //printf("loaded.(%s)\n",buf); + } //else printf("OS_loadfile couldnt load.(%s)\n",fname); + return(buf); } -/* - Display a tetris piece in a dedicated window. - */ -void display_piece(WINDOW *w, tetris_block block) +void *filestr(long *allocsizep,char *_fname) { - int b; - tetris_location c; - wclear(w); - box(w, 0, 0); - if (block.typ == -1) { - wnoutrefresh(w); - return; - } - for (b = 0; b < TETRIS; b++) { - c = TETROMINOS[block.typ][block.ori][b]; - wmove(w, c.row + 1, c.col * COLS_PER_CELL + 1); - ADD_BLOCK(w, TYPE_TO_CELL(block.typ)); - } - wnoutrefresh(w); + long filesize = 0; char *fname,*buf = 0; void *retptr; + *allocsizep = 0; + fname = malloc(strlen(_fname)+1); + strcpy(fname,_fname); + retptr = loadfile(fname,(uint8_t **)&buf,&filesize,allocsizep); + free(fname); + return(retptr); } -/* - Display score information in a dedicated window. - */ -void display_score(WINDOW *w, tetris_game *tg) +char *send_curl(char *url,char *fname) { - wclear(w); - box(w, 0, 0); - wprintw(w, (char *)"Score\n%d\n", tg->points); - wprintw(w, (char *)"Level\n%d\n", tg->level); - wprintw(w, (char *)"Lines\n%d\n", tg->lines_remaining); - wnoutrefresh(w); + long fsize; char curlstr[1024]; + sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); + system(curlstr); + return(filestr(&fsize,fname)); } -/* - Save and exit the game. - -void save(tetris_game *game, WINDOW *w) +cJSON *get_urljson(char *url,char *fname) { - FILE *f; - - wclear(w); - box(w, 0, 0); // return the border - wmove(w, 1, 1); - wprintw(w, (char *)"Save and exit? [Y/n] "); - wrefresh(w); - timeout(-1); - if (getch() == 'n') { - timeout(0); - return; + char *jsonstr; cJSON *json = 0; + if ( (jsonstr= send_curl(url,fname)) != 0 ) + { + //printf("(%s) -> (%s)\n",url,jsonstr); + json = cJSON_Parse(jsonstr); + free(jsonstr); } - f = fopen("tetris.save", "w"); - tg_save(game, f); - fclose(f); - tg_delete(game); - endwin(); - fprintf(stderr,"Game saved to \"tetris.save\".\n"); - fprintf(stderr,"Resume by passing the filename as an argument to this program.\n"); - exit(EXIT_SUCCESS); -}*/ + return(json); +} -/* - Do the NCURSES initialization steps for color blocks. - */ -void init_colors(void) +////////////////////////////////////////////// +// start of dapp +////////////////////////////////////////////// + +uint64_t get_btcusd() { - start_color(); - //init_color(COLOR_ORANGE, 1000, 647, 0); - init_pair(TC_CELLI, COLOR_CYAN, COLOR_BLACK); - init_pair(TC_CELLJ, COLOR_BLUE, COLOR_BLACK); - init_pair(TC_CELLL, COLOR_WHITE, COLOR_BLACK); - init_pair(TC_CELLO, COLOR_YELLOW, COLOR_BLACK); - init_pair(TC_CELLS, COLOR_GREEN, COLOR_BLACK); - init_pair(TC_CELLT, COLOR_MAGENTA, COLOR_BLACK); - init_pair(TC_CELLZ, COLOR_RED, COLOR_BLACK); + cJSON *pjson,*bpi,*usd; uint64_t btcusd = 0; + if ( (pjson= get_urljson("http://api.coindesk.com/v1/bpi/currentprice.json","/tmp/oraclefeed.json")) != 0 ) + { + if ( (bpi= jobj(pjson,"bpi")) != 0 && (usd= jobj(bpi,"USD")) != 0 ) + { + btcusd = jdouble(usd,"rate_float") * SATOSHIDEN; + printf("BTC/USD %.4f\n",dstr(btcusd)); + } + free_json(pjson); + } + return(btcusd); } struct games_state globalR; extern char Gametxidstr[]; int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c); -gamesevent games_readevent(struct games_state *rs); void *gamesiterate(struct games_state *rs) { - uint32_t counter = 0; bool running = true; tetris_move move = TM_NONE; - gamesevent c; uint16_t skipcount=0; int32_t prevlevel; uint32_t eventid = 0; tetris_game *tg; - WINDOW *board, *next, *hold, *score; + bool running = true; uint32_t eventid = 0; int64_t price; if ( rs->guiflag != 0 || rs->sleeptime != 0 ) { - // NCURSES initialization: - initscr(); // initialize curses - cbreak(); // pass key presses to program, but not signals - noecho(); // don't echo key presses to screen - keypad(stdscr, TRUE); // allow arrow keys - timeout(0); // no blocking on getch() - curs_set(0); // set the cursor to invisible - init_colors(); // setup tetris colors } - tg = tg_create(rs,22, 10); - prevlevel = tg->level; - // Create windows for each section of the interface. - board = newwin(tg->rows + 2, 2 * tg->cols + 2, 0, 0); - next = newwin(6, 10, 0, 2 * (tg->cols + 1) + 1); - hold = newwin(6, 10, 7, 2 * (tg->cols + 1) + 1); - score = newwin(6, 10, 14, 2 * (tg->cols + 1 ) + 1); while ( running != 0 ) { - running = tg_tick(rs,tg,move); - if ( 1 && (rs->guiflag != 0 || rs->sleeptime != 0) ) + //running = tg_tick(rs,tg,move); + if ( rs->guiflag != 0 || rs->sleeptime != 0 ) { - display_board(board,tg); - display_piece(next,tg->next); - display_piece(hold,tg->stored); - display_score(score,tg); } if ( rs->guiflag != 0 ) { #ifdef STANDALONE - sleep_milli(15); - if ( (counter++ % 10) == 0 ) + sleep_milli(10000); + price = get_btcusd(); + fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/COIN); + /*if ( (counter++ % 10) == 0 ) doupdate(); c = games_readevent(rs); if ( c <= 0x7f || skipcount == 0x3fff ) @@ -700,7 +156,7 @@ void *gamesiterate(struct games_state *rs) prevlevel = tg->level; } skipcount = 0; - } else skipcount++; + } else skipcount++;*/ #endif } else @@ -713,7 +169,7 @@ void *gamesiterate(struct games_state *rs) if ( (counter++ % 20) == 0 ) doupdate(); } - if ( skipcount == 0 ) + /*if ( skipcount == 0 ) { c = games_readevent(rs); //fprintf(stderr,"%04x score.%d level.%d\n",c,tg->points,tg->level); @@ -724,56 +180,14 @@ void *gamesiterate(struct games_state *rs) } } if ( skipcount > 0 ) - skipcount--; + skipcount--;*/ } eventid++; - switch ( c ) - { - case 'h': - move = TM_LEFT; - break; - case 'l': - move = TM_RIGHT; - break; - case 'k': - move = TM_CLOCK; - break; - case 'j': - move = TM_DROP; - break; - case 'q': - running = false; - move = TM_NONE; - break; - /*case 'p': - wclear(board); - box(board, 0, 0); - wmove(board, tg->rows/2, (tg->cols*COLS_PER_CELL-6)/2); - wprintw(board, "PAUSED"); - wrefresh(board); - timeout(-1); - getch(); - timeout(0); - move = TM_NONE; - break; - case 's': - save(tg, board); - move = TM_NONE; - break;*/ - case ' ': - move = TM_HOLD; - break; - default: - move = TM_NONE; - } } - return(tg); + return(0); } #ifdef STANDALONE -/* - Main tetris game! - */ #include "dapps/dappstd.c" @@ -835,7 +249,7 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve int prices(int argc, char **argv) { struct games_state *rs = &globalR; - int32_t c,skipcount=0; uint32_t eventid = 0; tetris_game *tg = 0; + int32_t c,skipcount=0; uint32_t eventid = 0; memset(rs,0,sizeof(*rs)); rs->guiflag = 1; rs->sleeptime = 1; // non-zero to allow refresh() @@ -862,33 +276,8 @@ int prices(int argc, char **argv) } } } else rs->seed = 777; - - /* Load file if given a filename. - if (argc >= 2) { - FILE *f = fopen(argv[1], "r"); - if (f == NULL) { - perror("tetris"); - exit(EXIT_FAILURE); - } - tg = tg_load(f); - fclose(f); - } else { - // Otherwise create new game. - tg = tg_create(rs,22, 10); - }*/ - - // Game loop - tg = (tetris_game *)gamesiterate(rs); - gamesbailout(rs); - // Deinitialize NCurses - wclear(stdscr); - endwin(); - // Output ending message. - printf("Game over!\n"); - printf("You finished with %d points on level %d.\n", tg->points, tg->level); - - // Deinitialize Tetris - tg_delete(tg); + gamesiterate(rs); + //gamesbailout(rs); return 0; } From 02e76e122bf1b71515e0707962d1f0f0b92fb834 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:40:09 -1100 Subject: [PATCH 020/115] syntax --- src/cc/games/prices.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index cb475c763..81f58521c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -31,6 +31,37 @@ void sleep_milli(int milliseconds) nanosleep(&ts, NULL); } +char *nonportable_path(char *str) +{ + int32_t i; + for (i=0; str[i]!=0; i++) + if ( str[i] == '/' ) + str[i] = '\\'; + return(str); +} + +char *portable_path(char *str) +{ +#ifdef _WIN32 + return(nonportable_path(str)); +#else +#ifdef __PNACL + /*int32_t i,n; + if ( str[0] == '/' ) + return(str); + else + { + n = (int32_t)strlen(str); + for (i=n; i>0; i--) + str[i] = str[i-1]; + str[0] = '/'; + str[n+1] = 0; + }*/ +#endif + return(str); +#endif +} + void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { FILE *fp; @@ -73,7 +104,7 @@ void *filestr(long *allocsizep,char *_fname) { long filesize = 0; char *fname,*buf = 0; void *retptr; *allocsizep = 0; - fname = malloc(strlen(_fname)+1); + fname = (char *)malloc(strlen(_fname)+1); strcpy(fname,_fname); retptr = loadfile(fname,(uint8_t **)&buf,&filesize,allocsizep); free(fname); From 2821c37693fe66b4fafef0120cc9a6546abe1f81 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:41:07 -1100 Subject: [PATCH 021/115] Ncurses --- src/cc/games/prices.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 81f58521c..96506b41c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,6 +1,7 @@ #include "prices.h" #include +#include /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From 4d66ed85244123fa136689bf3e760c1aa12c8dd8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:43:26 -1100 Subject: [PATCH 022/115] -curses.h --- src/cc/games/prices.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 96506b41c..81f58521c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,7 +1,6 @@ #include "prices.h" #include -#include /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From 55b3b006edb1e99a85de8b60b9e00f89f20b8f71 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:44:12 -1100 Subject: [PATCH 023/115] Test --- src/cc/games/prices.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 81f58521c..66cf54030 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -219,6 +219,7 @@ void *gamesiterate(struct games_state *rs) } #ifdef STANDALONE +#include #include "dapps/dappstd.c" From 20ff5d20d4182a17abf495ed0b9cbaab2419b0e3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:46:06 -1100 Subject: [PATCH 024/115] syntax --- src/cc/games/prices.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 66cf54030..e125a1602 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -116,7 +116,7 @@ char *send_curl(char *url,char *fname) long fsize; char curlstr[1024]; sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); system(curlstr); - return(filestr(&fsize,fname)); + return((char *)filestr(&fsize,fname)); } cJSON *get_urljson(char *url,char *fname) @@ -138,11 +138,11 @@ cJSON *get_urljson(char *url,char *fname) uint64_t get_btcusd() { cJSON *pjson,*bpi,*usd; uint64_t btcusd = 0; - if ( (pjson= get_urljson("http://api.coindesk.com/v1/bpi/currentprice.json","/tmp/oraclefeed.json")) != 0 ) + if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json","/tmp/oraclefeed.json")) != 0 ) { - if ( (bpi= jobj(pjson,"bpi")) != 0 && (usd= jobj(bpi,"USD")) != 0 ) + if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { - btcusd = jdouble(usd,"rate_float") * SATOSHIDEN; + btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; printf("BTC/USD %.4f\n",dstr(btcusd)); } free_json(pjson); @@ -197,8 +197,6 @@ void *gamesiterate(struct games_state *rs) if ( rs->sleeptime != 0 ) { sleep_milli(1); - if ( (counter++ % 20) == 0 ) - doupdate(); } /*if ( skipcount == 0 ) { From eca3bda195239b325d1f71b350b3983c46b6bc13 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:48:03 -1100 Subject: [PATCH 025/115] Prints --- src/cc/games/prices.c | 2 +- src/cc/makeprices | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index e125a1602..057e8be7f 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -138,7 +138,7 @@ cJSON *get_urljson(char *url,char *fname) uint64_t get_btcusd() { cJSON *pjson,*bpi,*usd; uint64_t btcusd = 0; - if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json","/tmp/oraclefeed.json")) != 0 ) + if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json",(char *)"/tmp/oraclefeed.json")) != 0 ) { if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { diff --git a/src/cc/makeprices b/src/cc/makeprices index ca8b80065..2779c1c5b 100755 --- a/src/cc/makeprices +++ b/src/cc/makeprices @@ -1,4 +1,6 @@ +echo pricescc.so gcc -O3 -DBUILD_GAMESCC -DBUILD_PRICES -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o pricescc.so cclib.cpp +echo prices cd games gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE -DBUILD_PRICES ../gamescc.cpp -lncurses -lcurl -o prices cd .. From 382dd5021874f14230ac7e448517f00563de1eef Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:49:50 -1100 Subject: [PATCH 026/115] Test --- src/cc/games/prices.c | 142 +++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 057e8be7f..fa8749605 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -31,6 +31,77 @@ void sleep_milli(int milliseconds) nanosleep(&ts, NULL); } +struct games_state globalR; +extern char Gametxidstr[]; +int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c); +uint64_t get_btcusd(); + +void *gamesiterate(struct games_state *rs) +{ + bool running = true; uint32_t eventid = 0; int64_t price; + if ( rs->guiflag != 0 || rs->sleeptime != 0 ) + { + } + while ( running != 0 ) + { + //running = tg_tick(rs,tg,move); + if ( rs->guiflag != 0 || rs->sleeptime != 0 ) + { + } + if ( rs->guiflag != 0 ) + { +#ifdef STANDALONE + sleep_milli(10000); + price = get_btcusd(); + fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/COIN); + /*if ( (counter++ % 10) == 0 ) + doupdate(); + c = games_readevent(rs); + if ( c <= 0x7f || skipcount == 0x3fff ) + { + if ( skipcount > 0 ) + issue_games_events(rs,Gametxidstr,eventid-skipcount,skipcount | 0x4000); + if ( c <= 0x7f ) + issue_games_events(rs,Gametxidstr,eventid,c); + if ( tg->level != prevlevel ) + { + flushkeystrokes(rs,0); + prevlevel = tg->level; + } + skipcount = 0; + } else skipcount++;*/ +#endif + } + else + { + if ( rs->replaydone != 0 ) + break; + if ( rs->sleeptime != 0 ) + { + sleep_milli(1); + } + /*if ( skipcount == 0 ) + { + c = games_readevent(rs); + //fprintf(stderr,"%04x score.%d level.%d\n",c,tg->points,tg->level); + if ( (c & 0x4000) == 0x4000 ) + { + skipcount = (c & 0x3fff); + c = 'S'; + } + } + if ( skipcount > 0 ) + skipcount--;*/ + } + eventid++; + } + return(0); +} + +#ifdef STANDALONE +#include +#include "dapps/dappstd.c" + char *nonportable_path(char *str) { int32_t i; @@ -150,77 +221,6 @@ uint64_t get_btcusd() return(btcusd); } -struct games_state globalR; -extern char Gametxidstr[]; -int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c); - -void *gamesiterate(struct games_state *rs) -{ - bool running = true; uint32_t eventid = 0; int64_t price; - if ( rs->guiflag != 0 || rs->sleeptime != 0 ) - { - } - while ( running != 0 ) - { - //running = tg_tick(rs,tg,move); - if ( rs->guiflag != 0 || rs->sleeptime != 0 ) - { - } - if ( rs->guiflag != 0 ) - { -#ifdef STANDALONE - sleep_milli(10000); - price = get_btcusd(); - fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/COIN); - /*if ( (counter++ % 10) == 0 ) - doupdate(); - c = games_readevent(rs); - if ( c <= 0x7f || skipcount == 0x3fff ) - { - if ( skipcount > 0 ) - issue_games_events(rs,Gametxidstr,eventid-skipcount,skipcount | 0x4000); - if ( c <= 0x7f ) - issue_games_events(rs,Gametxidstr,eventid,c); - if ( tg->level != prevlevel ) - { - flushkeystrokes(rs,0); - prevlevel = tg->level; - } - skipcount = 0; - } else skipcount++;*/ -#endif - } - else - { - if ( rs->replaydone != 0 ) - break; - if ( rs->sleeptime != 0 ) - { - sleep_milli(1); - } - /*if ( skipcount == 0 ) - { - c = games_readevent(rs); - //fprintf(stderr,"%04x score.%d level.%d\n",c,tg->points,tg->level); - if ( (c & 0x4000) == 0x4000 ) - { - skipcount = (c & 0x3fff); - c = 'S'; - } - } - if ( skipcount > 0 ) - skipcount--;*/ - } - eventid++; - } - return(0); -} - -#ifdef STANDALONE -#include -#include "dapps/dappstd.c" - - char *clonestr(char *str) { char *clone; int32_t len; From 498c813b674b21fd1557004cc029f2e65c0bf42c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:53:59 -1100 Subject: [PATCH 027/115] #include "rogue/cursesd.c" --- src/cc/cclib.cpp | 2 +- src/cc/games/prices.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 95748def4..975b0735c 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -691,8 +691,8 @@ int32_t cclib_parsehash(uint8_t *hash32,cJSON *item,int32_t len) #include "customcc.cpp" #elif BUILD_GAMESCC -#include "gamescc.cpp" #include "rogue/cursesd.c" +#include "gamescc.cpp" #else #include "sudoku.cpp" diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index fa8749605..b9da75fe2 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -53,7 +53,7 @@ void *gamesiterate(struct games_state *rs) #ifdef STANDALONE sleep_milli(10000); price = get_btcusd(); - fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/COIN); + fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/SATOSHIDEN); /*if ( (counter++ % 10) == 0 ) doupdate(); c = games_readevent(rs); @@ -184,9 +184,12 @@ void *filestr(long *allocsizep,char *_fname) char *send_curl(char *url,char *fname) { - long fsize; char curlstr[1024]; + long fsize; char curlstr[1024],*retstr,*retstr2; sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); - system(curlstr); + //retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,"","",""); + + if ( system(curlstr) != 0 ) + fprintf(stderr,"error doing system(%s)\n",curlstr); return((char *)filestr(&fsize,fname)); } From 75465c987cfbd6707f3a5438918ed12747a7ae1e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:55:08 -1100 Subject: [PATCH 028/115] SATOSHIDEN --- src/cc/games/prices.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index b9da75fe2..3d72965fc 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,6 +1,7 @@ #include "prices.h" #include +#define SATOSHIDEN 100000000 /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From 9f42c31c2a81872822e9944dd8d5fd82be943072 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:55:38 -1100 Subject: [PATCH 029/115] #define SATOSHIDEN ((uint64_t)100000000L) --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 3d72965fc..0ac0844c2 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,7 +1,7 @@ #include "prices.h" #include -#define SATOSHIDEN 100000000 +#define SATOSHIDEN ((uint64_t)100000000L) /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From 73e6a1cb6bb803cf529f51b1a0f6cd8083b7903a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Mar 2019 23:58:10 -1100 Subject: [PATCH 030/115] Fx --- src/cc/makecclib | 2 -- src/cc/maketetris | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index 8179623ad..81026e75a 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -9,11 +9,9 @@ echo sudoku/musig/dilithium gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o sudokucc.so cclib.cpp echo games tetris -gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp ./maketetris echo games prices -gcc -O3 -DBUILD_GAMESCC -DBUILD_PRICES -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o pricescc.so cclib.cpp ./makeprices echo customcc stub diff --git a/src/cc/maketetris b/src/cc/maketetris index f11a4de9c..c39536229 100755 --- a/src/cc/maketetris +++ b/src/cc/maketetris @@ -1,3 +1,6 @@ +echo gamescc.so with tetris +gcc -O3 -DBUILD_GAMESCC -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o gamescc.so cclib.cpp +echo tetris dapp cd games gcc -O3 -std=c++11 -I../secp256k1/include -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -DSTANDALONE ../gamescc.cpp -lncurses -lcurl -o tetris cd .. From f49f901f4f12d73b47915be3b9adb0ab526d3f11 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:06:18 -1100 Subject: [PATCH 031/115] Change Tetris washout --- src/cc/games/tetris.cpp | 4 ++-- src/cc/makecclib | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/games/tetris.cpp b/src/cc/games/tetris.cpp index 8a52dcb37..f1b6416ba 100644 --- a/src/cc/games/tetris.cpp +++ b/src/cc/games/tetris.cpp @@ -22,8 +22,8 @@ void games_packitemstr(char *packitemstr,struct games_packitem *item) int64_t games_cashout(struct games_player *P) { - int32_t dungeonlevel = P->dungeonlevel; int64_t mult=1000,cashout = 0; - cashout = (uint64_t)P->gold * mult * dungeonlevel * dungeonlevel; + int32_t dungeonlevel = P->dungeonlevel; int64_t mult=10000,cashout = 0; + cashout = (uint64_t)P->gold * mult; return(cashout); } diff --git a/src/cc/makecclib b/src/cc/makecclib index 81026e75a..b2f8e2ee1 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1,5 +1,5 @@ #!/bin/sh -rm *.so rogue/rogue tetris +rm *.so rogue/rogue games/tetris games/prices echo rogue make -f Makefile_rogue From 58be59a00d8876b492faeef0b176a09478ec704c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:10:17 -1100 Subject: [PATCH 032/115] Test --- src/cc/games/prices.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 0ac0844c2..658ed2406 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -185,13 +185,14 @@ void *filestr(long *allocsizep,char *_fname) char *send_curl(char *url,char *fname) { - long fsize; char curlstr[1024],*retstr,*retstr2; - sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); - //retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,"","",""); - - if ( system(curlstr) != 0 ) - fprintf(stderr,"error doing system(%s)\n",curlstr); - return((char *)filestr(&fsize,fname)); + //long fsize; char curlstr[1024]; + //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); + char *retstr=0,*retstr2; + retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,"","",""); + return(retstr2); + //if ( system(curlstr) != 0 ) + // fprintf(stderr,"error doing system(%s)\n",curlstr); + //return((char *)filestr(&fsize,fname)); } cJSON *get_urljson(char *url,char *fname) From 5bbe79b586a6ba832fd779806eed5ba8cc341d10 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:11:07 -1100 Subject: [PATCH 033/115] Sleep --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 658ed2406..8798fa68f 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -52,7 +52,7 @@ void *gamesiterate(struct games_state *rs) if ( rs->guiflag != 0 ) { #ifdef STANDALONE - sleep_milli(10000); + sleep(1); price = get_btcusd(); fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/SATOSHIDEN); /*if ( (counter++ % 10) == 0 ) From 73cc7ba642629343f8b7ef6e6553b3e19771c8b3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:11:31 -1100 Subject: [PATCH 034/115] (char *) --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 8798fa68f..32e7e509c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -188,7 +188,7 @@ char *send_curl(char *url,char *fname) //long fsize; char curlstr[1024]; //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); char *retstr=0,*retstr2; - retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,"","",""); + retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,(char *)"",(char *)"",(char *)""); return(retstr2); //if ( system(curlstr) != 0 ) // fprintf(stderr,"error doing system(%s)\n",curlstr); From e7373723f4693bcfcef5900cf4a847a764048f0e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:12:38 -1100 Subject: [PATCH 035/115] Units --- src/cc/games/prices.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 32e7e509c..41ddb8fd7 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -1,6 +1,7 @@ #include "prices.h" #include +#include #define SATOSHIDEN ((uint64_t)100000000L) /* @@ -52,7 +53,7 @@ void *gamesiterate(struct games_state *rs) if ( rs->guiflag != 0 ) { #ifdef STANDALONE - sleep(1); + sleep(10); price = get_btcusd(); fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/SATOSHIDEN); /*if ( (counter++ % 10) == 0 ) From e2527311f2bc8e8aa2d9bf6f18313b6556e1310f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:14:56 -1100 Subject: [PATCH 036/115] Test --- src/cc/games/prices.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 41ddb8fd7..339f3293c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -190,6 +190,10 @@ char *send_curl(char *url,char *fname) //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); char *retstr=0,*retstr2; retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,(char *)"",(char *)"",(char *)""); + if ( retstr2 != 0 ) + printf("retstr2 (%s)\n",retstr2); + if ( retstr != 0 ) + printf("retstr (%s)\n",retstr); return(retstr2); //if ( system(curlstr) != 0 ) // fprintf(stderr,"error doing system(%s)\n",curlstr); @@ -201,7 +205,7 @@ cJSON *get_urljson(char *url,char *fname) char *jsonstr; cJSON *json = 0; if ( (jsonstr= send_curl(url,fname)) != 0 ) { - //printf("(%s) -> (%s)\n",url,jsonstr); + printf("(%s) -> (%s)\n",url,jsonstr); json = cJSON_Parse(jsonstr); free(jsonstr); } From 63a02c688ab238141847f2ad93a0c0b1bebe7db4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:17:23 -1100 Subject: [PATCH 037/115] issue_curl --- src/cc/games/prices.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 339f3293c..6d3694d30 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -188,13 +188,11 @@ char *send_curl(char *url,char *fname) { //long fsize; char curlstr[1024]; //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); - char *retstr=0,*retstr2; - retstr2 = bitcoind_RPC(&retstr,(char *)"prices",url,(char *)"",(char *)"",(char *)""); - if ( retstr2 != 0 ) - printf("retstr2 (%s)\n",retstr2); + char *retstr; + retstr = issue_curl(url,10); if ( retstr != 0 ) printf("retstr (%s)\n",retstr); - return(retstr2); + return(retstr); //if ( system(curlstr) != 0 ) // fprintf(stderr,"error doing system(%s)\n",curlstr); //return((char *)filestr(&fsize,fname)); From 7d251f6f240333f7727384dc7fdd5dd390df4ed7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:18:30 -1100 Subject: [PATCH 038/115] #define issue_curl(cmdstr) bitcoind_RPC(0,"curl",cmdstr,0,0,0,0) --- src/cc/games/prices.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 6d3694d30..e45222c13 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -3,6 +3,7 @@ #include #include #define SATOSHIDEN ((uint64_t)100000000L) +#define issue_curl(cmdstr) bitcoind_RPC(0,"prices",cmdstr,0,0,0,0) /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. @@ -189,7 +190,7 @@ char *send_curl(char *url,char *fname) //long fsize; char curlstr[1024]; //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); char *retstr; - retstr = issue_curl(url,10); + retstr = issue_curl(url); if ( retstr != 0 ) printf("retstr (%s)\n",retstr); return(retstr); From 82850e26352730bfcbca46c608440764e65b4001 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:19:13 -1100 Subject: [PATCH 039/115] -0 --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index e45222c13..b5eca9c50 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -3,7 +3,7 @@ #include #include #define SATOSHIDEN ((uint64_t)100000000L) -#define issue_curl(cmdstr) bitcoind_RPC(0,"prices",cmdstr,0,0,0,0) +#define issue_curl(cmdstr) bitcoind_RPC(0,"prices",cmdstr,0,0,0) /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From a0667c2488b6c8bed921501b950623def355259d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:19:49 -1100 Subject: [PATCH 040/115] Char * --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index b5eca9c50..96b7b72e0 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -3,7 +3,7 @@ #include #include #define SATOSHIDEN ((uint64_t)100000000L) -#define issue_curl(cmdstr) bitcoind_RPC(0,"prices",cmdstr,0,0,0) +#define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"prices",cmdstr,0,0,0) /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. From a13b601d779caac72f7f98b579706df11317cece Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:20:17 -1100 Subject: [PATCH 041/115] sleep(10); --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 96b7b72e0..ff98da774 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -54,9 +54,9 @@ void *gamesiterate(struct games_state *rs) if ( rs->guiflag != 0 ) { #ifdef STANDALONE - sleep(10); price = get_btcusd(); fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/SATOSHIDEN); + sleep(10); /*if ( (counter++ % 10) == 0 ) doupdate(); c = games_readevent(rs); From ba37ac2c088950769b6e8665e398f2174f14ec66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:25:43 -1100 Subject: [PATCH 042/115] Encode price --- src/cc/games/prices.c | 98 ++++--------------------------------------- 1 file changed, 7 insertions(+), 91 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index ff98da774..4a95ef99e 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -55,7 +55,9 @@ void *gamesiterate(struct games_state *rs) { #ifdef STANDALONE price = get_btcusd(); - fprintf(stderr,"price %llu %.8f\n",(long long)price,(double)price/SATOSHIDEN); + fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); + issue_games_events(rs,Gametxidstr,eventid,price); + eventid++; sleep(10); /*if ( (counter++ % 10) == 0 ) doupdate(); @@ -105,98 +107,11 @@ void *gamesiterate(struct games_state *rs) #include #include "dapps/dappstd.c" -char *nonportable_path(char *str) -{ - int32_t i; - for (i=0; str[i]!=0; i++) - if ( str[i] == '/' ) - str[i] = '\\'; - return(str); -} - -char *portable_path(char *str) -{ -#ifdef _WIN32 - return(nonportable_path(str)); -#else -#ifdef __PNACL - /*int32_t i,n; - if ( str[0] == '/' ) - return(str); - else - { - n = (int32_t)strlen(str); - for (i=n; i>0; i--) - str[i] = str[i-1]; - str[0] = '/'; - str[n+1] = 0; - }*/ -#endif - return(str); -#endif -} - -void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) -{ - FILE *fp; - long filesize,buflen = *allocsizep; - uint8_t *buf = *bufp; - *lenp = 0; - if ( (fp= fopen(portable_path(fname),"rb")) != 0 ) - { - fseek(fp,0,SEEK_END); - filesize = ftell(fp); - if ( filesize == 0 ) - { - fclose(fp); - *lenp = 0; - //printf("loadfile null size.(%s)\n",fname); - return(0); - } - if ( filesize > buflen ) - { - *allocsizep = filesize; - *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); - } - rewind(fp); - if ( buf == 0 ) - printf("Null buf ???\n"); - else - { - if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) - printf("error reading filesize.%ld\n",(long)filesize); - buf[filesize] = 0; - } - fclose(fp); - *lenp = filesize; - //printf("loaded.(%s)\n",buf); - } //else printf("OS_loadfile couldnt load.(%s)\n",fname); - return(buf); -} - -void *filestr(long *allocsizep,char *_fname) -{ - long filesize = 0; char *fname,*buf = 0; void *retptr; - *allocsizep = 0; - fname = (char *)malloc(strlen(_fname)+1); - strcpy(fname,_fname); - retptr = loadfile(fname,(uint8_t **)&buf,&filesize,allocsizep); - free(fname); - return(retptr); -} - char *send_curl(char *url,char *fname) { - //long fsize; char curlstr[1024]; - //sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); char *retstr; retstr = issue_curl(url); - if ( retstr != 0 ) - printf("retstr (%s)\n",retstr); return(retstr); - //if ( system(curlstr) != 0 ) - // fprintf(stderr,"error doing system(%s)\n",curlstr); - //return((char *)filestr(&fsize,fname)); } cJSON *get_urljson(char *url,char *fname) @@ -204,7 +119,7 @@ cJSON *get_urljson(char *url,char *fname) char *jsonstr; cJSON *json = 0; if ( (jsonstr= send_curl(url,fname)) != 0 ) { - printf("(%s) -> (%s)\n",url,jsonstr); + //printf("(%s) -> (%s)\n",url,jsonstr); json = cJSON_Parse(jsonstr); free(jsonstr); } @@ -217,17 +132,18 @@ cJSON *get_urljson(char *url,char *fname) uint64_t get_btcusd() { - cJSON *pjson,*bpi,*usd; uint64_t btcusd = 0; + cJSON *pjson,*bpi,*usd; uint64_t x,btcusd = 0; if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json",(char *)"/tmp/oraclefeed.json")) != 0 ) { if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; + x = ((uint64_t)time(NULL) << 32) | (btcusd / 10000); printf("BTC/USD %.4f\n",dstr(btcusd)); } free_json(pjson); } - return(btcusd); + return(x); } char *clonestr(char *str) From adcf1042a1699b1f3980d5806200a421d4b43681 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:27:54 -1100 Subject: [PATCH 043/115] Broadcast --- src/cc/games/prices.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 4a95ef99e..7987c45a0 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -139,7 +139,7 @@ uint64_t get_btcusd() { btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; x = ((uint64_t)time(NULL) << 32) | (btcusd / 10000); - printf("BTC/USD %.4f\n",dstr(btcusd)); + //printf("BTC/USD %.4f\n",dstr(btcusd)); } free_json(pjson); } @@ -170,7 +170,7 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve if ( fp == 0 ) fp = fopen("events.log","wb"); rs->buffered[rs->num++] = c; - if ( 0 ) + if ( 1 ) { if ( sizeof(c) == 1 ) sprintf(params,"[\"events\",\"17\",\"[%%22%02x%%22,%%22%s%%22,%u]\"]",(uint8_t)c&0xff,gametxidstr,eventid); From 9e878493e9a7a55fea2ca44c019f2629d47b75af Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 00:32:04 -1100 Subject: [PATCH 044/115] +print --- src/cc/games/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index cb2de54a1..82e132c7f 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -59,9 +59,9 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+33] << 8; eventid |= (uint32_t)payload[len+34] << 16; eventid |= (uint32_t)payload[len+35] << 24; - //for (i=0; i Date: Wed, 27 Mar 2019 00:50:13 -1100 Subject: [PATCH 045/115] +prints --- src/cc/gamescc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 56fd13310..dc36cc757 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -553,14 +553,14 @@ void komodo_netevent(std::vector message) { if ( (rand() % 10) == 0 ) { - //fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); + fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); komodo_sendmessage(2,2,"events",message); } } } - //for (i=0; i Date: Wed, 27 Mar 2019 00:52:39 -1100 Subject: [PATCH 046/115] +print --- src/cc/gamescc.cpp | 4 +++- src/miner.cpp | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index dc36cc757..f04ffb22f 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -477,7 +477,9 @@ int32_t games_event(uint32_t timestamp,uint256 gametxid,int32_t eventid,std::vec games_payloadrecv(mypk,timestamp,payload); komodo_sendmessage(4,8,"events",vopret); return(0); - } else return(-1); + } + fprintf(stderr,"games_eventsign error\n"); + return(-1); } UniValue games_events(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) diff --git a/src/miner.cpp b/src/miner.cpp index eaba96ee1..ca4ce8714 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -941,6 +941,7 @@ void komodo_sendmessage(int32_t minpeers,int32_t maxpeers,const char *message,st continue; if ( numsent < minpeers || (rand() % 10) == 0 ) { + fprintf(stderr,"pushmessage\n"); pnode->PushMessage(message,payload); if ( numsent++ > maxpeers ) break; From 2bb01b26340a0e018e1241c5d34e81a2e99f17f8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:26:08 -1100 Subject: [PATCH 047/115] fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); --- src/cc/games/prices.cpp | 5 +++-- src/cc/gamescc.cpp | 6 +++--- src/miner.cpp | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 82e132c7f..4c9698593 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -49,7 +49,7 @@ int32_t disp_gamesplayer(char *str,struct games_player *P) int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector payload) { - uint256 gametxid; int32_t i,len; char str[67]; uint32_t eventid = 0; + uint256 gametxid; int32_t i,len; char str[67]; int64_t price; uint32_t eventid = 0; if ( (len= payload.size()) > 36 ) { len -= 36; @@ -60,7 +60,8 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+34] << 16; eventid |= (uint32_t)payload[len+35] << 24; for (i=0; i t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); return(0); } else return(-1); diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index f04ffb22f..17b0d371b 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -555,13 +555,13 @@ void komodo_netevent(std::vector message) { if ( (rand() % 10) == 0 ) { - fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); + //fprintf(stderr,"relay message.[%d]\n",(int32_t)message.size()); komodo_sendmessage(2,2,"events",message); } } } - for (i=0; iPushMessage(message,payload); if ( numsent++ > maxpeers ) break; From c9446180181a0d093e7af2ef9f319d0c611cc53b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:28:55 -1100 Subject: [PATCH 048/115] Test --- src/cc/games/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 4c9698593..3f71c786c 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -59,8 +59,8 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+33] << 8; eventid |= (uint32_t)payload[len+34] << 16; eventid |= (uint32_t)payload[len+35] << 24; - for (i=0; i t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); return(0); From c3e822b232a59a0255cbc8fc4856b3ffd5ff2197 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:37:10 -1100 Subject: [PATCH 049/115] prices_update --- src/cc/games/prices.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 3f71c786c..b9f9bfc76 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,6 +14,12 @@ * * ******************************************************************************/ + +void prices_update(uint32_t timestamp,uint32_t uprice) +{ + fprintf(stderr,"%t%u %.4f ",timstamp,uprice); +} + // game specific code for daemon void games_packitemstr(char *packitemstr,struct games_packitem *item) { @@ -61,8 +67,9 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+35] << 24; for (i=0; i t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); - fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); + prices_update((uint32_t)(price >> 32),(uint32_t)(price & 0xffffffff)/10000); + //fprintf(stderr,"%llu -> t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); + //fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); return(0); } else return(-1); } From c428d5b8cb86f09cc4c0b6d074f0707b4d472dfe Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:39:18 -1100 Subject: [PATCH 050/115] Timestamp --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index b9f9bfc76..a103c5f70 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,7 +17,7 @@ void prices_update(uint32_t timestamp,uint32_t uprice) { - fprintf(stderr,"%t%u %.4f ",timstamp,uprice); + fprintf(stderr,"%t%u %.4f ",timestamp,uprice); } // game specific code for daemon From 1b86b9a1a36644bb5795dfb0cc7c172b5addfe21 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:39:54 -1100 Subject: [PATCH 051/115] Fix --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index a103c5f70..c57fd5c36 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,7 +17,7 @@ void prices_update(uint32_t timestamp,uint32_t uprice) { - fprintf(stderr,"%t%u %.4f ",timestamp,uprice); + fprintf(stderr,"%t%u %.4f\n",timestamp,(double)uprice/1000); } // game specific code for daemon From 837200f4eec248e53c87c9ec7f8e93edd17655bb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:40:41 -1100 Subject: [PATCH 052/115] -% --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index c57fd5c36..f042b6b1f 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,7 +17,7 @@ void prices_update(uint32_t timestamp,uint32_t uprice) { - fprintf(stderr,"%t%u %.4f\n",timestamp,(double)uprice/1000); + fprintf(stderr,"t%u %.4f\n",timestamp,(double)uprice/1000); } // game specific code for daemon From 76bcf303657bec09079e52fa615a985bbd2a28e3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:42:07 -1100 Subject: [PATCH 053/115] -/10000 --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index f042b6b1f..29ca9759e 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -67,7 +67,7 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+35] << 24; for (i=0; i> 32),(uint32_t)(price & 0xffffffff)/10000); + prices_update((uint32_t)(price >> 32),(uint32_t)(price & 0xffffffff)); //fprintf(stderr,"%llu -> t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); //fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); return(0); From 1717fb6d87d5203149bfd239fc062a47d11b496b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:42:56 -1100 Subject: [PATCH 054/115] -print --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 7987c45a0..ed3dae051 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -55,7 +55,7 @@ void *gamesiterate(struct games_state *rs) { #ifdef STANDALONE price = get_btcusd(); - fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); + //fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); issue_games_events(rs,Gametxidstr,eventid,price); eventid++; sleep(10); From c993184197994d899a33073d3ef5b4b51c62168e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:45:39 -1100 Subject: [PATCH 055/115] 10000 --- src/cc/games/prices.c | 2 +- src/cc/games/prices.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index ed3dae051..67e66bfed 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -138,7 +138,7 @@ uint64_t get_btcusd() if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; - x = ((uint64_t)time(NULL) << 32) | (btcusd / 10000); + x = ((uint64_t)time(NULL) << 32) | ((btcusd / 10000) & 0xffffffff); //printf("BTC/USD %.4f\n",dstr(btcusd)); } free_json(pjson); diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 29ca9759e..7428dfb3b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,7 +17,7 @@ void prices_update(uint32_t timestamp,uint32_t uprice) { - fprintf(stderr,"t%u %.4f\n",timestamp,(double)uprice/1000); + fprintf(stderr,"t%u %.4f\n",timestamp,(double)uprice/10000); } // game specific code for daemon From a2d44907bd3009b333ba75bd37fd60a2b57afc4f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 01:48:14 -1100 Subject: [PATCH 056/115] Is mine --- src/cc/games/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 7428dfb3b..d3f74a287 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -15,9 +15,9 @@ ******************************************************************************/ -void prices_update(uint32_t timestamp,uint32_t uprice) +void prices_update(uint32_t timestamp,uint32_t uprice,int32_t ismine) { - fprintf(stderr,"t%u %.4f\n",timestamp,(double)uprice/10000); + fprintf(stderr,"%s t%u %.4f\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000); } // game specific code for daemon @@ -67,7 +67,7 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay eventid |= (uint32_t)payload[len+35] << 24; for (i=0; i> 32),(uint32_t)(price & 0xffffffff)); + prices_update((uint32_t)(price >> 32),(uint32_t)(price & 0xffffffff),pk == pubkey2pk(Mypubkey())); //fprintf(stderr,"%llu -> t%u %.4f ",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); //fprintf(stderr," got payload, from %s %s/e%d\n",pubkey33_str(str,(uint8_t *)&pk),gametxid.GetHex().c_str(),eventid); return(0); From 3d425a23fd790bf83592471e1914ba16cf4ef4c6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:07:01 -1100 Subject: [PATCH 057/115] Net_change --- src/cc/games/prices.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 67e66bfed..421fb8ff0 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -4,6 +4,7 @@ #include #define SATOSHIDEN ((uint64_t)100000000L) #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"prices",cmdstr,0,0,0) +extern int64_t Net_change; /* In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. @@ -59,6 +60,11 @@ void *gamesiterate(struct games_state *rs) issue_games_events(rs,Gametxidstr,eventid,price); eventid++; sleep(10); + switch ( getch() ) + { + case '+': Net_change++; break; + case '-': Net_change--; break; + } /*if ( (counter++ % 10) == 0 ) doupdate(); c = games_readevent(rs); @@ -106,6 +112,7 @@ void *gamesiterate(struct games_state *rs) #ifdef STANDALONE #include #include "dapps/dappstd.c" +int64_t Net_change; char *send_curl(char *url,char *fname) { @@ -132,14 +139,16 @@ cJSON *get_urljson(char *url,char *fname) uint64_t get_btcusd() { - cJSON *pjson,*bpi,*usd; uint64_t x,btcusd = 0; + cJSON *pjson,*bpi,*usd; uint64_t x,newprice,mult,btcusd = 0; if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json",(char *)"/tmp/oraclefeed.json")) != 0 ) { if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; - x = ((uint64_t)time(NULL) << 32) | ((btcusd / 10000) & 0xffffffff); - //printf("BTC/USD %.4f\n",dstr(btcusd)); + mult = SATOSHIDEN + Net_change*100000; + newprice = (btcusd * mult) / SATOSHIDEN; + x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); + printf("BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); } free_json(pjson); } From 9363ef04111d567bf316ee2def799a57ad5edce2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:10:14 -1100 Subject: [PATCH 058/115] cursesd --- src/cc/games/prices.c | 6 ++++++ src/cc/games/tetris.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 421fb8ff0..9d3014fdb 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -2,6 +2,12 @@ #include "prices.h" #include #include +#ifdef BUILD_GAMESCC +#include "../rogue/cursesd.h" +#else +#include +#endif + #define SATOSHIDEN ((uint64_t)100000000L) #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"prices",cmdstr,0,0,0) extern int64_t Net_change; diff --git a/src/cc/games/tetris.c b/src/cc/games/tetris.c index 254d324a8..711170b0d 100644 --- a/src/cc/games/tetris.c +++ b/src/cc/games/tetris.c @@ -43,7 +43,7 @@ int32_t tetrisdata(struct games_player *P,void *ptr) #include #ifdef BUILD_GAMESCC -#include "rogue/cursesd.h" +#include "../rogue/cursesd.h" #else #include #endif From f3123a4d5b40a205fff13664dc188c02cddf52cc Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:12:25 -1100 Subject: [PATCH 059/115] Test --- src/cc/games/prices.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 9d3014fdb..0856eaa32 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -151,8 +151,8 @@ uint64_t get_btcusd() if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) { btcusd = jdouble(usd,(char *)"rate_float") * SATOSHIDEN; - mult = SATOSHIDEN + Net_change*100000; - newprice = (btcusd * mult) / SATOSHIDEN; + mult = 10000 + Net_change*10; + newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); printf("BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); } From 3da222eae80b71ed44de24f4f8e1deba536554b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:14:33 -1100 Subject: [PATCH 060/115] Activate curses --- src/cc/games/prices.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 0856eaa32..b0a16a3cc 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -51,6 +51,9 @@ void *gamesiterate(struct games_state *rs) bool running = true; uint32_t eventid = 0; int64_t price; if ( rs->guiflag != 0 || rs->sleeptime != 0 ) { + initscr(); // initialize curses + cbreak(); // pass key presses to program, but not signals + noecho(); // don't echo key presses to screen } while ( running != 0 ) { From 86491b25f022a553a27fdc371fdeb53f95fa7eb8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:18:33 -1100 Subject: [PATCH 061/115] Test --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index b0a16a3cc..7d3daef1f 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -157,7 +157,7 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - printf("BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); + fprintf(stderr,"BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); } free_json(pjson); } From d87dcb4d44d197b5feafebbc85c0a67291650053 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:25:14 -1100 Subject: [PATCH 062/115] Display --- src/cc/games/prices.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 7d3daef1f..b6bb6a640 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -148,7 +148,7 @@ cJSON *get_urljson(char *url,char *fname) uint64_t get_btcusd() { - cJSON *pjson,*bpi,*usd; uint64_t x,newprice,mult,btcusd = 0; + cJSON *pjson,*bpi,*usd; char str[512]; uint64_t x,newprice,mult,btcusd = 0; if ( (pjson= get_urljson((char *)"http://api.coindesk.com/v1/bpi/currentprice.json",(char *)"/tmp/oraclefeed.json")) != 0 ) { if ( (bpi= jobj(pjson,(char *)"bpi")) != 0 && (usd= jobj(bpi,(char *)"USD")) != 0 ) @@ -157,7 +157,10 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - fprintf(stderr,"BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); + sprintf(str,"BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); + mvaddstr(0, 0, str); + clrtoeol(); + refresh(); } free_json(pjson); } From 829e9811647042f87441b4a2ea1c167f070c5c72 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:27:58 -1100 Subject: [PATCH 063/115] doupdate --- src/cc/games/prices.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index b6bb6a640..3ac7e76fe 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -68,6 +68,7 @@ void *gamesiterate(struct games_state *rs) //fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); issue_games_events(rs,Gametxidstr,eventid,price); eventid++; + doupdate(); sleep(10); switch ( getch() ) { @@ -160,7 +161,7 @@ uint64_t get_btcusd() sprintf(str,"BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); mvaddstr(0, 0, str); clrtoeol(); - refresh(); + doupdate(); } free_json(pjson); } From 6c14369eac9c83af9fab18b9527d1b542728193c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:44:28 -1100 Subject: [PATCH 064/115] timeout(0); --- src/cc/games/prices.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 3ac7e76fe..97c617be7 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -54,6 +54,7 @@ void *gamesiterate(struct games_state *rs) initscr(); // initialize curses cbreak(); // pass key presses to program, but not signals noecho(); // don't echo key presses to screen + timeout(0); } while ( running != 0 ) { From 46bd37f80083ca7fb5ab27e6d87ff56585935d55 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 02:50:42 -1100 Subject: [PATCH 065/115] -print --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index d3f74a287..9db720907 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,7 +17,7 @@ void prices_update(uint32_t timestamp,uint32_t uprice,int32_t ismine) { - fprintf(stderr,"%s t%u %.4f\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000); + //fprintf(stderr,"%s t%u %.4f\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000); } // game specific code for daemon From 89913fa6ae498be1d9bbe96dcf86694182a0e579 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 03:46:03 -1100 Subject: [PATCH 066/115] pricedata --- src/cc/games/prices.c | 6 ------ src/cc/games/prices.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/cc/games/tetris.cpp | 6 ++++++ src/cc/gamescc.cpp | 2 +- src/cc/gamescc.h | 6 +++++- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 97c617be7..3d4a65e14 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -12,12 +12,6 @@ #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"prices",cmdstr,0,0,0) extern int64_t Net_change; -/* - In order to port a game into gamesCC, the RNG needs to be seeded with the gametxid seed, also events needs to be broadcast using issue_games_events. Also the game engine needs to be daemonized, preferably by putting all globals into a single data structure. - - also, the standalone game needs to support argv of seed gametxid, along with replay args - */ - int random_tetromino(struct games_state *rs) { rs->seed = _games_rngnext(rs->seed); diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 9db720907..a903355e4 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,10 +14,48 @@ * * ******************************************************************************/ +UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t inputsum,price; CPubKey mypk; + if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + { + if ( cclib_parsehash(&price,jitem(params,0),8) < 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt parsehash")); + } + mypk = pubkey2pk(Mypubkey()); + if ( amount > GAMES_TXFEE ) + { + if ( (inputsum= AddNormalinputs(mtx,mypk,GAMES_TXFEE,64)) >= GAMES_TXFEE ) + { + rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); + return(games_rawtxresult(result,rawtx,1)); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough funds")); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","amount too small")); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt parse")); + } + return(result); +} void prices_update(uint32_t timestamp,uint32_t uprice,int32_t ismine) { - //fprintf(stderr,"%s t%u %.4f\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000); + fprintf(stderr,"%s t%u %.4f %16llx\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000,(long long)(timestamp<<32) | uprice); } // game specific code for daemon diff --git a/src/cc/games/tetris.cpp b/src/cc/games/tetris.cpp index f1b6416ba..12c443e1e 100644 --- a/src/cc/games/tetris.cpp +++ b/src/cc/games/tetris.cpp @@ -66,6 +66,12 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay } else return(-1); } +UniValue games_oracledata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + UniValue result; + return(result); +} + bool games_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx) { return(true); diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 17b0d371b..77b463822 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -1731,7 +1731,7 @@ UniValue games_fund(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,opret); - return(games_rawtxresult(result,rawtx,1)); + return(games_rawtxresult(resulft,rawtx,1)); } else { diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 63d029b0a..19f3f66cf 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -46,6 +46,7 @@ std::string Games_pname; { (char *)MYCCNAME, (char *)"highlander", (char *)"gametxid", 1, 1, 'H', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"events", (char *)"eventshex [gametxid [eventid]]", 1, 3, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"extract", (char *)"gametxid [pubkey]", 1, 2, ' ', EVAL_GAMES }, \ + { (char *)MYCCNAME, (char *)"pricedata", (char *)"hexstr", 1, 1, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"register", (char *)"gametxid [playertxid]", 1, 2, 'R', EVAL_GAMES }, bool games_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx); @@ -65,6 +66,7 @@ UniValue games_highlander(uint64_t txfee,struct CCcontract_info *cp,cJSON *param UniValue games_events(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_register(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); #define CUSTOM_DISPATCH \ if ( cp->evalcode == EVAL_GAMES ) \ @@ -100,7 +102,9 @@ if ( cp->evalcode == EVAL_GAMES ) \ else if ( strcmp(method,"highlander") == 0 ) \ return(games_highlander(txfee,cp,params)); \ else if ( strcmp(method,"fund") == 0 ) \ - return(games_fund(txfee,cp,params)); \ + return(games_fund(txfee,cp,params)); \ + else if ( strcmp(method,"pricedata") == 0 ) \ + return(games_pricedata(txfee,cp,params)); \ else \ { \ result.push_back(Pair("result","error")); \ From 9e4f5663da07dcfcafbdf12b172da00263bc385c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 03:49:23 -1100 Subject: [PATCH 067/115] Test --- src/cc/games/prices.cpp | 22 ++++++++-------------- src/cc/gamescc.cpp | 2 +- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index a903355e4..88fdab88a 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,35 +14,29 @@ * * ******************************************************************************/ +UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); + UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); UniValue result(UniValue::VOBJ); std::string rawtx; int64_t inputsum,price; CPubKey mypk; if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { - if ( cclib_parsehash(&price,jitem(params,0),8) < 0 ) + if ( cclib_parsehash((uint8_t *)&price,jitem(params,0),8) < 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parsehash")); } mypk = pubkey2pk(Mypubkey()); - if ( amount > GAMES_TXFEE ) + if ( (inputsum= AddNormalinputs(mtx,mypk,GAMES_TXFEE,64)) >= GAMES_TXFEE ) { - if ( (inputsum= AddNormalinputs(mtx,mypk,GAMES_TXFEE,64)) >= GAMES_TXFEE ) - { - rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); - return(games_rawtxresult(result,rawtx,1)); - } - else - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","not enough funds")); - } + rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); + return(games_rawtxresult(result,rawtx,1)); } else { result.push_back(Pair("result","error")); - result.push_back(Pair("error","amount too small")); + result.push_back(Pair("error","not enough funds")); } } else @@ -55,7 +49,7 @@ UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params void prices_update(uint32_t timestamp,uint32_t uprice,int32_t ismine) { - fprintf(stderr,"%s t%u %.4f %16llx\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000,(long long)(timestamp<<32) | uprice); + fprintf(stderr,"%s t%u %.4f %16llx\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000,(long long)((uint64_t)timestamp<<32) | uprice); } // game specific code for daemon diff --git a/src/cc/gamescc.cpp b/src/cc/gamescc.cpp index 77b463822..17b0d371b 100644 --- a/src/cc/gamescc.cpp +++ b/src/cc/gamescc.cpp @@ -1731,7 +1731,7 @@ UniValue games_fund(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,opret); - return(games_rawtxresult(resulft,rawtx,1)); + return(games_rawtxresult(result,rawtx,1)); } else { From c0798791e9d99b235d4efcf493a18bf9ee064d00 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:05:10 -1100 Subject: [PATCH 068/115] Bet --- src/cc/games/prices.cpp | 11 +++++++---- src/cc/gamescc.h | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 88fdab88a..03fcff33a 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -19,17 +19,20 @@ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastf UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t inputsum,price; CPubKey mypk; - if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey mypk; + if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { - if ( cclib_parsehash((uint8_t *)&price,jitem(params,0),8) < 0 ) + amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; + if ( cclib_parsehash((uint8_t *)&price,jitem(params,1),8) < 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parsehash")); } mypk = pubkey2pk(Mypubkey()); - if ( (inputsum= AddNormalinputs(mtx,mypk,GAMES_TXFEE,64)) >= GAMES_TXFEE ) + gamespk = GetUnspendable(cp,0); + if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); return(games_rawtxresult(result,rawtx,1)); } diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 19f3f66cf..bfaf036d0 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -46,7 +46,7 @@ std::string Games_pname; { (char *)MYCCNAME, (char *)"highlander", (char *)"gametxid", 1, 1, 'H', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"events", (char *)"eventshex [gametxid [eventid]]", 1, 3, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"extract", (char *)"gametxid [pubkey]", 1, 2, ' ', EVAL_GAMES }, \ - { (char *)MYCCNAME, (char *)"pricedata", (char *)"hexstr", 1, 1, ' ', EVAL_GAMES }, \ + { (char *)MYCCNAME, (char *)"bet", (char *)"amount hexstr", 2, 2, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"register", (char *)"gametxid [playertxid]", 1, 2, 'R', EVAL_GAMES }, bool games_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx); @@ -66,7 +66,7 @@ UniValue games_highlander(uint64_t txfee,struct CCcontract_info *cp,cJSON *param UniValue games_events(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_register(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); -UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); #define CUSTOM_DISPATCH \ if ( cp->evalcode == EVAL_GAMES ) \ @@ -103,8 +103,8 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_highlander(txfee,cp,params)); \ else if ( strcmp(method,"fund") == 0 ) \ return(games_fund(txfee,cp,params)); \ - else if ( strcmp(method,"pricedata") == 0 ) \ - return(games_pricedata(txfee,cp,params)); \ + else if ( strcmp(method,"bet") == 0 ) \ + return(games_bet(txfee,cp,params)); \ else \ { \ result.push_back(Pair("result","error")); \ From 29fc5b9ea069e79f0477619c26fb587e79fa3caa Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:05:50 -1100 Subject: [PATCH 069/115] Gamespk --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 03fcff33a..27d41ca9b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -19,7 +19,7 @@ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastf UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey mypk; + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey gamespk,mypk; if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; From 8e4a613eb06e25db6e32d97edb67df763b7d36c3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:06:39 -1100 Subject: [PATCH 070/115] games_bet --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 27d41ca9b..e099d57af 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -16,7 +16,7 @@ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); -UniValue games_pricedata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey gamespk,mypk; From 7c1c706f18a7062bae09d59df1754e8afae85a77 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:10:39 -1100 Subject: [PATCH 071/115] Test --- src/cc/games/prices.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index e099d57af..296d80a8c 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -28,6 +28,7 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parsehash")); } + fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) From 3de3e931bb5afee45f9cd1369676d3cbb6a9c648 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:12:41 -1100 Subject: [PATCH 072/115] Test --- src/cc/gamescc.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index bfaf036d0..52627cd1c 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -109,7 +109,6 @@ if ( cp->evalcode == EVAL_GAMES ) \ { \ result.push_back(Pair("result","error")); \ result.push_back(Pair("error","invalid gamescc method")); \ - result.push_back(Pair("method",method)); \ return(result); \ } \ } From 2bf9af5f57333ddfb49df056d89e5a2dd8949dfc Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:13:19 -1100 Subject: [PATCH 073/115] Test --- src/cc/gamescc.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 52627cd1c..af888ed05 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -71,6 +71,7 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); #define CUSTOM_DISPATCH \ if ( cp->evalcode == EVAL_GAMES ) \ { \ + UniValue res; \ if ( strcmp(method,"rng") == 0 ) \ return(games_rng(txfee,cp,params)); \ else if ( strcmp(method,"rngnext") == 0 ) \ @@ -107,9 +108,9 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_bet(txfee,cp,params)); \ else \ { \ - result.push_back(Pair("result","error")); \ - result.push_back(Pair("error","invalid gamescc method")); \ - return(result); \ + res.push_back(Pair("result","error")); \ + res.push_back(Pair("error","invalid gamescc method")); \ + return(res); \ } \ } #endif From 2660c2b7f8d43f10c13ea003e73505258c8fc4de Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:50:17 -1100 Subject: [PATCH 074/115] issue_bet --- src/cc/games/prices.c | 34 ++++++++++++++++++++++++++++++---- src/cc/gamescc.h | 7 +++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 3d4a65e14..52ddc477c 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -10,7 +10,7 @@ #define SATOSHIDEN ((uint64_t)100000000L) #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"prices",cmdstr,0,0,0) -extern int64_t Net_change; +extern int64_t Net_change,Betsize; int random_tetromino(struct games_state *rs) { @@ -39,6 +39,7 @@ struct games_state globalR; extern char Gametxidstr[]; int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eventid,gamesevent c); uint64_t get_btcusd(); +int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize); void *gamesiterate(struct games_state *rs) { @@ -61,7 +62,8 @@ void *gamesiterate(struct games_state *rs) #ifdef STANDALONE price = get_btcusd(); //fprintf(stderr,"%llu -> t%u %.4f\n",(long long)price,(uint32_t)(price >> 32),(double)(price & 0xffffffff)/10000); - issue_games_events(rs,Gametxidstr,eventid,price); + //issue_games_events(rs,Gametxidstr,eventid,price); + issue_bet(rs,price,Betsize); eventid++; doupdate(); sleep(10); @@ -69,6 +71,10 @@ void *gamesiterate(struct games_state *rs) { case '+': Net_change++; break; case '-': Net_change--; break; + case '0': Net_change = 0; break; + case '$': Betsize = SATOSHIDEN; break; + case '^': Betsize += (Betsize >> 3); break; + case '/': Betsize -= (Betsize >> 3); break; } /*if ( (counter++ % 10) == 0 ) doupdate(); @@ -117,7 +123,7 @@ void *gamesiterate(struct games_state *rs) #ifdef STANDALONE #include #include "dapps/dappstd.c" -int64_t Net_change; +int64_t Net_change,Betsize; char *send_curl(char *url,char *fname) { @@ -153,7 +159,7 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - sprintf(str,"BTC/USD %.4f Net_change %lld * 0.001 -> %.4f\n",dstr(btcusd),(long long)Net_change,dstr(newprice)); + sprintf(str,"BTC/USD %.4f Net_change %lld * 0.001 -> Betsize %.8f %.4f\n",dstr(btcusd),(long long)Net_change,dstr(Betsize),dstr(newprice)); mvaddstr(0, 0, str); clrtoeol(); doupdate(); @@ -218,6 +224,26 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve } else return(0); } +int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize) +{ + char params[512],hexstr[64],*retstr; cJSON *retjson,*resobj; int32_t retval = -1; + sprintf(params,"[\"bet\",\"17\",\"[%.8f,%%22%s%%22]\"]",dstr(x),hexstr); + if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (resobj= jobj(retjson,(char *)"result")) != 0 ) + { + retval = 0; + fprintf(stderr,"%s\n",jprint(resobj,0)); + } + free_json(retjson); + } + free(retstr); + } + return(retval); +} + int prices(int argc, char **argv) { struct games_state *rs = &globalR; diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index af888ed05..52627cd1c 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -71,7 +71,6 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); #define CUSTOM_DISPATCH \ if ( cp->evalcode == EVAL_GAMES ) \ { \ - UniValue res; \ if ( strcmp(method,"rng") == 0 ) \ return(games_rng(txfee,cp,params)); \ else if ( strcmp(method,"rngnext") == 0 ) \ @@ -108,9 +107,9 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_bet(txfee,cp,params)); \ else \ { \ - res.push_back(Pair("result","error")); \ - res.push_back(Pair("error","invalid gamescc method")); \ - return(res); \ + result.push_back(Pair("result","error")); \ + result.push_back(Pair("error","invalid gamescc method")); \ + return(result); \ } \ } #endif From 72922243f989904eb2cbe97bc60dce2d2bf52d0b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:53:38 -1100 Subject: [PATCH 075/115] Fix --- src/cc/games/prices.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 52ddc477c..93c89ab83 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -227,7 +227,13 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize) { char params[512],hexstr[64],*retstr; cJSON *retjson,*resobj; int32_t retval = -1; - sprintf(params,"[\"bet\",\"17\",\"[%.8f,%%22%s%%22]\"]",dstr(x),hexstr); + memset(hexstr,0,sizeof(hexstr)); + for (i=0; i<8; i++) + { + sprintf(&hexstr[i<<1],"%02x",x & 0xff); + x >>= 8; + } + sprintf(params,"[\"bet\",\"17\",\"[%.8f,%%22%s%%22]\"]",dstr(betsize),hexstr); if ( (retstr= komodo_issuemethod(USERPASS,(char *)"cclib",params,GAMES_PORT)) != 0 ) { if ( (retjson= cJSON_Parse(retstr)) != 0 ) From 251d6fb88f962c9b4fbaf165b42312d4138f778e Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:54:29 -1100 Subject: [PATCH 076/115] I --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 93c89ab83..13b0c4934 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -226,7 +226,7 @@ int32_t issue_games_events(struct games_state *rs,char *gametxidstr,uint32_t eve int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize) { - char params[512],hexstr[64],*retstr; cJSON *retjson,*resobj; int32_t retval = -1; + char params[512],hexstr[64],*retstr; cJSON *retjson,*resobj; int32_t i,retval = -1; memset(hexstr,0,sizeof(hexstr)); for (i=0; i<8; i++) { From 05b91ae06385927e506d040f330a5148057e2d79 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 04:55:34 -1100 Subject: [PATCH 077/115] Test --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 13b0c4934..f33838c64 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -230,7 +230,7 @@ int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize) memset(hexstr,0,sizeof(hexstr)); for (i=0; i<8; i++) { - sprintf(&hexstr[i<<1],"%02x",x & 0xff); + sprintf(&hexstr[i<<1],"%02x",(uint8_t)(x & 0xff)); x >>= 8; } sprintf(params,"[\"bet\",\"17\",\"[%.8f,%%22%s%%22]\"]",dstr(betsize),hexstr); From dbb8f0acc20634c44c6b5378f5ef5d35b349a9b8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 06:02:13 -1100 Subject: [PATCH 078/115] Test --- src/cc/games/prices.c | 6 +++--- src/cc/games/prices.cpp | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index f33838c64..271271a67 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -123,7 +123,7 @@ void *gamesiterate(struct games_state *rs) #ifdef STANDALONE #include #include "dapps/dappstd.c" -int64_t Net_change,Betsize; +int64_t Net_change,Betsize = SATOSHIDEN; char *send_curl(char *url,char *fname) { @@ -159,7 +159,7 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - sprintf(str,"BTC/USD %.4f Net_change %lld * 0.001 -> Betsize %.8f %.4f\n",dstr(btcusd),(long long)Net_change,dstr(Betsize),dstr(newprice)); + sprintf(str,"BTC/USD %.4f -> Betsize %.8f (^ / to change) && %.4f [+ - to change]\n",dstr(btcusd),dstr(Betsize),dstr(newprice)); mvaddstr(0, 0, str); clrtoeol(); doupdate(); @@ -241,7 +241,7 @@ int32_t issue_bet(struct games_state *rs,int64_t x,int64_t betsize) if ( (resobj= jobj(retjson,(char *)"result")) != 0 ) { retval = 0; - fprintf(stderr,"%s\n",jprint(resobj,0)); + //fprintf(stderr,"%s\n",jprint(resobj,0)); } free_json(retjson); } diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 296d80a8c..a49fc1982 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -15,11 +15,18 @@ ******************************************************************************/ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); +extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey gamespk,mypk; + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey gamespk,mypk,acpk; + if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error"," no -ac_pubkey for price reference")); + } + acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; @@ -28,7 +35,9 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parsehash")); } - fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); + if ( mypk == acpk ) + amount = 0; // i am the reference price feed + //fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) @@ -53,7 +62,7 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) void prices_update(uint32_t timestamp,uint32_t uprice,int32_t ismine) { - fprintf(stderr,"%s t%u %.4f %16llx\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000,(long long)((uint64_t)timestamp<<32) | uprice); + //fprintf(stderr,"%s t%u %.4f %16llx\n",ismine!=0?"mine":"ext ",timestamp,(double)uprice/10000,(long long)((uint64_t)timestamp<<32) | uprice); } // game specific code for daemon From f18867055dddeea88e1ce5e880c8f75e292e73c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 06:45:23 -1100 Subject: [PATCH 079/115] Test --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 271271a67..99b95f7c2 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -159,7 +159,7 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - sprintf(str,"BTC/USD %.4f -> Betsize %.8f (^ / to change) && %.4f [+ - to change]\n",dstr(btcusd),dstr(Betsize),dstr(newprice)); + sprintf(str,"BTC/USD %.4f -> Betsize %.8f (^ / to change) && %.4f Net %.1f%% [+ - to change]\n",dstr(btcusd),dstr(Betsize),dstr(newprice),(double)100*mult/10000); mvaddstr(0, 0, str); clrtoeol(); doupdate(); From 2dfd8dacdb4f192f70523c5bc83ff8fb069adac6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 06:47:03 -1100 Subject: [PATCH 080/115] Display perc --- src/cc/games/prices.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.c b/src/cc/games/prices.c index 99b95f7c2..1bc62533e 100644 --- a/src/cc/games/prices.c +++ b/src/cc/games/prices.c @@ -159,7 +159,7 @@ uint64_t get_btcusd() mult = 10000 + Net_change*10; newprice = (btcusd * mult) / 10000; x = ((uint64_t)time(NULL) << 32) | ((newprice / 10000) & 0xffffffff); - sprintf(str,"BTC/USD %.4f -> Betsize %.8f (^ / to change) && %.4f Net %.1f%% [+ - to change]\n",dstr(btcusd),dstr(Betsize),dstr(newprice),(double)100*mult/10000); + sprintf(str,"BTC/USD %.4f -> Betsize %.8f (^ / to change) && %.4f Net %.1f%% [+ - to change]\n",dstr(btcusd),dstr(Betsize),dstr(newprice),(double)100*(mult-10000)/10000); mvaddstr(0, 0, str); clrtoeol(); doupdate(); From 7596077f2a3fbe657dc6897906cf1d0d9c20cfc7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:10:30 -1100 Subject: [PATCH 081/115] Settle stub --- src/cc/games/prices.cpp | 7 +++++++ src/cc/games/tetris.cpp | 8 +++++++- src/cc/gamescc.h | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index a49fc1982..4098a3980 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -17,6 +17,13 @@ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; + +UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + UniValue result; + return(result); +} + UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); diff --git a/src/cc/games/tetris.cpp b/src/cc/games/tetris.cpp index 12c443e1e..d1ca9c30b 100644 --- a/src/cc/games/tetris.cpp +++ b/src/cc/games/tetris.cpp @@ -66,7 +66,13 @@ int32_t games_payloadrecv(CPubKey pk,uint32_t timestamp,std::vector pay } else return(-1); } -UniValue games_oracledata(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + UniValue result; + return(result); +} + +UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result; return(result); diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 52627cd1c..7a6b7c66c 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -47,6 +47,7 @@ std::string Games_pname; { (char *)MYCCNAME, (char *)"events", (char *)"eventshex [gametxid [eventid]]", 1, 3, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"extract", (char *)"gametxid [pubkey]", 1, 2, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"bet", (char *)"amount hexstr", 2, 2, ' ', EVAL_GAMES }, \ + { (char *)MYCCNAME, (char *)"settle", (char *)"height", 1, 1, ' ', EVAL_GAMES }, \ { (char *)MYCCNAME, (char *)"register", (char *)"gametxid [playertxid]", 1, 2, 'R', EVAL_GAMES }, bool games_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx); @@ -67,6 +68,7 @@ UniValue games_events(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_extract(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_register(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); #define CUSTOM_DISPATCH \ if ( cp->evalcode == EVAL_GAMES ) \ @@ -105,6 +107,8 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_fund(txfee,cp,params)); \ else if ( strcmp(method,"bet") == 0 ) \ return(games_bet(txfee,cp,params)); \ + else if ( strcmp(method,"settle") == 0 ) \ + return(games_settle(txfee,cp,params)); \ else \ { \ result.push_back(Pair("result","error")); \ From 00eee20935f82635587776b527de745628a9dedb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:33:33 -1100 Subject: [PATCH 082/115] Settle stub --- src/cc/games/prices.cpp | 50 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 4098a3980..7bc540171 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,24 +14,69 @@ * * ******************************************************************************/ +#define PRICES_BETPERIOD 3 UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + { + height = juint(jitem(params,0),0); + result.push_back(Pair("height",(int64_t)height)); + if ( (pindex= komodo_chainactive(height)) != 0 ) + { + if ( komodo_blockload(block,pindex) == 0 ) + { + n = pindex->block.vtx.size(); + for (i=0; i 1 ) + { + GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); + E_UNMARSHAL(vopret,ss >> pricebits); + fprintf(stderr,"i.%d %.8f %llx\n",i,(double)tx.vout[0].nValue/COIN,(long long)pricebits); + } + } + // display bets + if ( height <= nextheight-PRICES_BETPERIOD ) + { + // settle bets + } + result.push_back(Pair("result","success")); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant load block at height")); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant find block at height")); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt parse")); + } return(result); } UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum,price; CPubKey gamespk,mypk,acpk; + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum; uint64_t price; CPubKey gamespk,mypk,acpk; if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error"," no -ac_pubkey for price reference")); + return(result); } acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); if ( params != 0 && cJSON_GetArraySize(params) == 2 ) @@ -41,6 +86,7 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parsehash")); + return(result); } if ( mypk == acpk ) amount = 0; // i am the reference price feed From ed6a4160aa30c9fcc3d7d9ec0d6550cd1132a2b3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:38:43 -1100 Subject: [PATCH 083/115] block.vtx --- src/cc/CCinclude.h | 1 + src/cc/games/prices.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 839fd2c95..12b4a6d12 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -197,6 +197,7 @@ int64_t IsTokensvout(bool goDeeper, bool checkPubkeys, struct CCcontract_info *c bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx); void komodo_sendmessage(int32_t minpeers,int32_t maxpeers,const char *message,std::vector payload); int32_t payments_parsehexdata(std::vector &hexdata,cJSON *item,int32_t len); +int32_t komodo_blockload(CBlock& block,CBlockIndex *pindex); CScript EncodeTokenCreateOpRet(uint8_t funcid, std::vector origpubkey, std::string name, std::string description, vscript_t vopretNonfungible); CScript EncodeTokenCreateOpRet(uint8_t funcid, std::vector origpubkey, std::string name, std::string description, std::vector> oprets); diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 7bc540171..9909144f0 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -30,7 +30,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { if ( komodo_blockload(block,pindex) == 0 ) { - n = pindex->block.vtx.size(); + n = block.vtx.size(); for (i=0; i Date: Wed, 27 Mar 2019 07:51:09 -1100 Subject: [PATCH 084/115] +prints --- src/cc/games/prices.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 9909144f0..271470219 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -21,7 +21,7 @@ extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,pricebits; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); @@ -38,7 +38,9 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); E_UNMARSHAL(vopret,ss >> pricebits); - fprintf(stderr,"i.%d %.8f %llx\n",i,(double)tx.vout[0].nValue/COIN,(long long)pricebits); + timestamp = (uint32_t)(pricebits >> 32); + uprice = (uint32_t)pricebits; + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f\n",scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000); } } // display bets From 68802af3193756d5a4671f120f95893f630c079b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:51:56 -1100 Subject: [PATCH 085/115] Price --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 271470219..305d64f4e 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -21,7 +21,7 @@ extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,pricebits; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,uprice; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); From a26f3ea4a4c0eb4bfdae017c4c9883767475524c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:52:38 -1100 Subject: [PATCH 086/115] tx.vout[numvouts-1]. --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 305d64f4e..790f4a687 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -40,7 +40,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f\n",scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f\n",tx.vout[numvouts-1].scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000); } } // display bets From f003d0d8befcafd6010a47cf6e1fe7239af8f81d Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:53:33 -1100 Subject: [PATCH 087/115] Test --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 790f4a687..de16a7f2b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -40,7 +40,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f\n",tx.vout[numvouts-1].scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d\n",tx.vout[numvouts-1].scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts); } } // display bets From a1a8014f057355d12e1580bc2b4988c4890583f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 07:54:17 -1100 Subject: [PATCH 088/115] scriptPubKey --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index de16a7f2b..ba78e8560 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -40,7 +40,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d\n",tx.vout[numvouts-1].scriptPubkey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts); } } // display bets From 4a998b2595fe111192bada2dff1a425d057ec14c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:01:21 -1100 Subject: [PATCH 089/115] +print --- src/cc/games/prices.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index ba78e8560..1171c3aba 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -34,7 +34,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) for (i=0; i 1 ) + if ( (numvouts= tx.vout.size()) > 1 && tx.vout[numvouts-1].scriptPubKey[0] == 0x6a ) { GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); E_UNMARSHAL(vopret,ss >> pricebits); @@ -91,7 +91,10 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) return(result); } if ( mypk == acpk ) + { amount = 0; // i am the reference price feed + fprintf(stderr,"i am the reference\n"); + } //fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); From 03f663ea3795c3b146f54a819db3407ff1714950 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:04:06 -1100 Subject: [PATCH 090/115] Reorder --- src/cc/games/prices.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 1171c3aba..f5a866727 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -21,7 +21,10 @@ extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,uprice; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + mypk = pubkey2pk(Mypubkey()); + gamespk = GetUnspendable(cp,0); + acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); @@ -80,6 +83,8 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("error"," no -ac_pubkey for price reference")); return(result); } + mypk = pubkey2pk(Mypubkey()); + gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { @@ -93,11 +98,9 @@ UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) if ( mypk == acpk ) { amount = 0; // i am the reference price feed - fprintf(stderr,"i am the reference\n"); + //fprintf(stderr,"i am the reference\n"); } //fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); - mypk = pubkey2pk(Mypubkey()); - gamespk = GetUnspendable(cp,0); if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) { mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); From a28e03acdd52e6dec2c2b5b3ecdb646e3fbbb558 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:14:39 -1100 Subject: [PATCH 091/115] Test --- src/cc/games/prices.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index f5a866727..69c45f286 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -21,10 +21,11 @@ extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; int32_t i,n,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; char acaddr[64],destaddr[64]; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts,height,nextheight = komodo_nextheight(); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); + Getscriptaddress(acaddr,CScript() << ParseHex(HexStr(acpk)) << OP_CHECKSIG); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); @@ -34,16 +35,24 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) if ( komodo_blockload(block,pindex) == 0 ) { n = block.vtx.size(); + vini = 0; for (i=0; i 1 && tx.vout[numvouts-1].scriptPubKey[0] == 0x6a ) + if ( myGetTransaction(tx.vin[vini].prevout.hash,vintx,hashBlock) == 0 ) + continue; + else if ( Getscriptaddress(destaddr,vintx.vout[tx.vin[vini].prevout.n].scriptPubKey) == 0 ) + continue; + else if ( (numvouts= tx.vout.size()) > 1 && tx.vout[numvouts-1].scriptPubKey[0] == 0x6a ) { + prizefund += tx.vout[0].nValue; GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts); + if ( strcmp(acaddr,destaddr) == 0 ) + fprintf(stderr,"REF "); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); } } // display bets @@ -51,6 +60,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { // settle bets } + result.push_back(Pair("prizefund",ValueToAmount(prizefund))); result.push_back(Pair("result","success")); } else From d23d46d9534325fd2c4a149c2aab6845db73560c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:19:18 -1100 Subject: [PATCH 092/115] ValueFromAmount --- src/cc/games/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 69c45f286..64699dfe7 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -21,7 +21,7 @@ extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx; uint64_t pricebits; char acaddr[64],destaddr[64]; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx,vintx; uint64_t pricebits; char acaddr[64],destaddr[64]; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts,height,nextheight = komodo_nextheight(); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); @@ -60,7 +60,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { // settle bets } - result.push_back(Pair("prizefund",ValueToAmount(prizefund))); + result.push_back(Pair("prizefund",ValueFromAmount(prizefund))); result.push_back(Pair("result","success")); } else From d2fbc97ee88f5f421c6bd04ca7af37a7abc3bcbd Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:21:49 -1100 Subject: [PATCH 093/115] -print --- src/cc/games/prices.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 64699dfe7..146f6a926 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -50,9 +50,9 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - if ( strcmp(acaddr,destaddr) == 0 ) - fprintf(stderr,"REF "); - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); + //if ( strcmp(acaddr,destaddr) == 0 ) + // fprintf(stderr,"REF "); + //fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); } } // display bets From a5406418471059ca5594fbbf7c3d5ff7f6416221 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:30:19 -1100 Subject: [PATCH 094/115] Test --- src/cc/games/prices.cpp | 83 +++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 146f6a926..770cdfc8e 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -18,10 +18,41 @@ UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; +int64_t prices_blockinfo(int32_t height,char *acaddr) +{ + std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx,vintx; uint64_t pricebits; char destaddr[64]; uint32_t timestamp,uprice; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts; + if ( (pindex= komodo_chainactive(height)) != 0 ) + { + if ( komodo_blockload(block,pindex) == 0 ) + { + n = block.vtx.size(); + vini = 0; + for (i=0; i= vintx.vout.size() || Getscriptaddress(destaddr,vintx.vout[tx.vin[vini].prevout.n].scriptPubKey) == 0 ) + continue; + else if ( (numvouts= tx.vout.size()) > 1 && tx.vout[numvouts-1].scriptPubKey[0] == 0x6a ) + { + prizefund += tx.vout[0].nValue; + GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); + E_UNMARSHAL(vopret,ss >> pricebits); + timestamp = (uint32_t)(pricebits >> 32); + uprice = (uint32_t)pricebits; + //if ( strcmp(acaddr,destaddr) == 0 ) + // fprintf(stderr,"REF "); + //fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); + } + } + } else return(-2); + } else return(-1); +} UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx,vintx; uint64_t pricebits; char acaddr[64],destaddr[64]; uint32_t timestamp,uprice; CPubKey acpk,mypk,gamespk; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts,height,nextheight = komodo_nextheight(); + UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); @@ -30,49 +61,21 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { height = juint(jitem(params,0),0); result.push_back(Pair("height",(int64_t)height)); - if ( (pindex= komodo_chainactive(height)) != 0 ) + if ( 1 || (prizefund= prices_blockinfo(height,acaddr)) < 0 ) { - if ( komodo_blockload(block,pindex) == 0 ) - { - n = block.vtx.size(); - vini = 0; - for (i=0; i 1 && tx.vout[numvouts-1].scriptPubKey[0] == 0x6a ) - { - prizefund += tx.vout[0].nValue; - GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); - E_UNMARSHAL(vopret,ss >> pricebits); - timestamp = (uint32_t)(pricebits >> 32); - uprice = (uint32_t)pricebits; - //if ( strcmp(acaddr,destaddr) == 0 ) - // fprintf(stderr,"REF "); - //fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); - } - } - // display bets - if ( height <= nextheight-PRICES_BETPERIOD ) - { - // settle bets - } - result.push_back(Pair("prizefund",ValueFromAmount(prizefund))); - result.push_back(Pair("result","success")); - } - else - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","cant load block at height")); - } + result.push_back(Pair("result","error")); + result.push_back(Pair("errorcode",prizefund)); + result.push_back(Pair("error","blockinfo error")); } else { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","cant find block at height")); + // display bets + if ( height <= nextheight-PRICES_BETPERIOD ) + { + // settle bets + } + result.push_back(Pair("prizefund",ValueFromAmount(prizefund))); + result.push_back(Pair("result","success")); } } else From 513731ea367a4a03eeae8a3561d7f4887eb949ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:32:55 -1100 Subject: [PATCH 095/115] Test --- src/cc/gamescc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 7a6b7c66c..efdd3b8e1 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -77,6 +77,8 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_rng(txfee,cp,params)); \ else if ( strcmp(method,"rngnext") == 0 ) \ return(games_rngnext(txfee,cp,params)); \ + else if ( strcmp(method,"settle") == 0 ) \ + return(games_settle(txfee,cp,params)); \ else if ( strcmp(method,"newgame") == 0 ) \ return(games_newgame(txfee,cp,params)); \ else if ( strcmp(method,"gameinfo") == 0 ) \ @@ -107,8 +109,6 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_fund(txfee,cp,params)); \ else if ( strcmp(method,"bet") == 0 ) \ return(games_bet(txfee,cp,params)); \ - else if ( strcmp(method,"settle") == 0 ) \ - return(games_settle(txfee,cp,params)); \ else \ { \ result.push_back(Pair("result","error")); \ From 8de793c57bdc8159fb4f951151b7c452500333d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:35:06 -1100 Subject: [PATCH 096/115] Test --- src/cc/games/prices.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 770cdfc8e..ea5e28211 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -53,6 +53,8 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); + result.push_back(Pair("result","success")); + return(result); mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); From 0e1310dd8a3538b7cabc96072b493339be69a9c3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:42:44 -1100 Subject: [PATCH 097/115] Test --- src/cc/games/prices.cpp | 2 ++ src/cc/games/tetris.cpp | 2 ++ src/cc/gamescc.h | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index ea5e28211..275589b02 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,6 +14,8 @@ * * ******************************************************************************/ +std::string MYCCLIBNAME = (char *)"gamescc"; + #define PRICES_BETPERIOD 3 UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; diff --git a/src/cc/games/tetris.cpp b/src/cc/games/tetris.cpp index d1ca9c30b..cd609154d 100644 --- a/src/cc/games/tetris.cpp +++ b/src/cc/games/tetris.cpp @@ -14,6 +14,8 @@ * * ******************************************************************************/ +std::string MYCCLIBNAME = (char *)"gamescc"; + // game specific code for daemon void games_packitemstr(char *packitemstr,struct games_packitem *item) { diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index efdd3b8e1..b83276d8d 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -15,7 +15,6 @@ extern CWallet* pwalletMain; #include "CCinclude.h" #include "secp256k1.h" -std::string MYCCLIBNAME = (char *)"gamescc"; #define EVAL_GAMES (EVAL_FAUCET2+1) #define GAMES_TXFEE 10000 From aa24e63f86a17fee395204efa2edfa113b59ae1f Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:46:03 -1100 Subject: [PATCH 098/115] Test --- src/cc/gamescc.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index b83276d8d..4d095ab12 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -24,6 +24,7 @@ extern CWallet* pwalletMain; #define GAMES_REGISTRATIONSIZE (100 * 10000) #define GAMES_REGISTRATION 1 +extern std::string MYCCLIBNAME; #define MYCCNAME "games" @@ -76,8 +77,6 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_rng(txfee,cp,params)); \ else if ( strcmp(method,"rngnext") == 0 ) \ return(games_rngnext(txfee,cp,params)); \ - else if ( strcmp(method,"settle") == 0 ) \ - return(games_settle(txfee,cp,params)); \ else if ( strcmp(method,"newgame") == 0 ) \ return(games_newgame(txfee,cp,params)); \ else if ( strcmp(method,"gameinfo") == 0 ) \ @@ -108,6 +107,8 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_fund(txfee,cp,params)); \ else if ( strcmp(method,"bet") == 0 ) \ return(games_bet(txfee,cp,params)); \ + else if ( strcmp(method,"settle") == 0 ) \ + return(games_settle(txfee,cp,params)); \ else \ { \ result.push_back(Pair("result","error")); \ From 1a413d246f0302cbb066d05c4d898c9dbec57756 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 08:58:40 -1100 Subject: [PATCH 099/115] Prices --- src/cc/cclib.cpp | 2 ++ src/cc/games/prices.cpp | 2 +- src/cc/gamescc.h | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 975b0735c..00a7c6fe0 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -52,6 +52,8 @@ std::string MYCCLIBNAME = (char *)"sudoku"; void komodo_netevent(std::vector payload) {} #endif +extern std::string MYCCLIBNAME; + char *CClib_name() { return((char *)MYCCLIBNAME.c_str()); } struct CClib_rpcinfo diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 275589b02..7293a8de9 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,7 +14,7 @@ * * ******************************************************************************/ -std::string MYCCLIBNAME = (char *)"gamescc"; +std::string MYCCLIBNAME = (char *)"pricescc"; #define PRICES_BETPERIOD 3 UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 4d095ab12..b804216d7 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -24,7 +24,6 @@ extern CWallet* pwalletMain; #define GAMES_REGISTRATIONSIZE (100 * 10000) #define GAMES_REGISTRATION 1 -extern std::string MYCCLIBNAME; #define MYCCNAME "games" From 985386a7e31bde57567266305956f834051546aa Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:00:40 -1100 Subject: [PATCH 100/115] Prices --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 7293a8de9..8624bc635 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -14,7 +14,7 @@ * * ******************************************************************************/ -std::string MYCCLIBNAME = (char *)"pricescc"; +std::string MYCCLIBNAME = (char *)"prices"; #define PRICES_BETPERIOD 3 UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); From 2102294650bf5f4c4bd59ff47575bcf5ee7642ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:04:14 -1100 Subject: [PATCH 101/115] Test --- src/cc/games/prices.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 8624bc635..4777e9de0 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -55,8 +55,12 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); - result.push_back(Pair("result","success")); - return(result); + if ( 1 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error"," no -ac_pubkey for price reference")); + return(result); + } mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); From 5e817c05acf57db0342d028958d475eb6a4ca786 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:06:30 -1100 Subject: [PATCH 102/115] Test --- src/cc/games/prices.cpp | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 4777e9de0..91f4aa790 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -52,7 +52,7 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) } else return(-1); } -UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); if ( 1 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) @@ -93,6 +93,53 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) } return(result); } +UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum; uint64_t price; CPubKey gamespk,mypk,acpk; + if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error"," no -ac_pubkey for price reference")); + return(result); + } + mypk = pubkey2pk(Mypubkey()); + gamespk = GetUnspendable(cp,0); + acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); + if ( params != 0 && cJSON_GetArraySize(params) == 2 ) + { + amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; + if ( cclib_parsehash((uint8_t *)&price,jitem(params,1),8) < 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt parsehash")); + return(result); + } + if ( mypk == acpk ) + { + amount = 0; // i am the reference price feed + //fprintf(stderr,"i am the reference\n"); + } + //fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); + if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) + { + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); + return(games_rawtxresult(result,rawtx,1)); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough funds")); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt parse")); + } + return(result); +} UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { From dcfae63863f27e4dc82587d0da2a9e370df279b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:09:10 -1100 Subject: [PATCH 103/115] Test --- src/cc/games/prices.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 91f4aa790..112383554 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -106,7 +106,12 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); - if ( params != 0 && cJSON_GetArraySize(params) == 2 ) + if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + { + height = juint(jitem(params,0),0); + result.push_back(Pair("height",(int64_t)height)); + } + /*if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; if ( cclib_parsehash((uint8_t *)&price,jitem(params,1),8) < 0 ) @@ -137,7 +142,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parse")); - } + }*/ return(result); } From 23e378493fa7b9f5ec01df6a8b78e4c69f837ce5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:10:02 -1100 Subject: [PATCH 104/115] Test --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 112383554..c9826fd9c 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -96,7 +96,7 @@ UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *param UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum; uint64_t price; CPubKey gamespk,mypk,acpk; + UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum; uint64_t price; CPubKey gamespk,mypk,acpk; int32_t height; if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) { result.push_back(Pair("result","error")); From d294ec1b93d9fdd5b0d258e111658db5c228ae51 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:12:22 -1100 Subject: [PATCH 105/115] Test --- src/cc/games/prices.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index c9826fd9c..a638a7016 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -93,6 +93,7 @@ UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *param } return(result); } + UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); @@ -106,10 +107,11 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); + result.push_back(Pair("result","success")); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); - result.push_back(Pair("height",(int64_t)height)); + //result.push_back(Pair("height",(int64_t)height)); } /*if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { From 7b6ad019e63caadbc963b64627c7958f2635a8fe Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:13:48 -1100 Subject: [PATCH 106/115] Test --- src/cc/games/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index a638a7016..1ac7e3edb 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -110,8 +110,8 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","success")); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { - height = juint(jitem(params,0),0); - //result.push_back(Pair("height",(int64_t)height)); + //height = juint(jitem(params,0),0); + result.push_back(Pair("height",(int64_t)height)); } /*if ( params != 0 && cJSON_GetArraySize(params) == 2 ) { From ecd3a17c85b1b016aac8e971b61ca6f6545305cb Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:16:03 -1100 Subject: [PATCH 107/115] Test --- src/cc/games/prices.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 1ac7e3edb..7f807954d 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -52,10 +52,10 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) } else return(-1); } -UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); - if ( 1 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) + if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error"," no -ac_pubkey for price reference")); @@ -64,6 +64,16 @@ UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *param mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); + result.push_back(Pair("result","success")); + if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + { + height = juint(jitem(params,0),0); + result.push_back(Pair("height",(int64_t)height)); + } + return(result); + /*mypk = pubkey2pk(Mypubkey()); + gamespk = GetUnspendable(cp,0); + acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); Getscriptaddress(acaddr,CScript() << ParseHex(HexStr(acpk)) << OP_CHECKSIG); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { @@ -91,7 +101,7 @@ UniValue games_origsettle(uint64_t txfee,struct CCcontract_info *cp,cJSON *param result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parse")); } - return(result); + return(result);*/ } UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) @@ -110,7 +120,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","success")); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { - //height = juint(jitem(params,0),0); + height = juint(jitem(params,0),0); result.push_back(Pair("height",(int64_t)height)); } /*if ( params != 0 && cJSON_GetArraySize(params) == 2 ) From 42235ad98d80acd044f021e359f8f8c9bd752166 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:17:08 -1100 Subject: [PATCH 108/115] -2nd --- src/cc/games/prices.cpp | 54 ----------------------------------------- 1 file changed, 54 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 7f807954d..c88827fa2 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -104,60 +104,6 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) return(result);*/ } -UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) -{ - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); std::string rawtx; int64_t amount,inputsum; uint64_t price; CPubKey gamespk,mypk,acpk; int32_t height; - if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error"," no -ac_pubkey for price reference")); - return(result); - } - mypk = pubkey2pk(Mypubkey()); - gamespk = GetUnspendable(cp,0); - acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); - result.push_back(Pair("result","success")); - if ( params != 0 && cJSON_GetArraySize(params) == 1 ) - { - height = juint(jitem(params,0),0); - result.push_back(Pair("height",(int64_t)height)); - } - /*if ( params != 0 && cJSON_GetArraySize(params) == 2 ) - { - amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; - if ( cclib_parsehash((uint8_t *)&price,jitem(params,1),8) < 0 ) - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","couldnt parsehash")); - return(result); - } - if ( mypk == acpk ) - { - amount = 0; // i am the reference price feed - //fprintf(stderr,"i am the reference\n"); - } - //fprintf(stderr,"amount %llu price %llx\n",(long long)amount,(long long)price); - if ( (inputsum= AddNormalinputs(mtx,mypk,amount+GAMES_TXFEE,64)) >= amount+GAMES_TXFEE ) - { - mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,gamespk)); - rawtx = FinalizeCCTx(0,cp,mtx,mypk,GAMES_TXFEE,CScript() << OP_RETURN << price); - return(games_rawtxresult(result,rawtx,1)); - } - else - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","not enough funds")); - } - } - else - { - result.push_back(Pair("result","error")); - result.push_back(Pair("error","couldnt parse")); - }*/ - return(result); -} - UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); From e909810c76fc2c109ca8b71ea80dfaf0967d5466 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:18:29 -1100 Subject: [PATCH 109/115] Test --- src/cc/games/prices.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index c88827fa2..142bc4c6b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -65,11 +65,11 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); result.push_back(Pair("result","success")); - if ( params != 0 && cJSON_GetArraySize(params) == 1 ) + /*if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { height = juint(jitem(params,0),0); result.push_back(Pair("height",(int64_t)height)); - } + }*/ return(result); /*mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); From bdf32a01fb74f249bcf30807af3a75db4f5b256a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:19:27 -1100 Subject: [PATCH 110/115] Test --- src/cc/games/prices.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 142bc4c6b..be85b92f1 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -63,7 +63,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) } mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); - acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); + //acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); result.push_back(Pair("result","success")); /*if ( params != 0 && cJSON_GetArraySize(params) == 1 ) { From a94d3bd0f753f395bbab32de8bd55f02ec28df07 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:21:58 -1100 Subject: [PATCH 111/115] d'oh --- src/cc/games/prices.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index be85b92f1..7a43c8218 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -54,7 +54,7 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { - UniValue result; char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); + UniValue result(UniValue::VOBJ); char acaddr[64]; CPubKey acpk,mypk,gamespk; int64_t prizefund = 0; int32_t height,nextheight = komodo_nextheight(); if ( ASSETCHAINS_OVERRIDE_PUBKEY33[0] == 0 ) { result.push_back(Pair("result","error")); @@ -63,16 +63,6 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) } mypk = pubkey2pk(Mypubkey()); gamespk = GetUnspendable(cp,0); - //acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); - result.push_back(Pair("result","success")); - /*if ( params != 0 && cJSON_GetArraySize(params) == 1 ) - { - height = juint(jitem(params,0),0); - result.push_back(Pair("height",(int64_t)height)); - }*/ - return(result); - /*mypk = pubkey2pk(Mypubkey()); - gamespk = GetUnspendable(cp,0); acpk = buf2pk(ASSETCHAINS_OVERRIDE_PUBKEY33); Getscriptaddress(acaddr,CScript() << ParseHex(HexStr(acpk)) << OP_CHECKSIG); if ( params != 0 && cJSON_GetArraySize(params) == 1 ) @@ -101,7 +91,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","error")); result.push_back(Pair("error","couldnt parse")); } - return(result);*/ + return(result); } UniValue games_bet(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) From 40174b32742b8d481c166ceea263d09977351087 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:23:55 -1100 Subject: [PATCH 112/115] Test --- src/cc/games/prices.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 7a43c8218..6385626fc 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -20,6 +20,8 @@ std::string MYCCLIBNAME = (char *)"prices"; UniValue games_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag); extern uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; +// generate bars + int64_t prices_blockinfo(int32_t height,char *acaddr) { std::vector vopret; CBlockIndex *pindex; CBlock block; CTransaction tx,vintx; uint64_t pricebits; char destaddr[64]; uint32_t timestamp,uprice; uint256 hashBlock; int64_t prizefund = 0; int32_t i,n,vini,numvouts; @@ -69,7 +71,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { height = juint(jitem(params,0),0); result.push_back(Pair("height",(int64_t)height)); - if ( 1 || (prizefund= prices_blockinfo(height,acaddr)) < 0 ) + if ( (prizefund= prices_blockinfo(height,acaddr)) < 0 ) { result.push_back(Pair("result","error")); result.push_back(Pair("errorcode",prizefund)); @@ -80,7 +82,7 @@ UniValue games_settle(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) // display bets if ( height <= nextheight-PRICES_BETPERIOD ) { - // settle bets + // settle bets by first nonzero reference bar } result.push_back(Pair("prizefund",ValueFromAmount(prizefund))); result.push_back(Pair("result","success")); From 9c12a5523762798880c98b39a0cc0b4bfa111bab Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:25:35 -1100 Subject: [PATCH 113/115] Return prizefund --- src/cc/games/prices.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 6385626fc..177417c9b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -45,11 +45,12 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) E_UNMARSHAL(vopret,ss >> pricebits); timestamp = (uint32_t)(pricebits >> 32); uprice = (uint32_t)pricebits; - //if ( strcmp(acaddr,destaddr) == 0 ) - // fprintf(stderr,"REF "); - //fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); + if ( strcmp(acaddr,destaddr) == 0 ) + fprintf(stderr,"REF "); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); } } + return(prizefund); } else return(-2); } else return(-1); } From 5a557264fa1c98ef90f8ce8cb8c4fb5c58532782 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 09:27:56 -1100 Subject: [PATCH 114/115] Test --- src/cc/games/prices.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cc/games/prices.cpp b/src/cc/games/prices.cpp index 177417c9b..9178eb46b 100644 --- a/src/cc/games/prices.cpp +++ b/src/cc/games/prices.cpp @@ -42,12 +42,15 @@ int64_t prices_blockinfo(int32_t height,char *acaddr) { prizefund += tx.vout[0].nValue; GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); - E_UNMARSHAL(vopret,ss >> pricebits); - timestamp = (uint32_t)(pricebits >> 32); - uprice = (uint32_t)pricebits; - if ( strcmp(acaddr,destaddr) == 0 ) - fprintf(stderr,"REF "); - fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); + if ( vopret.size() == 8 ) + { + E_UNMARSHAL(vopret,ss >> pricebits); + timestamp = (uint32_t)(pricebits >> 32); + uprice = (uint32_t)pricebits; + if ( strcmp(acaddr,destaddr) == 0 ) + fprintf(stderr,"REF "); + fprintf(stderr,"[%02x] i.%d %.8f %llx t%u %.4f numvouts.%d %s lag.%d\n",tx.vout[numvouts-1].scriptPubKey[0],i,(double)tx.vout[0].nValue/COIN,(long long)pricebits,timestamp,(double)uprice/10000,numvouts,destaddr,(int32_t)(pindex->nTime-timestamp)); + } else return(-3); } } return(prizefund); From 0597306ea564fa640c7e91384421945e22804928 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 27 Mar 2019 19:31:39 -1100 Subject: [PATCH 115/115] 69522 exemption --- src/cc/rogue_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/rogue_rpc.cpp b/src/cc/rogue_rpc.cpp index 5040c34d3..c5350a68d 100644 --- a/src/cc/rogue_rpc.cpp +++ b/src/cc/rogue_rpc.cpp @@ -1521,7 +1521,7 @@ bool rogue_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const C if ( (numvouts= tx.vout.size()) > 1 ) { txid = tx.GetHash(); - if ( txid == Parseuint256("1ae04dc0c5f2fca2053819a3a1b2efe5d355c34f58d6f16d59e5e2573e7baf7f") ) // osx rogue chain ht.50902 + if ( txid == Parseuint256("1ae04dc0c5f2fca2053819a3a1b2efe5d355c34f58d6f16d59e5e2573e7baf7f") || txid == Parseuint256("2a34b36cc1292aecfaabdad79b42cab9989fa6dcc87ac8ca88aa6162dab1e2c4") ) // osx rogue chain ht.50902, 69522 enabled = 0; scriptPubKey = tx.vout[numvouts-1].scriptPubKey; GetOpReturnData(scriptPubKey,vopret);