From fb132238f0f0fcece0e0d657700852aa0edee441 Mon Sep 17 00:00:00 2001 From: "Anton \"TonyL\" Lysakov" Date: Wed, 27 Mar 2019 23:36:58 +0700 Subject: [PATCH 001/154] Create cryptoconditions.py Forgot that we using it as a "library" for other CC tests. So deletion was a mistake. --- qa/rpc-tests/cryptoconditions.py | 690 +++++++++++++++++++++++++++++++ 1 file changed, 690 insertions(+) create mode 100644 qa/rpc-tests/cryptoconditions.py diff --git a/qa/rpc-tests/cryptoconditions.py b/qa/rpc-tests/cryptoconditions.py new file mode 100644 index 000000000..d5456e801 --- /dev/null +++ b/qa/rpc-tests/cryptoconditions.py @@ -0,0 +1,690 @@ +#!/usr/bin/env python2 +# Copyright (c) 2018 SuperNET developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +from test_framework.test_framework import BitcoinTestFramework +from test_framework.authproxy import JSONRPCException +from test_framework.util import assert_equal, assert_greater_than, \ + initialize_chain_clean, initialize_chain, start_nodes, start_node, connect_nodes_bi, \ + stop_nodes, sync_blocks, sync_mempools, wait_bitcoinds, rpc_port, assert_raises + +import time +from decimal import Decimal +from random import choice +from string import ascii_uppercase + +def assert_success(result): + assert_equal(result['result'], 'success') + +def assert_error(result): + assert_equal(result['result'], 'error') + +def generate_random_string(length): + random_string = ''.join(choice(ascii_uppercase) for i in range(length)) + return random_string + +class CryptoConditionsTest (BitcoinTestFramework): + + def setup_chain(self): + print("Initializing CC test directory "+self.options.tmpdir) + self.num_nodes = 2 + initialize_chain_clean(self.options.tmpdir, self.num_nodes) + + def setup_network(self, split = False): + print("Setting up network...") + self.addr = "RWPg8B91kfK5UtUN7z6s6TeV9cHSGtVY8D" + self.pubkey = "02676d00110c2cd14ae24f95969e8598f7ccfaa675498b82654a5b5bd57fc1d8cf" + self.privkey = "UqMgxk7ySPNQ4r9nKAFPjkXy6r5t898yhuNCjSZJLg3RAM4WW1m9" + self.addr1 = "RXEXoa1nRmKhMbuZovpcYwQMsicwzccZBp" + self.pubkey1 = "024026d4ad4ecfc1f705a9b42ca64af6d2ad947509c085534a30b8861d756c6ff0" + self.privkey1 = "UtdydP56pGTFmawHzHr1wDrc4oUwCNW1ttX8Pc3KrvH3MA8P49Wi" + self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, + extra_args=[[ + # always give -ac_name as first extra_arg and port as third + '-ac_name=REGTEST', + '-conf='+self.options.tmpdir+'/node0/REGTEST.conf', + '-port=64367', + '-rpcport=64368', + '-regtest', + '-addressindex=1', + '-spentindex=1', + '-ac_supply=5555555', + '-ac_reward=10000000000000', + '-pubkey=' + self.pubkey, + '-ac_cc=2', + '-whitelist=127.0.0.1', + '-debug', + '--daemon', + '-rpcuser=rt', + '-rpcpassword=rt' + ], + ['-ac_name=REGTEST', + '-conf='+self.options.tmpdir+'/node1/REGTEST.conf', + '-port=64365', + '-rpcport=64366', + '-regtest', + '-addressindex=1', + '-spentindex=1', + '-ac_supply=5555555', + '-ac_reward=10000000000000', + '-pubkey=' + self.pubkey1, + '-ac_cc=2', + '-whitelist=127.0.0.1', + '-debug', + '-addnode=127.0.0.1:64367', + '--daemon', + '-rpcuser=rt', + '-rpcpassword=rt']] + ) + self.is_network_split = split + self.rpc = self.nodes[0] + self.rpc1 = self.nodes[1] + self.sync_all() + print("Done setting up network") + + def send_and_mine(self, xtn, rpc_connection): + txid = rpc_connection.sendrawtransaction(xtn) + assert txid, 'got txid' + # we need the tx above to be confirmed in the next block + rpc_connection.generate(1) + return txid + + def run_faucet_tests(self): + rpc = self.rpc + rpc1 = self.rpc1 + + # basic sanity tests + result = rpc.getwalletinfo() + assert_greater_than(result['txcount'], 100) + assert_greater_than(result['balance'], 0.0) + balance = result['balance'] + + faucet = rpc.faucetaddress() + assert_equal(faucet['result'], 'success') + # verify all keys look like valid AC addrs, could be better + for x in ['myCCAddress(Faucet)', 'FaucetCCAddress', 'FaucetCCTokensAddress', 'myaddress', 'FaucetNormalAddress']: + assert_equal(faucet[x][0], 'R') + + result = rpc.faucetaddress(self.pubkey) + assert_success(result) + # test that additional CCaddress key is returned + for x in ['myCCAddress(Faucet)', 'FaucetCCAddress', 'FaucetCCTokensAddress', 'myaddress', 'FaucetNormalAddress']: + assert_equal(result[x][0], 'R') + + # no funds in the faucet yet + result = rpc.faucetget() + assert_error(result) + + result = rpc.faucetinfo() + assert_success(result) + + result = rpc.faucetfund("0") + assert_error(result) + + result = rpc.faucetfund("-1") + assert_error(result) + + # we need at least 1 + txfee to get + result = rpc.faucetfund("2") + assert_success(result) + assert result['hex'], "hex key found" + + # broadcast the xtn + result = rpc.sendrawtransaction(result['hex']) + txid = result[0] + assert txid, "found txid" + + # we need the tx above to be confirmed in the next block + rpc.generate(1) + self.sync_all() + + result = rpc.getwalletinfo() + # minus one block reward + balance2 = result['balance'] - 100000 + # make sure our balance is less now + assert_greater_than(balance, balance2) + + result = rpc.faucetinfo() + assert_success(result) + assert_greater_than( result['funding'], 0 ) + + # claiming faucet on second node + faucetgethex = rpc1.faucetget() + assert_success(faucetgethex) + assert faucetgethex['hex'], "hex key found" + + balance1 = rpc1.getwalletinfo()['balance'] + + # try to broadcast the faucetget transaction + result = self.send_and_mine(faucetgethex['hex'], rpc1) + assert txid, "transaction broadcasted" + + balance2 = rpc1.getwalletinfo()['balance'] + assert_greater_than(balance2, balance1) + + self.sync_all() + + def run_dice_tests(self): + rpc = self.nodes[0] + rpc1 = self.nodes[1] + self.sync_all() + + # have to generate few blocks on second node to be able to place bets + rpc1.generate(10) + result = rpc1.getbalance() + assert_greater_than(result, 100000) + + dice = rpc.diceaddress() + assert_equal(dice['result'], 'success') + for x in ['myCCAddress(Dice)', 'DiceCCAddress', 'DiceCCTokensAddress', 'myaddress', 'DiceNormalAddress']: + assert_equal(dice[x][0], 'R') + + dice = rpc.diceaddress(self.pubkey) + assert_equal(dice['result'], 'success') + for x in ['myCCAddress(Dice)', 'DiceCCAddress', 'DiceCCTokensAddress', 'myaddress', 'DiceNormalAddress']: + assert_equal(dice[x][0], 'R') + + # no dice created yet + result = rpc.dicelist() + assert_equal(result, []) + + # creating dice plan with too long name (>8 chars) + result = rpc.dicefund("THISISTOOLONG", "10000", "10", "10000", "10", "5") + assert_error(result) + + # creating dice plan with < 100 funding + result = rpc.dicefund("LUCKY","10","1","10000","10","5") + assert_error(result) + + # creating dice plan with 0 blocks timeout + result = rpc.dicefund("LUCKY","10","1","10000","10","0") + assert_error(result) + + # creating dice plan + dicefundtx = rpc.dicefund("LUCKY","1000","1","800","10","5") + diceid = self.send_and_mine(dicefundtx['hex'], rpc) + + # checking if it in plans list now + result = rpc.dicelist() + assert_equal(result[0], diceid) + + # set dice name for futher usage + dicename = "LUCKY" + + # adding zero funds to plan + result = rpc.diceaddfunds(dicename,diceid,"0") + assert_error(result) + + # adding negative funds to plan + result = rpc.diceaddfunds(dicename,diceid,"-1") + assert_error(result) + + # adding funds to plan + addfundstx = rpc.diceaddfunds(dicename,diceid,"1100") + result = self.send_and_mine(addfundstx['hex'], rpc) + + # checking if funds added to plan + result = rpc.diceinfo(diceid) + assert_equal(result["funding"], "2100.00000000") + + # not valid dice info checking + result = rpc.diceinfo("invalid") + assert_error(result) + + # placing 0 amount bet + result = rpc1.dicebet(dicename,diceid,"0","2") + assert_error(result) + + # placing negative amount bet + result = rpc1.dicebet(dicename,diceid,"-1","2") + assert_error(result) + + # placing bet more than maxbet + result = rpc1.dicebet(dicename,diceid,"900","2") + assert_error(result) + + # placing bet with amount more than funding + result = rpc1.dicebet(dicename,diceid,"3000","2") + assert_error(result) + + # placing bet with potential won more than funding + result = rpc1.dicebet(dicename,diceid,"750","9") + assert_error(result) + + # placing 0 odds bet + result = rpc1.dicebet(dicename,diceid,"1","0") + assert_error(result) + + # placing negative odds bet + result = rpc1.dicebet(dicename,diceid,"1","-1") + assert_error(result) + + # placing bet with odds more than allowed + result = rpc1.dicebet(dicename,diceid,"1","11") + assert_error(result) + + # placing bet with not correct dice name + result = rpc1.dicebet("nope",diceid,"100","2") + assert_error(result) + + # placing bet with not correct dice id + result = rpc1.dicebet(dicename,self.pubkey,"100","2") + assert_error(result) + + # have to make some entropy for the next test + entropytx = 0 + fundingsum = 1 + while entropytx < 110: + fundingsuminput = str(fundingsum) + fundinghex = rpc.diceaddfunds(dicename,diceid,fundingsuminput) + result = self.send_and_mine(fundinghex['hex'], rpc) + entropytx = entropytx + 1 + fundingsum = fundingsum + 1 + + rpc.generate(2) + self.sync_all() + + # valid bet placing + placebet = rpc1.dicebet(dicename,diceid,"100","2") + betid = self.send_and_mine(placebet["hex"], rpc1) + assert result, "bet placed" + + # check bet status + result = rpc1.dicestatus(dicename,diceid,betid) + assert_success(result) + + # note initial dice funding state at this point. + # TODO: track player balance somehow (hard to do because of mining and fees) + diceinfo = rpc.diceinfo(diceid) + funding = float(diceinfo['funding']) + + # # placing same amount bets with amount 1 and odds 1:3, checking if balance changed correct + # losscounter = 0 + # wincounter = 0 + # betcounter = 0 + # + # while (betcounter < 10): + # placebet = rpc1.dicebet(dicename,diceid,"1","2") + # betid = self.send_and_mine(placebet["hex"], rpc1) + # time.sleep(3) + # self.sync_all() + # finish = rpc.dicefinish(dicename,diceid,betid) + # self.send_and_mine(finish["hex"], rpc1) + # self.sync_all() + # time.sleep(3) + # betresult = rpc1.dicestatus(dicename,diceid,betid) + # betcounter = betcounter + 1 + # if betresult["status"] == "loss": + # losscounter = losscounter + 1 + # elif betresult["status"] == "win": + # wincounter = wincounter + 1 + # else: + # pass + # + # # funding balance should increase if player loss, decrease if player won + # fundbalanceguess = funding + losscounter - wincounter * 2 + # fundinfoactual = rpc.diceinfo(diceid) + # assert_equal(round(fundbalanceguess),round(float(fundinfoactual['funding']))) + + def run_token_tests(self): + rpc = self.nodes[0] + result = rpc.tokenaddress() + assert_success(result) + for x in ['myCCAddress(Tokens)', 'TokensNormalAddress', 'TokensNormalAddress', 'myaddress','TokensCCAddress']: + assert_equal(result[x][0], 'R') + + result = rpc.tokenaddress(self.pubkey) + assert_success(result) + for x in ['myCCAddress(Tokens)', 'TokensNormalAddress', 'TokensNormalAddress', 'myaddress','TokensCCAddress']: + assert_equal(result[x][0], 'R') + # there are no tokens created yet + result = rpc.tokenlist() + assert_equal(result, []) + + # trying to create token with negaive supply + result = rpc.tokencreate("NUKE", "-1987420", "no bueno supply") + assert_error(result) + + # creating token with name more than 32 chars + result = rpc.tokencreate("NUKE123456789012345678901234567890", "1987420", "name too long") + assert_error(result) + + # creating valid token + result = rpc.tokencreate("DUKE", "1987.420", "Duke's custom token") + assert_success(result) + + tokenid = self.send_and_mine(result['hex'], rpc) + + result = rpc.tokenlist() + assert_equal(result[0], tokenid) + + # get token balance for token with pubkey + result = rpc.tokenbalance(tokenid, self.pubkey) + assert_success(result) + assert_equal(result['balance'], 198742000000) + assert_equal(result['tokenid'], tokenid) + + # get token balance for token without pubkey + result = rpc.tokenbalance(tokenid) + assert_success(result) + assert_equal(result['balance'], 198742000000) + assert_equal(result['tokenid'], tokenid) + + # this is not a valid assetid + result = rpc.tokeninfo(self.pubkey) + assert_error(result) + + # check tokeninfo for valid token + result = rpc.tokeninfo(tokenid) + assert_success(result) + assert_equal(result['tokenid'], tokenid) + assert_equal(result['owner'], self.pubkey) + assert_equal(result['name'], "DUKE") + assert_equal(result['supply'], 198742000000) + assert_equal(result['description'], "Duke's custom token") + + # invalid numtokens ask + result = rpc.tokenask("-1", tokenid, "1") + assert_error(result) + + # invalid numtokens ask + result = rpc.tokenask("0", tokenid, "1") + assert_error(result) + + # invalid price ask + result = rpc.tokenask("1", tokenid, "-1") + assert_error(result) + + # invalid price ask + result = rpc.tokenask("1", tokenid, "0") + assert_error(result) + + # invalid tokenid ask + result = rpc.tokenask("100", "deadbeef", "1") + assert_error(result) + + # valid ask + tokenask = rpc.tokenask("100", tokenid, "7.77") + tokenaskhex = tokenask['hex'] + tokenaskid = self.send_and_mine(tokenask['hex'], rpc) + result = rpc.tokenorders(tokenid) + order = result[0] + assert order, "found order" + + # invalid ask fillunits + result = rpc.tokenfillask(tokenid, tokenaskid, "0") + assert_error(result) + + # invalid ask fillunits + result = rpc.tokenfillask(tokenid, tokenaskid, "-777") + assert_error(result) + + # valid ask fillunits + fillask = rpc.tokenfillask(tokenid, tokenaskid, "777") + result = self.send_and_mine(fillask['hex'], rpc) + txid = result[0] + assert txid, "found txid" + + # should be no token orders + result = rpc.tokenorders(tokenid) + assert_equal(result, []) + + # checking ask cancellation + testorder = rpc.tokenask("100", tokenid, "7.77") + testorderid = self.send_and_mine(testorder['hex'], rpc) + cancel = rpc.tokencancelask(tokenid, testorderid) + self.send_and_mine(cancel["hex"], rpc) + result = rpc.tokenorders(tokenid) + assert_equal(result, []) + + # invalid numtokens bid + result = rpc.tokenbid("-1", tokenid, "1") + assert_error(result) + + # invalid numtokens bid + result = rpc.tokenbid("0", tokenid, "1") + assert_error(result) + + # invalid price bid + result = rpc.tokenbid("1", tokenid, "-1") + assert_error(result) + + # invalid price bid + result = rpc.tokenbid("1", tokenid, "0") + assert_error(result) + + # invalid tokenid bid + result = rpc.tokenbid("100", "deadbeef", "1") + assert_error(result) + + tokenbid = rpc.tokenbid("100", tokenid, "10") + tokenbidhex = tokenbid['hex'] + tokenbidid = self.send_and_mine(tokenbid['hex'], rpc) + result = rpc.tokenorders(tokenid) + order = result[0] + assert order, "found order" + + # invalid bid fillunits + result = rpc.tokenfillbid(tokenid, tokenbidid, "0") + assert_error(result) + + # invalid bid fillunits + result = rpc.tokenfillbid(tokenid, tokenbidid, "-777") + assert_error(result) + + # valid bid fillunits + fillbid = rpc.tokenfillbid(tokenid, tokenbidid, "1000") + result = self.send_and_mine(fillbid['hex'], rpc) + txid = result[0] + assert txid, "found txid" + + # should be no token orders + result = rpc.tokenorders(tokenid) + assert_equal(result, []) + + # checking bid cancellation + testorder = rpc.tokenbid("100", tokenid, "7.77") + testorderid = self.send_and_mine(testorder['hex'], rpc) + cancel = rpc.tokencancelbid(tokenid, testorderid) + self.send_and_mine(cancel["hex"], rpc) + result = rpc.tokenorders(tokenid) + assert_equal(result, []) + + # invalid token transfer amount (have to add status to CC code!) + randompubkey = "021a559101e355c907d9c553671044d619769a6e71d624f68bfec7d0afa6bd6a96" + result = rpc.tokentransfer(tokenid,randompubkey,"0") + assert_error(result) + + # invalid token transfer amount (have to add status to CC code!) + result = rpc.tokentransfer(tokenid,randompubkey,"-1") + assert_error(result) + + # valid token transfer + sendtokens = rpc.tokentransfer(tokenid,randompubkey,"1") + self.send_and_mine(sendtokens["hex"], rpc) + result = rpc.tokenbalance(tokenid,randompubkey) + assert_equal(result["balance"], 1) + + def run_rewards_tests(self): + rpc = self.nodes[0] + result = rpc.rewardsaddress() + for x in ['myCCAddress(Rewards)', 'myaddress', 'RewardsCCAddress', 'RewardsCCTokensAddress', 'RewardsNormalAddress']: + assert_equal(result[x][0], 'R') + + result = rpc.rewardsaddress(self.pubkey) + for x in ['myCCAddress(Rewards)', 'myaddress', 'RewardsCCAddress', 'RewardsCCTokensAddress', 'RewardsNormalAddress']: + assert_equal(result[x][0], 'R') + + # no rewards yet + result = rpc.rewardslist() + assert_equal(result, []) + + # looking up non-existent reward should return error + result = rpc.rewardsinfo("none") + assert_error(result) + + # creating rewards plan with name > 8 chars, should return error + result = rpc.rewardscreatefunding("STUFFSTUFF", "7777", "25", "0", "10", "10") + assert_error(result) + + # creating rewards plan with 0 funding + result = rpc.rewardscreatefunding("STUFF", "0", "25", "0", "10", "10") + assert_error(result) + + # creating rewards plan with 0 maxdays + result = rpc.rewardscreatefunding("STUFF", "7777", "25", "0", "10", "0") + assert_error(result) + + # creating rewards plan with > 25% APR + result = rpc.rewardscreatefunding("STUFF", "7777", "30", "0", "10", "10") + assert_error(result) + + # creating valid rewards plan + result = rpc.rewardscreatefunding("STUFF", "7777", "25", "0", "10", "10") + assert result['hex'], 'got raw xtn' + fundingtxid = rpc.sendrawtransaction(result['hex']) + assert fundingtxid, 'got txid' + + # confirm the above xtn + rpc.generate(1) + result = rpc.rewardsinfo(fundingtxid) + assert_success(result) + assert_equal(result['name'], 'STUFF') + assert_equal(result['APR'], "25.00000000") + assert_equal(result['minseconds'], 0) + assert_equal(result['maxseconds'], 864000) + assert_equal(result['funding'], "7777.00000000") + assert_equal(result['mindeposit'], "10.00000000") + assert_equal(result['fundingtxid'], fundingtxid) + + # checking if new plan in rewardslist + result = rpc.rewardslist() + assert_equal(result[0], fundingtxid) + + # creating reward plan with already existing name, should return error + result = rpc.rewardscreatefunding("STUFF", "7777", "25", "0", "10", "10") + assert_error(result) + + # add funding amount must be positive + result = rpc.rewardsaddfunding("STUFF", fundingtxid, "-1") + assert_error(result) + + # add funding amount must be positive + result = rpc.rewardsaddfunding("STUFF", fundingtxid, "0") + assert_error(result) + + # adding valid funding + result = rpc.rewardsaddfunding("STUFF", fundingtxid, "555") + addfundingtxid = self.send_and_mine(result['hex'], rpc) + assert addfundingtxid, 'got funding txid' + + # checking if funding added to rewardsplan + result = rpc.rewardsinfo(fundingtxid) + assert_equal(result['funding'], "8332.00000000") + + # trying to lock funds, locking funds amount must be positive + result = rpc.rewardslock("STUFF", fundingtxid, "-5") + assert_error(result) + + # trying to lock funds, locking funds amount must be positive + result = rpc.rewardslock("STUFF", fundingtxid, "0") + assert_error(result) + + # trying to lock less than the min amount is an error + result = rpc.rewardslock("STUFF", fundingtxid, "7") + assert_error(result) + + # locking funds in rewards plan + result = rpc.rewardslock("STUFF", fundingtxid, "10") + assert_success(result) + locktxid = result['hex'] + assert locktxid, "got lock txid" + + # locktxid has not been broadcast yet + result = rpc.rewardsunlock("STUFF", fundingtxid, locktxid) + assert_error(result) + + # broadcast xtn + txid = rpc.sendrawtransaction(locktxid) + assert txid, 'got txid from sendrawtransaction' + + # confirm the xtn above + rpc.generate(1) + + # will not unlock since reward amount is less than tx fee + result = rpc.rewardsunlock("STUFF", fundingtxid, locktxid) + assert_error(result) + + def run_oracles_tests(self): + rpc = self.nodes[0] + rpc1 = self.nodes[1] + + result = rpc1.oraclesaddress() + + result = rpc.oraclesaddress() + assert_success(result) + + for x in ['OraclesCCAddress', 'OraclesNormalAddress', 'myCCAddress(Oracles)','OraclesCCTokensAddress', 'myaddress']: + assert_equal(result[x][0], 'R') + + result = rpc.oraclesaddress(self.pubkey) + assert_success(result) + for x in ['OraclesCCAddress', 'OraclesNormalAddress', 'myCCAddress(Oracles)','OraclesCCTokensAddress', 'myaddress']: + assert_equal(result[x][0], 'R') + + # there are no oracles created yet + result = rpc.oracleslist() + assert_equal(result, []) + + # looking up non-existent oracle should return error. + result = rpc.oraclesinfo("none") + assert_error(result) + + # attempt to create oracle with not valid data type should return error + result = rpc.oraclescreate("Test", "Test", "Test") + assert_error(result) + + # attempt to create oracle with description > 32 symbols should return error + too_long_name = generate_random_string(33) + result = rpc.oraclescreate(too_long_name, "Test", "s") + + + # attempt to create oracle with description > 4096 symbols should return error + too_long_description = generate_random_string(4100) + result = rpc.oraclescreate("Test", too_long_description, "s") + assert_error(result) + # # valid creating oracles of different types + # # using such naming to re-use it for data publishing / reading (e.g. oracle_s for s type) + # valid_formats = ["s", "S", "d", "D", "c", "C", "t", "T", "i", "I", "l", "L", "h", "Ihh"] + # for f in valid_formats: + # result = rpc.oraclescreate("Test", "Test", f) + # assert_success(result) + # globals()["oracle_{}".format(f)] = self.send_and_mine(result['hex'], rpc) + + def run_test (self): + print("Mining blocks...") + rpc = self.nodes[0] + rpc1 = self.nodes[1] + # utxos from block 1 become mature in block 101 + rpc.generate(101) + self.sync_all() + rpc.getinfo() + rpc1.getinfo() + # this corresponds to -pubkey above + print("Importing privkeys") + rpc.importprivkey(self.privkey) + rpc1.importprivkey(self.privkey1) + self.run_faucet_tests() + self.sync_all() + self.run_rewards_tests() + self.sync_all() + self.run_dice_tests() + self.sync_all() + self.run_token_tests() + self.sync_all() + self.run_oracles_tests() + + +if __name__ == '__main__': + CryptoConditionsTest ().main() From f6acb611122551d7a096ac18d5d859c7556c6cc8 Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Thu, 28 Mar 2019 15:57:16 +0100 Subject: [PATCH 002/154] Updated ChannelsCC validation (#20) - More constrained vins/vouts - Fixed CC marker value - Added validation of ChannelsOpen in all tx - Switched to LOGSTREAM for logging --- src/cc/channels.cpp | 284 ++++++++++++++++++++++---------------------- 1 file changed, 142 insertions(+), 142 deletions(-) diff --git a/src/cc/channels.cpp b/src/cc/channels.cpp index 504ba629a..08dedb3f9 100644 --- a/src/cc/channels.cpp +++ b/src/cc/channels.cpp @@ -62,6 +62,8 @@ Possible third iteration: // start of consensus code +#define CC_MARKER_VALUE 10000 + int64_t IsChannelsvout(struct CCcontract_info *cp,const CTransaction& tx,CPubKey srcpub, CPubKey destpub,int32_t v) { char destaddr[65],channeladdr[65],tokenschanneladdr[65]; @@ -126,8 +128,8 @@ uint8_t DecodeChannelsOpRet(const CScript &scriptPubKey, uint256 &tokenid, uint2 { return(f); } - } else fprintf(stderr,"script[0] %02x != EVAL_CHANNELS\n",script[0]); - } else fprintf(stderr,"not enough opret.[%d]\n",(int32_t)vopret.size()); + } else LOGSTREAM("channelscc",CCLOG_DEBUG1, stream << "script[0] " << script[0] << " != EVAL_CHANNELS" << std::endl); + } else LOGSTREAM("channelscc",CCLOG_DEBUG1, stream << "not enough opret.[" << vopret.size() << "]" << std::endl); return(0); } @@ -167,7 +169,7 @@ bool ChannelsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransacti } if ( inputs != outputs ) { - fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs); + LOGSTREAM("channelscc",CCLOG_INFO, stream << "inputs " << inputs << " vs outputs " << outputs << std::endl); return eval->Invalid("mismatched inputs != outputs"); } else return (true); @@ -183,7 +185,7 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & { int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numpayments,p1,param1; bool retval; uint256 txid,hashblock,p3,param3,opentxid,tmp_txid,genhashchain,hashchain,tokenid; - uint8_t funcid,hash[32],hashdest[32]; + uint8_t funcid,hash[32],hashdest[32]; char channeladdress[65],srcmarker[65],destmarker[65],destaddr[65],srcaddr[65],desttokensaddr[65],srctokensaddr[65]; int64_t p2,param2,payment; CPubKey srcpub, destpub; CTransaction channelOpenTx,channelCloseTx,prevTx; @@ -202,9 +204,20 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & else { txid = tx.GetHash(); - memcpy(hash,&txid,sizeof(hash)); + memcpy(hash,&txid,sizeof(hash)); if ( (funcid = DecodeChannelsOpRet(tx.vout[numvouts-1].scriptPubKey, tokenid, opentxid, srcpub, destpub, param1, param2, param3)) != 0) { + if (myGetTransaction(opentxid,channelOpenTx,hashblock)== 0) + return eval->Invalid("invalid channelopen tx!"); + else if ((numvouts=channelOpenTx.vout.size()) > 0 && (DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, numpayments, payment, hashchain)) != 'O') + return eval->Invalid("invalid channelopen OP_RETURN data!"); + GetCCaddress1of2(cp,channeladdress,srcpub,destpub); + GetCCaddress(cp,srcmarker,srcpub); + GetCCaddress(cp,destmarker,destpub); + Getscriptaddress(srcaddr,CScript() << ParseHex(HexStr(srcpub)) << OP_CHECKSIG); + Getscriptaddress(destaddr,CScript() << ParseHex(HexStr(destpub)) << OP_CHECKSIG); + _GetCCaddress(srctokensaddr,EVAL_TOKENS,srcpub); + _GetCCaddress(desttokensaddr,EVAL_TOKENS,destpub); switch ( funcid ) { case 'O': @@ -225,56 +238,53 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & //vout.3: normal output of payment amount to receiver pubkey //vout.n-2: normal change //vout.n-1: opreturn - 'P' opentxid senderspubkey receiverspubkey depth numpayments secret - if (komodo_txnotarizedconfirmed(opentxid) == 0) - return eval->Invalid("channelOpen is not yet confirmed(notarised)!"); + if ( IsCCInput(channelOpenTx.vin[0].scriptSig) != 0 ) + return eval->Invalid("vin.0 is normal for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[0],1,channeladdress,numpayments*payment)==0 ) + return eval->Invalid("vout.0 is CC or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.2 is CC marker to destpub or invalid amount for channelopen!"); + else if (komodo_txnotarizedconfirmed(opentxid) == 0) + return eval->Invalid("channelopen is not yet confirmed(notarised)!"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) - return eval->Invalid("vin.0 is normal for channelPayment!"); + return eval->Invalid("vin.0 is normal for channelpayment!"); else if ( IsCCInput(tx.vin[1].scriptSig) == 0 ) - return eval->Invalid("vin.1 is CC for channelPayment!"); + return eval->Invalid("vin.1 is CC for channelpayment!"); else if ( IsCCInput(tx.vin[2].scriptSig) == 0 ) - return eval->Invalid("vin.2 is CC for channelPayment!"); - else if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition()==0 ) - return eval->Invalid("vout.0 is CC for channelPayment!"); - else if ( IsChannelsMarkervout(cp,tx,srcpub,1)==0 ) - return eval->Invalid("vout.1 is CC for channelPayment (marker to srcPub)!"); - else if ( IsChannelsMarkervout(cp,tx,destpub,2)==0 ) - return eval->Invalid("vout.2 is CC for channelPayment (marker to dstPub)!"); - else if ( tokenid!=zeroid && tx.vout[3].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("vout.3 is CC for channelPayment!"); - else if ( tokenid==zeroid && tx.vout[3].scriptPubKey.IsPayToCryptoCondition() != 0 ) - return eval->Invalid("vout.3 is normal for channelPayment!"); - else if ( tokenid!=zeroid && tx.vout[3].scriptPubKey!=MakeCC1vout(EVAL_TOKENS,tx.vout[3].nValue,destpub).scriptPubKey) - return eval->Invalid("payment funds do not go to receiver!"); - else if ( tokenid==zeroid && tx.vout[3].scriptPubKey!=CScript() << ParseHex(HexStr(destpub)) << OP_CHECKSIG) - return eval->Invalid("payment funds do not go to receiver!"); + return eval->Invalid("vin.2 is CC for channelpayment!"); + else if ( ConstrainVout(tx.vout[0],1,channeladdress,(numpayments-param2)*payment)==0 ) + return eval->Invalid("vout.0 is CC or invalid CC change amount for channelpayment!"); + else if ( ConstrainVout(tx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelpayment!"); + else if ( ConstrainVout(tx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.2 is CC marker to destpub or invalid amount for channelpayment!"); + else if ( tokenid!=zeroid && ConstrainVout(tx.vout[3],1,desttokensaddr,param2*payment)==0 ) + return eval->Invalid("vout.3 is CC or invalid amount or invalid receiver for channelpayment!"); + else if ( tokenid==zeroid && ConstrainVout(tx.vout[3],0,destaddr,param2*payment)==0 ) + return eval->Invalid("vout.3 is normal or invalid amount or invalid receiver for channelpayment!"); else if ( param1 > CHANNELS_MAXPAYMENTS) return eval->Invalid("too many payment increments!"); else - { - if (myGetTransaction(opentxid,channelOpenTx,hashblock) != 0) + { + endiancpy(hash, (uint8_t * ) & param3, 32); + for (i = 0; i < numpayments-param1; i++) { - if ((numvouts=channelOpenTx.vout.size()) > 0 && (funcid=DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, numpayments, payment, hashchain)) != 0 && funcid!='O') - return eval->Invalid("invalid channelopen OP_RETURN data!"); - endiancpy(hash, (uint8_t * ) & param3, 32); - for (i = 0; i < numpayments-param1; i++) - { - vcalc_sha256(0, hashdest, hash, 32); - memcpy(hash, hashdest, 32); - } - endiancpy((uint8_t*)&genhashchain,hashdest,32); - if (hashchain!=genhashchain) - return eval->Invalid("invalid secret for payment, does not reach final hashchain!"); - else if (tx.vout[3].nValue != param2*payment) - return eval->Invalid("vout amount does not match number_of_payments*payment!"); + vcalc_sha256(0, hashdest, hash, 32); + memcpy(hash, hashdest, 32); } + endiancpy((uint8_t*)&genhashchain,hashdest,32); + if (hashchain!=genhashchain) + return eval->Invalid("invalid secret for payment, does not reach final hashchain!"); + else if (tx.vout[3].nValue != param2*payment) + return eval->Invalid("vout amount does not match number_of_payments*payment!"); if (myGetTransaction(tx.vin[1].prevout.hash,prevTx,hashblock) != 0) { if ((numvouts=prevTx.vout.size()) > 0 && DecodeChannelsOpRet(prevTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, p1, p2, p3) == 0) return eval->Invalid("invalid previous tx OP_RETURN data!"); - else if (tx.vout[1].scriptPubKey != prevTx.vout[1].scriptPubKey) - return eval->Invalid("invalid destination for sender marker!"); - else if (tx.vout[2].scriptPubKey != prevTx.vout[2].scriptPubKey) - return eval->Invalid("invalid destination for receiver marker!"); + else if ((*cp->ismyvin)(tx.vin[2].scriptSig) == 0 || prevTx.vout[tx.vin[2].prevout.n].nValue!=CC_MARKER_VALUE) + return eval->Invalid("vin.2 is CC marker or invalid marker amount for channelpayment!"); else if (param1+param2!=p1) return eval->Invalid("invalid payment depth!"); else if (tx.vout[3].nValue > prevTx.vout[0].nValue) @@ -290,37 +300,37 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & //vout.1: CC vout marker to senders pubKey //vout.2: CC vout marker to receiver pubkey //vout.n-2: normal change - //vout.n-1: opreturn - 'C' opentxid senderspubkey receiverspubkey 0 0 0 - if (komodo_txnotarizedconfirmed(opentxid) == 0) - return eval->Invalid("channelOpen is not yet confirmed(notarised)!"); + //vout.n-1: opreturn - 'C' opentxid senderspubkey receiverspubkey numpayments payment 0 + if ( IsCCInput(channelOpenTx.vin[0].scriptSig) != 0 ) + return eval->Invalid("vin.0 is normal for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[0],1,channeladdress,numpayments*payment)==0 ) + return eval->Invalid("vout.0 is CC or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.2 is CC marker to destpub or invalid amount for channelopen!"); + else if (komodo_txnotarizedconfirmed(opentxid) == 0) + return eval->Invalid("channelopen is not yet confirmed(notarised)!"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) - return eval->Invalid("vin.0 is normal for channelClose!"); + return eval->Invalid("vin.0 is normal for channelclose!"); else if ( IsCCInput(tx.vin[1].scriptSig) == 0 ) - return eval->Invalid("vin.1 is CC for channelClose!"); + return eval->Invalid("vin.1 is CC for channelclose!"); else if ( IsCCInput(tx.vin[2].scriptSig) == 0 ) - return eval->Invalid("vin.2 is CC for channelClose!"); - else if ( IsChannelsvout(cp,tx,srcpub,destpub,0)==0 ) - return eval->Invalid("vout.0 is CC for channelClose!"); - else if ( IsChannelsMarkervout(cp,tx,srcpub,1)==0 ) - return eval->Invalid("vout.1 is CC for channelClose (marker to srcPub)!"); - else if ( IsChannelsMarkervout(cp,tx,destpub,2)==0 ) - return eval->Invalid("vout.2 is CC for channelClose (marker to dstPub)!"); + return eval->Invalid("vin.2 is CC for channelclose!"); + else if ( ConstrainVout(tx.vout[0],1,channeladdress,0)==0 ) + return eval->Invalid("vout.0 is CC for channelclose!"); + else if ( ConstrainVout(tx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelclose!"); + else if ( ConstrainVout(tx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.2 is CC marker to destpub or invalid amount for channelclose!"); else if ( param1 > CHANNELS_MAXPAYMENTS) return eval->Invalid("too many payment increments!"); - else if (myGetTransaction(opentxid,channelOpenTx,hashblock) == 0) - return eval->Invalid("invalid open txid!"); - else if ((numvouts=channelOpenTx.vout.size()) > 0 && DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, numpayments, payment, hashchain) != 'O') - return eval->Invalid("invalid channelopen OP_RETURN data!"); - else if (tx.vout[0].nValue != param1*payment) - return eval->Invalid("vout amount does not match number_of_payments*payment!"); else if (myGetTransaction(tx.vin[1].prevout.hash,prevTx,hashblock) != 0) { if ((numvouts=prevTx.vout.size()) > 0 && DecodeChannelsOpRet(prevTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, p1, p2, p3) == 0) return eval->Invalid("invalid previous tx OP_RETURN data!"); - else if (tx.vout[1].scriptPubKey != prevTx.vout[1].scriptPubKey) - return eval->Invalid("invalid destination for sender marker!"); - else if (tx.vout[2].scriptPubKey != prevTx.vout[2].scriptPubKey) - return eval->Invalid("invalid destination for receiver marker!"); + else if ((*cp->ismyvin)(tx.vin[2].scriptSig) == 0 || prevTx.vout[tx.vin[2].prevout.n].nValue!=CC_MARKER_VALUE) + return eval->Invalid("vin.2 is CC marker or invalid marker amount for channelclose!"); else if (tx.vout[0].nValue != prevTx.vout[0].nValue) return eval->Invalid("invalid CC amount, amount must match funds in channel"); } @@ -334,50 +344,40 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & //vout.2: normal output of CC input to senders pubkey //vout.n-2: normal change //vout.n-1: opreturn - 'R' opentxid senderspubkey receiverspubkey numpayments payment closetxid - if (komodo_txnotarizedconfirmed(opentxid) == 0) - return eval->Invalid("channelOpen is not yet confirmed(notarised)!"); + if ( IsCCInput(channelOpenTx.vin[0].scriptSig) != 0 ) + return eval->Invalid("vin.0 is normal for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[0],1,channeladdress,numpayments*payment)==0 ) + return eval->Invalid("vout.0 is CC or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelopen!"); + else if ( ConstrainVout(channelOpenTx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.2 is CC marker to destpub or invalid amount for channelopen!"); + else if (komodo_txnotarizedconfirmed(opentxid) == 0) + return eval->Invalid("channelopen is not yet confirmed(notarised)!"); else if (komodo_txnotarizedconfirmed(param3) == 0) return eval->Invalid("channelClose is not yet confirmed(notarised)!"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) - return eval->Invalid("vin.0 is normal for channelRefund!"); + return eval->Invalid("vin.0 is normal for channelrefund!"); else if ( IsCCInput(tx.vin[1].scriptSig) == 0 ) - return eval->Invalid("vin.1 is CC for channelRefund!"); + return eval->Invalid("vin.1 is CC for channelrefund!"); else if ( IsCCInput(tx.vin[2].scriptSig) == 0 ) - return eval->Invalid("vin.2 is CC for channelRefund!"); - else if ( IsChannelsMarkervout(cp,tx,srcpub,0)==0 ) - return eval->Invalid("vout.0 is CC for channelRefund (marker to srcPub)!"); - else if ( IsChannelsMarkervout(cp,tx,destpub,1)==0 ) - return eval->Invalid("vout.1 is CC for channelRefund (marker to dstPub)!"); - else if ( tokenid!=zeroid && tx.vout[2].scriptPubKey.IsPayToCryptoCondition() == 0 ) - return eval->Invalid("vout.2 is CC for channelPayment!"); - else if ( tokenid==zeroid && tx.vout[2].scriptPubKey.IsPayToCryptoCondition() != 0 ) - return eval->Invalid("vout.2 is normal for channelPayment!"); - else if ( tokenid!=zeroid && tx.vout[2].scriptPubKey!=MakeCC1vout(EVAL_TOKENS,tx.vout[2].nValue,srcpub).scriptPubKey) - return eval->Invalid("payment funds do not go to sender!"); - else if ( tokenid==zeroid && tx.vout[2].scriptPubKey!=CScript() << ParseHex(HexStr(srcpub)) << OP_CHECKSIG) - return eval->Invalid("payment funds do not go to sender!"); + return eval->Invalid("vin.2 is CC for channelrefund!"); + else if ( ConstrainVout(tx.vout[0],1,srcmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.0 is CC marker to srcpub or invalid amount for channelrefund!"); + else if ( ConstrainVout(tx.vout[1],1,destmarker,CC_MARKER_VALUE)==0 ) + return eval->Invalid("vout.1 is CC marker to destpub or invalid amount for channelrefund!"); + else if ( tokenid!=zeroid && ConstrainVout(tx.vout[2],1,srctokensaddr,param1*payment)==0 ) + return eval->Invalid("vout.2 is CC or invalid amount or invalid receiver for channelrefund!"); + else if ( tokenid==zeroid && ConstrainVout(tx.vout[2],0,srcaddr,param1*payment)==0 ) + return eval->Invalid("vout.2 is normal or invalid amount or invalid receiver for channelrefund!"); else if ( param1 > CHANNELS_MAXPAYMENTS) return eval->Invalid("too many payment increments!"); - else if (myGetTransaction(opentxid,channelOpenTx,hashblock) == 0) - return eval->Invalid("invalid open txid!"); - else if ((numvouts=channelOpenTx.vout.size()) > 0 && DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, numpayments, payment, hashchain) != 'O') - return eval->Invalid("invalid channelopen OP_RETURN data!"); - else if (myGetTransaction(param3,channelCloseTx,hashblock) == 0) - return eval->Invalid("invalid close txid!"); - else if ((numvouts=channelCloseTx.vout.size()) > 0 && DecodeChannelsOpRet(channelCloseTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, param1, param2, param3) != 'C') - return eval->Invalid("invalid channelclose OP_RETURN data!"); - else if (tmp_txid!=opentxid) - return eval->Invalid("invalid close tx, opentxid do not match on close and refund!"); - else if (tx.vout[2].nValue != param1*payment) - return eval->Invalid("vout amount does not match number_of_payments*payment!"); else if (myGetTransaction(tx.vin[1].prevout.hash,prevTx,hashblock) != 0) { if ((numvouts=prevTx.vout.size()) > 0 && DecodeChannelsOpRet(prevTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, p1, p2, p3) == 0) return eval->Invalid("invalid previous tx OP_RETURN data!"); - else if (tx.vout[0].scriptPubKey != prevTx.vout[1].scriptPubKey) - return eval->Invalid("invalid destination for sender marker!"); - else if (tx.vout[1].scriptPubKey != prevTx.vout[2].scriptPubKey) - return eval->Invalid("invalid destination for receiver marker!"); + else if ((*cp->ismyvin)(tx.vin[2].scriptSig) == 0 || prevTx.vout[tx.vin[2].prevout.n].nValue!=CC_MARKER_VALUE) + return eval->Invalid("vin.2 is CC marker or invalid marker amount for channelrefund!"); else if (tx.vout[2].nValue != prevTx.vout[0].nValue) return eval->Invalid("invalid amount, refund amount and funds in channel must match!"); } @@ -390,8 +390,8 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & else return eval->Invalid("unexpected channels missing funcid"); retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); if ( retval != 0 ) - fprintf(stderr,"Channel tx validated\n"); - else fprintf(stderr,"Channel tx invalid\n"); + LOGSTREAM("channels",CCLOG_INFO, stream << "Channels tx validated" << std::endl); + else fprintf(stderr,"Channels tx invalid\n"); return(retval); } } @@ -415,7 +415,7 @@ int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx, C } else { - fprintf(stderr,"invalid channel open txid\n"); + LOGSTREAM("channelscc",CCLOG_INFO, stream << "invalid channel open txid" << std::endl); return 0; } if (srcpub==mypk) marker=1; @@ -473,7 +473,7 @@ std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int64 if ( numpayments <= 0 || payment <= 0 || numpayments > CHANNELS_MAXPAYMENTS ) { CCerror = strprintf("invalid ChannelOpen param numpayments.%d max.%d payment.%lld\n",numpayments,CHANNELS_MAXPAYMENTS,(long long)payment); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } cp = CCinit(&C,EVAL_CHANNELS); @@ -484,11 +484,11 @@ std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int64 funds = numpayments * payment; if (tokenid!=zeroid) { - amount=AddNormalinputs(mtx,mypk,3*txfee,5); + amount=AddNormalinputs(mtx,mypk,txfee+2*CC_MARKER_VALUE,5); tokens=AddTokenCCInputs(cpTokens, mtx, mypk, tokenid, funds, 64); } - else amount=AddNormalinputs(mtx,mypk,funds+3*txfee,64); - if (amount+tokens >= funds+2*txfee) + else amount=AddNormalinputs(mtx,mypk,funds+txfee+2*CC_MARKER_VALUE,64); + if (amount+tokens >= funds+txfee+2*CC_MARKER_VALUE) { hentropy = DiceHashEntropy(entropy,mtx.vin[0].prevout.hash,mtx.vin[0].prevout.n,1); endiancpy(hash,(uint8_t *)&hentropy,32); @@ -500,13 +500,13 @@ std::string ChannelOpen(uint64_t txfee,CPubKey destpub,int32_t numpayments,int64 endiancpy((uint8_t *)&hashchain,hashdest,32); if (tokenid!=zeroid) mtx.vout.push_back(MakeTokensCC1of2vout(EVAL_CHANNELS,funds,mypk,destpub)); else mtx.vout.push_back(MakeCC1of2vout(EVAL_CHANNELS,funds,mypk,destpub)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,mypk)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,destpub)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,mypk)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,destpub)); if (tokenid!=zeroid && tokens>funds) mtx.vout.push_back(MakeCC1vout(EVAL_TOKENS,tokens-funds,mypk)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeChannelsOpRet('O',tokenid,zeroid,mypk,destpub,numpayments,payment,hashchain))); } CCerror = strprintf("error adding funds"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } @@ -526,7 +526,7 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 if (GetTransaction(opentxid,channelOpenTx,hashblock,false) == 0) { CCerror = strprintf("invalid channel open txid"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } if ((numvouts=channelOpenTx.vout.size()) > 0 && DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey, tokenid, txid, srcpub, destpub, totalnumpayments, payment, hashchain)=='O') @@ -534,23 +534,23 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 if (mypk != srcpub && mypk != destpub) { CCerror = strprintf("this is not our channel"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } else if (amount % payment != 0 || amount 0) + if (AddNormalinputs(mtx,mypk,txfee+CC_MARKER_VALUE,3) > 0) { if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && (change=funds-amount)>=0) { @@ -562,12 +562,12 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 if (numpayments > prevdepth) { CCerror = strprintf("not enough funds in channel for that amount"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } else if (numpayments == 0) { CCerror = strprintf("invalid amount"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if (secret!=zeroid) @@ -582,7 +582,7 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 if (gensecret!=hashchain) { CCerror = strprintf("invalid secret supplied"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } } @@ -605,13 +605,13 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 else { CCerror = strprintf("invalid previous tx"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } if (tokenid!=zeroid) mtx.vout.push_back(MakeTokensCC1of2vout(EVAL_CHANNELS, change, srcpub, destpub)); else mtx.vout.push_back(MakeCC1of2vout(EVAL_CHANNELS, change, srcpub, destpub)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,srcpub)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,destpub)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,srcpub)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,destpub)); if (tokenid!=zeroid) mtx.vout.push_back(MakeCC1vout(EVAL_TOKENS, amount, destpub)); else mtx.vout.push_back(CTxOut(amount, CScript() << ParseHex(HexStr(destpub)) << OP_CHECKSIG)); return (FinalizeCCTx(0, cp, mtx, mypk, txfee, EncodeChannelsOpRet('P', tokenid, opentxid, srcpub, destpub, prevdepth-numpayments, numpayments, secret))); @@ -619,12 +619,12 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 else { CCerror = strprintf("error adding CC inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } } CCerror = strprintf("error adding normal inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } @@ -645,40 +645,40 @@ std::string ChannelClose(uint64_t txfee,uint256 opentxid) if (GetTransaction(opentxid,channelOpenTx,hashblock,false) == 0) { CCerror = strprintf("invalid channel open txid"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if ((numvouts=channelOpenTx.vout.size()) < 1 || DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey,tokenid,tmp_txid,srcpub,destpub,numpayments,payment,hashchain)!='O') { CCerror = strprintf("invalid channel open tx"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if (mypk != srcpub) { CCerror = strprintf("cannot close, you are not channel owner"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } - if ( AddNormalinputs(mtx,mypk,2*txfee,3) > 0 ) + if ( AddNormalinputs(mtx,mypk,txfee+CC_MARKER_VALUE,3) > 0 ) { if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && funds>0) { if (tokenid!=zeroid) mtx.vout.push_back(MakeTokensCC1of2vout(EVAL_CHANNELS, funds, mypk, destpub)); else mtx.vout.push_back(MakeCC1of2vout(EVAL_CHANNELS, funds, mypk, destpub)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,mypk)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,destpub)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,mypk)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,destpub)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeChannelsOpRet('C',tokenid,opentxid,mypk,destpub,funds/payment,payment,zeroid))); } else { CCerror = strprintf("error adding CC inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } } CCerror = strprintf("error adding normal inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } @@ -699,48 +699,48 @@ std::string ChannelRefund(uint64_t txfee,uint256 opentxid,uint256 closetxid) if (GetTransaction(closetxid,channelCloseTx,hashblock,false) == 0) { CCerror = strprintf("invalid channel close txid"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if ((numvouts=channelCloseTx.vout.size()) < 1 || DecodeChannelsOpRet(channelCloseTx.vout[numvouts-1].scriptPubKey,tokenid,txid,srcpub,destpub,param1,param2,param3)!='C') { CCerror = strprintf("invalid channel close tx"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if (txid!=opentxid) { CCerror = strprintf("open and close txid are not from same channel"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if (GetTransaction(opentxid,channelOpenTx,hashblock,false) == 0) { CCerror = strprintf("invalid channel open txid"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if ((numvouts=channelOpenTx.vout.size()) < 1 || DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey,tokenid,txid,srcpub,destpub,numpayments,payment,hashchain)!='O') { CCerror = strprintf("invalid channel open tx"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } if (mypk != srcpub) { CCerror = strprintf("cannot refund, you are not the channel owner"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } - if ( AddNormalinputs(mtx,mypk,2*txfee,3) > 0 ) + if ( AddNormalinputs(mtx,mypk,txfee+CC_MARKER_VALUE,3) > 0 ) { if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && funds>0) { if ((GetTransaction(prevtxid,prevTx,hashblock,false) != 0) && (numvouts=prevTx.vout.size()) > 0 && DecodeChannelsOpRet(prevTx.vout[numvouts-1].scriptPubKey, tokenid, txid, srcpub, destpub, param1, param2, param3) != 0) { - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,mypk)); - mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,txfee,destpub)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,mypk)); + mtx.vout.push_back(MakeCC1vout(EVAL_CHANNELS,CC_MARKER_VALUE,destpub)); if (tokenid!=zeroid) mtx.vout.push_back(MakeCC1vout(EVAL_TOKENS,funds,mypk)); else mtx.vout.push_back(CTxOut(funds,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeChannelsOpRet('R',tokenid,opentxid,mypk,destpub,funds/payment,payment,closetxid))); @@ -748,19 +748,19 @@ std::string ChannelRefund(uint64_t txfee,uint256 opentxid,uint256 closetxid) else { CCerror = strprintf("previous tx is invalid"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } } else { CCerror = strprintf("error adding CC inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } } CCerror = strprintf("error adding normal inputs"); - fprintf(stderr,"%s\n",CCerror.c_str()); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return(""); } @@ -781,7 +781,7 @@ UniValue ChannelsList() txid = it->first.txhash; vout = (int32_t)it->first.index; nValue = (int64_t)it->second; - if ( (vout == 1 || vout == 2) && nValue == 10000 && GetTransaction(txid,tx,hashBlock,false) != 0 && (numvouts= tx.vout.size()) > 0 ) + if ( (vout == 1 || vout == 2) && nValue == CC_MARKER_VALUE && GetTransaction(txid,tx,hashBlock,false) != 0 && (numvouts= tx.vout.size()) > 0 ) { if (DecodeChannelsOpRet(tx.vout[numvouts-1].scriptPubKey,tokenid,tmp_txid,srcpub,destpub,param1,param2,param3) == 'O') { From 180ca470b0a9f79f7a8964f4224e62ac218c0c0e Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Thu, 28 Mar 2019 17:16:09 +0100 Subject: [PATCH 003/154] Fix CC change calculation for channelspayment --- src/cc/channels.cpp | 28 ++++++++++++++++++++++++++-- src/wallet/rpcwallet.cpp | 4 ++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/cc/channels.cpp b/src/cc/channels.cpp index 08dedb3f9..2887c18e1 100644 --- a/src/cc/channels.cpp +++ b/src/cc/channels.cpp @@ -254,8 +254,6 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & return eval->Invalid("vin.1 is CC for channelpayment!"); else if ( IsCCInput(tx.vin[2].scriptSig) == 0 ) return eval->Invalid("vin.2 is CC for channelpayment!"); - else if ( ConstrainVout(tx.vout[0],1,channeladdress,(numpayments-param2)*payment)==0 ) - return eval->Invalid("vout.0 is CC or invalid CC change amount for channelpayment!"); else if ( ConstrainVout(tx.vout[1],1,srcmarker,CC_MARKER_VALUE)==0 ) return eval->Invalid("vout.1 is CC marker to srcpub or invalid amount for channelpayment!"); else if ( ConstrainVout(tx.vout[2],1,destmarker,CC_MARKER_VALUE)==0 ) @@ -283,6 +281,8 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & { if ((numvouts=prevTx.vout.size()) > 0 && DecodeChannelsOpRet(prevTx.vout[numvouts-1].scriptPubKey, tokenid, tmp_txid, srcpub, destpub, p1, p2, p3) == 0) return eval->Invalid("invalid previous tx OP_RETURN data!"); + else if ( ConstrainVout(tx.vout[0],1,channeladdress,(p1-param2)*payment)==0 ) + return eval->Invalid("vout.0 is CC or invalid CC change amount for channelpayment!"); else if ((*cp->ismyvin)(tx.vin[2].scriptSig) == 0 || prevTx.vout[tx.vin[2].prevout.n].nValue!=CC_MARKER_VALUE) return eval->Invalid("vin.2 is CC marker or invalid marker amount for channelpayment!"); else if (param1+param2!=p1) @@ -550,6 +550,12 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } + if (komodo_txnotarizedconfirmed(opentxid)==false) + { + CCerror = strprintf("channelsopen tx not yet confirmed/notarized"); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); + return(""); + } if (AddNormalinputs(mtx,mypk,txfee+CC_MARKER_VALUE,3) > 0) { if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && (change=funds-amount)>=0) @@ -654,6 +660,12 @@ std::string ChannelClose(uint64_t txfee,uint256 opentxid) LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } + if (komodo_txnotarizedconfirmed(opentxid)==false) + { + CCerror = strprintf("channelsopen tx not yet confirmed/notarized"); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); + return(""); + } if (mypk != srcpub) { CCerror = strprintf("cannot close, you are not channel owner"); @@ -708,6 +720,12 @@ std::string ChannelRefund(uint64_t txfee,uint256 opentxid,uint256 closetxid) LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } + if (komodo_txnotarizedconfirmed(closetxid)==false) + { + CCerror = strprintf("channelsclose tx not yet confirmed/notarized"); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); + return(""); + } if (txid!=opentxid) { CCerror = strprintf("open and close txid are not from same channel"); @@ -720,6 +738,12 @@ std::string ChannelRefund(uint64_t txfee,uint256 opentxid,uint256 closetxid) LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); return (""); } + if (komodo_txnotarizedconfirmed(opentxid)==false) + { + CCerror = strprintf("channelsopen tx not yet confirmed/notarized"); + LOGSTREAM("channelscc",CCLOG_INFO, stream << CCerror << std::endl); + return(""); + } if ((numvouts=channelOpenTx.vout.size()) < 1 || DecodeChannelsOpRet(channelOpenTx.vout[numvouts-1].scriptPubKey,tokenid,txid,srcpub,destpub,numpayments,payment,hashchain)!='O') { CCerror = strprintf("invalid channel open tx"); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 107508c18..5456f3b9c 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6105,6 +6105,7 @@ UniValue channelsopen(const UniValue& params, bool fHelp) tokenid=Parseuint256((char *)params[3].get_str().c_str()); } hex = ChannelOpen(0,pubkey2pk(destpub),numpayments,payment,tokenid); + RETURN_IF_ERROR(CCerror); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -6135,6 +6136,7 @@ UniValue channelspayment(const UniValue& params, bool fHelp) secret = Parseuint256((char *)params[2].get_str().c_str()); } hex = ChannelPayment(0,opentxid,amount,secret); + RETURN_IF_ERROR(CCerror); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -6155,6 +6157,7 @@ UniValue channelsclose(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); opentxid = Parseuint256((char *)params[0].get_str().c_str()); hex = ChannelClose(0,opentxid); + RETURN_IF_ERROR(CCerror); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -6176,6 +6179,7 @@ UniValue channelsrefund(const UniValue& params, bool fHelp) opentxid = Parseuint256((char *)params[0].get_str().c_str()); closetxid = Parseuint256((char *)params[1].get_str().c_str()); hex = ChannelRefund(0,opentxid,closetxid); + RETURN_IF_ERROR(CCerror); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); From d464a48a0d6aba57bcaf880ff28391f09fc07146 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 1 Apr 2019 22:11:26 -1100 Subject: [PATCH 004/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 3ed1cab49..52d9f0776 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1718,7 +1718,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -ASSETCHAINS_BLOCKTIME ) // must be close to last block timestamp + if ( lag2 < -60 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) From 79dab848b4b73a652e7a81aa9b36047a6b096872 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 1 Apr 2019 22:16:21 -1100 Subject: [PATCH 005/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 52d9f0776..7624a57d5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1718,7 +1718,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -60 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp + if ( lag2 < -63 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) From cbd4c636d4ea16550b2bcbef5d79d5848803368d Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 1 Apr 2019 22:17:59 -1100 Subject: [PATCH 006/154] -120 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7624a57d5..7e1ede5d5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1718,7 +1718,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -63 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp + if ( lag2 < -120 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) From 175d0fa4a6f78d75d3d493f0b596fb9fb59c0811 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 1 Apr 2019 22:19:33 -1100 Subject: [PATCH 007/154] 500 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7e1ede5d5..401199837 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1697,7 +1697,7 @@ CScript komodo_mineropret(int32_t nHeight) int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 350; + int32_t testchain_exemption = 500; std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,prevtime,maxflag,lag,lag2,lag3,n; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { From c90448edbae897f41dca1225d240656ecfec5135 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 1 Apr 2019 23:09:51 -1100 Subject: [PATCH 008/154] prices rpc call stub --- src/komodo_gateway.h | 26 ++++++++++---- src/rpc/blockchain.cpp | 80 ++++++++++++++++++++++++++++++++---------- src/rpc/server.cpp | 1 + src/rpc/server.h | 2 ++ 4 files changed, 84 insertions(+), 25 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 401199837..a59e47a04 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1553,7 +1553,7 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR // komodo_heightpricebits() extracts the price data in the coinbase for nHeight -int32_t komodo_heightpricebits(uint32_t prevbits[4],int32_t nHeight) +int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) { CBlockIndex *pindex; CBlock block; CTransaction tx; int32_t numvouts; std::vector vopret; if ( (pindex= komodo_chainactive(nHeight)) != 0 ) @@ -1565,8 +1565,8 @@ int32_t komodo_heightpricebits(uint32_t prevbits[4],int32_t nHeight) GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); if ( vopret.size() >= PRICES_SIZEBIT0 ) { - memcpy(prevbits,vopret.data(),vopret.size()); - return(0); + memcpy(heightbits,vopret.data(),vopret.size()); + return((int32_t)(vopret.size()/sizeof(uint32_t))); } } } @@ -1574,6 +1574,20 @@ int32_t komodo_heightpricebits(uint32_t prevbits[4],int32_t nHeight) return(-1); } +int32_t komodo_prices(uint32_t *prices,uint32_t *correlated,uint32_t *smoothed,int32_t height) +{ + int32_t i,n; + n = komodo_heightpricebits(prices,height); + for (i=0; i 0 ) { memcpy(pricebits,Mineropret.data(),Mineropret.size()); memset(maxflags,0,sizeof(maxflags)); @@ -1718,7 +1732,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -120 )//-ASSETCHAINS_BLOCKTIME/2 ) // must be close to last block timestamp + if ( lag2 < -testchain_exemption ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) @@ -1734,7 +1748,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i btcgbp = (double)pricebits[2]/10000; btceur = (double)pricebits[3]/10000; fprintf(stderr,"ht.%d: lag.%d %.4f USD, %.4f GBP, %.4f EUR, GBPUSD %.6f, EURUSD %.6f, EURGBP %.6f [%d]\n",nHeight,lag,btcusd,btcgbp,btceur,btcusd/btcgbp,btcusd/btceur,btcgbp/btceur,lag2); - if ( komodo_heightpricebits(prevbits,nHeight-1) == 0 ) + if ( komodo_heightpricebits(prevbits,nHeight-1) > 0 ) { if ( nHeight < testchain_exemption ) { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6dac120e4..fc9ec5f7a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1174,38 +1174,80 @@ UniValue paxprice(const UniValue& params, bool fHelp) return ret; } -UniValue paxprices(const UniValue& params, bool fHelp) +int32_t komodo_prices(uint32_t *prices,uint32_t *correlated,uint32_t *smoothed,int32_t height); + +UniValue prices(const UniValue& params, bool fHelp) { - if ( fHelp || params.size() != 3 ) - throw runtime_error("paxprices \"base\" \"rel\" maxsamples\n"); + if ( fHelp || params.size() != 1 ) + throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t relvolume,prices[4096]; uint32_t i,n; int32_t heights[sizeof(prices)/sizeof(*prices)]; - std::string base = params[0].get_str(); - std::string rel = params[1].get_str(); - int32_t maxsamples = atoi(params[2].get_str().c_str()); + UniValue ret(UniValue::VOBJ); uint32_t *prices,*correlated,*smoothed; uint32_t i,firstn=-1,n,nextheight,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + int32_t maxsamples = atoi(params[0].get_str().c_str()); if ( maxsamples < 1 ) maxsamples = 1; else if ( maxsamples > sizeof(heights)/sizeof(*heights) ) maxsamples = sizeof(heights)/sizeof(*heights); - ret.push_back(Pair("base", base)); - ret.push_back(Pair("rel", rel)); - n = komodo_paxprices(heights,prices,maxsamples,(char *)base.c_str(),(char *)rel.c_str()); + nextheight = komodo_nextheight(); UniValue a(UniValue::VARR); - for (i=0; idaywindow+2; i++,ht--) { - UniValue item(UniValue::VOBJ); - if ( heights[i] < 0 || heights[i] > chainActive.Height() ) + if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); else { - CBlockIndex *pblockindex = chainActive[heights[i]]; - - item.push_back(Pair("t", (int64_t)pblockindex->nTime)); - item.push_back(Pair("p", (double)prices[i] / COIN)); - a.push_back(item); + if ( (n= komodo_prices(rawprices,ht)) > 0 ) + { + if ( firstn == -1 ) + firstn = n; + else if ( n != firstn ) + throw JSONRPCError(RPC_INVALID_PARAMETER, "numprices != first numprices"); + else + { + for (j=0; j Date: Tue, 2 Apr 2019 00:08:01 -1100 Subject: [PATCH 009/154] KOMODO_LOCALPRICE_CACHESIZE --- src/bitcoind.cpp | 4 +- src/komodo_gateway.h | 176 ++++++++++++++++++++++++++++++++--------- src/komodo_utils.h | 4 +- src/rpc/blockchain.cpp | 82 ++++++++++--------- 4 files changed, 187 insertions(+), 79 deletions(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index b65fcf1a9..4e010d28e 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -64,7 +64,7 @@ extern uint64_t ASSETCHAINS_CBOPRET; void komodo_passport_iteration(); uint64_t komodo_interestsum(); int32_t komodo_longestchain(); -void komodo_cbopretupdate(); +void komodo_cbopretupdate(int32_t forceflag); void WaitForShutdown(boost::thread_group* threadGroup) { @@ -89,7 +89,7 @@ void WaitForShutdown(boost::thread_group* threadGroup) //komodo_interestsum(); //komodo_longestchain(); if ( ASSETCHAINS_CBOPRET != 0 ) - komodo_cbopretupdate(); + komodo_cbopretupdate(0); for (i=0; i<=ASSETCHAINS_BLOCKTIME/5; i++) { fShutdown = ShutdownRequested(); diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a59e47a04..6035cf82f 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1551,6 +1551,15 @@ void komodo_passport_iteration() extern std::vector Mineropret; // opreturn data set by the data gathering code #define PRICES_MAXCHANGE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR +#define KOMODO_LOCALPRICE_CACHESIZE 7 +uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; + +void komodo_PriceCache_shift() +{ + int32_t i; + for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>=0; i--) + memcpy(PriceCache[i],PriceCache[i-1],sizeof(PriceCache[i])); +} // komodo_heightpricebits() extracts the price data in the coinbase for nHeight int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) @@ -1574,20 +1583,6 @@ int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) return(-1); } -int32_t komodo_prices(uint32_t *prices,uint32_t *correlated,uint32_t *smoothed,int32_t height) -{ - int32_t i,n; - n = komodo_heightpricebits(prices,height); - for (i=0; i vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,prevtime,maxflag,lag,lag2,lag3,n; uint32_t now = (uint32_t)time(NULL); + std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { GetOpReturnData(scriptPubKey,vopret); @@ -1778,16 +1773,48 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( localbits[i] == 0 ) localbits[i] = prevbits[i]; } - for (i=1; i 0 && localbits[i] < prevbits[i] ) - return(-1); - else if ( maxflag < 0 && localbits[i] > prevbits[i] ) - return(-1); + if ( (maxflag= maxflags[i]) != 0 ) + { + // make sure local price is moving in right direction + fprintf(stderr,"maxflag.%d i.%d localbits.%u vs pricebits.%u prevbits.%u\n",maxflag,i,localbits[i],pricebits[i],prevbits[i]); + if ( maxflag > 0 && localbits[i] < prevbits[i] ) + { + if ( iter == 0 ) + break; + // second iteration checks recent prices to see if within local volatility + for (j=0; j= prevbits[i] ) + { + fprintf(stderr,"within recent localprices[%d] %u >= %u\n",j,PriceCache[j][i],prevbits[i]); + continue; + } + break; + } + else if ( maxflag < 0 && localbits[i] > prevbits[i] ) + { + if ( iter == 0 ) + break; + for (j=0; j 333 ) - ASSETCHAINS_CBOPRET = 7; +if ( komodo_nextheight() > 333 ) // for debug only! + ASSETCHAINS_CBOPRET = 7; size = PRICES_SIZEBIT0; if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) size += sizeof(forexprices); @@ -2076,31 +2104,48 @@ void komodo_cbopretupdate() if ( Mineropret.size() < size ) Mineropret.resize(size); size = PRICES_SIZEBIT0; - if ( now > lastbtc+120 && get_btcusd(pricebits) == 0 ) + if ( (forceflag != 0 || now > lastbtc+120) && get_btcusd(pricebits) == 0 ) { - memcpy(Mineropret.data(),pricebits,PRICES_SIZEBIT0); - lastbtc = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + memcpy(PriceCache[0],pricebits,PRICES_SIZEBIT0); + flags |= 1; } if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) { - if ( now > lasttime+3600*5 || forexprices[0] == 0 ) + if ( now > lasttime+3600*5 || forexprices[0] == 0 ) // cant assume timestamp is valid for forex price as it is a daily weekday changing thing anyway. { get_dailyfx(forexprices); - memcpy(&Mineropret.data()[size],forexprices,sizeof(forexprices)); - lasttime = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + flags |= 2; + memcpy(&PriceCache[0][size],forexprices,sizeof(forexprices)); } size += sizeof(forexprices); } if ( (ASSETCHAINS_CBOPRET & 4) != 0 ) { - if ( now > lastcrypto+100 ) + if ( forceflag != 0 || flags != 0 ) { get_cryptoprices(cryptoprices,Cryptos,(int32_t)(sizeof(Cryptos)/sizeof(*Cryptos))); - memcpy(&Mineropret.data()[size],cryptoprices,sizeof(cryptoprices)); - lastcrypto = (uint32_t)time(NULL); + if ( flags == 0 ) + komodo_PriceCache_shift(); + memcpy(&PriceCache[0][size],cryptoprices,sizeof(cryptoprices)); + flags |= 4; // very rarely we can see flags == 6 case } size += sizeof(cryptoprices); } + if ( flags != 0 ) + { + now = (uint32_t)time(NULL); + if ( (flags & 1) != 0 ) + lastbtc = now; + if ( (flags & 2) != 0 ) + lasttime = now; + if ( (flags & 4) != 0 ) + lastcrypto = now; + memcpy(Mineropret.data(),PriceCache[0],size); + } //int32_t i; for (i=0; i sizeof(heights)/sizeof(*heights) ) - maxsamples = sizeof(heights)/sizeof(*heights); nextheight = komodo_nextheight(); UniValue a(UniValue::VARR); + if ( daywindow < 7 ) + daywindow = 7; prices = (uint32_t *)calloc(sizeof(*prices),maxsamples+daywindow); correlated = (uint32_t *)calloc(sizeof(*correlated),maxsamples+daywindow); smoothed = (uint32_t *)calloc(sizeof(*smoothed),maxsamples+daywindow); @@ -1198,7 +1201,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); else { - if ( (n= komodo_prices(rawprices,ht)) > 0 ) + if ( (n= komodo_heightpricebits(rawprices,ht)) > 0 ) { if ( firstn == -1 ) firstn = n; @@ -1212,42 +1215,45 @@ UniValue prices(const UniValue& params, bool fHelp) } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } } + for (i=0; i Date: Tue, 2 Apr 2019 00:10:28 -1100 Subject: [PATCH 010/154] Rearrange --- src/komodo_gateway.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6035cf82f..ccc1e05ce 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1552,6 +1552,16 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define PRICES_MAXCHANGE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR #define KOMODO_LOCALPRICE_CACHESIZE 7 + + +#define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) + +const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "DASH", "XEM", "ZEC", "WAVES", "RVN", "LSK", "DCR", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" }; // must be on binance (for now) + +const char *Forex[] = +{ "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" +}; // must be in ECB list + uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; void komodo_PriceCache_shift() @@ -1923,13 +1933,6 @@ cJSON *send_curl(char *url,char *fname) // get_urljson just returns the JSON returned by the URL using issue_curl -#define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) - -const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "DASH", "XEM", "ZEC", "WAVES", "RVN", "LSK", "DCR", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" }; - -const char *Forex[] = -{ "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" -}; /* const char *Techstocks[] = From aa2f4d910926f9d6f71fce28f306ad55131802e0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 00:14:47 -1100 Subject: [PATCH 011/154] Arr --- src/rpc/blockchain.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 20a2a8ac2..29979a489 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1178,6 +1178,8 @@ int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); uint32_t komodo_pricesmoothed(uint32_t *correlatedp,uint32_t *rawprices,int32_t numprices); uint32_t komodo_timestampset(uint32_t *correlatedp,uint32_t *rawprices,int32_t numprices); +int32_t komodo_nextheight(); +uint32_t komodo_heightstamp(int32_t height); UniValue prices(const UniValue& params, bool fHelp) { @@ -1237,9 +1239,9 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*(maxsamples+daywindow) + i; smoothed[offset] = komodo_pricesmoothed(&correlated[offset],&rawprices[offset],daywindow); UniValue parr(UniValue::VARR); - parr.push_back((double)prices[offset]/10000.); - parr.push_back((double)correlated[offset]/10000.); - parr.push_back((double)smoothed[offset]/10000.); + parr.push_back((uint64_t)prices[offset]); + parr.push_back((uint64_t)correlated[offset]); + parr.push_back((uint64_t)smoothed[offset]); prices.push_back(parr); } item.push_back(Pair("prices",prices)); From a29c2f179ea0545ce05b583cc7a925ab7681a6df Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 00:29:03 -1100 Subject: [PATCH 012/154] Test --- src/komodo_gateway.h | 12 +++++++---- src/rpc/blockchain.cpp | 46 +++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index ccc1e05ce..021abbd60 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2213,14 +2213,18 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -uint32_t komodo_timestampset(uint32_t *correlatedp,uint32_t *rawtimestamps,int32_t numtimestamps) +int64_t komodo_timestampset(int64_t *correlatedp,uint32_t *rawtimestamps,int32_t numtimestamps) { *correlatedp = rawtimestamps[0]; - return(rawtimestamps[0]); // really to do this would need to do it on a per pricefeed and which prices were used in the correlation, but that is a lot of extra work for a field which is not critical + return(*correlatedp); // really to do this would need to do it on a per pricefeed and which prices were used in the correlation, but that is a lot of extra work for a field which is not critical } -uint32_t komodo_pricesmoothed(uint32_t *correlatedp,uint32_t *rawprices,int32_t numprices) +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) { - + return(0); } +int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) +{ + return(0); +} diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 29979a489..01939bb7d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,8 +1176,9 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -uint32_t komodo_pricesmoothed(uint32_t *correlatedp,uint32_t *rawprices,int32_t numprices); -uint32_t komodo_timestampset(uint32_t *correlatedp,uint32_t *rawprices,int32_t numprices); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices); +int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices); +int64_t komodo_timestampset(int64_t *correlatedp,uint32_t *rawprices,int32_t numprices); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1186,7 +1187,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); char name[64],*str; uint32_t rawprices[2048],*prices,*correlated,*smoothed; uint32_t i,j,firstn=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,firstn=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; int32_t maxsamples = atoi(params[0].get_str().c_str()); if ( maxsamples < 1 ) maxsamples = 1; @@ -1194,10 +1195,11 @@ UniValue prices(const UniValue& params, bool fHelp) UniValue a(UniValue::VARR); if ( daywindow < 7 ) daywindow = 7; - prices = (uint32_t *)calloc(sizeof(*prices),maxsamples+daywindow); - correlated = (uint32_t *)calloc(sizeof(*correlated),maxsamples+daywindow); - smoothed = (uint32_t *)calloc(sizeof(*smoothed),maxsamples+daywindow); - for (ht=nextheight-1,i=0; idaywindow+2; i++,ht--) + width = maxsamples+2*daywindow; + prices = (uint32_t *)calloc(sizeof(*prices),width); + correlated = (int64_t *)calloc(sizeof(*correlated),width); + //smoothed = (uint32_t *)calloc(sizeof(*smoothed),width); + for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); @@ -1212,7 +1214,7 @@ UniValue prices(const UniValue& params, bool fHelp) else { for (j=0; j Date: Tue, 2 Apr 2019 00:55:09 -1100 Subject: [PATCH 013/154] Fix --- src/komodo_gateway.h | 21 +++++++++++++-------- src/rpc/blockchain.cpp | 36 ++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 021abbd60..450f42aeb 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2213,18 +2213,23 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_timestampset(int64_t *correlatedp,uint32_t *rawtimestamps,int32_t numtimestamps) -{ - *correlatedp = rawtimestamps[0]; - return(*correlatedp); // really to do this would need to do it on a per pricefeed and which prices were used in the correlation, but that is a lot of extra work for a field which is not critical -} - int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) { - return(0); + int32_t i; + for (i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) @@ -1207,9 +1208,7 @@ UniValue prices(const UniValue& params, bool fHelp) { if ( (n= komodo_heightpricebits(rawprices,ht)) > 0 ) { - if ( firstn == -1 ) - firstn = n; - else if ( n != firstn ) + if ( n != numpricefeeds ) throw JSONRPCError(RPC_INVALID_PARAMETER, "numprices != first numprices"); else { @@ -1222,15 +1221,12 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 00:56:12 -1100 Subject: [PATCH 014/154] uint32_t price; --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 450f42aeb..578b36cd6 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2224,7 +2224,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) { - int64_t sum = 0; uint32_t price; + int32_t i; uint32_t price; int64_t sum = 0; for (i=0; i Date: Tue, 2 Apr 2019 00:58:55 -1100 Subject: [PATCH 015/154] daywindow --- src/rpc/blockchain.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 09c297ab9..bc8df8132 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1187,6 +1187,9 @@ UniValue prices(const UniValue& params, bool fHelp) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); UniValue ret(UniValue::VOBJ); int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + if ( ASSETCHAINS_CBOPRET == 0 ) + throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); + int32_t maxsamples = atoi(params[0].get_str().c_str()); if ( maxsamples < 1 ) maxsamples = 1; @@ -1256,7 +1259,7 @@ UniValue prices(const UniValue& params, bool fHelp) ret.push_back(Pair("height",(int64_t)nextheight-1)); ret.push_back(Pair("maxsamples",(int64_t)maxsamples)); ret.push_back(Pair("width",(int64_t)width)); - ret.push_back(Pair("daywidth",(int64_t)daywidth)); + ret.push_back(Pair("daywindow",(int64_t)daywindow)); ret.push_back(Pair("numpricefeeds",(int64_t)numpricefeeds)); free(prices); free(correlated); From ef41124c631d401166bbbd1899e5d01b0650ec98 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 01:06:00 -1100 Subject: [PATCH 016/154] +print --- src/rpc/blockchain.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index bc8df8132..6485bc2cc 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1235,6 +1235,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( (str= komodo_pricename(name,j)) != 0 ) { item.push_back(Pair("name",str)); + fprintf(stderr,"%s from %d to %d width.%d\n",name,j*width,j*width+maxsamples+daywindow,width); for (i=0; i Date: Tue, 2 Apr 2019 01:11:41 -1100 Subject: [PATCH 017/154] Fix off by one in PriceCache copy --- src/komodo_gateway.h | 8 ++++++-- src/rpc/blockchain.cpp | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 578b36cd6..c780306d0 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1567,8 +1567,12 @@ uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Crypt void komodo_PriceCache_shift() { int32_t i; - for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>=0; i--) + for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>0; i--) + { memcpy(PriceCache[i],PriceCache[i-1],sizeof(PriceCache[i])); + //for (j=0; j<4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex); j++) + // PriceCache[i][j] = PriceCache[i-1][j]; + } } // komodo_heightpricebits() extracts the price data in the coinbase for nHeight @@ -1787,7 +1791,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i { for (i=1; i Date: Tue, 2 Apr 2019 01:16:42 -1100 Subject: [PATCH 018/154] NOT raw prices, that is just single blocks data --- src/komodo_gateway.h | 2 ++ src/rpc/blockchain.cpp | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index c780306d0..a78927c05 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2199,6 +2199,7 @@ char *komodo_pricename(char *name,int32_t ind) if ( ind < sizeof(Forex)/sizeof(*Forex) ) { strcpy(name,Forex[ind]); + strcat(name,"USD"); return(name); } else ind -= sizeof(Forex)/sizeof(*Forex); } @@ -2209,6 +2210,7 @@ char *komodo_pricename(char *name,int32_t ind) if ( ind < sizeof(Cryptos)/sizeof(*Cryptos) ) { strcpy(name,Cryptos[ind]); + strcat(name,"BTC"); return(name); } else ind -= sizeof(Cryptos)/sizeof(*Cryptos); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 52a56f1ac..2ee802fb3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1224,7 +1224,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 01:19:26 -1100 Subject: [PATCH 019/154] Test --- src/rpc/blockchain.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 2ee802fb3..a1beefefd 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1231,7 +1231,7 @@ UniValue prices(const UniValue& params, bool fHelp) } for (j=1; j Date: Tue, 2 Apr 2019 01:20:34 -1100 Subject: [PATCH 020/154] Double --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a1beefefd..ac1690634 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1246,7 +1246,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back((double)prices[offset]/10000); + parr.push_back((double)(prices[offset]/10000)); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From f6232685cd04397b681e97e9a4a4e804a0dd78eb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 01:21:19 -1100 Subject: [PATCH 021/154] Cons --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index ac1690634..550217e2d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1246,7 +1246,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back((double)(prices[offset]/10000)); + parr.push_back(ValueFromAmount(prices[offset]*10000)); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From d93b34b04a92360a531bc941a6bbea9b90232c96 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 01:25:45 -1100 Subject: [PATCH 022/154] Test --- src/komodo_gateway.h | 2 ++ src/rpc/blockchain.cpp | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a78927c05..6b8df1834 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2235,7 +2235,9 @@ int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) { if ( (price= rawprices[i]) == 0 ) return(0); + fprintf(stderr,"%.4f ",(double)price/10000); sum += price * 10000; } + fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); return(sum / numprices); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 550217e2d..8a2752088 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1229,13 +1229,12 @@ UniValue prices(const UniValue& params, bool fHelp) ret.push_back(Pair("timestamps",timestamps)); ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); } - for (j=1; j Date: Tue, 2 Apr 2019 01:29:54 -1100 Subject: [PATCH 023/154] Limit copy range --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 8a2752088..a748e63c4 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1203,7 +1203,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); correlated = (int64_t *)calloc(sizeof(*correlated),width); - for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) + for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); From 61e193953fa380ccb41fb22c786d80972e987a8d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 01:33:28 -1100 Subject: [PATCH 024/154] Cross copy --- src/rpc/blockchain.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a748e63c4..c1eb1bac6 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1203,7 +1203,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); correlated = (int64_t *)calloc(sizeof(*correlated),width); - for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) + for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); @@ -1215,8 +1215,12 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "numprices != first numprices"); else { - for (j=0; j Date: Tue, 2 Apr 2019 01:34:31 -1100 Subject: [PATCH 025/154] ; --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c1eb1bac6..d5c78e68a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1219,7 +1219,7 @@ UniValue prices(const UniValue& params, bool fHelp) { prices[j*width + i] = rawprices[j]; if ( j == 1 ) - fprintf(stderr,"[%d] <- %.1f ",j*width+i,(double)rawprices[j]/10000) + fprintf(stderr,"[%d] <- %.1f ",j*width+i,(double)rawprices[j]/10000); } } } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); From 7b0d2efa74c8a3939ad5670180408ab2d0c212e9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 01:37:16 -1100 Subject: [PATCH 026/154] cast --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6b8df1834..6ecfd170c 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2235,8 +2235,8 @@ int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) { if ( (price= rawprices[i]) == 0 ) return(0); - fprintf(stderr,"%.4f ",(double)price/10000); - sum += price * 10000; + fprintf(stderr,"%.0f ",(double)price/10000); + sum += (int64_t)price * 10000; } fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); return(sum / numprices); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index d5c78e68a..32b1c9cb7 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1216,11 +1216,7 @@ UniValue prices(const UniValue& params, bool fHelp) else { for (j=0; j Date: Tue, 2 Apr 2019 01:45:33 -1100 Subject: [PATCH 027/154] Fix --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6ecfd170c..a87a04e57 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2235,9 +2235,9 @@ int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) { if ( (price= rawprices[i]) == 0 ) return(0); - fprintf(stderr,"%.0f ",(double)price/10000); + //fprintf(stderr,"%.0f ",(double)price/10000); sum += (int64_t)price * 10000; } - fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); + //fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); return(sum / numprices); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 32b1c9cb7..f927cf050 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1229,7 +1229,7 @@ UniValue prices(const UniValue& params, bool fHelp) ret.push_back(Pair("timestamps",timestamps)); ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); } - for (j=1; j<2; j++) + for (j=1; j Date: Tue, 2 Apr 2019 01:54:47 -1100 Subject: [PATCH 028/154] -print --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a87a04e57..bb67d1e8d 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2230,13 +2230,13 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) { - int32_t i; uint32_t price; int64_t sum = 0; + int32_t i; int64_t price,sum = 0; for (i=0; i Date: Tue, 2 Apr 2019 01:58:18 -1100 Subject: [PATCH 029/154] Rescale --- src/rpc/blockchain.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 1288c02ea..2b0186f22 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,9 +1245,9 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(((int64_t)prices[offset]*10000)); - parr.push_back((correlated[i])); - parr.push_back((smoothed)); + parr.push_back(ValueFromAmount((int64_t)prices[offset])); + parr.push_back(ValueFromAmount(correlated[i]/10000)); + parr.push_back(ValueFromAmount(smoothed/10000)); p.push_back(parr); } item.push_back(Pair("prices",p)); From b5af10d99f56f11d17bb22dcef8ebab7e6502a8a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:05:27 -1100 Subject: [PATCH 030/154] Normalize all prices --- src/komodo_gateway.h | 7 ++++--- src/rpc/blockchain.cpp | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index bb67d1e8d..bbad94c51 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2228,15 +2228,16 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) return(correlated[0]); } -int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices) +int64_t komodo_pricecorrelated(int32_t ind,uint32_t *rawprices,int32_t numprices) { int32_t i; int64_t price,sum = 0; for (i=0; i= 36 ) + price *= 10000; + sum += price; } //fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); return(sum / numprices); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 2b0186f22..c628ab074 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1246,8 +1246,8 @@ UniValue prices(const UniValue& params, bool fHelp) smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); parr.push_back(ValueFromAmount((int64_t)prices[offset])); - parr.push_back(ValueFromAmount(correlated[i]/10000)); - parr.push_back(ValueFromAmount(smoothed/10000)); + parr.push_back(ValueFromAmount(correlated[i])); + parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); } item.push_back(Pair("prices",p)); From cf281b048edc19fdbdeb1fbb567f8964d1bcaa48 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:07:32 -1100 Subject: [PATCH 031/154] int64_t komodo_pricecorrelated(int32_t ind,uint32_t *rawprices,int32_t numprices) --- src/rpc/blockchain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c628ab074..eab787f17 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1177,7 +1177,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices); -int64_t komodo_pricecorrelated(uint32_t *rawprices,int32_t numprices); +int64_t komodo_pricecorrelated(int32_t ind,uint32_t *rawprices,int32_t numprices); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1238,7 +1238,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 02:13:20 -1100 Subject: [PATCH 032/154] Reverse --- src/komodo_gateway.h | 14 +++++++++----- src/rpc/blockchain.cpp | 12 ++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index bbad94c51..293b57367 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1576,9 +1576,11 @@ void komodo_PriceCache_shift() } // komodo_heightpricebits() extracts the price data in the coinbase for nHeight -int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) +int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight) { CBlockIndex *pindex; CBlock block; CTransaction tx; int32_t numvouts; std::vector vopret; + if ( seedp != 0 ) + *seedp = 0; if ( (pindex= komodo_chainactive(nHeight)) != 0 ) { if ( komodo_blockload(block,pindex) == 0 ) @@ -1588,6 +1590,8 @@ int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight) GetOpReturnData(tx.vout[numvouts-1].scriptPubKey,vopret); if ( vopret.size() >= PRICES_SIZEBIT0 ) { + if ( seedp != 0 ) + memcpy(seedp,&pindex->hashMerkleRoot,sizeof(*seedp)); memcpy(heightbits,vopret.data(),vopret.size()); return((int32_t)(vopret.size()/sizeof(uint32_t))); } @@ -1682,7 +1686,7 @@ CScript komodo_mineropret(int32_t nHeight) if ( numzero != 0 ) fprintf(stderr,"numzero.%d\n",numzero); } - if ( komodo_heightpricebits(prevbits,nHeight-1) > 0 ) + if ( komodo_heightpricebits(0,prevbits,nHeight-1) > 0 ) { memcpy(pricebits,Mineropret.data(),Mineropret.size()); memset(maxflags,0,sizeof(maxflags)); @@ -1757,7 +1761,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i btcgbp = (double)pricebits[2]/10000; btceur = (double)pricebits[3]/10000; fprintf(stderr,"ht.%d: lag.%d %.4f USD, %.4f GBP, %.4f EUR, GBPUSD %.6f, EURUSD %.6f, EURGBP %.6f [%d]\n",nHeight,lag,btcusd,btcgbp,btceur,btcusd/btcgbp,btcusd/btceur,btcgbp/btceur,lag2); - if ( komodo_heightpricebits(prevbits,nHeight-1) > 0 ) + if ( komodo_heightpricebits(0,prevbits,nHeight-1) > 0 ) { if ( nHeight < testchain_exemption ) { @@ -2228,14 +2232,14 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) return(correlated[0]); } -int64_t komodo_pricecorrelated(int32_t ind,uint32_t *rawprices,int32_t numprices) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices) { int32_t i; int64_t price,sum = 0; for (i=0; i= 36 ) + if ( ind < 36 ) price *= 10000; sum += price; } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index eab787f17..1cfcf4eb7 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1174,10 +1174,10 @@ UniValue paxprice(const UniValue& params, bool fHelp) return ret; } -int32_t komodo_heightpricebits(uint32_t *heightbits,int32_t nHeight); +int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices); -int64_t komodo_pricecorrelated(int32_t ind,uint32_t *rawprices,int32_t numprices); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1186,7 +1186,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1198,7 +1198,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( daywindow < 7 ) daywindow = 7; width = maxsamples+2*daywindow; - numpricefeeds = komodo_heightpricebits(rawprices,nextheight-1); + numpricefeeds = komodo_heightpricebits(&seed,rawprices,nextheight-1); if ( numpricefeeds <= 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); @@ -1209,7 +1209,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); else { - if ( (n= komodo_heightpricebits(rawprices,ht)) > 0 ) + if ( (n= komodo_heightpricebits(0,rawprices,ht)) > 0 ) { if ( n != numpricefeeds ) throw JSONRPCError(RPC_INVALID_PARAMETER, "numprices != first numprices"); @@ -1238,7 +1238,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 02:17:35 -1100 Subject: [PATCH 033/154] Again --- src/rpc/blockchain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 1cfcf4eb7..5bbf1ef1f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,8 +1245,8 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset])); - parr.push_back(ValueFromAmount(correlated[i])); + parr.push_back(ValueFromAmount((int64_t)prices[offset]*10000)); + parr.push_back(ValueFromAmount(correlated[i]*ind<36?10000:1)); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); } From 57b769c44d11e62a0af9dd73eb22991fc238efcd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:19:32 -1100 Subject: [PATCH 034/154] J --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 5bbf1ef1f..772c590b6 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1246,7 +1246,7 @@ UniValue prices(const UniValue& params, bool fHelp) smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); parr.push_back(ValueFromAmount((int64_t)prices[offset]*10000)); - parr.push_back(ValueFromAmount(correlated[i]*ind<36?10000:1)); + parr.push_back(ValueFromAmount(correlated[i]*j<36?10000:1)); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); } From 8e84d8e0ea186f1645b8d83e660d30fb71ecc172 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:24:31 -1100 Subject: [PATCH 035/154] ugh --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 293b57367..1f1ce8dc6 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2202,8 +2202,8 @@ char *komodo_pricename(char *name,int32_t ind) return(0); if ( ind < sizeof(Forex)/sizeof(*Forex) ) { - strcpy(name,Forex[ind]); - strcat(name,"USD"); + name[0] = 'U', name[1] = 'S', name[2] = 'D'; + strcpy(name+3,Forex[ind]); return(name); } else ind -= sizeof(Forex)/sizeof(*Forex); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 772c590b6..5ba28438b 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,8 +1245,8 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset]*10000)); - parr.push_back(ValueFromAmount(correlated[i]*j<36?10000:1)); + parr.push_back(ValueFromAmount((int64_t)prices[offset]*j<36?10000:1)); + parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); } From d701423bfbc274a0ab520f4c8ff52a17ac9d4db0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:27:08 -1100 Subject: [PATCH 036/154] Fiddle --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 5ba28438b..9f4b24235 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,7 +1245,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset]*j<36?10000:1)); + parr.push_back(ValueFromAmount((int64_t)prices[offset]*10000)); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From 2df1a61d73ed3dc27c7ae91353313dda6127cde5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:28:59 -1100 Subject: [PATCH 037/154] Finally --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9f4b24235..bbf90f163 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,7 +1245,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset]*10000)); + parr.push_back(ValueFromAmount((int64_t)prices[offset]* j<36?10000:1)); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From b952e57e95448b2b85b0f2c492eca4941e708e06 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 02:34:32 -1100 Subject: [PATCH 038/154] ?: precedence --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index bbf90f163..668081934 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1245,7 +1245,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset]* j<36?10000:1)); + parr.push_back(ValueFromAmount((int64_t)prices[offset] * (j<36?10000:1))); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From d725af022effeb935cefc2f05d8efe19cc7ef00a Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Tue, 2 Apr 2019 06:44:09 -0700 Subject: [PATCH 039/154] Client name is not a consensus-related option, it can be configured by any node --- src/clientversion.cpp | 2 +- src/init.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clientversion.cpp b/src/clientversion.cpp index 663e2e937..084bbd5ce 100644 --- a/src/clientversion.cpp +++ b/src/clientversion.cpp @@ -35,7 +35,7 @@ * for both bitcoind and bitcoin-core, to make it harder for attackers to * target servers or GUI users specifically. */ -const std::string CLIENT_NAME = GetArg("-ac_clientname", "MagicBean"); +const std::string CLIENT_NAME = GetArg("-clientname", "MagicBean"); /** * Client version number diff --git a/src/init.cpp b/src/init.cpp index d2546347d..24b32a720 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -367,6 +367,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-blocknotify=", _("Execute command when the best block changes (%s in cmd is replaced by block hash)")); strUsage += HelpMessageOpt("-checkblocks=", strprintf(_("How many blocks to check at startup (default: %u, 0 = all)"), 288)); strUsage += HelpMessageOpt("-checklevel=", strprintf(_("How thorough the block verification of -checkblocks is (0-4, default: %u)"), 3)); + strUsage += HelpMessageOpt("-clientname=", _("Full node client name, default 'MagicBean'")); strUsage += HelpMessageOpt("-conf=", strprintf(_("Specify configuration file (default: %s)"), "komodo.conf")); if (mode == HMM_BITCOIND) { @@ -571,7 +572,6 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-ac_cclib", _("Cryptoconditions dynamicly loadable library")); strUsage += HelpMessageOpt("-ac_ccenable", _("Cryptoconditions to enable")); strUsage += HelpMessageOpt("-ac_ccactivate", _("Block height to enable Cryptoconditions")); - strUsage += HelpMessageOpt("-ac_clientname", _("Full node client name, default 'MagicBean'")); strUsage += HelpMessageOpt("-ac_decay", _("Percentage of block reward decrease at each halving")); strUsage += HelpMessageOpt("-ac_end", _("Block height at which block rewards will end")); strUsage += HelpMessageOpt("-ac_eras", _("Block reward eras")); From 4b7a2ca365e9d37fd21584c927ea19a643ecbbe2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:16:20 -1100 Subject: [PATCH 040/154] First version correlator --- src/komodo_gateway.h | 42 ++++++++++++++++++++++++++++++++---------- src/rpc/blockchain.cpp | 4 +++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 1f1ce8dc6..b59a6fbf1 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2232,17 +2232,39 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) return(correlated[0]); } -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) { - int32_t i; int64_t price,sum = 0; - for (i=0; i= daywindow ) + i = 0; + if ( (price= rawprices[i]) == 0 ) + return(-1); + if ( price >= lowprice && price <= highprice ) + { + sum += price; + correlation++; + if ( correlation > (daywindow>>1) ) + { + fprintf(stderr,"iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + return(sum / correlation) + } + } + } } - //fprintf(stderr," ave %.8f [%d]\n",((double)sum/numprices)/COIN,numprices); - return(sum / numprices); + return(0); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 668081934..c68d4b14b 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1238,7 +1238,8 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 03:17:38 -1100 Subject: [PATCH 041/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index b59a6fbf1..9bfc76835 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2261,7 +2261,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( correlation > (daywindow>>1) ) { fprintf(stderr,"iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); - return(sum / correlation) + return(sum / correlation); } } } From e3a910b8cbf3c181cc37e15f0a65f7df03511a17 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:30:46 -1100 Subject: [PATCH 042/154] +print --- src/komodo_gateway.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9bfc76835..d19325778 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1807,7 +1807,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i for (j=0; j= prevbits[i] ) { - fprintf(stderr,"within recent localprices[%d] %u >= %u\n",j,PriceCache[j][i],prevbits[i]); + fprintf(stderr,"i.%d within recent localprices[%d] %u >= %u\n",i,j,PriceCache[j][i],prevbits[i]); continue; } break; @@ -1819,7 +1819,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i for (j=0; j 333 ) // for debug only! get_stocks(Techstocks,(int32_t)(sizeof(Techstocks)/sizeof(*Techstocks))); }*/ } + pending = 0; } char *komodo_pricename(char *name,int32_t ind) @@ -2242,8 +2250,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int sum = correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i] * (ind < 36 ? 10000 : 1); - highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE/2)) / COIN; - lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE/2)) / COIN; + highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE/10)) / COIN; + lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE/10)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) @@ -2260,7 +2268,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation++; if ( correlation > (daywindow>>1) ) { - fprintf(stderr,"iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); return(sum / correlation); } } From 790873660f8d93046bf70a4b42d4bb17f295c870 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:33:11 -1100 Subject: [PATCH 043/154] Test --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d19325778..bda57fab4 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2250,8 +2250,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int sum = correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i] * (ind < 36 ? 10000 : 1); - highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE/10)) / COIN; - lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE/10)) / COIN; + highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE)) / COIN; + lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) From 8209b487d8eb9d7ae94d4964ef708ed224acca46 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:36:05 -1100 Subject: [PATCH 044/154] Test --- src/komodo_gateway.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index bda57fab4..2e7a3f5ea 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2242,7 +2242,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) { - int32_t i,j,iter,correlation; int64_t price,sum; uint32_t refprice,lowprice,highprice; + int32_t i,j,iter,correlation,maxcorrelation=0; int64_t price,sum; uint32_t refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); for (iter=0; iter maxcorrelation ) + maxcorrelation = correlation; } + fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d\n",ind,itermaxcorrelation); return(0); } From c808e4c196cd08a1d25c2c96d86f538a53d0721a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:45:08 -1100 Subject: [PATCH 045/154] Weighted correlation --- src/komodo_gateway.h | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 2e7a3f5ea..3dc5b1bd8 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2242,12 +2242,12 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) { - int32_t i,j,iter,correlation,maxcorrelation=0; int64_t price,sum; uint32_t refprice,lowprice,highprice; + int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den; uint32_t refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); for (iter=0; iter= lowprice && price <= highprice ) { - sum += price; correlation++; if ( correlation > (daywindow>>1) ) { - fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); - return(sum / correlation); + n = 0; + i = (j + seed) % daywindow; + for (j=0; j= daywindow ) + i = 0; + if ( n >= (daywindow>>1) ) + rawprices[i] = 0; + else + { + price = rawprices[i]; + if ( price < lowprice || price > highprice ) + rawprices[i] = 0; + else n++; + } + } + fprintf(stderr,"ind.%d iter.%d j.%d i.%d n.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,n,correlation,(long long)refprice,(long long)sum/correlation); + if ( n != correlation ) + return(-1); + sum = den = n = 0; + for (i=0; i %.8f\n",((double)sum / den) / COIN); + return(sum / den); } } } From 24ff9d6e54b281a2ce558f51ad7f3bc4218f16ea Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:46:40 -1100 Subject: [PATCH 046/154] if ( n != correlation ) return(-1); --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 3dc5b1bd8..9254980b2 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2250,8 +2250,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i] * (ind < 36 ? 10000 : 1); - highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE)) / COIN; - lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE)) / COIN; + highprice = ((int64_t)refprice * (COIN + 10*PRICES_MAXCHANGE)) / COIN; + lowprice = ((int64_t)refprice * (COIN - 10*PRICES_MAXCHANGE)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) @@ -2306,6 +2306,6 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( correlation > maxcorrelation ) maxcorrelation = correlation; } - fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d\n",ind,itermaxcorrelation); + fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d\n",ind,iter,maxcorrelation); return(0); } From db8fff57ab9a8ad1b8e3c989ccac3eadf545b686 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:55:45 -1100 Subject: [PATCH 047/154] Scale prices --- src/komodo_gateway.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9254980b2..06e263616 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2250,8 +2250,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i] * (ind < 36 ? 10000 : 1); - highprice = ((int64_t)refprice * (COIN + 10*PRICES_MAXCHANGE)) / COIN; - lowprice = ((int64_t)refprice * (COIN - 10*PRICES_MAXCHANGE)) / COIN; + highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE)) / COIN; + lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) @@ -2262,6 +2262,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int i = 0; if ( (price= rawprices[i]) == 0 ) return(-1); + if ( ind < 36 ) + price *= 10000; if ( price >= lowprice && price <= highprice ) { correlation++; @@ -2306,6 +2308,6 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( correlation > maxcorrelation ) maxcorrelation = correlation; } - fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d\n",ind,iter,maxcorrelation); + fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d ref.%u high.%u low.%u\n",ind,iter,maxcorrelation,refprice,highprice,lowprice); return(0); } From a4788f61e7550553a2ba86e6314267771f65819d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 03:56:29 -1100 Subject: [PATCH 048/154] Test --- src/komodo_gateway.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 06e263616..5c578fc5e 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2242,14 +2242,17 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) { - int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den; uint32_t refprice,lowprice,highprice; + int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den; uint32_t mult,refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); + if ( ind < 36 ) + mult = 10000; + else mult = 1; for (iter=0; iter= lowprice && price <= highprice ) { correlation++; From f99fa65eda198c2e976c5f0c45c33f8d623643b0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:01:54 -1100 Subject: [PATCH 049/154] Test --- src/komodo_gateway.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 5c578fc5e..b5e000eb9 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2242,7 +2242,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) { - int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den; uint32_t mult,refprice,lowprice,highprice; + int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); if ( ind < 36 ) @@ -2253,8 +2253,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i] * mult; - highprice = ((int64_t)refprice * (COIN + PRICES_MAXCHANGE)) / COIN; - lowprice = ((int64_t)refprice * (COIN - PRICES_MAXCHANGE)) / COIN; + highprice = (refprice * (COIN + PRICES_MAXCHANGE)) / COIN; + lowprice = (refprice * (COIN - PRICES_MAXCHANGE)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) @@ -2268,10 +2268,11 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int price *= mult; if ( price >= lowprice && price <= highprice ) { + sum += price; correlation++; if ( correlation > (daywindow>>1) ) { - n = 0; + /*n = 0; i = (j + seed) % daywindow; for (j=0; j %llu\n",ind,iter,j,i,n,correlation,(long long)refprice,(long long)sum/correlation); - if ( n != correlation ) + }*/ + fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + /*if ( n != correlation ) return(-1); sum = den = n = 0; for (i=0; i %.8f\n",((double)sum / den) / COIN); - return(sum / den); + return(sum / den);*/ + return(sum / correlation); } } } From c159b78f6f0bb33c89c86dac13ab2dba7640130b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:08:11 -1100 Subject: [PATCH 050/154] Test --- src/komodo_gateway.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index b5e000eb9..09559d960 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2231,15 +2231,6 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) -{ - int32_t i; - for (i=0; i %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + //fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); /*if ( n != correlation ) return(-1); sum = den = n = 0; @@ -2312,6 +2303,17 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( correlation > maxcorrelation ) maxcorrelation = correlation; } - fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d ref.%u high.%u low.%u\n",ind,iter,maxcorrelation,refprice,highprice,lowprice); + fprintf(stderr,"ind.%d iter.%d maxcorrelation.%d ref.%llu high.%llu low.%llu\n",ind,iter,maxcorrelation,(long long)refprice,(long long)highprice,(long long)lowprice); return(0); } + + +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) +{ + int32_t i; + for (i=0; i Date: Tue, 2 Apr 2019 04:18:44 -1100 Subject: [PATCH 051/154] Init sum --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 09559d960..b0b21b49f 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2243,23 +2243,23 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int { correlation = 0; i = (j + seed) % daywindow; - refprice = rawprices[i] * mult; + refprice = rawprices[i]; highprice = (refprice * (COIN + PRICES_MAXCHANGE)) / COIN; lowprice = (refprice * (COIN - PRICES_MAXCHANGE)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) lowprice--; + sum = 0; for (j=0; j= daywindow ) i = 0; if ( (price= rawprices[i]) == 0 ) return(-1); - price *= mult; if ( price >= lowprice && price <= highprice ) { - sum += price; + sum += price * mult; correlation++; if ( correlation > (daywindow>>1) ) { From 2b1dff7b0c5c6d9325de82bf4fd6395a7f7a1781 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:19:05 -1100 Subject: [PATCH 052/154] Allow more variance --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index b0b21b49f..7df402d69 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2244,8 +2244,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (j + seed) % daywindow; refprice = rawprices[i]; - highprice = (refprice * (COIN + PRICES_MAXCHANGE)) / COIN; - lowprice = (refprice * (COIN - PRICES_MAXCHANGE)) / COIN; + highprice = (refprice * (COIN + PRICES_MAXCHANGE*2)) / COIN; + lowprice = (refprice * (COIN - PRICES_MAXCHANGE*2)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) From 47f2069490780624642adf8d4350da9033b1cfbf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:29:54 -1100 Subject: [PATCH 053/154] Weight correlated price --- src/komodo_gateway.h | 33 +++++++++++++++++++-------------- src/rpc/blockchain.cpp | 9 ++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7df402d69..66e9fae78 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2231,7 +2231,7 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *rawprices2) { int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) @@ -2239,13 +2239,15 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( ind < 36 ) mult = 10000; else mult = 1; + if ( memcmp(rawprices,rawprices2,daywindow*sizeof(*rawprices)) != 0 ) + fprintf(stderr,"ind.%d rawprices2 != rawprices\n",ind); for (iter=0; iter (daywindow>>1) ) { - /*n = 0; + n = 0; i = (j + seed) % daywindow; for (j=0; j= daywindow ) i = 0; if ( n >= (daywindow>>1) ) - rawprices[i] = 0; + rawprices2[i] = 0; else { price = rawprices[i]; if ( price < lowprice || price > highprice ) - rawprices[i] = 0; - else n++; + rawprices2[i] = 0; + else + { + rawprices2[i] = price; + n++; + } } - }*/ - //fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); - /*if ( n != correlation ) + } + fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + if ( n != correlation ) return(-1); sum = den = n = 0; for (i=0; i %.8f\n",((double)sum / den) / COIN); - return(sum / den);*/ - return(sum / correlation); + fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); + return((sum * mult) / den); } } } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c68d4b14b..c41803908 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1177,7 +1177,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices); -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1186,7 +1186,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1203,6 +1203,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); correlated = (int64_t *)calloc(sizeof(*correlated),width); + prices2 = (int64_t *)calloc(sizeof(*prices2),width); for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) @@ -1238,7 +1239,8 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 04:31:40 -1100 Subject: [PATCH 054/154] Test --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c41803908..9b0f5ec0a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1203,7 +1203,7 @@ UniValue prices(const UniValue& params, bool fHelp) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); prices = (uint32_t *)calloc(sizeof(*prices),width*numpricefeeds); correlated = (int64_t *)calloc(sizeof(*correlated),width); - prices2 = (int64_t *)calloc(sizeof(*prices2),width); + prices2 = (uint32_t *)calloc(sizeof(*prices2),width); for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) From a589a789f1eeee708896253bc50d125e7e1f7bb6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:35:59 -1100 Subject: [PATCH 055/154] +print --- src/komodo_gateway.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 66e9fae78..85c24c475 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2299,7 +2299,10 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int } } if ( n != correlation || sum == 0 || den == 0 ) + { + fprintf(stderr,"n.%d vs correlation.%d sum %llu, den %llu\n",n,correlation,(long long)sum,(long long)den); return(-1); + } fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } From 41748a83fc55884bbd78e101245f59e8a1562153 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:39:01 -1100 Subject: [PATCH 056/154] Test --- src/komodo_gateway.h | 10 +++++----- src/rpc/blockchain.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 85c24c475..a3a6445f6 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2233,14 +2233,14 @@ char *komodo_pricename(char *name,int32_t ind) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *rawprices2) { - int32_t i,j,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; + int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); if ( ind < 36 ) mult = 10000; else mult = 1; - if ( memcmp(rawprices,rawprices2,daywindow*sizeof(*rawprices)) != 0 ) - fprintf(stderr,"ind.%d rawprices2 != rawprices\n",ind); + //if ( memcmp(rawprices,rawprices2,daywindow*sizeof(*rawprices)) != 0 ) + // fprintf(stderr,"ind.%d rawprices2 != rawprices\n",ind); for (iter=0; iter= daywindow ) i = 0; @@ -2300,7 +2300,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int } if ( n != correlation || sum == 0 || den == 0 ) { - fprintf(stderr,"n.%d vs correlation.%d sum %llu, den %llu\n",n,correlation,(long long)sum,(long long)den); + fprintf(stderr,"seed.%llu n.%d vs correlation.%d sum %llu, den %llu\n",(long long)seed,n,correlation,(long long)sum,(long long)den); return(-1); } fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9b0f5ec0a..bc2e50fe4 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1239,7 +1239,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 04:42:42 -1100 Subject: [PATCH 057/154] Test --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a3a6445f6..5fabf72b2 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2271,7 +2271,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int { if ( i >= daywindow ) i = 0; - if ( n >= (daywindow>>1) ) + if ( n > (daywindow>>1) ) rawprices2[i] = 0; else { @@ -2285,7 +2285,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int } } } - fprintf(stderr,"ind.%d iter.%d j.%d i.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,correlation,(long long)refprice,(long long)sum/correlation); + fprintf(stderr,"ind.%d iter.%d j.%d i.%d n.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,n,correlation,(long long)refprice,(long long)sum/correlation); if ( n != correlation ) return(-1); sum = den = n = 0; From 306314dceef91dc37ad0de86bf2ddf707c15e36b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 04:54:34 -1100 Subject: [PATCH 058/154] Smoother --- src/komodo_gateway.h | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 5fabf72b2..d4b629f0d 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2239,8 +2239,6 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( ind < 36 ) mult = 10000; else mult = 1; - //if ( memcmp(rawprices,rawprices2,daywindow*sizeof(*rawprices)) != 0 ) - // fprintf(stderr,"ind.%d rawprices2 != rawprices\n",ind); for (iter=0; iter %llu\n",ind,iter,j,i,n,correlation,(long long)refprice,(long long)sum/correlation); + //fprintf(stderr,"ind.%d iter.%d j.%d i.%d n.%d correlation.%d ref %llu -> %llu\n",ind,iter,j,i,n,correlation,(long long)refprice,(long long)sum/correlation); if ( n != correlation ) return(-1); sum = den = n = 0; @@ -2303,7 +2301,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int fprintf(stderr,"seed.%llu n.%d vs correlation.%d sum %llu, den %llu\n",(long long)seed,n,correlation,(long long)sum,(long long)den); return(-1); } - fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); + //fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } } @@ -2318,10 +2316,32 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) { - int32_t i; - for (i=0; i Date: Tue, 2 Apr 2019 05:00:31 -1100 Subject: [PATCH 059/154] Anchor to latest --- src/komodo_gateway.h | 7 ++++--- src/rpc/blockchain.cpp | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d4b629f0d..e7186ccc3 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2233,7 +2233,7 @@ char *komodo_pricename(char *name,int32_t ind) int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *rawprices2) { - int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t price,sum,den,mult,refprice,lowprice,highprice; + int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t firstprice,price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) return(-1); if ( ind < 36 ) @@ -2287,12 +2287,13 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( n != correlation ) return(-1); sum = den = n = 0; + firstprice = rawprices2[0]; for (i=0; i> 1); n++; } } @@ -2337,7 +2338,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) sum = den = 0; for (i=0; i> 1); den += (numprices - i); } smoothed = (sum / den); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index bc2e50fe4..2ff865f88 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1256,6 +1256,7 @@ UniValue prices(const UniValue& params, bool fHelp) item.push_back(Pair("prices",p)); } else item.push_back(Pair("name","error")); a.push_back(item); +break; } ret.push_back(Pair("pricefeeds",a)); ret.push_back(Pair("result","success")); From fd3996cb88064c718cede483310d0d62b4d4b8bf Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:04:22 -1100 Subject: [PATCH 060/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e7186ccc3..9f0f1e7fe 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2302,7 +2302,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int fprintf(stderr,"seed.%llu n.%d vs correlation.%d sum %llu, den %llu\n",(long long)seed,n,correlation,(long long)sum,(long long)den); return(-1); } - //fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); + fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } } From 88eee07c7ffca378f30f8b07d6c9ad845438f30f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:08:03 -1100 Subject: [PATCH 061/154] tst --- src/komodo_gateway.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9f0f1e7fe..57835ab7a 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1617,7 +1617,7 @@ uint32_t komodo_pricenew(char *maxflagp,uint32_t price,uint32_t refprice,int64_t lowprice--; if ( price >= highprice ) { - fprintf(stderr,"high %u vs h%llu l%llu tolerance.%llu\n",price,(long long)highprice,(long long)lowprice,(long long)tolerance); + //fprintf(stderr,"high %u vs h%llu l%llu tolerance.%llu\n",price,(long long)highprice,(long long)lowprice,(long long)tolerance); if ( price > highprice ) // return non-zero only if we violate the tolerance { *maxflagp = 2; @@ -2287,7 +2287,10 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( n != correlation ) return(-1); sum = den = n = 0; - firstprice = rawprices2[0]; + for (i=0; i> 1); n++; - } + } else return(-1); } if ( n != correlation || sum == 0 || den == 0 ) { fprintf(stderr,"seed.%llu n.%d vs correlation.%d sum %llu, den %llu\n",(long long)seed,n,correlation,(long long)sum,(long long)den); return(-1); } - fprintf(stderr,"weighted -> %.8f\n",((double)(sum*mult) / den) / COIN); + //fprintf(stderr,"firstprice.%llu weighted -> %.8f\n",(long long)firstprice,((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } } From fcea5524d7df451cb9ce0dafaf82e1c94d0f842a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:10:45 -1100 Subject: [PATCH 062/154] Skip --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 57835ab7a..e0efd5734 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2298,7 +2298,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int den += (daywindow - i); sum += (daywindow - i) * ((price + firstprice) >> 1); n++; - } else return(-1); + } } if ( n != correlation || sum == 0 || den == 0 ) { From 4c0eaa7a1f5411f3eddf2faa3d5e8ede5b7e7ffd Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:15:02 -1100 Subject: [PATCH 063/154] 2/3 --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e0efd5734..3e6d8061a 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2296,7 +2296,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= rawprices2[i]) != 0 ) { den += (daywindow - i); - sum += (daywindow - i) * ((price + firstprice) >> 1); + sum += (daywindow - i) * ((price + firstprice*2) / 3); n++; } } @@ -2305,7 +2305,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int fprintf(stderr,"seed.%llu n.%d vs correlation.%d sum %llu, den %llu\n",(long long)seed,n,correlation,(long long)sum,(long long)den); return(-1); } - //fprintf(stderr,"firstprice.%llu weighted -> %.8f\n",(long long)firstprice,((double)(sum*mult) / den) / COIN); + fprintf(stderr,"firstprice.%llu weighted -> %.8f\n",(long long)firstprice,((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } } @@ -2341,7 +2341,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices) sum = den = 0; for (i=0; i> 1); + sum += (numprices - i) * ((correlated[i] + firstprice*2) / 3); den += (numprices - i); } smoothed = (sum / den); From fe9eb8df39f1ca763cfdddb22c0435f1942de5f6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:19:01 -1100 Subject: [PATCH 064/154] nonzprices --- src/komodo_gateway.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 3e6d8061a..57b537606 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2231,7 +2231,7 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *rawprices2) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *nonzprices) { int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t firstprice,price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) @@ -2270,15 +2270,16 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( i >= daywindow ) i = 0; if ( n > (daywindow>>1) ) - rawprices2[i] = 0; + nonzprices[i] = 0; else { price = rawprices[i]; if ( price < lowprice || price > highprice ) - rawprices2[i] = 0; + nonzprices[i] = 0; else { - rawprices2[i] = price; + nonzprices[i] = price; + fprintf(stderr,"(%d %u) ",i,rawprices[i]); n++; } } @@ -2288,12 +2289,13 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int return(-1); sum = den = n = 0; for (i=0; i Date: Tue, 2 Apr 2019 05:23:32 -1100 Subject: [PATCH 065/154] Test --- src/rpc/blockchain.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 2ff865f88..bc8302589 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1186,7 +1186,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t seed; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated; char name[64],*str; uint32_t rawprices[2048],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1230,8 +1230,10 @@ UniValue prices(const UniValue& params, bool fHelp) ret.push_back(Pair("timestamps",timestamps)); ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); } + rngval = seed; for (j=1; j Date: Tue, 2 Apr 2019 05:28:11 -1100 Subject: [PATCH 066/154] Iter + seed --- src/komodo_gateway.h | 4 ++-- src/rpc/blockchain.cpp | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 57b537606..eff32b495 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2242,7 +2242,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int for (iter=0; iter (daywindow>>1) ) { n = 0; - i = (j + seed) % daywindow; + i = (iter + seed) % daywindow; for (k=0; k= daywindow ) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index bc8302589..7055e65c3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1241,7 +1241,6 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 05:31:47 -1100 Subject: [PATCH 067/154] Rngval inside inner loop --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 7055e65c3..fc476657d 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1233,7 +1233,6 @@ UniValue prices(const UniValue& params, bool fHelp) rngval = seed; for (j=1; j Date: Tue, 2 Apr 2019 05:38:06 -1100 Subject: [PATCH 068/154] Test --- src/rpc/blockchain.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index fc476657d..eaeae8f60 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1231,6 +1231,9 @@ UniValue prices(const UniValue& params, bool fHelp) ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); } rngval = seed; + for (i=0; i Date: Tue, 2 Apr 2019 05:42:40 -1100 Subject: [PATCH 069/154] tst --- src/rpc/blockchain.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index eaeae8f60..a72f0c341 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1222,16 +1222,13 @@ UniValue prices(const UniValue& params, bool fHelp) } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } } + ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); + UniValue timestamps(UniValue::VARR); for (i=0; i Date: Tue, 2 Apr 2019 05:47:10 -1100 Subject: [PATCH 070/154] Test --- src/komodo_gateway.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index eff32b495..2ea4c2753 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2263,6 +2263,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation++; if ( correlation > (daywindow>>1) ) { + return(sum*mult/correlation); n = 0; i = (iter + seed) % daywindow; for (k=0; k Date: Tue, 2 Apr 2019 05:50:12 -1100 Subject: [PATCH 071/154] Test --- src/komodo_gateway.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 2ea4c2753..e6bdd9a8f 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2251,6 +2251,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( lowprice == refprice ) lowprice--; sum = 0; + fprintf(stderr,"firsti.%d: ",firsti); for (j=0; j= daywindow ) @@ -2259,10 +2260,12 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int return(-1); if ( price >= lowprice && price <= highprice ) { - sum += price * mult; + fprintf(stderr,"%.1f ",(double)price/10000); + sum += price; correlation++; if ( correlation > (daywindow>>1) ) { + fprintf(stderr,"-> %.4f\n",(double)sum*mult/correlation); return(sum*mult/correlation); n = 0; i = (iter + seed) % daywindow; From 9b75e70178dddf5064f0eae97861e1f3dcdc4de2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:51:17 -1100 Subject: [PATCH 072/154] I --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e6bdd9a8f..446619eb7 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2251,7 +2251,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( lowprice == refprice ) lowprice--; sum = 0; - fprintf(stderr,"firsti.%d: ",firsti); + fprintf(stderr,"firsti.%d: ",i); for (j=0; j= daywindow ) From 355ce81643952bfb6e5beddb40665b2cc68781fc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:54:30 -1100 Subject: [PATCH 073/154] -print --- src/komodo_gateway.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 446619eb7..36de71951 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2251,7 +2251,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( lowprice == refprice ) lowprice--; sum = 0; - fprintf(stderr,"firsti.%d: ",i); + //fprintf(stderr,"firsti.%d: ",i); for (j=0; j= daywindow ) @@ -2260,13 +2260,13 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int return(-1); if ( price >= lowprice && price <= highprice ) { - fprintf(stderr,"%.1f ",(double)price/10000); + //fprintf(stderr,"%.1f ",(double)price/10000); sum += price; correlation++; if ( correlation > (daywindow>>1) ) { - fprintf(stderr,"-> %.4f\n",(double)sum*mult/correlation); - return(sum*mult/correlation); + //fprintf(stderr,"-> %.4f\n",(double)sum*mult/correlation); + //return(sum*mult/correlation); n = 0; i = (iter + seed) % daywindow; for (k=0; k %.8f\n",(long long)firstprice,((double)(sum*mult) / den) / COIN); + //fprintf(stderr,"firstprice.%llu weighted -> %.8f\n",(long long)firstprice,((double)(sum*mult) / den) / COIN); return((sum * mult) / den); } } From fae4422d85b83a007d4c7e52441e6feeab8a94fb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 05:55:26 -1100 Subject: [PATCH 074/154] Rc --- src/rpc/blockchain.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a72f0c341..5e426b88e 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1228,9 +1228,9 @@ UniValue prices(const UniValue& params, bool fHelp) timestamps.push_back((int64_t)prices[i]); ret.push_back(Pair("timestamps",timestamps)); rngval = seed; - for (i=0; i Date: Tue, 2 Apr 2019 06:03:24 -1100 Subject: [PATCH 075/154] Test --- src/komodo_gateway.h | 18 ++++++++++-------- src/rpc/blockchain.cpp | 8 +++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 36de71951..2d708f036 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2239,6 +2239,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( ind < 36 ) mult = 10000; else mult = 1; + memset(nonzprices,0,sizeof(*nonzprices)*daywindow); for (iter=0; iter2*daywindow+2; i++,ht--) { @@ -1247,7 +1248,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Tue, 2 Apr 2019 06:31:42 -1100 Subject: [PATCH 076/154] Update --- src/komodo_gateway.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 2d708f036..65f534778 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1832,6 +1832,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i { fprintf(stderr,"force update prices\n"); komodo_cbopretupdate(1); + memcpy(localbits,Mineropret.data(),Mineropret.size()); } else return(-1); } } From 503eadb7d989ff3db774ef8cfe7acdded257686f Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 06:48:55 -1100 Subject: [PATCH 077/154] komodo_cbopretupdate if zero price --- src/komodo_gateway.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 65f534778..7b6227b92 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1684,7 +1684,10 @@ CScript komodo_mineropret(int32_t nHeight) if ( pricebits[i] == 0 ) numzero++; if ( numzero != 0 ) - fprintf(stderr,"numzero.%d\n",numzero); + { + fprintf(stderr,"komodo_mineropret numzero.%d vs n.%d\n",numzero,n); + komodo_cbopretupdate(1); + } } if ( komodo_heightpricebits(0,prevbits,nHeight-1) > 0 ) { From 8be53197b396c244eab12b09d8e27e26aaa7223d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 06:52:50 -1100 Subject: [PATCH 078/154] +prnt --- src/komodo_gateway.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7b6227b92..c04bfb381 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1682,11 +1682,15 @@ CScript komodo_mineropret(int32_t nHeight) memcpy(pricebits,Mineropret.data(),Mineropret.size()); for (i=numzero=0; i 0 ) @@ -2167,10 +2171,10 @@ if ( komodo_nextheight() > 333 ) // for debug only! if ( (flags & 4) != 0 ) lastcrypto = now; memcpy(Mineropret.data(),PriceCache[0],size); + int32_t i; for (i=0; i Date: Tue, 2 Apr 2019 06:58:35 -1100 Subject: [PATCH 079/154] Fix init conditions --- src/komodo_gateway.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index c04bfb381..99462fdd3 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1573,6 +1573,7 @@ void komodo_PriceCache_shift() //for (j=0; j<4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex); j++) // PriceCache[i][j] = PriceCache[i-1][j]; } + memcpy(PriceCache[0],Mineropret.data(),Mineropret.size()); } // komodo_heightpricebits() extracts the price data in the coinbase for nHeight @@ -2173,7 +2174,7 @@ if ( komodo_nextheight() > 333 ) // for debug only! memcpy(Mineropret.data(),PriceCache[0],size); int32_t i; for (i=0; i Date: Tue, 2 Apr 2019 07:02:57 -1100 Subject: [PATCH 080/154] /sizeof(uint32_t) --- src/komodo_gateway.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 99462fdd3..f13dbdd47 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1691,7 +1691,7 @@ CScript komodo_mineropret(int32_t nHeight) { fprintf(stderr," komodo_mineropret numzero.%d vs n.%d\n",numzero,n); komodo_cbopretupdate(1); - sleep(5); + sleep(61); } } if ( komodo_heightpricebits(0,prevbits,nHeight-1) > 0 ) @@ -1702,7 +1702,7 @@ CScript komodo_mineropret(int32_t nHeight) { // if the new prices are outside tolerance, update Mineropret with clamped prices komodo_priceclamp(n,pricebits,prevbits,PRICES_MAXCHANGE); - fprintf(stderr,"update Mineropret to clamped prices\n"); + //fprintf(stderr,"update Mineropret to clamped prices\n"); memcpy(Mineropret.data(),pricebits,Mineropret.size()); } } @@ -2146,7 +2146,7 @@ if ( komodo_nextheight() > 333 ) // for debug only! if ( flags == 0 ) komodo_PriceCache_shift(); flags |= 2; - memcpy(&PriceCache[0][size],forexprices,sizeof(forexprices)); + memcpy(&PriceCache[0][size/sizeof(uint32_t)],forexprices,sizeof(forexprices)); } size += sizeof(forexprices); } @@ -2157,7 +2157,7 @@ if ( komodo_nextheight() > 333 ) // for debug only! get_cryptoprices(cryptoprices,Cryptos,(int32_t)(sizeof(Cryptos)/sizeof(*Cryptos))); if ( flags == 0 ) komodo_PriceCache_shift(); - memcpy(&PriceCache[0][size],cryptoprices,sizeof(cryptoprices)); + memcpy(&PriceCache[0][size/sizeof(uint32_t)],cryptoprices,sizeof(cryptoprices)); flags |= 4; // very rarely we can see flags == 6 case } size += sizeof(cryptoprices); From 62a9756bc0de1d2b130b3ad34330e7b14ce813ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 07:07:09 -1100 Subject: [PATCH 081/154] Test --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index f13dbdd47..6d2fef363 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2172,9 +2172,9 @@ if ( komodo_nextheight() > 333 ) // for debug only! if ( (flags & 4) != 0 ) lastcrypto = now; memcpy(Mineropret.data(),PriceCache[0],size); - int32_t i; for (i=0; i Date: Tue, 2 Apr 2019 08:13:43 -1100 Subject: [PATCH 082/154] Test --- src/komodo_gateway.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6d2fef363..dbc879806 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2345,7 +2345,10 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz if ( correlated[i] == 0 ) correlated[i] = correlated[i-1]; if ( firstprice == 0 && correlated[i] != 0 ) + { firstprice = correlated[i]; + break; + } } if ( firstprice != 0 ) { @@ -2358,7 +2361,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 08:34:57 -1100 Subject: [PATCH 083/154] Pre-smoother --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index dbc879806..276bb8667 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2312,7 +2312,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += (daywindow - i) * price;//((price + firstprice*2) / 3); + sum += (daywindow - i) * ((price + firstprice) / 2); n++; } } @@ -2361,7 +2361,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 08:38:18 -1100 Subject: [PATCH 084/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 276bb8667..d5f9c98f9 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2361,7 +2361,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 09:35:01 -1100 Subject: [PATCH 085/154] 2/3 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d5f9c98f9..a926e3de0 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2361,7 +2361,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 09:38:09 -1100 Subject: [PATCH 086/154] 1:1 --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a926e3de0..9c9b3959e 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2312,7 +2312,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += (daywindow - i) * ((price + firstprice) / 2); + sum += (daywindow - i) * price; n++; } } @@ -2361,7 +2361,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 09:43:58 -1100 Subject: [PATCH 087/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9c9b3959e..120267dad 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2364,7 +2364,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum += (daywindow - i) * correlated[i]; den += (daywindow - i); } - smoothed = (sum / den); + smoothed = ((sum / den) + firstprice) >> 1; } return(smoothed); } From 480349a221c09c65957e9becc61a75df6a4067e8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 09:49:48 -1100 Subject: [PATCH 088/154] 0.9 decay --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 120267dad..78d320c91 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2312,7 +2312,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += (daywindow - i) * price; + sum += ((daywindow - i) * (price + firstprice*9)) / 10; n++; } } @@ -2361,10 +2361,10 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i> 1; + smoothed = (sum / den); } return(smoothed); } From 05bd8d8af63d51ea3ba11f4c7816f48b97f7bd7c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:16:03 -1100 Subject: [PATCH 089/154] Smooth coifs --- src/komodo_gateway.h | 36 +++++++++++++++++++++++++++--------- src/rpc/blockchain.cpp | 11 ++++++----- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 78d320c91..afa3b40c1 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2312,7 +2312,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += ((daywindow - i) * (price + firstprice*9)) / 10; + sum += ((daywindow - i) * (price + firstprice*2)) / 3; n++; } } @@ -2334,11 +2334,17 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int } -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices) +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices,int32_t smoothwidth) { - int32_t i; int64_t sum,den,smoothed=0,firstprice = correlated[0]; + const int64_t coeffs[7] = { 7, 13, 5, 4, 6, 7, 3 }; + int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); + if ( sizeof(smoothed)/sizeof(*smoothed) ) + { + fprintf(stderr,"smoothwidth %d != %d\n",smoothwidth,(int32_t)(sizeof(smoothed)/sizeof(*smoothed))); + return(0); + } memset(nonzprices,0,sizeof(*nonzprices)*daywindow); for (i=1; i %.4f\n",(double)(smoothedsum/smoothedden)/10000); + return(smoothedsum/smoothedden); } - return(smoothed); + return(0); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 0080c9b2c..520307c4b 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1183,6 +1183,7 @@ uint32_t komodo_heightstamp(int32_t height); UniValue prices(const UniValue& params, bool fHelp) { + int32_t smoothwidth = 7; if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); @@ -1197,7 +1198,7 @@ UniValue prices(const UniValue& params, bool fHelp) UniValue a(UniValue::VARR); if ( daywindow < 7 ) daywindow = 7; - width = maxsamples+2*daywindow; + width = maxsamples+2*daywindow+smoothwidth; numpricefeeds = komodo_heightpricebits(&seed,rawprices,nextheight-1); if ( numpricefeeds <= 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "illegal numpricefeeds"); @@ -1205,7 +1206,7 @@ UniValue prices(const UniValue& params, bool fHelp) correlated = (int64_t *)calloc(sizeof(*correlated),width); correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); prices2 = (uint32_t *)calloc(sizeof(*prices2),width); - for (ht=nextheight-1,i=0; i2*daywindow+2; i++,ht--) + for (ht=nextheight-1,i=0; i2*daywindow+2+smoothwidth; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); @@ -1238,17 +1239,17 @@ UniValue prices(const UniValue& params, bool fHelp) if ( (str= komodo_pricename(name,j)) != 0 ) { item.push_back(Pair("name",str)); - for (i=0; i Date: Tue, 2 Apr 2019 10:17:38 -1100 Subject: [PATCH 090/154] Smooth width --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 520307c4b..e31f5d8a4 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,7 +1176,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices,int64_t *correlated2); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices,int64_t *correlated2,int32_t smoothwidth); int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); From 778b70285efca1d9aab687f007644c638ae9be4b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:19:24 -1100 Subject: [PATCH 091/154] Add condition --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index afa3b40c1..d0539426e 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2340,7 +2340,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); - if ( sizeof(smoothed)/sizeof(*smoothed) ) + if ( smoothwidth != sizeof(smoothed)/sizeof(*smoothed) ) { fprintf(stderr,"smoothwidth %d != %d\n",smoothwidth,(int32_t)(sizeof(smoothed)/sizeof(*smoothed))); return(0); From 903455b07b0332e031c561e186615fae64d1ad7e Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:24:52 -1100 Subject: [PATCH 092/154] Gilt --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d0539426e..9543cc1b9 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2336,7 +2336,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices,int32_t smoothwidth) { - const int64_t coeffs[7] = { 7, 13, 5, 4, 6, 7, 3 }; + const int64_t coeffs[7] = { 7, 7, 7, 7, 7, 7, 7 }; int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); @@ -2378,8 +2378,8 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz for (i=0; i<7; i++) { fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); - smoothedsum += coeffs[i] * smoothed[i]; - smoothedden += coeffs[i]; + smoothedsum += (7-i) * smoothed[i]; + smoothedden += (7-i); } fprintf(stderr,"-> %.4f\n",(double)(smoothedsum/smoothedden)/10000); return(smoothedsum/smoothedden); From a5957af75a3170c0816866658656787bc5e56109 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:25:59 -1100 Subject: [PATCH 093/154] 1:1 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 9543cc1b9..8da2a94bf 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2369,7 +2369,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 10:32:08 -1100 Subject: [PATCH 094/154] Test --- src/komodo_gateway.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 8da2a94bf..63e6898eb 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2254,8 +2254,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (iter + seed) % daywindow; refprice = rawprices[i]; - highprice = (refprice * (COIN + PRICES_MAXCHANGE*3)) / COIN; - lowprice = (refprice * (COIN - PRICES_MAXCHANGE*3)) / COIN; + highprice = (refprice * (COIN + PRICES_MAXCHANGE*4)) / COIN; + lowprice = (refprice * (COIN - PRICES_MAXCHANGE*4)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) @@ -2312,7 +2312,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += ((daywindow - i) * (price + firstprice*2)) / 3; + sum += ((daywindow - i) * (price + firstprice*9)) / 10; n++; } } @@ -2369,7 +2369,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 10:34:58 -1100 Subject: [PATCH 095/154] Arg --- src/komodo_gateway.h | 7 +++++-- src/rpc/blockchain.cpp | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 63e6898eb..0d4274ae5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2240,7 +2240,7 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *nonzprices) +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t daywindow,uint32_t *nonzprices,int32_t smoothwidth) { int32_t i,j,k,n,iter,correlation,maxcorrelation=0; int64_t firstprice,price,sum,den,mult,refprice,lowprice,highprice; if ( daywindow < 2 ) @@ -2249,7 +2249,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int mult = 10000; else mult = 1; memset(nonzprices,0,sizeof(*nonzprices)*daywindow); - for (iter=0; iter= daywindow ) i = 0; if ( (price= rawprices[i]) == 0 ) + { + fprintf(stderr,"null rawprice.[%d]\n",i); return(-1); + } if ( price >= lowprice && price <= highprice ) { //fprintf(stderr,"%.1f ",(double)price/10000); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index e31f5d8a4..6e6746a30 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1177,7 +1177,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices,int64_t *correlated2,int32_t smoothwidth); -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); @@ -1243,7 +1243,7 @@ UniValue prices(const UniValue& params, bool fHelp) { offset = j*width + i; rngval = (rngval*11109 + 13849); - if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow+smoothwidth,prices2)) < 0 ) + if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow+smoothwidth,prices2,smoothwidth)) < 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); } for (i=0; i Date: Tue, 2 Apr 2019 10:38:42 -1100 Subject: [PATCH 096/154] -smoothwidth --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6e6746a30..e471492ce 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1243,7 +1243,7 @@ UniValue prices(const UniValue& params, bool fHelp) { offset = j*width + i; rngval = (rngval*11109 + 13849); - if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow+smoothwidth,prices2,smoothwidth)) < 0 ) + if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow,prices2,smoothwidth)) < 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); } for (i=0; i Date: Tue, 2 Apr 2019 10:42:03 -1100 Subject: [PATCH 097/154] .9 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 0d4274ae5..28913c08e 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2372,7 +2372,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 10:47:41 -1100 Subject: [PATCH 098/154] Test --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 28913c08e..6d6f8f155 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2372,7 +2372,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz sum = den = 0; for (i=0; i Date: Tue, 2 Apr 2019 10:53:33 -1100 Subject: [PATCH 099/154] Test --- src/komodo_gateway.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6d6f8f155..64fcbb957 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2339,7 +2339,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices,int32_t smoothwidth) { - const int64_t coeffs[7] = { 7, 7, 7, 7, 7, 7, 7 }; + const int64_t coeffs[7] = { -1, 9, -45, 0, 45, -9, 1 }; // / 60 int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); @@ -2377,12 +2377,13 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz } smoothed[iter] = (sum / den); } - smoothedsum = smoothedden = 0; + smoothedsum = 0; + smoothedden = 60; for (i=0; i<7; i++) { fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); - smoothedsum += (7-i) * smoothed[i]; - smoothedden += (7-i); + smoothedsum += coeffs[i] * smoothed[i]; + //smoothedden += (7-i); } fprintf(stderr,"-> %.4f\n",(double)(smoothedsum/smoothedden)/10000); return(smoothedsum/smoothedden); From 966fafb2700a4c5543e5381f9191cadf7e1e84f1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:54:20 -1100 Subject: [PATCH 100/154] Test --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 64fcbb957..6f72f855b 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2339,7 +2339,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices,int32_t smoothwidth) { - const int64_t coeffs[7] = { -1, 9, -45, 0, 45, -9, 1 }; // / 60 + const int64_t coeffs[7] = { -1, 9, -45, 1, 45, -9, 1 }; // / 60 int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); @@ -2378,7 +2378,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz smoothed[iter] = (sum / den); } smoothedsum = 0; - smoothedden = 60; + smoothedden = 1; for (i=0; i<7; i++) { fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); From 79120f3cac81e208c97156640c05c9f0b73b7f6d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 10:58:48 -1100 Subject: [PATCH 101/154] Test --- src/komodo_gateway.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6f72f855b..7507122f3 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2339,7 +2339,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonzprices,int32_t smoothwidth) { - const int64_t coeffs[7] = { -1, 9, -45, 1, 45, -9, 1 }; // / 60 + //const int64_t coeffs[7] = { -1, 9, -45, 1, 45, -9, 1 }; // / 60 + const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); @@ -2378,7 +2379,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz smoothed[iter] = (sum / den); } smoothedsum = 0; - smoothedden = 1; + smoothedden = 64; for (i=0; i<7; i++) { fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); From 8adcc5022c167ac1aed17181a1eabe2b7caa1bd8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 11:04:32 -1100 Subject: [PATCH 102/154] Test --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7507122f3..778f5b318 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2315,7 +2315,7 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( (price= nonzprices[i]) != 0 ) { den += (daywindow - i); - sum += ((daywindow - i) * (price + firstprice*9)) / 10; + sum += ((daywindow - i) * (price + firstprice*4)) / 5; n++; } } @@ -2382,11 +2382,11 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz smoothedden = 64; for (i=0; i<7; i++) { - fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); + //fprintf(stderr,"%.4f ",(double)smoothed[i]/10000); smoothedsum += coeffs[i] * smoothed[i]; //smoothedden += (7-i); } - fprintf(stderr,"-> %.4f\n",(double)(smoothedsum/smoothedden)/10000); + //fprintf(stderr,"-> %.4f\n",(double)(smoothedsum/smoothedden)/10000); return(smoothedsum/smoothedden); } return(0); From 6cf121c5d5d1f9c82d399b2358ac2db6934933ef Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 11:18:19 -1100 Subject: [PATCH 103/154] Smooth64 --- src/komodo_gateway.h | 76 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 778f5b318..6659dcced 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2336,12 +2336,82 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int return(0); } +int64_t _pairave64(int64_t valA,int64_t valB) +{ + if ( valA != 0 && valB != 0 ) + return((valA + valB) / 2); + else if ( valA != 0 ) return(valA); + else return(valB); +} + +int64_t _pairdiff64(register int64_t valA,register int64_t valB) +{ + if ( valA != 0 && valB != 0 ) + return(valA - valB); + else return(0); +} + +int64_t balanced_ave64(int64_t buf[],int32_t i,int32_t width) +{ + register int32_t nonz,j; register int64_t sum,price; + nonz = 0; + sum = 0; + for (j=-width; j<=width; j++) + { + price = buf[i + j]; + if ( price != 0 ) + { + sum += price; + nonz++; + } + } + if ( nonz != 0 ) + sum /= nonz; + return(sum); +} + +void buf_trioave64(int64_t dest[],int64_t src[],int32_t n) +{ + register int32_t i,j,width = 3; + for (i=0; i<128; i++) + src[i] = 0; + //for (i=n-width-1; i>width; i--) + // dest[i] = balanced_ave(src,i,width); + //for (i=width; i>0; i--) + // dest[i] = balanced_ave(src,i,i); + for (i=1; i Date: Tue, 2 Apr 2019 11:19:38 -1100 Subject: [PATCH 104/154] smoothwidth --- src/komodo_gateway.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6659dcced..25b87d6e3 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2400,8 +2400,8 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) buf_trioave64(smoothbufA,src,width); for (i=0; i Date: Tue, 2 Apr 2019 11:20:39 -1100 Subject: [PATCH 105/154] Trioave64 --- src/komodo_gateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 25b87d6e3..b1dbfbfe7 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2403,7 +2403,7 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) buf_trioave64(smoothbufB,smoothbufA,width); buf_trioave64(smoothbufA,smoothbufB,width); } - buf_trioave(dest,smoothbufA,width); + buf_trioave64(dest,smoothbufA,width); } else memcpy(dest,src,width*sizeof(*dest)); } From 9b72450a1f2064c080ad86a6083b361be0e71afb Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 11:24:20 -1100 Subject: [PATCH 106/154] Revert --- src/komodo_gateway.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index b1dbfbfe7..d16fa4bc5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2411,7 +2411,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz { //const int64_t coeffs[7] = { -1, 9, -45, 1, 45, -9, 1 }; // / 60 const int64_t coeffs[7] = { -2, 0, 18, 32, 18, 0, -2 }; - int32_t i,iter; int64_t dest[1440*3],orig[1440*3],smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; + int32_t i,iter; int64_t smoothedden,smoothedsum,sum,den,smoothed[7],firstprice = correlated[0]; if ( daywindow < 2 ) return(0); if ( smoothwidth != sizeof(smoothed)/sizeof(*smoothed) ) @@ -2438,19 +2438,19 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz correlated[i] = firstprice; else break; } - memcpy(orig,correlated,(daywindow+smoothwidth)*sizeof(*correlated)); + //memcpy(orig,correlated,(daywindow+smoothwidth)*sizeof(*correlated)); for (iter=0; iter Date: Tue, 2 Apr 2019 21:12:33 -1100 Subject: [PATCH 107/154] Fix localcache scan control flow --- src/komodo_gateway.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index d16fa4bc5..16a96b526 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1816,9 +1816,10 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( PriceCache[j][i] >= prevbits[i] ) { fprintf(stderr,"i.%d within recent localprices[%d] %u >= %u\n",i,j,PriceCache[j][i],prevbits[i]); - continue; + break; } - break; + if ( j == KOMODO_LOCALPRICE_CACHESIZE ) + break; } else if ( maxflag < 0 && localbits[i] > prevbits[i] ) { @@ -1828,9 +1829,10 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i if ( PriceCache[j][i] <= prevbits[i] ) { fprintf(stderr,"i.%d within recent localprices[%d] %u <= prev %u\n",i,j,PriceCache[j][i],prevbits[i]); - continue; + break; } - break; + if ( j == KOMODO_LOCALPRICE_CACHESIZE ) + break; } } } From 4a704517b9d3cbafe5e841a61ef72cd12a310c42 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 21:17:05 -1100 Subject: [PATCH 108/154] +print --- src/cc/gamescc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index b804216d7..2a8be815e 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -110,6 +110,7 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_settle(txfee,cp,params)); \ else \ { \ +fprintf(stderr,"invalid method (%s)\n",method); \ result.push_back(Pair("result","error")); \ result.push_back(Pair("error","invalid gamescc method")); \ return(result); \ From 1b6bb2d0a18aa58b22164345013e9dd7477da1bc Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 21:18:45 -1100 Subject: [PATCH 109/154] +print --- src/cc/cclib.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 00a7c6fe0..370b1b8e3 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -195,7 +195,7 @@ cJSON *cclib_reparse(int32_t *nump,char *jsonstr) // assumes origparams will be UniValue CClib_method(struct CCcontract_info *cp,char *method,char *jsonstr) { UniValue result(UniValue::VOBJ); uint64_t txfee = 10000; int32_t m; cJSON *params = cclib_reparse(&m,jsonstr); - //fprintf(stderr,"method.(%s) -> (%s)\n",jsonstr!=0?jsonstr:"",params!=0?jprint(params,0):""); + fprintf(stderr,"method.(%s) -> (%s)\n",jsonstr!=0?jsonstr:"",params!=0?jprint(params,0):""); #ifdef BUILD_ROGUE if ( cp->evalcode == EVAL_ROGUE ) { From 679d310c5edbd762cd59443f7ae3645afeed1d60 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 21:20:45 -1100 Subject: [PATCH 110/154] Fix crash on missing method --- src/cc/cclib.cpp | 4 ++-- src/cc/gamescc.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 370b1b8e3..8552cfa1b 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -351,7 +351,7 @@ UniValue CClib_info(struct CCcontract_info *cp) UniValue CClib(struct CCcontract_info *cp,char *method,char *jsonstr) { UniValue result(UniValue::VOBJ); int32_t i; std::string rawtx; cJSON *params; - //printf("CClib params.(%s)\n",jsonstr!=0?jsonstr:""); +//printf("CClib params.(%s)\n",jsonstr!=0?jsonstr:""); for (i=0; ievalcode == CClib_methods[i].evalcode && strcmp(method,CClib_methods[i].method) == 0 ) @@ -369,7 +369,7 @@ UniValue CClib(struct CCcontract_info *cp,char *method,char *jsonstr) } } result.push_back(Pair("result","error")); - result.push_back(Pair("method",CClib_methods[i].method)); + result.push_back(Pair("method",method)); result.push_back(Pair("error","method not found")); return(result); } diff --git a/src/cc/gamescc.h b/src/cc/gamescc.h index 2a8be815e..b804216d7 100644 --- a/src/cc/gamescc.h +++ b/src/cc/gamescc.h @@ -110,7 +110,6 @@ if ( cp->evalcode == EVAL_GAMES ) \ return(games_settle(txfee,cp,params)); \ else \ { \ -fprintf(stderr,"invalid method (%s)\n",method); \ result.push_back(Pair("result","error")); \ result.push_back(Pair("error","invalid gamescc method")); \ return(result); \ From 5d861c188abddd2c0db60fdf31008a51f52503ed Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 23:37:01 -1100 Subject: [PATCH 111/154] -ac_prices --- src/cc/pegs.cpp | 10 ++++++---- src/komodo_defs.h | 1 + src/komodo_gateway.h | 31 ++++++++++++++++++++----------- src/komodo_globals.h | 1 + src/komodo_utils.h | 7 +++++++ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/cc/pegs.cpp b/src/cc/pegs.cpp index c435d9920..dba9fc818 100644 --- a/src/cc/pegs.cpp +++ b/src/cc/pegs.cpp @@ -16,15 +16,17 @@ #include "CCPegs.h" /* - Pegs CC builds on top of Prices CC and would bind a pricefeed to a token bid/ask automated marketmaking. + Pegs CC builds on top of CCOPRET modes which will create pricefeeds in the coinbase. + flag&1: BTC/USD 4966.1400, BTC/GBP 3770.0402, BTC/EUR 4416.8452 GBPUSD 1.317264, EURUSD 1.124364 EURGBP 0.853560 + flag&2: (BGN 1.7462) (NZD 1.4771) (ILS 3.6258) (RUB 65.3700) (CAD 1.3320) (PHP 52.4160) (CHF 0.9995) (AUD 1.4122) (JPY 111.3660) (TRY 5.5483) (HKD 7.8498) (MYR 4.0816) (HRK 6.6337) (CZK 22.9937) (IDR 14220.0000) (DKK 6.6648) (NOK 8.6111) (HUF 287.2142) (GBP 0.7678) (MXN 19.1046) (THB 31.7500) (ISK 122.4107) (ZAR 14.1300) (BRL 3.8604) (SGD 1.3551) (PLN 3.8350) (INR 68.7531) (KRW 1136.3839) (RON 4.2516) (CNY 6.7200) (SEK 9.3230) (EUR 0.8928) (2019-04-02) +flag&4: (KMD 0.00025110) (ETH 0.03357500) (LTC 0.01642400) (BCHABC 0.05167400) (XMR 0.01398800) (IOTA 0.00007217) (DASH 0.02552600) (XEM 0.00001459) (ZEC 0.01440500) (WAVES 0.00062170) (RVN 0.00001215) (LSK 0.00041130) (DCR 0.00496000) (BTS 0.00001444) (ICX 0.00008750) (HOT 0.00000027) (STEEM 0.00010070) (ENJ 0.00003221) (STRAT 0.00022790) errs.0 + Funds deposited into CC address for a specific peg would then be used to fund the bid/ask as the pricefeed changes the price. Profits/losses would accumulate in the associated address. In the event funds exceed a specified level, it can be spent into a collection address. The idea is that the collection address can further be used for revshares. - int64_t OraclePrice(int32_t height,uint256 reforacletxid,char *markeraddr,char *format); - - OraclePrice is very useful for pegs. + */ diff --git a/src/komodo_defs.h b/src/komodo_defs.h index b0f618e93..c1eeb375b 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -59,6 +59,7 @@ extern uint32_t ASSETCHAINS_VERUSHASH, ASSETCHAINS_VERUSHASHV1_1, ASSETCHAINS_NO extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN]; extern std::string NOTARY_PUBKEY,ASSETCHAINS_OVERRIDE_PUBKEY,ASSETCHAINS_SCRIPTPUB; extern uint8_t NOTARY_PUBKEY33[33],ASSETCHAINS_OVERRIDE_PUBKEY33[33],ASSETCHAINS_MARMARA; +extern std::vector ASSETCHAINS_PRICES; extern char ASSETCHAINS_SYMBOL[65]; extern int32_t VERUS_BLOCK_POSUNITS, VERUS_CONSECUTIVE_POS_THRESHOLD, VERUS_NOPOS_THRESHHOLD; diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 16a96b526..620ba1f9b 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1553,7 +1553,6 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR #define KOMODO_LOCALPRICE_CACHESIZE 7 - #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "DASH", "XEM", "ZEC", "WAVES", "RVN", "LSK", "DCR", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" }; // must be on binance (for now) @@ -1563,6 +1562,7 @@ const char *Forex[] = }; // must be in ECB list uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; +int32_t komodo_cbopretsize(uint64_t flags); void komodo_PriceCache_shift() { @@ -1722,7 +1722,7 @@ CScript komodo_mineropret(int32_t nHeight) The only way komodo_opretvalidate() doesnt return an error is if maxflag is set or it is within tolerance of both the prior block and the local data. The local data validation only happens if it is a recent block and not a block from the past as the local node is only getting the current price data. */ -// reconsiderblock 002aca768b09dfcf36bd934ab34b23983148b116e12cb0b6e1a3f895d1db63aa +// for PRICES: reconsiderblock 002aca768b09dfcf36bd934ab34b23983148b116e12cb0b6e1a3f895d1db63aa // and // reconsiderblock 0034cf582018eacc0b4ae001491ce460113514cb1a3f217567ef4a2207de361a // reconsiderbloc 000abf51c023b64af327c50c1b060797b8cb281c696d30ab92fd002a8b8c9aea @@ -1732,7 +1732,7 @@ CScript komodo_mineropret(int32_t nHeight) int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 500; + int32_t testchain_exemption = 0; std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { @@ -1753,7 +1753,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -testchain_exemption ) // must be close to last block timestamp + if ( lag2 < -60 ) //testchain_exemption ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) @@ -2107,6 +2107,19 @@ int32_t get_btcusd(uint32_t pricebits[4]) // komodo_cbopretupdate() obtains the external price data and encodes it into Mineropret, which will then be used by the miner and validation // save history, use new data to approve past rejection, where is the auto-reconsiderblock? +int32_t komodo_cbopretsize(uint64_t flags) +{ + int32_t size = 0; + if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) + { + size = PRICES_SIZEBIT0; + if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) + size += sizeof(forexprices); + if ( (ASSETCHAINS_CBOPRET & 4) != 0 ) + size += sizeof(cryptoprices); + } + return(size); +} void komodo_cbopretupdate(int32_t forceflag) { @@ -2123,13 +2136,9 @@ void komodo_cbopretupdate(int32_t forceflag) now = (uint32_t)time(NULL); if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) { -if ( komodo_nextheight() > 333 ) // for debug only! - ASSETCHAINS_CBOPRET = 7; - size = PRICES_SIZEBIT0; - if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) - size += sizeof(forexprices); - if ( (ASSETCHAINS_CBOPRET & 4) != 0 ) - size += sizeof(cryptoprices); +//if ( komodo_nextheight() > 333 ) // for debug only! +// ASSETCHAINS_CBOPRET = 7; + size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); size = PRICES_SIZEBIT0; diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 097d10da0..1c47e72e6 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -72,6 +72,7 @@ uint64_t ASSETCHAINS_TIMEUNLOCKFROM = 0, ASSETCHAINS_TIMEUNLOCKTO = 0,ASSETCHAIN uint64_t ASSETCHAINS_LASTERA = 1; uint64_t ASSETCHAINS_ENDSUBSIDY[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_REWARD[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_HALVING[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_DECAY[ASSETCHAINS_MAX_ERAS],ASSETCHAINS_NOTARY_PAY[ASSETCHAINS_MAX_ERAS]; uint8_t ASSETCHAINS_CCDISABLES[256]; +std::vector ASSETCHAINS_PRICES; #define _ASSETCHAINS_EQUIHASH 0 uint32_t ASSETCHAINS_NUMALGOS = 3; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index a2eb3d5a1..a382e8c22 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1812,6 +1812,13 @@ void komodo_args(char *argv0) ASSETCHAINS_CODAPORT = GetArg("-ac_coda",0); ASSETCHAINS_MARMARA = GetArg("-ac_marmara",0); ASSETCHAINS_CBOPRET = GetArg("-ac_cbopret",0); + if ( ASSETCHAINS_CBOPRET != 0 ) + { + Split(GetArg("-ac_prices",""), ASSETCHAINS_PRICES, 0); + for (i=0; i Date: Tue, 2 Apr 2019 23:47:37 -1100 Subject: [PATCH 112/154] SplitStr --- src/komodo_gateway.h | 3 ++- src/komodo_utils.h | 5 +++-- src/util.cpp | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 620ba1f9b..1d1379c15 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2109,7 +2109,8 @@ int32_t get_btcusd(uint32_t pricebits[4]) int32_t komodo_cbopretsize(uint64_t flags) { - int32_t size = 0; + int32_t size = 0; uint32_t cryptoprices[sizeof(Cryptos)/sizeof(*Cryptos)],forexprices[sizeof(Forex)/sizeof(*Forex)]; + if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) { size = PRICES_SIZEBIT0; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index a382e8c22..0d8b2ed63 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1665,6 +1665,7 @@ uint64_t komodo_ac_block_subsidy(int nHeight) extern int64_t MAX_MONEY; void komodo_cbopretupdate(int32_t forceflag); +void SplitStr(const std::string& strVal, std::vector outVals); void komodo_args(char *argv0) { @@ -1814,9 +1815,9 @@ void komodo_args(char *argv0) ASSETCHAINS_CBOPRET = GetArg("-ac_cbopret",0); if ( ASSETCHAINS_CBOPRET != 0 ) { - Split(GetArg("-ac_prices",""), ASSETCHAINS_PRICES, 0); + SplitStr(GetArg("-ac_prices",""), ASSETCHAINS_PRICES); for (i=0; i outVals) +{ + stringstream ss(strVal); + vector str; + + while ( ss.peek() == ' ' ) + ss.ignore(); + + while ( ss >> str ) + { + //outVals[numVals] = i; + outVals.push_back(i); + numVals += 1; + + while ( ss.peek() == ' ' ) + ss.ignore(); + if ( ss.peek() == ',' ) + ss.ignore(); + while ( ss.peek() == ' ' ) + ss.ignore(); + } +} + void Split(const std::string& strVal, uint64_t *outVals, const uint64_t nDefault) { stringstream ss(strVal); From 2c272b175d0924ea452da3104ed6e153a45bfa42 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 23:53:14 -1100 Subject: [PATCH 113/154] Str --- src/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.cpp b/src/util.cpp index 44e2c2dac..cca39ca38 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -405,7 +405,7 @@ void SplitStr(const std::string& strVal, std::vector outVals) while ( ss >> str ) { //outVals[numVals] = i; - outVals.push_back(i); + outVals.push_back(str); numVals += 1; while ( ss.peek() == ' ' ) From 7d4e16646ef3ba0c0e959323f4597bc966ffd6c6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Apr 2019 23:54:51 -1100 Subject: [PATCH 114/154] -= --- src/util.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index cca39ca38..ebc6dbccb 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -404,10 +404,7 @@ void SplitStr(const std::string& strVal, std::vector outVals) while ( ss >> str ) { - //outVals[numVals] = i; outVals.push_back(str); - numVals += 1; - while ( ss.peek() == ' ' ) ss.ignore(); if ( ss.peek() == ',' ) From cf437222feb010d555c4267d377789fb98bcccc0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 00:03:17 -1100 Subject: [PATCH 115/154] Unsmooth --- src/komodo_gateway.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 1d1379c15..4f59141f5 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2261,13 +2261,13 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int mult = 10000; else mult = 1; memset(nonzprices,0,sizeof(*nonzprices)*daywindow); - for (iter=0; iter (daywindow>>1) ) { + return(refprice * mult); //fprintf(stderr,"-> %.4f\n",(double)sum*mult/correlation); //return(sum*mult/correlation); n = 0; @@ -2458,8 +2459,8 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t daywindow,int64_t *nonz //smooth64(correlated+iter,dest,daywindow,1); for (i=0; i Date: Wed, 3 Apr 2019 00:06:05 -1100 Subject: [PATCH 116/154] Std::string --- src/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.cpp b/src/util.cpp index ebc6dbccb..4265ff759 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -397,7 +397,7 @@ void ParseParameters(int argc, const char* const argv[]) void SplitStr(const std::string& strVal, std::vector outVals) { stringstream ss(strVal); - vector str; + std::string str; while ( ss.peek() == ' ' ) ss.ignore(); From 5b9a4c353a8cfc9552c8afde59f849feb72d81b7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 00:27:09 -1100 Subject: [PATCH 117/154] Revert incompatible changes --- src/komodo_gateway.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 4f59141f5..6862fcf14 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1732,7 +1732,7 @@ CScript komodo_mineropret(int32_t nHeight) int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 0; + int32_t testchain_exemption = 350; std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { @@ -1753,7 +1753,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -60 ) //testchain_exemption ) // must be close to last block timestamp + if ( lag2 < -testchain_exemption ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) @@ -2137,8 +2137,8 @@ void komodo_cbopretupdate(int32_t forceflag) now = (uint32_t)time(NULL); if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) { -//if ( komodo_nextheight() > 333 ) // for debug only! -// ASSETCHAINS_CBOPRET = 7; +if ( komodo_nextheight() > 333 ) // for debug only! + ASSETCHAINS_CBOPRET = 7; size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); From 43e0d2fec6f390fdd391e94c714f306097556252 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 05:58:14 -1100 Subject: [PATCH 118/154] -ac_prices --- src/komodo_gateway.h | 98 ++++++++++++++++++++++++++---------------- src/komodo_utils.h | 13 +++++- src/rpc/blockchain.cpp | 5 ++- 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6862fcf14..b70574677 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1552,6 +1552,7 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define PRICES_MAXCHANGE (COIN / 100) // maximum acceptable change, set at 1% #define PRICES_SIZEBIT0 (sizeof(uint32_t) * 4) // 4 uint32_t unixtimestamp, BTCUSD, BTCGBP and BTCEUR #define KOMODO_LOCALPRICE_CACHESIZE 7 +#define KOMODO_MAXPRICES 2048 #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) @@ -1561,18 +1562,15 @@ const char *Forex[] = { "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" }; // must be in ECB list -uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; +uint32_t PriceCache[KOMODO_LOCALPRICE_CACHESIZE][KOMODO_MAXPRICES];//4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex)]; +int64_t PriceMult[KOMODO_MAXPRICES]; int32_t komodo_cbopretsize(uint64_t flags); void komodo_PriceCache_shift() { int32_t i; for (i=KOMODO_LOCALPRICE_CACHESIZE-1; i>0; i--) - { memcpy(PriceCache[i],PriceCache[i-1],sizeof(PriceCache[i])); - //for (j=0; j<4+sizeof(Cryptos)/sizeof(*Cryptos)+sizeof(Forex)/sizeof(*Forex); j++) - // PriceCache[i][j] = PriceCache[i-1][j]; - } memcpy(PriceCache[0],Mineropret.data(),Mineropret.size()); } @@ -1657,7 +1655,7 @@ int32_t komodo_pricecmp(int32_t nHeight,int32_t n,char *maxflags,uint32_t *price // komodo_priceclamp() clamps any price that is beyond tolerance int32_t komodo_priceclamp(int32_t n,uint32_t *pricebits,uint32_t *refprices,int64_t tolerance) { - int32_t i; uint32_t newprice; char maxflags[2048]; + int32_t i; uint32_t newprice; char maxflags[KOMODO_MAXPRICES]; memset(maxflags,0,sizeof(maxflags)); for (i=1; i= PRICES_SIZEBIT0 ) { n = (int32_t)(Mineropret.size() / sizeof(uint32_t)); @@ -1727,13 +1725,11 @@ CScript komodo_mineropret(int32_t nHeight) // reconsiderblock 0034cf582018eacc0b4ae001491ce460113514cb1a3f217567ef4a2207de361a // reconsiderbloc 000abf51c023b64af327c50c1b060797b8cb281c696d30ab92fd002a8b8c9aea // are needed to sync past initial blocks with different data set -// pass in blockhash and nTime, latch if it is rejected due to local price, then if localprice changes in a way that would validate then issue reconsiderblock -// add rpc call for extracting rawprices int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 350; - std::vector vopret; char maxflags[2048]; double btcusd,btcgbp,btceur; uint32_t localbits[2048],pricebits[2048],prevbits[2048],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); + int32_t testchain_exemption = 0; + std::vector vopret; char maxflags[KOMODO_MAXPRICES]; double btcusd,btcgbp,btceur; uint32_t localbits[KOMODO_MAXPRICES],pricebits[KOMODO_MAXPRICES],prevbits[KOMODO_MAXPRICES],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { GetOpReturnData(scriptPubKey,vopret); @@ -1753,7 +1749,7 @@ int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,i fprintf(stderr,"A ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3); return(-1); } - if ( lag2 < -testchain_exemption ) // must be close to last block timestamp + if ( lag2 < -60 ) //testchain_exemption ) // must be close to last block timestamp { fprintf(stderr,"B ht.%d now.%u htstamp.%u %u - pricebits[0] %u -> lags.%d %d %d vs %d cmp.%d\n",nHeight,now,prevtime,block->nTime,pricebits[0],lag,lag2,lag3,ASSETCHAINS_BLOCKTIME,lag2<-ASSETCHAINS_BLOCKTIME); if ( nHeight > testchain_exemption ) @@ -2028,14 +2024,17 @@ uint32_t get_binanceprice(const char *symbol) return(price); } -int32_t get_cryptoprices(uint32_t *prices,const char *list[],int32_t n) +int32_t get_cryptoprices(uint32_t *prices,const char *list[],int32_t n,std::vector strvec) { - int32_t i,errs=0; uint32_t price; + int32_t i,errs=0; uint32_t price; char *symbol; for (i=0; i 333 ) // for debug only! - ASSETCHAINS_CBOPRET = 7; +//if ( komodo_nextheight() > 333 ) // for debug only! +// ASSETCHAINS_CBOPRET = 7; size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); @@ -2160,19 +2158,19 @@ if ( komodo_nextheight() > 333 ) // for debug only! flags |= 2; memcpy(&PriceCache[0][size/sizeof(uint32_t)],forexprices,sizeof(forexprices)); } - size += sizeof(forexprices); + size += (sizeof(Forex)/sizeof(*Forex)) * sizeof(uint32_t); } if ( (ASSETCHAINS_CBOPRET & 4) != 0 ) { if ( forceflag != 0 || flags != 0 ) { - get_cryptoprices(cryptoprices,Cryptos,(int32_t)(sizeof(Cryptos)/sizeof(*Cryptos))); + get_cryptoprices(cryptoprices,Cryptos,(int32_t)(sizeof(Cryptos)/sizeof(*Cryptos)),ASSETCHAINS_PRICES); if ( flags == 0 ) komodo_PriceCache_shift(); - memcpy(&PriceCache[0][size/sizeof(uint32_t)],cryptoprices,sizeof(cryptoprices)); + memcpy(&PriceCache[0][size/sizeof(uint32_t)],cryptoprices,(sizeof(Cryptos)/sizeof(*Cryptos)+ASSETCHAINS_PRICES.size()) * sizeof(uint32_t)); flags |= 4; // very rarely we can see flags == 6 case } - size += sizeof(cryptoprices); + size += (sizeof(Cryptos)/sizeof(*Cryptos)+ASSETCHAINS_PRICES.size()) * sizeof(uint32_t); } if ( flags != 0 ) { @@ -2205,10 +2203,35 @@ if ( komodo_nextheight() > 333 ) // for debug only! pending = 0; } +int64_t komodo_pricemult(int32_t ind) +{ + int32_t i,j; + if ( (ASSETCHAINS_CBOPRET & 1) != 0 && ind < KOMODO_MAXPRICES ) + { + if ( PriceMult[0] == 0 ) + { + for (i=0; i<4; i++) + PriceMult[i] = 10000; + if ( (ASSETCHAINS_CBOPRET & 2) != 0 ) + { + for (j=0; j= KOMODO_MAXPRICES ) return(-1); - if ( ind < 36 ) - mult = 10000; - else mult = 1; + mult = PriceMult[ind]; memset(nonzprices,0,sizeof(*nonzprices)*daywindow); for (iter=0; iter outVals); void komodo_args(char *argv0) { extern const char *Notaries_elected1[][2]; - std::string name,addn,hexstr; char *dirname,fname[512],arg0str[64],magicstr[9]; uint8_t magic[4],extrabuf[8192],disablebits[32],*extraptr=0; FILE *fp; uint64_t val; uint16_t port; int32_t i,nonz=0,baseid,len,n,extralen = 0; uint64_t ccenables[256]; + std::string name,addn,hexstr,symbol; char *dirname,fname[512],arg0str[64],magicstr[9]; uint8_t magic[4],extrabuf[8192],disablebits[32],*extraptr=0; FILE *fp; uint64_t val; uint16_t port; int32_t i,nonz=0,baseid,len,n,extralen = 0; uint64_t ccenables[256]; IS_KOMODO_NOTARY = GetBoolArg("-notary", false); IS_STAKED_NOTARY = GetArg("-stakednotary", -1); if ( IS_STAKED_NOTARY != -1 && IS_KOMODO_NOTARY == true ) { @@ -1735,7 +1735,7 @@ void komodo_args(char *argv0) { printf("KOMODO_REWIND %d\n",KOMODO_REWIND); } - if ( name.c_str()[0] != 0 ) + if ( name.c_str()[0] != 0 ) { std::string selectedAlgo = GetArg("-ac_algo", std::string(ASSETCHAINS_ALGORITHMS[0])); @@ -2075,6 +2075,15 @@ void komodo_args(char *argv0) if ( ASSETCHAINS_CBOPRET != 0 ) { extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_CBOPRET),(void *)&ASSETCHAINS_CBOPRET); + if ( ASSETCHAINS_PRICES.size() != 0 ) + { + for (i=0; i Date: Wed, 3 Apr 2019 06:07:13 -1100 Subject: [PATCH 119/154] Move symbols --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index b70574677..8c976ebe1 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1556,8 +1556,8 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) -const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "DASH", "XEM", "ZEC", "WAVES", "RVN", "LSK", "DCR", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" }; // must be on binance (for now) - +const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "ZEC", "WAVES", "LSK", "DCR" }; // must be on binance (for now) +//"RVN", "DASH", "XEM", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" const char *Forex[] = { "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" }; // must be in ECB list From ddee092afbe481b075c4c5107eaad02d06822aa7 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 06:08:56 -1100 Subject: [PATCH 120/154] Fix --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 8c976ebe1..a81a6c6f3 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2030,8 +2030,8 @@ int32_t get_cryptoprices(uint32_t *prices,const char *list[],int32_t n,std::vect for (i=0; i Date: Wed, 3 Apr 2019 06:10:27 -1100 Subject: [PATCH 121/154] "RVN", "DASH", "XEM", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" --- src/rpc/blockchain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 7e7a44f7c..5f1d671e4 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1180,7 +1180,7 @@ int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices,int64_t *corr int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); -int64_t komodo_pricesmult(int32_t ind); +int64_t komodo_pricemult(int32_t ind); UniValue prices(const UniValue& params, bool fHelp) { @@ -1252,7 +1252,7 @@ UniValue prices(const UniValue& params, bool fHelp) offset = j*width + i; smoothed = komodo_pricesmoothed(&correlated[i],daywindow,correlated2,smoothwidth); UniValue parr(UniValue::VARR); - parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricesmult(j))); + parr.push_back(ValueFromAmount((int64_t)prices[offset] * komodo_pricemult(j))); parr.push_back(ValueFromAmount(correlated[i])); parr.push_back(ValueFromAmount(smoothed)); p.push_back(parr); From 02938d0a127484a3e2b979fd09c8a7d57898242c Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 06:13:37 -1100 Subject: [PATCH 122/154] Test --- src/komodo_utils.h | 2 +- src/util.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_utils.h b/src/komodo_utils.h index c807eff48..2d0fe4f4e 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1665,7 +1665,7 @@ uint64_t komodo_ac_block_subsidy(int nHeight) extern int64_t MAX_MONEY; void komodo_cbopretupdate(int32_t forceflag); -void SplitStr(const std::string& strVal, std::vector outVals); +void SplitStr(const std::string& strVal, std::vector &outVals); void komodo_args(char *argv0) { diff --git a/src/util.cpp b/src/util.cpp index 4265ff759..b8733ffca 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -394,7 +394,7 @@ void ParseParameters(int argc, const char* const argv[]) } } -void SplitStr(const std::string& strVal, std::vector outVals) +void SplitStr(const std::string& strVal, std::vector &outVals) { stringstream ss(strVal); std::string str; From cd16c88711502ce727863730fb39b209e363a62a Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 06:22:53 -1100 Subject: [PATCH 123/154] Fix --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index a81a6c6f3..28ed3ee3a 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1556,8 +1556,8 @@ extern std::vector Mineropret; // opreturn data set by the data gatheri #define issue_curl(cmdstr) bitcoind_RPC(0,(char *)"CBCOINBASE",cmdstr,0,0,0) -const char *Cryptos[] = { "KMD", "ETH", "LTC", "BCHABC", "XMR", "IOTA", "ZEC", "WAVES", "LSK", "DCR" }; // must be on binance (for now) -//"RVN", "DASH", "XEM", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" +const char *Cryptos[] = { "KMD", "ETH" }; // must be on binance (for now) +// "LTC", "BCHABC", "XMR", "IOTA", "ZEC", "WAVES", "LSK", "DCR", "RVN", "DASH", "XEM", "BTS", "ICX", "HOT", "STEEM", "ENJ", "STRAT" const char *Forex[] = { "BGN","NZD","ILS","RUB","CAD","PHP","CHF","AUD","JPY","TRY","HKD","MYR","HRK","CZK","IDR","DKK","NOK","HUF","GBP","MXN","THB","ISK","ZAR","BRL","SGD","PLN","INR","KRW","RON","CNY","SEK","EUR" }; // must be in ECB list @@ -2027,7 +2027,7 @@ uint32_t get_binanceprice(const char *symbol) int32_t get_cryptoprices(uint32_t *prices,const char *list[],int32_t n,std::vector strvec) { int32_t i,errs=0; uint32_t price; char *symbol; - for (i=0; i Date: Wed, 3 Apr 2019 06:37:57 -1100 Subject: [PATCH 124/154] Remove , --- src/util.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/util.cpp b/src/util.cpp index b8733ffca..980d82ac9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -404,6 +404,10 @@ void SplitStr(const std::string& strVal, std::vector &outVals) while ( ss >> str ) { + if ( str.size() == 0 ) + continue; + if ( str[str.size()-1] == ',' ) + str.resize(str.size()-1); outVals.push_back(str); while ( ss.peek() == ' ' ) ss.ignore(); From 9b565ea675e39a68214e51ee64cd992d38f81305 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 07:00:43 -1100 Subject: [PATCH 125/154] Handle early block prices --- src/rpc/blockchain.cpp | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 5f1d671e4..ce27238a9 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1188,7 +1188,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,num,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1225,6 +1225,7 @@ UniValue prices(const UniValue& params, bool fHelp) } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } } + num = i; ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); UniValue timestamps(UniValue::VARR); for (i=0; i= width ) { - offset = j*width + i; - rngval = (rngval*11109 + 13849); - if ( (correlated[i]= komodo_pricecorrelated(rngval,j,&prices[offset],daywindow,prices2,smoothwidth)) < 0 ) - throw JSONRPCError(RPC_INVALID_PARAMETER, "null correlated price"); + for (i=0; i Date: Wed, 3 Apr 2019 07:01:10 -1100 Subject: [PATCH 126/154] Parr -> p --- src/rpc/blockchain.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index ce27238a9..61ea71476 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1266,9 +1266,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Wed, 3 Apr 2019 07:02:44 -1100 Subject: [PATCH 127/154] numsamples --- src/rpc/blockchain.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 61ea71476..18927ce8b 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1188,7 +1188,7 @@ UniValue prices(const UniValue& params, bool fHelp) if ( fHelp || params.size() != 1 ) throw runtime_error("prices maxsamples\n"); LOCK(cs_main); - UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,num,nextheight,offset,ht,num=0,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + UniValue ret(UniValue::VOBJ); uint64_t seed,rngval; int64_t smoothed,*correlated,*correlated2; char name[64],*str; uint32_t rawprices[1440*6],*prices,*prices2; uint32_t i,width,j,numpricefeeds=-1,n,numsamples,nextheight,offset,ht,daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; if ( ASSETCHAINS_CBOPRET == 0 ) throw JSONRPCError(RPC_INVALID_PARAMETER, "only -ac_cbopret chains have prices"); @@ -1225,7 +1225,7 @@ UniValue prices(const UniValue& params, bool fHelp) } else throw JSONRPCError(RPC_INVALID_PARAMETER, "no komodo_rawprices found"); } } - num = i; + numsamples = i; ret.push_back(Pair("firstheight", (int64_t)nextheight-1-i)); UniValue timestamps(UniValue::VARR); for (i=0; i= width ) + if ( numsamples >= width ) { - for (i=0; i Date: Wed, 3 Apr 2019 07:07:25 -1100 Subject: [PATCH 128/154] Test --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 18927ce8b..e7893a458 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1266,7 +1266,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Wed, 3 Apr 2019 07:10:39 -1100 Subject: [PATCH 129/154] -) --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index e7893a458..fe4d71bf4 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1266,7 +1266,7 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Wed, 3 Apr 2019 07:13:40 -1100 Subject: [PATCH 130/154] Nested --- src/rpc/blockchain.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index fe4d71bf4..11c8067f7 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1266,7 +1266,9 @@ UniValue prices(const UniValue& params, bool fHelp) for (i=0; i Date: Wed, 3 Apr 2019 07:16:02 -1100 Subject: [PATCH 131/154] Ht limit --- src/rpc/blockchain.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 11c8067f7..0b0f0030c 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1207,7 +1207,8 @@ UniValue prices(const UniValue& params, bool fHelp) correlated = (int64_t *)calloc(sizeof(*correlated),width); correlated2 = (int64_t *)calloc(sizeof(*correlated2),width); prices2 = (uint32_t *)calloc(sizeof(*prices2),width); - for (ht=nextheight-1,i=0; i2*daywindow+2+smoothwidth; i++,ht--) + i = 0; + for (ht=nextheight-1,i=0; i2; i++,ht--) { if ( ht < 0 || ht > chainActive.Height() ) throw JSONRPCError(RPC_INVALID_PARAMETER, "Block height out of range"); From bf4a8750f3a18c2cdf814ac6c89e28b585d3c32b Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 20:44:46 -1100 Subject: [PATCH 132/154] Change correlation factor to 4 --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6862fcf14..7076f7808 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2266,8 +2266,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (iter + seed) % daywindow; refprice = rawprices[i]; - highprice = (refprice * (COIN + PRICES_MAXCHANGE*3)) / COIN; - lowprice = (refprice * (COIN - PRICES_MAXCHANGE*3)) / COIN; + highprice = (refprice * (COIN + PRICES_MAXCHANGE*4)) / COIN; + lowprice = (refprice * (COIN - PRICES_MAXCHANGE*4)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) From 9561e30a283b42def4621ad3e3c94fab8cc52cd4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 3 Apr 2019 20:48:38 -1100 Subject: [PATCH 133/154] 5% --- src/komodo_gateway.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 7076f7808..e42b8aa4b 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2266,8 +2266,8 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int correlation = 0; i = (iter + seed) % daywindow; refprice = rawprices[i]; - highprice = (refprice * (COIN + PRICES_MAXCHANGE*4)) / COIN; - lowprice = (refprice * (COIN - PRICES_MAXCHANGE*4)) / COIN; + highprice = (refprice * (COIN + PRICES_MAXCHANGE*5)) / COIN; + lowprice = (refprice * (COIN - PRICES_MAXCHANGE*5)) / COIN; if ( highprice == refprice ) highprice++; if ( lowprice == refprice ) From 3155223e5176b534eea98b4448fb81954a453de3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 21:17:53 -1100 Subject: [PATCH 134/154] Compiler errors --- src/cc/CCPrices.h | 22 +- src/cc/payments.cpp | 72 ++++ src/cc/prices.cpp | 835 +++++++++++++++++++++++++---------------- src/komodo_defs.h | 1 + src/komodo_gateway.h | 117 +++--- src/rpc/blockchain.cpp | 69 +++- src/txdb.cpp | 109 +++++- 7 files changed, 800 insertions(+), 425 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 1a68be7dd..9f3039a6b 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -18,17 +18,27 @@ #define CC_PRICES_H #include "CCinclude.h" +int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind,int32_t daywindow); + +#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) +#define PRICES_TXFEE 10000 +#define PRICES_SMOOTHWIDTH 1 +#define KOMODO_MAXPRICES 2048 // must be power of 2 and less than 8192 +#define KOMODO_PRICEMASK (~(KOMODO_MAXPRICES - 1)) +#define PRICES_WEIGHT (KOMODO_MAXPRICES * 1) +#define PRICES_MUL (KOMODO_MAXPRICES * 2) +#define PRICES_DIV (KOMODO_MAXPRICES * 3) +#define PRICES_INV (KOMODO_MAXPRICES * 4) +#define PRICES_MDD (KOMODO_MAXPRICES * 5) +#define PRICES_MMD (KOMODO_MAXPRICES * 6) +#define PRICES_MMM (KOMODO_MAXPRICES * 7) +#define PRICES_DDD (KOMODO_MAXPRICES * 8) bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); // CCcustom UniValue PricesList(); -UniValue PricesInfo(uint256 fundingtxid); -UniValue PricesStatus(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid); -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); -std::string PricesAddFunding(uint64_t txfee,uint256 bettoken,uint256 fundingtxid,int64_t amount); -std::string PricesBet(uint64_t txfee,uint256 bettoken,uint256 fundingtxid,int64_t amount,int32_t leverage); -std::string PricesFinish(uint64_t txfee,uint256 bettoken,uint256 fundingtxid,uint256 bettxid); +UniValue PricesBet(uint64_t txfee,int64_t amount,int32_t leverage,std::string synthetic); #endif diff --git a/src/cc/payments.cpp b/src/cc/payments.cpp index e0c4c8ada..ae83e2661 100644 --- a/src/cc/payments.cpp +++ b/src/cc/payments.cpp @@ -732,6 +732,78 @@ UniValue PaymentsCreate(struct CCcontract_info *cp,char *jsonstr) return(result); } +UniValue PaymentsAirdrop(struct CCcontract_info *cp,char *jsonstr) +{ + // need to code: exclude list of tokenid, dust threshold, maxpayees, excluded pubkeys[] + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + UniValue result(UniValue::VOBJ); CTransaction tx; CPubKey Paymentspk,mypk; char markeraddr[64]; std::vector txidoprets; uint256 hashBlock; int32_t i,n,numoprets=0,lockedblocks,minrelease; std::string rawtx; int64_t totalallocations = 0; + cJSON *params = payments_reparse(&n,jsonstr); + if ( params != 0 && n >= 4 ) + { + lockedblocks = juint(jitem(params,0),0); + minrelease = juint(jitem(params,1),0); + if ( lockedblocks < 0 || minrelease < 0 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","negative parameter")); + if ( params != 0 ) + free_json(params); + return(result); + } + for (i=0; i scriptPubKey,opret; int64_t allocation; + if ( myGetTransaction(txidoprets[i],tx,hashBlock) != 0 && tx.vout.size() > 1 && DecodePaymentsTxidOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,allocation,scriptPubKey,opret) == 'T' ) + { + totalallocations += allocation; + if ( opret.size() > 0 ) + numoprets++; + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid txidopret")); + result.push_back(Pair("txid",txidoprets[i].GetHex())); + result.push_back(Pair("txi",(int64_t)i)); + if ( params != 0 ) + free_json(params); + return(result); + } + } + if ( numoprets > 1 ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","too many opreturns")); + result.push_back(Pair("numoprets",(int64_t)numoprets)); + if ( params != 0 ) + free_json(params); + return(result); + } + mypk = pubkey2pk(Mypubkey()); + Paymentspk = GetUnspendable(cp,0); + if ( AddNormalinputs(mtx,mypk,2*PAYMENTS_TXFEE,60) > 0 ) + { + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,PAYMENTS_TXFEE,Paymentspk,Paymentspk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,PAYMENTS_TXFEE,EncodePaymentsOpRet(lockedblocks,minrelease,totalallocations,txidoprets)); + if ( params != 0 ) + free_json(params); + return(payments_rawtxresult(result,rawtx,1)); + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough normal funds")); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","parameters error")); + } + if ( params != 0 ) + free_json(params); + return(result); +} + UniValue PaymentsInfo(struct CCcontract_info *cp,char *jsonstr) { UniValue result(UniValue::VOBJ),a(UniValue::VARR); CTransaction tx,txO; CPubKey Paymentspk,txidpk; int32_t i,j,n,flag=0,numoprets=0,lockedblocks,minrelease; std::vector txidoprets; int64_t funds,fundsopret,totalallocations=0,allocation; char fundsaddr[64],fundsopretaddr[64],txidaddr[64],*outstr; uint256 createtxid,hashBlock; diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 1ab5aa1cb..eab975acd 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -17,123 +17,115 @@ #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. +CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; - 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. + 0.5% fee based on betamount, NOT leveraged betamount!! + 0.1% collected by price basis determinant + 0.2% collected by rekt tx - 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! + PricesBet -> +/-leverage, amount, synthetic -> opreturn includes current price + funds are locked into 1of2 global CC address + for first day, long basis is MAX(correlated,smoothed), short is MIN() + reference price is the smoothed of the previous block + if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price + original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund + 0.5% of bet -> globalfund + + PricesStatus -> bettxid maxsamples returns initial params, cost basis, amount left, rekt:true/false, rektheight, initial synthetic price, current synthetic price, net gain - Funds work like with dice, ie. there is a Prices plan that traders bet against. + PricesRekt -> bettxid height -> 0.1% to miner, rest to global CC - PricesFunding oracletxid, margin, priceaveraging, maxleverage, funding, longtoken, shorttoken, N [pubkeys] + PricesClose -> bettxid returns (synthetic value + amount) - 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 + PricesList -> all bettxid -> list [bettxid, netgain] - 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) -{ - 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); -} - -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) +CScript prices_betopret(CPubKey mypk,int32_t height,int64_t amount,int16_t leverage,int64_t firstprice,std::vector vec,uint256 tokenid) { CScript opret; - fprintf(stderr,"implement EncodePricesFundingOpRet\n"); + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'B' << mypk << height << amount << leverage << firstprice << vec << tokenid); return(opret); } -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) +uint8_t prices_betopretdecode(CScript scriptPubKey,CPubKey &pk,int32_t &height,int64_t &amount,int16_t &leverage,int64_t &firstprice,std::vector &vec,uint256 &tokenid) { - fprintf(stderr,"implement DecodePricesFundingOpRet\n"); + std::vector vopret; uint8_t e,f; + GetOpReturnData(scriptPubKey,vopret); + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> pk; ss >> height; ss >> amount; ss >> leverage; ss >> firstprice; ss >> vec; ss >> tokenid) != 0 && e == EVAL_PRICES && f == 'B' ) + { + return(f); + } + return(0); +} + +CScript prices_addopret(uint256 bettxid,CPubKey mypk,int64_t amount) +{ + CScript opret; + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'A' << bettxid << mypk << amount); + return(opret); +} + +uint8_t prices_addopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int64_t &amount) +{ + std::vector vopret; uint8_t e,f; + GetOpReturnData(scriptPubKey,vopret); + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> amount) != 0 && e == EVAL_PRICES && f == 'A' ) + { + return(f); + } + return(0); +} + +CScript prices_costbasisopret(uint256 bettxid,CPubKey mypk,int32_t height,int64_t costbasis) +{ + CScript opret; + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'C' << bettxid << mypk << height << costbasis); + return(opret); +} + +uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int32_t &height,int64_t &costbasis) +{ + std::vector vopret; uint8_t e,f; + GetOpReturnData(scriptPubKey,vopret); + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> pk; ss >> height; ss >> costbasis) != 0 && e == EVAL_PRICES && f == 'C' ) + { + return(f); + } + return(0); +} + +CScript prices_finalopret(uint256 bettxid,int64_t profits,int32_t height,CPubKey mypk,int64_t firstprice,int64_t costbasis,int64_t addedbets,int64_t positionsize,int16_t leverage) +{ + CScript opret; + opret << OP_RETURN << E_MARSHAL(ss << EVAL_PRICES << 'F' << bettxid << profits << height << mypk << firstprice << costbasis << addedbets << positionsize << leverage); + return(opret); +} + +uint8_t prices_finalopretdecode(CScript scriptPubKey,uint256 &bettxid,int64_t &profits,int32_t &height,CPubKey &pk,int64_t &firstprice,int64_t &costbasis,int64_t &addedbets,int64_t &positionsize,int16_t &leverage) +{ + std::vector vopret; uint8_t e,f; + GetOpReturnData(scriptPubKey,vopret); + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> bettxid; ss >> profits; ss >> height; ss >> pk; ss >> firstprice; ss >> costbasis; ss >> addedbets; ss >> positionsize; ss >> leverage) != 0 && e == EVAL_PRICES && f == 'F' ) + { + return(f); + } return(0); } bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn) { - 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 - { - 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); - } - } + return true; } // 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) +int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char *destaddr,int64_t total,int32_t maxinputs,uint256 vintxid,int32_t vinvout) { - // 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); @@ -141,11 +133,11 @@ int64_t AddTokensInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char { txid = it->first.txhash; vout = (int32_t)it->first.index; - // need to prevent dup + if ( vout == vinvout && txid == vintxid ) + continue; 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 ( (nValue= vintx.vout[vout].nValue) >= total/maxinputs && myIsutxo_spentinmempool(ignoretxid,ignorevin,txid,vout) == 0 ) { if ( total != 0 && maxinputs != 0 ) mtx.vin.push_back(CTxIn(txid,vout,CScript())); @@ -162,7 +154,7 @@ int64_t AddTokensInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char 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; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; cp = CCinit(&C,EVAL_PRICES); pricespk = GetUnspendable(cp,0); SetCCtxids(addressIndex,cp->normaladdr); @@ -171,7 +163,7 @@ UniValue PricesList() 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' ) + if ( vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout[vintx.vout.size()-1].scriptPubKey,pk,height,amount,leverage,firstprice,vec) == 'B' ) { result.push_back(uint256_str(str,txid)); } @@ -180,274 +172,455 @@ UniValue PricesList() 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) +UniValue prices_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag) { - 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 ) + CTransaction tx; + if ( rawtx.size() > 0 ) { - 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(""); + if ( broadcastflag != 0 && myAddtomempool(tx) != 0 ) + RelayTransaction(tx); + result.push_back(Pair("txid",tx.GetHash().ToString())); + result.push_back(Pair("result","success")); + } else result.push_back(Pair("error","decode hex")); + } else result.push_back(Pair("error","couldnt finalize CCtx")); + return(result); } -UniValue PricesInfo(uint256 fundingtxid) +int32_t prices_syntheticvec(std::vector &vec,std::vector synthetic) { - 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 ) + int32_t i,need,depth = 0; std::string opstr; uint16_t opcode,weight; + if ( synthetic.size() == 0 ) + return(-1); + for (i=0; i= 0 ) + opcode = ind, need = 0; + else if ( (weight= atoi(opstr.c_str())) > 0 && weight < KOMODO_MAXPRICES ) + { + opcode = PRICES_WEIGHT | weight; + need = 1; + } else return(-2); + if ( depth < need ) + return(-3); + depth -= need; + if ( (opcode & KOMODO_PRICEMASK) != PRICES_WEIGHT ) // weight + depth++; + if ( depth > 3 ) + return(-4); + vec.push_back(opcode); + } + if ( depth != 0 ) + { + fprintf(stderr,"depth.%d not empty\n",depth); + return(-5); + } + return(0); +} + +int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t minmax,int16_t leverage) +{ + int32_t i,errcode,depth,retval = -1; uint16_t opcode; int64_t *pricedata,stack[4],price,den,a,b,c; + pricedata = (int64_t *)calloc(sizeof(*pricedata)*3,numblocks + PRICES_DAYWINDOW*2 + PRICES_SMOOTHWIDTH); + price = den = depth = errcode = 0; + for (i=0; i 0 ) + pricestack[depth] = MAX(pricedata[1],pricedata[2]); + else pricestack[depth] = MIN(pricedata[1],pricedata[2]); + } + } + if ( pricestack[depth] == 0 ) + errcode = -1; + depth++; + break; + case PRICES_WEIGHT: // multiply by weight and consume top of stack by updating price + if ( depth == 1 ) + { + depth--; + price += pricestack[0] * ind; + den += ind; + } else errcode = -2; + break; + case PRICES_MUL: + if ( depth >= 2 ) + { + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = (a * b) / SATOSHIDEN; + } else errcode = -3; + break; + case PRICES_DIV: + if ( depth >= 2 ) + { + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = (a * SATOSHIDEN) / b; + } else errcode = -4; + break; + case PRICES_INV: + if ( depth >= 1 ) + { + a = pricestack[--depth]; + pricestack[depth++] = (SATOSHIDEN * SATOSHIDEN) / a; + } else errcode = -5; + break; + case PRICES_MDD: + if ( depth >= 3 ) + { + c = pricestack[--depth]; + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = (((a * SATOSHIDEN) / b) * SATOSHIDEN) / c; + } else errcode = -6; + break; + case PRICES_MMD: + if ( depth >= 3 ) + { + c = pricestack[--depth]; + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = (a * b) / c; + } else errcode = -7; + break; + case PRICES_MMM: + if ( depth >= 3 ) + { + c = pricestack[--depth]; + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = ((a * b) / SATOSHIDEN) * c; + } else errcode = -8; + break; + case PRICES_DDD: + if ( depth >= 3 ) + { + c = pricestack[--depth]; + b = pricestack[--depth]; + a = pricestack[--depth]; + pricestack[depth++] = (((((SATOSHIDEN * SATOSHIDEN) / a) * SATOSHIDEN) / b) * SATOSHIDEN) / c; + } else errcode = -9; + break; + default: + errcode = -10; + break; + } + if ( errcode != 0 ) + break; + } + free(pricedata); + if ( den == 0 ) + return(-11); + else if ( depth != 0 ) + return(-12); + else if ( errcode != 0 ) + return(errcode); + return(price / den); +} + +int64_t prices_syntheticprofits(int64_t &costbasis,int32_t firstheight,int32_t height,int16_t leverage,std::vector vec,int64_t positionsize,int64_t addedbets) +{ + int64_t price,profits = 0; int32_t minmax; + minmax = (height > firstheight+PRICES_DAYWINDOW); + if ( (price= prices_syntheticprice(vec,height,minmax)) < 0 ) + { + fprintf(stderr,"unexpected zero synthetic price at height.%d\n",height); + return(0); + } + if ( minmax != 0 ) + { + if ( leverage > 0 && price > costbasis ) + costbasis = price; + else if ( leverage < 0 && (costbasis == 0 || price < costbasis) ) + costbasis = price; + } + profits = ((price * SATOSHIDEN) / costbasis) - SATOSHIDEN; + profits *= leverage * positionsize; + return(positionsize + addedbets + profits) +} + +void prices_betjson(UniValue &result,int64_t profits,int64_t costbasis,int64_t positionsize,int64_t addedbets,int16_t leverage,int32_t firstheight,int64_t firstprice) +{ + result.push_back(Pair("profits",ValueFromAmount(profits))); + result.push_back(Pair("costbasis",ValueFromAmount(costbasis))); + result.push_back(Pair("positionsize",ValueFromAmount(positionsize))); + result.push_back(Pair("addedbets",ValueFromAmount(addedbets))); + result.push_back(Pair("leverage",(int64_t)leverage)); + result.push_back(Pair("firstheight",(int64_t)firstheight)); + result.push_back(Pair("firstprice",ValueFromAmount(firstprice))); +} + +int64_t prices_costbasis(CTransaction bettx) +{ + int64_t costbasis = 0; + // if vout1 is spent, follow and extract costbasis from opreturn + //uint8_t prices_costbasisopretdecode(CScript scriptPubKey,uint256 &bettxid,CPubKey &pk,int32_t &height,int64_t &costbasis) + + return(costbasis); +} + +int64_t prices_batontxid(uint256 &batontxid,CTransaction bettx,uint256 bettxid); +{ + int64_t addedbets = 0; + // iterate through batons, adding up vout1 -> addedbets + return(addedbets); +} + +UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector synthetic) +{ + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t betamount,firstprice; std::vector vec; + if ( leverage > PRICES_MAXLEVERAGE || leverage < -PRICES_MAXLEVERAGE ) + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","leverage too big")); 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; + txfee = PRICES_TXFEE; mypk = pubkey2pk(Mypubkey()); pricespk = GetUnspendable(cp,0); GetCCaddress(cp,myaddr,mypk); - if ( GetTransaction(fundingtxid,tx,hashBlock,false) == 0 ) + if ( prices_syntheticvec(vec,synthetic) < 0 || (firstprice= prices_syntheticprice(vec,nextheight-1,1)) < 0 || vec.size() == 0 || vec.size() > 4096 ) { - fprintf(stderr,"cant find fundingtxid\n"); - return(""); + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid synthetic")); + return(result); } - 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 ( AddNormalinputs(mtx,mypk,amount+4*txfee,64) >= amount+4*txfee ) { - 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 + betamount = (amount * 199) / 200; + mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,mypk)); // baton for total funding + mtx.vout.push_back(MakeCC1vout(cp->evalcode,(amount-betamount)+2*txfee,pricespk)); + mtx.vout.push_back(MakeCC1of2vout(cp->evalcode,betamount,pricespk,mypk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_betopret(mypk,nextheight-1,amount,leverage,firstprice,vec,zeroid)); + return(prices_rawtxresult(result,rawtx,0)); + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough funds")); + return(result); +} - 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()))))); +UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) +{ + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batonttxid; + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = PRICES_TXFEE; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + GetCCaddress(cp,myaddr,mypk); + if ( AddNormalinputs(mtx,mypk,amount+txfee,64) >= amount+txfee ) + { + if ( prices_batontxid(addedbets,batontxid,bettxid) >= 0 ) + { + mtx.vin.push_back(CTxVin(batontxid,0,CScript())); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,mypk)); // baton for total funding + mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,pricespk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_addopret(bettxid,mypk,amount)); + return(prices_rawtxresult(result,rawtx,0)); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","couldnt find batonttxid")); + return(result); + } + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","not enough funds")); + return(result); +} + +UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) +{ + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock; int64_t myfee,positionsize=0,addedbets,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = PRICES_TXFEE; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) + { + if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec) == 'B' ) + { + addedbets = prices_addedbets(bettx); + mtx.vin.push_back(CTxVin(bettx,1,CScript())); + for (i=0; ievalcode,bettx.vout[1].nValue-myfee-txfee,pricespk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_costbasisopret(bettxid,mypk,firstheight+PRICES_DAYWINDOW-1,costbasis)); + return(prices_rawtxresult(result,rawtx,0)); + } + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant find bettxid")); + return(result); +} + +UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight) +{ + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t firstheight,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = PRICES_TXFEE; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) + { + if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) + { + costbasis = prices_costbasis(bettx); + addedbets = prices_batontxid(batontxid,bettx,bettxid); + if ( (profits= prices_syntheticprofits(&ignore,firstheight,rektheight,leverage,vec,positionsize,addedbets)) < 0 ) + { + myfee = (positionsize + addedbets) / 500; + } + prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); + if ( myfee != 0 ) + { + mtx.vin.push_back(CTxVin(bettxid,2,CScript())); + mtx.vout.push_back(CTxOut(myfee,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,bettx.vout[2].nValue-myfee-txfee,pricespk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_finalopret(bettxid,profits,rektheight,mypk,firstprice,costbasis,addedbets,positionsize,leverage)); + return(prices_rawtxresult(result,rawtx,0)); } else { - CCerror = "cant find enough bet inputs"; - fprintf(stderr,"%s\n", CCerror.c_str() ); + result.push_back(Pair("result","error")); + result.push_back(Pair("error","position not rekt")); + return(result); } } else { - CCerror = "cant find enough inputs"; - fprintf(stderr,"%s\n", CCerror.c_str() ); + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant decode opret")); + return(result); } } - 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? + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant find bettxid")); return(result); } -std::string PricesFinish(uint64_t txfee,uint256 refbettoken,uint256 fundingtxid,uint256 bettxid) +UniValue PricesCashout(uint64_t txfee,uint256 bettxid) { - return(""); + int32_t nextheight = komodo_nextheight(); + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); + struct CCcontract_info *cp,C; char destaddr[64]; CTransaction bettx; uint256 hashBlock,tokenid; int64_t CCchange=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t i,firstheight,height,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + cp = CCinit(&C,EVAL_PRICES); + if ( txfee == 0 ) + txfee = PRICES_TXFEE; + mypk = pubkey2pk(Mypubkey()); + pricespk = GetUnspendable(cp,0); + GetCCaddress(cp,destaddr,pricespk); + if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) + { + if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) + { + costbasis = prices_costbasis(bettx); + addedbets = prices_batontxid(batontxid,bettx,bettxid); + if ( (profits= prices_syntheticprofits(&ignore,firstheight,nextheight-1,leverage,vec,positionsize,addedbets)) < 0 ) + { + prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); + result.push_back(Pair("result","error")); + result.push_back(Pair("error","position rekt")); + return(result); + } + prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); + mtx.vin.push_back(CTxVin(bettxid,2,CScript())); + if ( (inputsum= AddPricesInputs(cp,mtx,destaddr,profits+txfee,64,bettxid,2)) > profits+txfee ) + CCchange = (inputsum - profits); + mtx.vout.push_back(CTxOut(bettx.vout[2].nValue + profits,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); + if ( CCchange >= txfee ) + mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,pricespk)); + rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_finalopret(bettxid,profits,nextheight-1,mypk,firstprice,costbasis,addedbets,positionsize,leverage)); + return(prices_rawtxresult(result,rawtx,0)); + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant decode opret")); + return(result); + } + } + return(result); +} + +UniValue PricesInfo(uint256 bettxid,int32_t height) +{ + UniValue result(UniValue::VOBJ); uint256 hashBlock,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) + { + if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) + { + costbasis = prices_costbasis(bettx); + addedbets = prices_batontxid(batontxid,bettx,bettxid); + if ( (profits= prices_syntheticprofits(&ignore,firstheight,firstheight+i,leverage,vec,positionsize,addedbets)) < 0 ) + { + result.push_back(Pair("rekt",1)); + result.push_back(Pair("rektfee",(positionsize + addedbets) / 500)); + } else result.push_back(Pair("rekt",0)); + result.push_back(Pair("batontxid",batontxid.GetHex())); + prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); + return(result); + } + } + result.push_back(Pair("result","error")); + result.push_back(Pair("error","cant find bettxid")); + return(result); } diff --git a/src/komodo_defs.h b/src/komodo_defs.h index c1eeb375b..285bbb63d 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -37,6 +37,7 @@ #define KOMODO_MAXNVALUE (((uint64_t)1 << 63) - 1) #define KOMODO_BIT63SET(x) ((x) & ((uint64_t)1 << 63)) #define KOMODO_VALUETOOBIG(x) ((x) > (uint64_t)10000000001*COIN) +#define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) extern uint8_t ASSETCHAINS_TXPOW,ASSETCHAINS_PUBLIC; int32_t MAX_BLOCK_SIZE(int32_t height); diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 28ed3ee3a..6ea86eb9d 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2182,6 +2182,9 @@ void komodo_cbopretupdate(int32_t forceflag) if ( (flags & 4) != 0 ) lastcrypto = now; memcpy(Mineropret.data(),PriceCache[0],size); + // high volatility still strands nodes so we need to check new prices to approve a stuck block + // scan list of stuck blocks (one?) and auto reconsiderblock if it changed state + //int32_t i; for (i=0; i= KOMODO_MAXPRICES ) + if ( PRICES_DAYWINDOW < 2 || ind >= KOMODO_MAXPRICES ) return(-1); mult = PriceMult[ind]; - memset(nonzprices,0,sizeof(*nonzprices)*daywindow); - for (iter=0; iter= daywindow ) + if ( i >= PRICES_DAYWINDOW ) i = 0; - if ( (price= rawprices[i]) == 0 ) + if ( (price= rawprices[i*rawskip]) == 0 ) { fprintf(stderr,"null rawprice.[%d]\n",i); return(-1); @@ -2315,28 +2319,29 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int //fprintf(stderr,"%.1f ",(double)price/10000); sum += price; correlation++; - if ( correlation > (daywindow>>1) ) + if ( correlation > (PRICES_DAYWINDOW>>1) ) { - return(refprice * mult); + if ( nonzprices == 0 ) + return(refprice * mult); //fprintf(stderr,"-> %.4f\n",(double)sum*mult/correlation); //return(sum*mult/correlation); n = 0; - i = (iter + seed) % daywindow; - for (k=0; k= daywindow ) + if ( i >= PRICES_DAYWINDOW ) i = 0; - if ( n > (daywindow>>1) ) + if ( n > (PRICES_DAYWINDOW>>1) ) nonzprices[i] = 0; else { - price = rawprices[i]; + price = rawprices[i*rawskip]; if ( price < lowprice || price > highprice ) nonzprices[i] = 0; else { nonzprices[i] = price; - //fprintf(stderr,"(%d %u) ",i,rawprices[i]); + //fprintf(stderr,"(%d %u) ",i,rawprices[i*rawskip]); n++; } } @@ -2345,17 +2350,17 @@ int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int if ( n != correlation ) return(-1); sum = den = n = 0; - for (i=0; i %.4f\n",(double)(smoothedsum/smoothedden)/10000); - return(smoothedsum/smoothedden); + if ( (price= correlated[i*cskip]) != 0 ) + nonzprice = price; + //correlated2[i] = nonzprice / PRICES_DAYWINDOW; // reduce precision + sum += nonzprice; } - return(0); + price = sum / PRICES_DAYWINDOW; + // improve smoothing with correlated2 processing + // price = smooth(correlated2,PRICES_DAYWINDOW,price/daywindow) * PRICES_DAYWINDOW; + return(price); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 0b0f0030c..c5dbd8601 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,19 +1176,58 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t numprices,int64_t *correlated2,int32_t smoothwidth); -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int32_t int64_t *correlated2,int32_t numprices); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t skip,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); int64_t komodo_pricemult(int32_t ind); +#define PRICES_SMOOTHWIDTH 1 + +int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) +{ + int32_t height,i,width,numpricefeeds = -1; uint64_t seed,rngval,*correlated2; uint32_t rawprices[1440*6],*ptr; + //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; + //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); + width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; + komodo_heightpricebits(&seed,rawprices,firstheight + numblocks - 1); + if ( firstheight < width ) + return(-1); + for (i=0; i2; i++,ht--) { @@ -1233,7 +1272,7 @@ UniValue prices(const UniValue& params, bool fHelp) timestamps.push_back((int64_t)prices[i]); ret.push_back(Pair("timestamps",timestamps)); rngval = seed; - //for (i=0; i= width ) { - for (i=0; i ignoredMap = { + {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1}, + {"RMUF3UDmzWFLSKV82iFbMaqzJpUnrWjcT4", 1}, + {"RA5imhVyJa7yHhggmBytWuDr923j2P1bxx", 1}, + {"RBM5LofZFodMeewUzoMWcxedm3L3hYRaWg", 1}, + {"RAdcko2d94TQUcJhtFHZZjMyWBKEVfgn4J", 1}, + {"RLzUaZ934k2EFCsAiVjrJqM8uU1vmMRFzk", 1}, + {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, + {"RUDrX1v5toCsJMUgtvBmScKjwCB5NaR8py", 1}, + {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, + {"RRvwmbkxR5YRzPGL5kMFHMe1AH33MeD8rN", 1}, + {"RQLQvSgpPAJNPgnpc8MrYsbBhep95nCS8L", 1}, + {"RK8JtBV78HdvEPvtV5ckeMPSTojZPzHUTe", 1}, + {"RHVs2KaCTGUMNv3cyWiG1jkEvZjigbCnD2", 1}, + {"RE3SVaDgdjkRPYA6TRobbthsfCmxQedVgF", 1}, + {"RW6S5Lw5ZCCvDyq4QV9vVy7jDHfnynr5mn", 1}, + {"RTkJwAYtdXXhVsS3JXBAJPnKaBfMDEswF8", 1}, + {"RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY", 1} //Burnaddress for null privkey +}; + +UniValue CBlockTreeDB::Snapshot2(int64_t dustthreshold,int32_t top,std::vector > &vaddr) +{ + int64_t total = 0; int64_t totalAddresses = 0; std::string address; + int64_t utxos = 0; int64_t ignoredAddresses = 0; + boost::scoped_ptr iter(NewIterator()); + std::map addressAmounts; + for (iter->SeekToLast(); iter->Valid(); iter->Prev()) + { + boost::this_thread::interruption_point(); + try + { + std::vector slKey = std::vector(); + pair keyObj; + iter->GetKey(keyObj); + char chType = keyObj.first; + CAddressIndexIteratorKey indexKey = keyObj.second; + //fprintf(stderr, "chType=%d\n", chType); + if (chType == DB_ADDRESSUNSPENTINDEX) + { + try { + CAmount nValue; + iter->GetValue(nValue); + getAddressFromIndex(indexKey.type, indexKey.hashBytes, address); + if ( nValue > dustthreshold ) + { + std::map ::iterator ignored = ignoredMap.find(address); + if (ignored != ignoredMap.end()) + { + fprintf(stderr,"ignoring %s\n", address.c_str()); + ignoredAddresses++; + continue; + } + std::map ::iterator pos = addressAmounts.find(address); + if ( pos == addressAmounts.end() ) + { + // insert new address + utxo amount + //fprintf(stderr, "inserting new address %s with amount %li\n", address.c_str(), nValue); + addressAmounts[address] = nValue; + totalAddresses++; + } + else + { + // update unspent tally for this address + //fprintf(stderr, "updating address %s with new utxo amount %li\n", address.c_str(), nValue); + addressAmounts[address] += nValue; + } + //fprintf(stderr,"{\"%s\", %.8f},\n",address.c_str(),(double)nValue/COIN); + // total += nValue; + utxos++; + } else fprintf(stderr,"ignoring amount=0 UTXO for %s\n", address.c_str()); + } + catch (const std::exception& e) + { + fprintf(stderr, "DONE %s: LevelDB addressindex exception! - %s\n", __func__, e.what()); + break; + } + } + } + catch (const std::exception& e) + { + fprintf(stderr, "DONE reading index entries\n"); + break; + } + } + fprintf(stderr, "total=%f, totalAddresses=%li, utxos=%li, ignored=%li\n", (double) total / COIN, totalAddresses, utxos, ignoredAddresses); + for (std::pair element : addressAmounts) + vaddr.push_back( make_pair(element.second, element.first) ); + std::sort(vaddr.rbegin(), vaddr.rend()); + int topN = 0; + for (std::vector>::iterator it = vaddr.begin(); it!=vaddr.end(); ++it) + { + //obj.push_back( make_pair("addr", it->second.c_str() ) ); + //char amount[32]; + //sprintf(amount, "%.8f", (double) it->first / COIN); + //obj.push_back( make_pair("amount", amount) ); + //obj.push_back( make_pair("segid",(int32_t)komodo_segid32((char *)it->second.c_str()) & 0x3f) ); + //addressesSorted.push_back(obj); + total += it->first; + topN++; + // If requested, only show top N addresses in output JSON + if ( top == topN ) + break; + } +} + UniValue CBlockTreeDB::Snapshot(int top) { int64_t total = 0; int64_t totalAddresses = 0; std::string address; @@ -448,7 +553,7 @@ UniValue CBlockTreeDB::Snapshot(int top) UniValue result(UniValue::VOBJ); result.push_back(Pair("start_time", (int) time(NULL))); - std::map ignoredMap = { + /* std::map ignoredMap = { {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1}, {"RMUF3UDmzWFLSKV82iFbMaqzJpUnrWjcT4", 1}, {"RA5imhVyJa7yHhggmBytWuDr923j2P1bxx", 1}, @@ -466,7 +571,7 @@ UniValue CBlockTreeDB::Snapshot(int top) {"RW6S5Lw5ZCCvDyq4QV9vVy7jDHfnynr5mn", 1}, {"RTkJwAYtdXXhVsS3JXBAJPnKaBfMDEswF8", 1}, {"RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY", 1} //Burnaddress for null privkey - }; + };*/ int64_t startingHeight = chainActive.Height(); //fprintf(stderr, "Starting snapshot at height %lli\n", startingHeight); From b3d939ef5b84cca8cf61a46bf5aeba7490f08177 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 21:21:56 -1100 Subject: [PATCH 135/154] Test --- src/cc/prices.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index eab975acd..85b7f2077 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -154,7 +154,7 @@ int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char UniValue PricesList() { - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock,tokenid; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; cp = CCinit(&C,EVAL_PRICES); pricespk = GetUnspendable(cp,0); SetCCtxids(addressIndex,cp->normaladdr); @@ -163,7 +163,7 @@ UniValue PricesList() txid = it->first.txhash; if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) { - if ( vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout[vintx.vout.size()-1].scriptPubKey,pk,height,amount,leverage,firstprice,vec) == 'B' ) + if ( vintx.vout.size() > 0 && prices_betopretdecode(vintx.vout[vintx.vout.size()-1].scriptPubKey,pk,height,amount,leverage,firstprice,vec,tokenid) == 'B' ) { result.push_back(uint256_str(str,txid)); } @@ -470,7 +470,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock; int64_t myfee,positionsize=0,addedbets,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee,positionsize=0,addedbets,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -478,7 +478,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) pricespk = GetUnspendable(cp,0); if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) { - if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec) == 'B' ) + if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) { addedbets = prices_addedbets(bettx); mtx.vin.push_back(CTxVin(bettx,1,CScript())); From 9c3418cf8d23dd5bdc763eb9a331cb267c79d470 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 22:50:50 -1100 Subject: [PATCH 136/154] Syntax --- src/cc/CCPrices.h | 2 +- src/cc/prices.cpp | 22 +++++++++++----------- src/komodo_defs.h | 1 + src/komodo_gateway.h | 12 ++++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 9f3039a6b..bb3b74ba6 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -26,7 +26,7 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, #define KOMODO_MAXPRICES 2048 // must be power of 2 and less than 8192 #define KOMODO_PRICEMASK (~(KOMODO_MAXPRICES - 1)) #define PRICES_WEIGHT (KOMODO_MAXPRICES * 1) -#define PRICES_MUL (KOMODO_MAXPRICES * 2) +#define PRICES_MULT (KOMODO_MAXPRICES * 2) #define PRICES_DIV (KOMODO_MAXPRICES * 3) #define PRICES_INV (KOMODO_MAXPRICES * 4) #define PRICES_MDD (KOMODO_MAXPRICES * 5) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 85b7f2077..1cb7b6667 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -154,7 +154,7 @@ int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char UniValue PricesList() { - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock,tokenid; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock,tokenid; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; char str[65]; cp = CCinit(&C,EVAL_PRICES); pricespk = GetUnspendable(cp,0); SetCCtxids(addressIndex,cp->normaladdr); @@ -191,7 +191,7 @@ UniValue prices_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcast int32_t prices_syntheticvec(std::vector &vec,std::vector synthetic) { - int32_t i,need,depth = 0; std::string opstr; uint16_t opcode,weight; + int32_t i,need,ind,depth = 0; std::string opstr; uint16_t opcode,weight; if ( synthetic.size() == 0 ) return(-1); for (i=0; i &vec,std::vector int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t minmax,int16_t leverage) { - int32_t i,errcode,depth,retval = -1; uint16_t opcode; int64_t *pricedata,stack[4],price,den,a,b,c; - pricedata = (int64_t *)calloc(sizeof(*pricedata)*3,numblocks + PRICES_DAYWINDOW*2 + PRICES_SMOOTHWIDTH); + int32_t i,errcode,depth,retval = -1; uint16_t opcode; int64_t *pricedata,pricestack[4],price,den,a,b,c; + pricedata = (int64_t *)calloc(sizeof(*pricedata)*3,1 + PRICES_DAYWINDOW*2 + PRICES_SMOOTHWIDTH); price = den = depth = errcode = 0; for (i=0; i firstheight+PRICES_DAYWINDOW); - if ( (price= prices_syntheticprice(vec,height,minmax)) < 0 ) + if ( (price= prices_syntheticprice(vec,height,minmax,leverage)) < 0 ) { fprintf(stderr,"unexpected zero synthetic price at height.%d\n",height); return(0); @@ -366,7 +366,7 @@ int64_t prices_syntheticprofits(int64_t &costbasis,int32_t firstheight,int32_t h } profits = ((price * SATOSHIDEN) / costbasis) - SATOSHIDEN; profits *= leverage * positionsize; - return(positionsize + addedbets + profits) + return(positionsize + addedbets + profits); } void prices_betjson(UniValue &result,int64_t profits,int64_t costbasis,int64_t positionsize,int64_t addedbets,int16_t leverage,int32_t firstheight,int64_t firstprice) @@ -400,7 +400,7 @@ UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector vec; + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t betamount,firstprice; std::vector vec; char myaddr[64]; if ( leverage > PRICES_MAXLEVERAGE || leverage < -PRICES_MAXLEVERAGE ) { result.push_back(Pair("result","error")); @@ -437,7 +437,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batonttxid; + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batontxid; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -511,7 +511,7 @@ UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t firstheight,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee=0,positionsize,addedbets,firstprice,profits,ignore,costbasis=0; int32_t firstheight,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -559,7 +559,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; char destaddr[64]; CTransaction bettx; uint256 hashBlock,tokenid; int64_t CCchange=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t i,firstheight,height,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; char destaddr[64]; CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t CCchange=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t i,firstheight,height,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -601,7 +601,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) UniValue PricesInfo(uint256 bettxid,int32_t height) { - UniValue result(UniValue::VOBJ); uint256 hashBlock,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + UniValue result(UniValue::VOBJ); CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,ignore,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) { if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) diff --git a/src/komodo_defs.h b/src/komodo_defs.h index 285bbb63d..49aa67081 100644 --- a/src/komodo_defs.h +++ b/src/komodo_defs.h @@ -84,5 +84,6 @@ int tx_height( const uint256 &hash ); extern char NOTARYADDRS[64][36]; extern uint8_t NUM_NOTARIES; void komodo_netevent(std::vector payload); +int32_t komodo_priceind(char *symbol); #endif diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index 6ea86eb9d..ba15746e0 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2284,6 +2284,18 @@ char *komodo_pricename(char *name,int32_t ind) return(0); } +int32_t komodo_priceind(char *symbol) +{ + char name[65]; int32_t i,n = (int32_t)(komodo_cbopretsize(ASSETCHAINS_CBOPRET) / sizeof(uint32_t)); + for (i=1; i Date: Fri, 5 Apr 2019 22:59:44 -1100 Subject: [PATCH 137/154] syntax --- src/cc/CCPrices.h | 1 + src/cc/prices.cpp | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index bb3b74ba6..db0e66e88 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -22,6 +22,7 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, #define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) #define PRICES_TXFEE 10000 +#define PRICES_MAXLEVERAGE 777 #define PRICES_SMOOTHWIDTH 1 #define KOMODO_MAXPRICES 2048 // must be power of 2 and less than 8192 #define KOMODO_PRICEMASK (~(KOMODO_MAXPRICES - 1)) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 1cb7b6667..4b567f139 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -212,7 +212,7 @@ int32_t prices_syntheticvec(std::vector &vec,std::vector opcode = PRICES_MMM, need = 3; else if ( opstr == "///" ) opcode = PRICES_DDD, need = 3; - else if ( (ind= prices_ind(opstr.c_str())) >= 0 ) + else if ( (ind= komodo_pricesind(opstr.c_str())) >= 0 ) opcode = ind, need = 0; else if ( (weight= atoi(opstr.c_str())) > 0 && weight < KOMODO_MAXPRICES ) { @@ -238,7 +238,7 @@ int32_t prices_syntheticvec(std::vector &vec,std::vector int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t minmax,int16_t leverage) { - int32_t i,errcode,depth,retval = -1; uint16_t opcode; int64_t *pricedata,pricestack[4],price,den,a,b,c; + int32_t i,ind,errcode,depth,retval = -1; uint16_t opcode; int64_t *pricedata,pricestack[4],price,den,a,b,c; pricedata = (int64_t *)calloc(sizeof(*pricedata)*3,1 + PRICES_DAYWINDOW*2 + PRICES_SMOOTHWIDTH); price = den = depth = errcode = 0; for (i=0; i vec,int32_t height,int32_t m den += ind; } else errcode = -2; break; - case PRICES_MUL: + case PRICES_MULT: if ( depth >= 2 ) { b = pricestack[--depth]; @@ -400,7 +400,7 @@ UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector vec; char myaddr[64]; + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t betamount,firstprice; std::vector vec; char myaddr[64]; std::string rawtx; if ( leverage > PRICES_MAXLEVERAGE || leverage < -PRICES_MAXLEVERAGE ) { result.push_back(Pair("result","error")); @@ -413,7 +413,7 @@ UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector 4096 ) + if ( prices_syntheticvec(vec,synthetic) < 0 || (firstprice= prices_syntheticprice(vec,nextheight-1,1,leverage)) < 0 || vec.size() == 0 || vec.size() > 4096 ) { result.push_back(Pair("result","error")); result.push_back(Pair("error","invalid synthetic")); @@ -437,7 +437,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batontxid; std::string rawtx; + struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batontxid; std::string rawtx; char myaddr[64]; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -448,7 +448,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) { if ( prices_batontxid(addedbets,batontxid,bettxid) >= 0 ) { - mtx.vin.push_back(CTxVin(batontxid,0,CScript())); + mtx.vin.push_back(CTxIn(batontxid,0,CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,mypk)); // baton for total funding mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,pricespk)); rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_addopret(bettxid,mypk,amount)); @@ -470,7 +470,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee,positionsize=0,addedbets,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t myfee,positionsize=0,addedbets,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -480,8 +480,8 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) { if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) { - addedbets = prices_addedbets(bettx); - mtx.vin.push_back(CTxVin(bettx,1,CScript())); + addedbets = prices_batontxid(batontxid,bettx,bettxid); + mtx.vin.push_back(CTxIn(bettx,1,CScript())); for (i=0; ievalcode,bettx.vout[2].nValue-myfee-txfee,pricespk)); rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,prices_finalopret(bettxid,profits,rektheight,mypk,firstprice,costbasis,addedbets,positionsize,leverage)); @@ -559,7 +559,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; char destaddr[64]; CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t CCchange=0,positionsize,addedbets,firstprice,profits,costbasis=0; int32_t i,firstheight,height,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; char destaddr[64]; CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t CCchange=0,positionsize,inputsum,ignore,addedbets,firstprice,profits,costbasis=0; int32_t i,firstheight,height,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -580,7 +580,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) return(result); } prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); - mtx.vin.push_back(CTxVin(bettxid,2,CScript())); + mtx.vin.push_back(CTxIn(bettxid,2,CScript())); if ( (inputsum= AddPricesInputs(cp,mtx,destaddr,profits+txfee,64,bettxid,2)) > profits+txfee ) CCchange = (inputsum - profits); mtx.vout.push_back(CTxOut(bettx.vout[2].nValue + profits,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); @@ -601,7 +601,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) UniValue PricesInfo(uint256 bettxid,int32_t height) { - UniValue result(UniValue::VOBJ); CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,ignore,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + UniValue result(UniValue::VOBJ); CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) { if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) From a44709745c6e66dd85c053127f7f375e9f2a83e1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 23:02:48 -1100 Subject: [PATCH 138/154] Syntax --- src/cc/CCPrices.h | 2 +- src/cc/prices.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index db0e66e88..672d6d9d8 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -18,7 +18,7 @@ #define CC_PRICES_H #include "CCinclude.h" -int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind,int32_t daywindow); +int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind); #define PRICES_DAYWINDOW ((3600*24/ASSETCHAINS_BLOCKTIME) + 1) #define PRICES_TXFEE 10000 diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 4b567f139..38777e672 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -212,7 +212,7 @@ int32_t prices_syntheticvec(std::vector &vec,std::vector opcode = PRICES_MMM, need = 3; else if ( opstr == "///" ) opcode = PRICES_DDD, need = 3; - else if ( (ind= komodo_pricesind(opstr.c_str())) >= 0 ) + else if ( (ind= komodo_priceind(opstr.c_str())) >= 0 ) opcode = ind, need = 0; else if ( (weight= atoi(opstr.c_str())) > 0 && weight < KOMODO_MAXPRICES ) { @@ -446,7 +446,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) GetCCaddress(cp,myaddr,mypk); if ( AddNormalinputs(mtx,mypk,amount+txfee,64) >= amount+txfee ) { - if ( prices_batontxid(addedbets,batontxid,bettxid) >= 0 ) + if ( prices_batontxid(batontxid,bettx,bettxid) >= 0 ) { mtx.vin.push_back(CTxIn(batontxid,0,CScript())); mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,mypk)); // baton for total funding @@ -481,7 +481,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) if ( prices_betopretdecode(bettx.vout[numvouts-1].scriptPubKey,pk,firstheight,positionsize,leverage,firstprice,vec,tokenid) == 'B' ) { addedbets = prices_batontxid(batontxid,bettx,bettxid); - mtx.vin.push_back(CTxIn(bettx,1,CScript())); + mtx.vin.push_back(CTxIn(bettxid,1,CScript())); for (i=0; i Date: Fri, 5 Apr 2019 23:07:28 -1100 Subject: [PATCH 139/154] Syntax --- src/cc/prices.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 38777e672..8cb91e539 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -212,7 +212,7 @@ int32_t prices_syntheticvec(std::vector &vec,std::vector opcode = PRICES_MMM, need = 3; else if ( opstr == "///" ) opcode = PRICES_DDD, need = 3; - else if ( (ind= komodo_priceind(opstr.c_str())) >= 0 ) + else if ( (ind= komodo_priceind((char *)opstr.c_str())) >= 0 ) opcode = ind, need = 0; else if ( (weight= atoi(opstr.c_str())) > 0 && weight < KOMODO_MAXPRICES ) { @@ -256,8 +256,8 @@ int64_t prices_syntheticprice(std::vector vec,int32_t height,int32_t m else { if ( leverage > 0 ) - pricestack[depth] = MAX(pricedata[1],pricedata[2]); - else pricestack[depth] = MIN(pricedata[1],pricedata[2]); + pricestack[depth] = (pricedata[1] > pricedata[2]) ? pricedata[1] : pricedata[2]; // MAX + else pricestack[depth] = (pricedata[1] < pricedata[2]) ? pricedata[1] : pricedata[2]; // MIN } } if ( pricestack[depth] == 0 ) @@ -389,7 +389,7 @@ int64_t prices_costbasis(CTransaction bettx) return(costbasis); } -int64_t prices_batontxid(uint256 &batontxid,CTransaction bettx,uint256 bettxid); +int64_t prices_batontxid(uint256 &batontxid,CTransaction bettx,uint256 bettxid) { int64_t addedbets = 0; // iterate through batons, adding up vout1 -> addedbets @@ -437,7 +437,7 @@ UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batontxid; std::string rawtx; char myaddr[64]; + struct CCcontract_info *cp,C; CTransaction bettx; CPubKey pricespk,mypk; int64_t addedbets=0,betamount,firstprice; std::vector vec; uint256 batontxid; std::string rawtx; char myaddr[64]; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -484,7 +484,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) mtx.vin.push_back(CTxIn(bettxid,1,CScript())); for (i=0; i Date: Fri, 5 Apr 2019 23:10:37 -1100 Subject: [PATCH 140/154] Syntax --- src/cc/prices.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 8cb91e539..8dc9d7d02 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -511,7 +511,7 @@ UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight) { int32_t nextheight = komodo_nextheight(); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(),nextheight); UniValue result(UniValue::VOBJ); - struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid; int64_t myfee=0,positionsize,addedbets,firstprice,profits,ignore,costbasis=0; int32_t firstheight,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; + struct CCcontract_info *cp,C; CTransaction bettx; uint256 hashBlock,tokenid,batontxid; int64_t myfee=0,positionsize,addedbets,firstprice,profits,ignore,costbasis=0; int32_t firstheight,numvouts; int16_t leverage; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; cp = CCinit(&C,EVAL_PRICES); if ( txfee == 0 ) txfee = PRICES_TXFEE; @@ -599,7 +599,7 @@ UniValue PricesCashout(uint64_t txfee,uint256 bettxid) return(result); } -UniValue PricesInfo(uint256 bettxid,int32_t height) +UniValue PricesInfo(uint256 bettxid,int32_t refheight) { UniValue result(UniValue::VOBJ); CTransaction bettx; uint256 hashBlock,batontxid,tokenid; int64_t myfee,ignore,positionsize=0,addedbets=0,firstprice=0,profits=0,costbasis=0; int32_t i,firstheight=0,height,numvouts; int16_t leverage=0; std::vector vec; CPubKey pk,mypk,pricespk; std::string rawtx; if ( myGetTransaction(bettxid,bettx,hashBlock) != 0 && (numvouts= bettx.vout.size()) > 3 ) @@ -608,13 +608,14 @@ UniValue PricesInfo(uint256 bettxid,int32_t height) { costbasis = prices_costbasis(bettx); addedbets = prices_batontxid(batontxid,bettx,bettxid); - if ( (profits= prices_syntheticprofits(ignore,firstheight,firstheight+i,leverage,vec,positionsize,addedbets)) < 0 ) + if ( (profits= prices_syntheticprofits(ignore,firstheight,refheight,leverage,vec,positionsize,addedbets)) < 0 ) { result.push_back(Pair("rekt",1)); result.push_back(Pair("rektfee",(positionsize + addedbets) / 500)); } else result.push_back(Pair("rekt",0)); result.push_back(Pair("batontxid",batontxid.GetHex())); prices_betjson(result,profits,costbasis,positionsize,addedbets,leverage,firstheight,firstprice); + result.push_back(Pair("height",(int64_t)refheight)); return(result); } } From 50e9ffd9e0721acc3cf54d04b27a4caec12dffcb Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 23:46:42 -1100 Subject: [PATCH 141/154] syntax --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c5dbd8601..3ef92e300 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1185,7 +1185,7 @@ int64_t komodo_pricemult(int32_t ind); int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) { - int32_t height,i,width,numpricefeeds = -1; uint64_t seed,rngval,*correlated2; uint32_t rawprices[1440*6],*ptr; + int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,rngval; int64_t *correlated2; uint32_t rawprices[1440*6],*ptr; //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; From 60a903bc2b71b77b4ca59c3e1eac44adb8f840de Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 23:53:42 -1100 Subject: [PATCH 142/154] syntax --- src/rpc/blockchain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 3ef92e300..6b0c92552 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1176,7 +1176,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); -int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int32_t int64_t *correlated2,int32_t numprices); +int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *correlated2,int32_t numprices); int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t skip,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); From 6dec94bf748e14143a6da276a3cad1fd8c64e79b Mon Sep 17 00:00:00 2001 From: jl777 Date: Fri, 5 Apr 2019 23:57:03 -1100 Subject: [PATCH 143/154] syntax --- src/rpc/blockchain.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6b0c92552..fc9a71fe2 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1177,7 +1177,7 @@ UniValue paxprice(const UniValue& params, bool fHelp) int32_t komodo_heightpricebits(uint64_t *seedp,uint32_t *heightbits,int32_t nHeight); char *komodo_pricename(char *name,int32_t ind); int64_t komodo_pricesmoothed(int64_t *correlated,int32_t cskip,int64_t *correlated2,int32_t numprices); -int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t skip,int32_t numprices,uint32_t *rawprices2,int32_t smoothwidth); +int64_t komodo_pricecorrelated(uint64_t seed,int32_t ind,uint32_t *rawprices,int32_t rawskip,uint32_t *nonzprices,int32_t smoothwidth); int32_t komodo_nextheight(); uint32_t komodo_heightstamp(int32_t height); int64_t komodo_pricemult(int32_t ind); @@ -1185,7 +1185,7 @@ int64_t komodo_pricemult(int32_t ind); int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks,int32_t ind) { - int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,rngval; int64_t *correlated2; uint32_t rawprices[1440*6],*ptr; + int32_t height,i,n,width,numpricefeeds = -1; uint64_t seed,ignore,rngval; int64_t *correlated2; uint32_t rawprices[1440*6],*ptr; //daywindow = (3600*24/ASSETCHAINS_BLOCKTIME) + 1; //pricedata = (uint32_t *)calloc(sizeof(*prices)*3,numblocks + daywindow*2 + PRICES_SMOOTHWIDTH); width = numblocks+PRICES_DAYWINDOW*2+PRICES_SMOOTHWIDTH; @@ -1211,7 +1211,7 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, rngval = (rngval*11109 + 13849); ptr = (uint32_t *)&pricedata[i*3]; correlated2[i] = ptr[0]; - if ( (pricedata[i*3+1]= komodo_pricecorrelated(rngval,ind,(uint32_t)&pricedata[i*3],6,0,PRICES_SMOOTHWIDTH)) < 0 ) + if ( (pricedata[i*3+1]= komodo_pricecorrelated(rngval,ind,(uint32_t *)&pricedata[i*3],6,0,PRICES_SMOOTHWIDTH)) < 0 ) { free(correlated2); return(-3); From e2ddb727d4cf22f4305625ca4774bd907202da0f Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:01:49 -1100 Subject: [PATCH 144/154] SnapShot2 --- src/txdb.cpp | 41 ++++++++++++++++++++++------------------- src/txdb.h | 1 + 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/txdb.cpp b/src/txdb.cpp index 7fa658f1e..ace04ebdb 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -438,30 +438,31 @@ bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, bool getAddressFromIndex(const int &type, const uint160 &hash, std::string &address); uint32_t komodo_segid32(char *coinaddr); -const std::map ignoredMap = { - {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1}, - {"RMUF3UDmzWFLSKV82iFbMaqzJpUnrWjcT4", 1}, - {"RA5imhVyJa7yHhggmBytWuDr923j2P1bxx", 1}, - {"RBM5LofZFodMeewUzoMWcxedm3L3hYRaWg", 1}, - {"RAdcko2d94TQUcJhtFHZZjMyWBKEVfgn4J", 1}, - {"RLzUaZ934k2EFCsAiVjrJqM8uU1vmMRFzk", 1}, - {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, - {"RUDrX1v5toCsJMUgtvBmScKjwCB5NaR8py", 1}, - {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, - {"RRvwmbkxR5YRzPGL5kMFHMe1AH33MeD8rN", 1}, - {"RQLQvSgpPAJNPgnpc8MrYsbBhep95nCS8L", 1}, - {"RK8JtBV78HdvEPvtV5ckeMPSTojZPzHUTe", 1}, - {"RHVs2KaCTGUMNv3cyWiG1jkEvZjigbCnD2", 1}, - {"RE3SVaDgdjkRPYA6TRobbthsfCmxQedVgF", 1}, - {"RW6S5Lw5ZCCvDyq4QV9vVy7jDHfnynr5mn", 1}, - {"RTkJwAYtdXXhVsS3JXBAJPnKaBfMDEswF8", 1}, - {"RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY", 1} //Burnaddress for null privkey +#define DECLARE_IGNORELIST std::map ignoredMap = { \ + {"RReUxSs5hGE39ELU23DfydX8riUuzdrHAE", 1}, \ + {"RMUF3UDmzWFLSKV82iFbMaqzJpUnrWjcT4", 1}, \ + {"RA5imhVyJa7yHhggmBytWuDr923j2P1bxx", 1}, \ + {"RBM5LofZFodMeewUzoMWcxedm3L3hYRaWg", 1}, \ + {"RAdcko2d94TQUcJhtFHZZjMyWBKEVfgn4J", 1}, \ + {"RLzUaZ934k2EFCsAiVjrJqM8uU1vmMRFzk", 1}, \ + {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, \ + {"RUDrX1v5toCsJMUgtvBmScKjwCB5NaR8py", 1}, \ + {"RMSZMWZXv4FhUgWhEo4R3AQXmRDJ6rsGyt", 1}, \ + {"RRvwmbkxR5YRzPGL5kMFHMe1AH33MeD8rN", 1}, \ + {"RQLQvSgpPAJNPgnpc8MrYsbBhep95nCS8L", 1}, \ + {"RK8JtBV78HdvEPvtV5ckeMPSTojZPzHUTe", 1}, \ + {"RHVs2KaCTGUMNv3cyWiG1jkEvZjigbCnD2", 1}, \ + {"RE3SVaDgdjkRPYA6TRobbthsfCmxQedVgF", 1}, \ + {"RW6S5Lw5ZCCvDyq4QV9vVy7jDHfnynr5mn", 1}, \ + {"RTkJwAYtdXXhVsS3JXBAJPnKaBfMDEswF8", 1}, \ + {"RD6GgnrMpPaTSMn8vai6yiGA7mN4QGPVMY", 1} \ }; -UniValue CBlockTreeDB::Snapshot2(int64_t dustthreshold,int32_t top,std::vector > &vaddr) +int32_t CBlockTreeDB::Snapshot2(int64_t dustthreshold,int32_t top,std::vector > &vaddr) { int64_t total = 0; int64_t totalAddresses = 0; std::string address; int64_t utxos = 0; int64_t ignoredAddresses = 0; + DECLARE_IGNORELIST boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; for (iter->SeekToLast(); iter->Valid(); iter->Prev()) @@ -541,12 +542,14 @@ UniValue CBlockTreeDB::Snapshot2(int64_t dustthreshold,int32_t top,std::vector < if ( top == topN ) break; } + return(topN); } UniValue CBlockTreeDB::Snapshot(int top) { int64_t total = 0; int64_t totalAddresses = 0; std::string address; int64_t utxos = 0; int64_t ignoredAddresses = 0; + DECLARE_IGNORELIST boost::scoped_ptr iter(NewIterator()); std::map addressAmounts; std::vector > vaddr; diff --git a/src/txdb.h b/src/txdb.h index b9bae2fe4..9b80b922b 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -116,6 +116,7 @@ public: bool LoadBlockIndexGuts(); bool blockOnchainActive(const uint256 &hash); UniValue Snapshot(int top); + int32_t Snapshot2(int64_t dustthreshold,int32_t top,std::vector > &vaddr); }; #endif // BITCOIN_TXDB_H From 2f9dfa9ad24fe49d8a65c47293b3fde66029e0d5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:21:26 -1100 Subject: [PATCH 145/154] Truncated prices roc --- src/cc/CCPrices.h | 9 +++++++-- src/cc/prices.cpp | 40 ++++++++++++++++++++-------------------- src/rpc/server.cpp | 5 ----- src/rpc/server.h | 5 ----- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 672d6d9d8..9ff4f2957 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -38,8 +38,13 @@ int32_t prices_extract(int64_t *pricedata,int32_t firstheight,int32_t numblocks, bool PricesValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn); // CCcustom -UniValue PricesList(); -UniValue PricesBet(uint64_t txfee,int64_t amount,int32_t leverage,std::string synthetic); +UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector synthetic); +UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount); +UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid); +UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight); +UniValue PricesCashout(uint64_t txfee,uint256 bettxid); +UniValue PricesInfo(uint256 bettxid,int32_t refheight); +UniValue PricesList() #endif diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 8dc9d7d02..cedf05f2b 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -152,26 +152,6 @@ int64_t AddPricesInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,char return(totalinputs); } -UniValue PricesList() -{ - UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock,tokenid; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; char str[65]; - 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 && prices_betopretdecode(vintx.vout[vintx.vout.size()-1].scriptPubKey,pk,height,amount,leverage,firstprice,vec,tokenid) == 'B' ) - { - result.push_back(uint256_str(str,txid)); - } - } - } - return(result); -} - UniValue prices_rawtxresult(UniValue &result,std::string rawtx,int32_t broadcastflag) { CTransaction tx; @@ -624,4 +604,24 @@ UniValue PricesInfo(uint256 bettxid,int32_t refheight) return(result); } +UniValue PricesList() +{ + UniValue result(UniValue::VARR); std::vector > addressIndex; struct CCcontract_info *cp,C; int64_t amount,firstprice; int32_t height; int16_t leverage; uint256 txid,hashBlock,tokenid; CPubKey pk,pricespk; std::vector vec; CTransaction vintx; char str[65]; + 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 && prices_betopretdecode(vintx.vout[vintx.vout.size()-1].scriptPubKey,pk,height,amount,leverage,firstprice,vec,tokenid) == 'B' ) + { + result.push_back(uint256_str(str,txid)); + } + } + } + return(result); +} + diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index e3f4ca0cf..a48ef6cb1 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -445,11 +445,6 @@ static const CRPCCommand vRPCCommands[] = { "prices", "pricesaddress", &pricesaddress, true }, { "prices", "priceslist", &priceslist, true }, { "prices", "pricesinfo", &pricesinfo, true }, - { "prices", "pricescreate", &pricescreate, true }, - { "prices", "pricesaddfunding", &pricesaddfunding, true }, - { "prices", "pricesbet", &pricesbet, true }, - { "prices", "pricesstatus", &pricesstatus, true }, - { "prices", "pricesfinish", &pricesfinish, true }, // Pegs { "pegs", "pegsaddress", &pegsaddress, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index 997a418b4..79a12bb7b 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -272,11 +272,6 @@ extern UniValue oraclessamples(const UniValue& params, bool fHelp); extern UniValue pricesaddress(const UniValue& params, bool fHelp); extern UniValue priceslist(const UniValue& params, bool fHelp); extern UniValue pricesinfo(const UniValue& params, bool fHelp); -extern UniValue pricescreate(const UniValue& params, bool fHelp); -extern UniValue pricesaddfunding(const UniValue& params, bool fHelp); -extern UniValue pricesbet(const UniValue& params, bool fHelp); -extern UniValue pricesstatus(const UniValue& params, bool fHelp); -extern UniValue pricesfinish(const UniValue& params, bool fHelp); extern UniValue pegsaddress(const UniValue& params, bool fHelp); extern UniValue marmaraaddress(const UniValue& params, bool fHelp); extern UniValue marmara_poolpayout(const UniValue& params, bool fHelp); From 115f49cfc99b8aba6fc2e85603e988fdbb54d61e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:22:29 -1100 Subject: [PATCH 146/154] ; --- src/cc/CCPrices.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/CCPrices.h b/src/cc/CCPrices.h index 9ff4f2957..1ecaebb2c 100644 --- a/src/cc/CCPrices.h +++ b/src/cc/CCPrices.h @@ -44,7 +44,7 @@ UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid); UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight); UniValue PricesCashout(uint64_t txfee,uint256 bettxid); UniValue PricesInfo(uint256 bettxid,int32_t refheight); -UniValue PricesList() +UniValue PricesList(); #endif From 5204312224b2b37ca3d29e2b2eb498383c688765 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:26:51 -1100 Subject: [PATCH 147/154] Remove rpc funds --- src/wallet/rpcwallet.cpp | 132 +-------------------------------------- 1 file changed, 1 insertion(+), 131 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 107508c18..ff56708f2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6915,137 +6915,7 @@ UniValue pricesinfo(const UniValue& params, bool fHelp) if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - return(PricesInfo(fundingtxid)); -} - -UniValue pricescreate(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); uint64_t mode; int64_t funding; int32_t i,n,margin,maxleverage; std::string hex; uint256 oracletxid,longtoken,shorttoken,bettoken; std::vector pubkeys; std::vectorpubkey; - if ( fHelp || params.size() < 8 ) - throw runtime_error("pricescreate bettoken oracletxid margin mode longtoken shorttoken maxleverage funding N [pubkeys]\n"); - if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - bettoken = Parseuint256((char *)params[0].get_str().c_str()); - oracletxid = Parseuint256((char *)params[1].get_str().c_str()); - margin = atof(params[2].get_str().c_str()) * 1000; - mode = atol(params[3].get_str().c_str()); - longtoken = Parseuint256((char *)params[4].get_str().c_str()); - shorttoken = Parseuint256((char *)params[5].get_str().c_str()); - maxleverage = atol(params[6].get_str().c_str()); - funding = atof(params[7].get_str().c_str()) * COIN + 0.00000000499999; - n = atoi(params[8].get_str().c_str()); - if ( n > 0 ) - { - for (i=0; i 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } - else - { - ERR_RESULT("couldnt create prices funding transaction"); - } - return(result); -} - -UniValue pricesaddfunding(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); std::string hex; uint256 fundingtxid,bettoken; int64_t amount; - if ( fHelp || params.size() != 3 ) - throw runtime_error("pricesaddfunding fundingtxid bettoken amount\n"); - if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - bettoken = Parseuint256((char *)params[1].get_str().c_str()); - amount = atof(params[2].get_str().c_str()) * COIN + 0.00000000499999; - hex = PricesAddFunding(0,bettoken,fundingtxid,amount); - if ( hex.size() > 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } - else - { - ERR_RESULT("couldnt create pricesaddfunding transaction"); - } - return(result); -} - -UniValue pricesbet(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); std::string hex; uint256 fundingtxid,bettoken; int64_t amount; int32_t leverage; - if ( fHelp || params.size() != 4 ) - throw runtime_error("pricesbet fundingtxid bettoken amount leverage\n"); - if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - bettoken = Parseuint256((char *)params[1].get_str().c_str()); - amount = atof(params[2].get_str().c_str()) * COIN + 0.00000000499999; - leverage = atoi(params[3].get_str().c_str()); - hex = PricesBet(0,bettoken,fundingtxid,amount,leverage); - if ( hex.size() > 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } - else - { - ERR_RESULT("couldnt create pricesbet transaction"); - } - return(result); -} - -UniValue pricesstatus(const UniValue& params, bool fHelp) -{ - uint256 fundingtxid,bettxid,bettoken; - if ( fHelp || params.size() != 3 ) - throw runtime_error("pricesstatus fundingtxid bettoken bettxid\n"); - if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - bettoken = Parseuint256((char *)params[1].get_str().c_str()); - bettxid = Parseuint256((char *)params[2].get_str().c_str()); - return(PricesStatus(0,bettoken,fundingtxid,bettxid)); -} - -UniValue pricesfinish(const UniValue& params, bool fHelp) -{ - UniValue result(UniValue::VOBJ); uint256 fundingtxid,bettxid,bettoken; std::string hex; - if ( fHelp || params.size() != 3 ) - throw runtime_error("pricesfinish fundingtxid bettoken bettxid\n"); - if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) - throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - bettoken = Parseuint256((char *)params[1].get_str().c_str()); - bettxid = Parseuint256((char *)params[2].get_str().c_str()); - hex = PricesFinish(0,bettoken,fundingtxid,bettxid); - if ( hex.size() > 0 ) - { - result.push_back(Pair("result", "success")); - result.push_back(Pair("hex", hex)); - } - else - { - ERR_RESULT("couldnt create pricesfinish transaction"); - } - return(result); + return(PricesInfo(txfee,fundingtxid)); } UniValue dicefund(const UniValue& params, bool fHelp) From 559fb2e0d0d9fc010e07ff2ca609cb3f4276b83e Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:27:42 -1100 Subject: [PATCH 148/154] 0 --- src/wallet/rpcwallet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index ff56708f2..1a55763da 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6915,7 +6915,7 @@ UniValue pricesinfo(const UniValue& params, bool fHelp) if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - return(PricesInfo(txfee,fundingtxid)); + return(PricesInfo(0,fundingtxid)); } UniValue dicefund(const UniValue& params, bool fHelp) From a8c729d8fdf2f1ab24ed29705d3c8e464c40f5c4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:29:54 -1100 Subject: [PATCH 149/154] Height --- src/wallet/rpcwallet.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1a55763da..fb87ff9ea 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6909,13 +6909,14 @@ UniValue priceslist(const UniValue& params, bool fHelp) UniValue pricesinfo(const UniValue& params, bool fHelp) { - uint256 fundingtxid; - if ( fHelp || params.size() != 1 ) + uint256 bettxid; int32_t height; + if ( fHelp || params.size() != 2 ) throw runtime_error("pricesinfo fundingtxid\n"); if ( ensure_CCrequirements(EVAL_PRICES) < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - fundingtxid = Parseuint256((char *)params[0].get_str().c_str()); - return(PricesInfo(0,fundingtxid)); + bettxid = Parseuint256((char *)params[0].get_str().c_str()); + height = atoi(params[1].get_str().c_str()); + return(PricesInfo(bettxid,height)); } UniValue dicefund(const UniValue& params, bool fHelp) From b81f1d4bee6da33a398be4d757530772c737c609 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:38:13 -1100 Subject: [PATCH 150/154] Make it PRICES compatible --- src/komodo_gateway.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index ba15746e0..f7f7874ce 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1728,7 +1728,7 @@ CScript komodo_mineropret(int32_t nHeight) int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 0; + int32_t testchain_exemption = 350; std::vector vopret; char maxflags[KOMODO_MAXPRICES]; double btcusd,btcgbp,btceur; uint32_t localbits[KOMODO_MAXPRICES],pricebits[KOMODO_MAXPRICES],prevbits[KOMODO_MAXPRICES],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { @@ -2135,8 +2135,8 @@ void komodo_cbopretupdate(int32_t forceflag) now = (uint32_t)time(NULL); if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) { -//if ( komodo_nextheight() > 333 ) // for debug only! -// ASSETCHAINS_CBOPRET = 7; +if ( komodo_nextheight() > 333 ) // for debug only! + ASSETCHAINS_CBOPRET = 7; size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); From 88fb116c539541967c2adb5578df2fb5d47ed25a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 00:55:23 -1100 Subject: [PATCH 151/154] Comments --- src/cc/prices.cpp | 36 ++++++++++++++++++++++++------------ src/komodo_gateway.h | 6 +++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index cedf05f2b..85e2c645d 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -19,26 +19,38 @@ /* CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; + + 0.5% fee based on betamount, NOT leveraged betamount!! 0.1% collected by price basis determinant 0.2% collected by rekt tx - PricesBet -> +/-leverage, amount, synthetic -> opreturn includes current price - funds are locked into 1of2 global CC address - for first day, long basis is MAX(correlated,smoothed), short is MIN() - reference price is the smoothed of the previous block - if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price - original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund - 0.5% of bet -> globalfund - - PricesStatus -> bettxid maxsamples returns initial params, cost basis, amount left, rekt:true/false, rektheight, initial synthetic price, current synthetic price, net gain + UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector synthetic) + funds are locked into 1of2 global CC address + for first day, long basis is MAX(correlated,smoothed), short is MIN() + reference price is the smoothed of the previous block + if synthetic value + amount goes negative, then anybody can rekt it to collect a rektfee, proof of rekt must be included to show cost basis, rekt price + original creator can liquidate at anytime and collect (synthetic value + amount) from globalfund + 0.5% of bet -> globalfund + + UniValue PricesAddFunding(uint64_t txfee,uint256 bettxid,int64_t amount) + add funding to an existing bet, doesnt change the profit calcs but does make the bet less likely to be rekt - PricesRekt -> bettxid height -> 0.1% to miner, rest to global CC + UniValue PricesSetcostbasis(uint64_t txfee,uint256 bettxid) + in the first day from the bet, the costbasis can (and usually does) change based on the MAX(correlated,smoothed) for long and MIN() for shorts + to calculate this requires a bit of work, so whatever node is first to get the proper calculation confirmed, gets a 0.1% costbasis fee - PricesClose -> bettxid returns (synthetic value + amount) + UniValue PricesRekt(uint64_t txfee,uint256 bettxid,int32_t rektheight) + similarily, any node can submit a rekt tx and cash in on 0.2% fee if their claim is confirmed - PricesList -> all bettxid -> list [bettxid, netgain] + UniValue PricesCashout(uint64_t txfee,uint256 bettxid) + only the actually creator of bet is able to cashout and only if it isnt rekt at that moment + UniValue PricesInfo(uint256 bettxid,int32_t refheight) + all relevant info about a bet + + UniValue PricesList() + a list of all pending and completed bets in different lists */ diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index f7f7874ce..ba15746e0 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -1728,7 +1728,7 @@ CScript komodo_mineropret(int32_t nHeight) int32_t komodo_opretvalidate(const CBlock *block,CBlockIndex * const previndex,int32_t nHeight,CScript scriptPubKey) { - int32_t testchain_exemption = 350; + int32_t testchain_exemption = 0; std::vector vopret; char maxflags[KOMODO_MAXPRICES]; double btcusd,btcgbp,btceur; uint32_t localbits[KOMODO_MAXPRICES],pricebits[KOMODO_MAXPRICES],prevbits[KOMODO_MAXPRICES],newprice; int32_t i,j,prevtime,maxflag,lag,lag2,lag3,n,errflag,iter; uint32_t now = (uint32_t)time(NULL); if ( ASSETCHAINS_CBOPRET != 0 && nHeight > 0 ) { @@ -2135,8 +2135,8 @@ void komodo_cbopretupdate(int32_t forceflag) now = (uint32_t)time(NULL); if ( (ASSETCHAINS_CBOPRET & 1) != 0 ) { -if ( komodo_nextheight() > 333 ) // for debug only! - ASSETCHAINS_CBOPRET = 7; +//if ( komodo_nextheight() > 333 ) // for debug only! +// ASSETCHAINS_CBOPRET = 7; size = komodo_cbopretsize(ASSETCHAINS_CBOPRET); if ( Mineropret.size() < size ) Mineropret.resize(size); From 1de369ea40560da31194b1469c9385b8de271f8a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 01:23:21 -1100 Subject: [PATCH 152/154] Prices CC rpc purpose documented --- src/cc/prices.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 85e2c645d..7e999fe25 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -19,11 +19,39 @@ /* CBOPRET creates trustless oracles, which can be used for making a synthetic cash settlement system based on real world prices; - - 0.5% fee based on betamount, NOT leveraged betamount!! 0.1% collected by price basis determinant 0.2% collected by rekt tx + + At the simplest, prices CC allows to make leveraged cash settled bets on long and short BTCUSD: + BTCUSD, 1 with positive leverage parameter up to 777 + BTCUSD, 1 with negative leverage parameter up to -777 + + These specific limits of 0.5%, 0.1%, 0.2%, 777 should be able to be changed based on #define + + Another configuration is to send the 0.4% (or 0.2%) fees to a fee collection address (this is not currently implemented, but would be needed for systems that dont use -ac_perc to collect a global override) + + The definition of the synthetic instrument that is being tracked is actually defined with a forth type of syntax and is quite flexible to allow unlimited combinations and weights, but that is an independent aspect and will be covered later. + + Let us discuss the simplest synthetic case of a long or short of BTCUSD (or any other direct pricepoint from the trustless oracle). If you look at the charts, you will see the blue line that is direct from the miners (and therefore cant be directly used). The orange line is the 51% correlated price that is deterministically random selected from the prior 24 hours. And finally the green line which is simply the average value from the priot 24 hours. + + We will assume that the orange and green prices are able to be deterministically calculated from the raw coinbase data (blue). The prices rpc is currently working reasonably well and appears to return deterministic prices, however for use in the prices CC, it is often needed to find just a single data point. To that effect there is the temporary function prices_syntheticprice, which uses a bruteforce and totally inefficient way to calculate the correlated and smoothed prices. This inefficient process will need to be changed to a permanent storage of correlated and smoothed prices for each trustless oracle that is updated each block. This will then allow to directly lookup each value for any of the trustless prices. Such speed will indeed be needed to scale up usage of a REKT chain. + + Since the mined prices can be manipulated by any miner, combined with the +/-1% tolerance, it would be possible for an external miner to obtain a large hashrate and skew the price +1%, then to -1% to create a 2% price difference, which under high leverage might be able to generate a large profit. + + To avoid this precisely controllable biasing, the 51% correlation of past day is used (orange), which makes the price jump around a lot. The green line that sums the prior day is needed to remove this jitter back to the average value. However, this creates a 1.5 day delay to the price movement of the smoothed price compared to the current price. What this means is that if we want to use the green line to settle prices automatically, a trivial way to make money is to bet in the direct that the mined prices are relative to the smoothed prices. Given 1.5 day head start, it wont be any mystery which direction the smoothed line will move in the next 24 hours. + + So this makes the smoothed price unusable for settling the bets with. However, we cant use the correlated price either, as it would also allow to make bets when the correlated price picked a significantly lower/higher price than the raw price. The solution is to have a floating basis for the costbasis of the bet. In order to allow finding the right costbasis, for long bets the maximum price between the correlated and smoothed price is needed over the first day after the bet. For short bets, the minimum price is needed. + + What this means is that the actual starting price for a bet wont be known for sure when the bet is made, but if the price is relatively stable, there wont be much of a change. If there is a lot of recent volatility, then the starting price will have a high variability. In order to calculate the costbasis, it currently requires many calculations to find the MAX (or MIN) for the first day. Even when this is made to be a lookup, it still requires to issue a transaction to change the state of a bet from a "starting" state to a "in effect" state and this state change is indicated by whether the bettx vout that contains the costbasis utxo is spent or not. + + Once a bet goes into effect, then block by block, it is checked by the decentralized set of rekt scanners if it is rekt or not, in this case for double the reward for calculating the cost basis. This fully decentralized mechanism ensures that some node will decide to collect the free money and ensures that the bankroll is growing. To miss a rekt bet and not close it out when it can be is to allow it to survive for another block, which can change it profitability from negative to positive. + + Which comes to how profits are calculated. Once the costbasis is locked, then the profit calculation can be made based on the ratio between the costbasis and the current price, multiplied by the leverage. So, if a long position gains 10% at a 10x leverage, then approximately the entire bet amount will be made, ie. a double. Similarily with a short position, a 10% drop in price at 10x leverage will double the bet amount. Since it takes a full day to establish the costbasis, it is not possible to allow changing the costbasis for an existing bet, however it is possible to add funds so that it is less likely to be rekt. The sum of the initial bet and all added funds must be greater than the loss at every block to prevent a position from being rekt. To make it simple to calculate the amount of funds added, another bettx vout is used as a baton. Techniques similar to rogue CC to find where the bettx vout was spent and looking at each such transaction to find the total funds added can be used. + + The once that makes the bet is able to add funds or cashout at any block, as long as it isnt rekt. If it is rekt, the bettor is able to collect the rekt fee, so at least that is some consolation, but it is advised to increase the total funding to avoid being rekt. On a cashout all the funds that were bet adjusted by the profits can be collected. + + Hopefully the above description makes it clear what the following rpc calls should be doing: UniValue PricesBet(uint64_t txfee,int64_t amount,int16_t leverage,std::vector synthetic) funds are locked into 1of2 global CC address @@ -52,6 +80,9 @@ CBOPRET creates trustless oracles, which can be used for making a synthetic cash UniValue PricesList() a list of all pending and completed bets in different lists + + Appendix Synthetic position definition language: + */ // start of consensus code From ed6a0ade4400bf502b241e11477a50b02e202573 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 02:40:49 -1100 Subject: [PATCH 153/154] Synthetic definition language --- src/cc/prices.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 7e999fe25..6e95af3af 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -83,7 +83,58 @@ CBOPRET creates trustless oracles, which can be used for making a synthetic cash Appendix Synthetic position definition language: -*/ + Let us start from the familiar BTCUSD nomenclature. this is similar (identical) to forex EURUSD and the equivalent. Notice the price needs two things as it is actually a ratio, ie. BTCUSD is how many USD does 1 BTC get converted to. It can be thought of as the value of BTC/USD, in other words a ratio. + + The value of BTC alone, or USD alone, it is actually quite an abstract issue and it can only be approximated. Specific ways of how to do this can be discussed later, for now it is only important to understand that all prices are actually ratios. + + And these ratios work as normal algebra does. So a/b * b/c == a/c! You can try this out with BTCUSD and USDJPY -> BTC/USD * USD/JPY -> BTC/JPY or the BTCJPY price + + division is the reciprocal, so BTCUSD reciprocated is USDBTC + + Without getting into all the possible combinations of things, let us first understand what the language allows. It uses a stack based language, where individual tokens update the state. The final state needs to have an empty stack and it will have a calculated price. + + The most important is pushing a specific price onto the stack. All the correlated and smoothed prices are normalized so it has each integer unit equal to 0.00000001, amounts above 100 million are above one, amounts less are below one. 100 million is the unit constant. + + In the prices CC synthetic definition language, the symbol that is returned pushes the adjusted price to the stack. The adjustment is selecting between the correlated and smoothed if within a day from the bet creation, or just the smoothed if after a day. You can have a maximum depth of 3, any more than that and it should return an error. + + This means there are operations that are possible on 1, 2 and 3 symbols. For 1 symbol, it is easy, the only direct operation is the inverse, which is represented by "!". All items in the language are separated by "," + + "BTCUSD, !" + + The above is the inverse or USD/BTC, which is another way to short BTCUSD. It is also possible to short the entire synthetic with a negative leverage. The exact results of a -1 leverage and inverse, might not be exact due to the math involved with calculating the profit, but it should generally be similar. + + For two symbols, there is a bit more we can do, namely multiply and divide, combined with inverting, however to simplify the language any inverting is required to be done by the ordering of the symbols and usage of multiply or divide. multiply is "*" and divide is "/" the top of the stack (last to be pushed) is the divisor, the one right before the divisor is the numerator. + + "BTCUSD, USDJPY, *" <- That will create a BTCJPY synthetic + + "BTCEUR, BTCUSD, /" <- That will create a USDEUR synthetic + + If you experiment around with this, you will see that given two symbols and the proper order and * or /, you can always create the synthetic that you want, assuming what you want is the cancelling out of the term in common with the two symbols and the resulting ratio is between the two unique terms. + */ + +// Now we get to the three symbol operations, which there are 4 of *//, **/, *** and /// + +/* + these four operators work on the top of the stack in left to right order as the syntax of the definition lists it, though it is even possible to have the value from an earlier computation on the top of the stack. Ultimately all three will be multiplied together, so a * in a spot means it us used without changing. A / means its inverse will be used. + + "KMDBTC, BTCUSD, USDJPY, ***" <- this would create a KMDJPY synthetic. The various location of the / to make an inverse is to orient the raw symbol into the right orientation as the pricefeed is only getting one orientation of the ratio. + + So now we have covered all ways to map 1, 2 and 3 values on the top of the stack. A value can be on the top of the stack directly from a symbol, or as the result of some 1, 2 or 3 symbol operation. With a maximum stack depth of 3, it might require some experimentation to get a very complex synthetic to compile. Alternately, a stack deeper than 3 might be the acceptable solution if there are a family of synthetics that need it. + + At this point, it is time to describe the weights. It turns out that all above examples are incomplete and the synthetic descriptions are all insufficient and should generate an error. The reason is that the actual synthetic price is the value of the accumulator, which adds up all the weighted prices. In order to assign a weight to a price value on the stack, you simply use a number that is less than 2048. + + What such a weight number does, is consume the top of the stack, which also must be at depth of 1 and adds it to the accumulator. This allows combining multiple different synthetics into a meta synthetic, like for an aggregated index that is weighted by marketcap, or whatever other type of ratio trade that is desired. + + "BTCUSD, 1000, ETHBTC, BTCUSD, *, 300" -> that creates a dual index of BTCUSD and ETHUSD with a 30% ETH weight + + all weight operations consumes the one and only stack element and adds its weight to the accumulator, using this a very large number of terms can be all added together. Using inverses, allows to get the short direction into the equation mixed with longs, but all terms must be positive. If you want to create a spread between two synthetics, you need to create two different synthetics and go long with one and short with another. + + "BTCUSD, 1" with leverage -2 and "KMDBTC, BTCUSD, *, 1" with leverage 1 this will setup a +KMDUSD -2 BTCUSD spread when the two are combined, and would be at breakeven when KMDUSD gains 2x more percentage wise than BTC does. anytime KMD gains more, will be positive, if the gains are less, would be negative. + + Believe it or not, the string to binary compiler for synthetic description and interpretation of it is less than 200 lines of code. + + */ + // start of consensus code From dae0476a0adc89ca8c0ffca8412866af55a4cbee Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 6 Apr 2019 06:49:05 -1100 Subject: [PATCH 154/154] More docs --- src/cc/pegs.cpp | 10 ---------- src/cc/prices.cpp | 5 +++++ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/cc/pegs.cpp b/src/cc/pegs.cpp index dba9fc818..341e34c58 100644 --- a/src/cc/pegs.cpp +++ b/src/cc/pegs.cpp @@ -16,17 +16,7 @@ #include "CCPegs.h" /* - Pegs CC builds on top of CCOPRET modes which will create pricefeeds in the coinbase. - - flag&1: BTC/USD 4966.1400, BTC/GBP 3770.0402, BTC/EUR 4416.8452 GBPUSD 1.317264, EURUSD 1.124364 EURGBP 0.853560 - flag&2: (BGN 1.7462) (NZD 1.4771) (ILS 3.6258) (RUB 65.3700) (CAD 1.3320) (PHP 52.4160) (CHF 0.9995) (AUD 1.4122) (JPY 111.3660) (TRY 5.5483) (HKD 7.8498) (MYR 4.0816) (HRK 6.6337) (CZK 22.9937) (IDR 14220.0000) (DKK 6.6648) (NOK 8.6111) (HUF 287.2142) (GBP 0.7678) (MXN 19.1046) (THB 31.7500) (ISK 122.4107) (ZAR 14.1300) (BRL 3.8604) (SGD 1.3551) (PLN 3.8350) (INR 68.7531) (KRW 1136.3839) (RON 4.2516) (CNY 6.7200) (SEK 9.3230) (EUR 0.8928) (2019-04-02) -flag&4: (KMD 0.00025110) (ETH 0.03357500) (LTC 0.01642400) (BCHABC 0.05167400) (XMR 0.01398800) (IOTA 0.00007217) (DASH 0.02552600) (XEM 0.00001459) (ZEC 0.01440500) (WAVES 0.00062170) (RVN 0.00001215) (LSK 0.00041130) (DCR 0.00496000) (BTS 0.00001444) (ICX 0.00008750) (HOT 0.00000027) (STEEM 0.00010070) (ENJ 0.00003221) (STRAT 0.00022790) errs.0 - Funds deposited into CC address for a specific peg would then be used to fund the bid/ask as the pricefeed changes the price. Profits/losses would accumulate in the associated address. - - In the event funds exceed a specified level, it can be spent into a collection address. The idea is that the collection address can further be used for revshares. - - */ diff --git a/src/cc/prices.cpp b/src/cc/prices.cpp index 6e95af3af..ce257d33f 100644 --- a/src/cc/prices.cpp +++ b/src/cc/prices.cpp @@ -133,6 +133,11 @@ CBOPRET creates trustless oracles, which can be used for making a synthetic cash Believe it or not, the string to binary compiler for synthetic description and interpretation of it is less than 200 lines of code. + todo: complete all the above, bet tokens, cross chain prices within cluster These specific limits of 0.5%, 0.1%, 0.2%, 777 should be able to be changed based on #define + + Another configuration is to send the 0.4% (or 0.2%) fees to a fee collection scriptPubKey (this is not currently implemented, but would be needed for systems that dont use -ac_perc to collect a global override) this requires adding new vouts + + Modification: in the event there is one price in the accumulator and one price on the stack at the end, then it is a (A - B) spread */