Skip to content

Commit

Permalink
Merge pull request #201 from jl777/dev
Browse files Browse the repository at this point in the history
sync dev
  • Loading branch information
ca333 authored Aug 10, 2019
2 parents d4a257f + 4806405 commit 4ebe46a
Show file tree
Hide file tree
Showing 84 changed files with 6,542 additions and 929 deletions.
169 changes: 169 additions & 0 deletions qa/rpc-tests/nspv_client_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import sys
sys.path.append('../../src/tui')

from lib import tuilib
import unittest
import time

'''
specify chain ticker (daemon should be up), wif which will be imported and address to which you want to broadcast
change chain parameters if needed or add a new chain to test below
added 1 second sleep after each case to surely not face the nSPV server limitation (1 call/second)
'''

wif = ''
dest_address = ''
amount = '0.01'
chain = 'ILN'

if not wif or not dest_address:
raise Exception("Please set test wif and address to send transactions to")

rpc_proxy = tuilib.def_credentials(chain)

chain_params = {"KMD": {
'tx_list_address': 'RGShWG446Pv24CKzzxjA23obrzYwNbs1kA',
'min_chain_height': 1468080,
'notarization_height': '1468000',
'prev_notarization_h': 1467980,
'next_notarization_h': 1468020,
'hdrs_proof_low': '1468100',
'hdrs_proof_high': '1468200',
'numhdrs_expected': 151,
'tx_proof_id': 'f7beb36a65bc5bcbc9c8f398345aab7948160493955eb4a1f05da08c4ac3784f',
'tx_spent_height': 1456212,
'tx_proof_height': '1468520',
},
"ILN": {
'tx_list_address': 'RUp3xudmdTtxvaRnt3oq78FJBjotXy55uu',
'min_chain_height': 3689,
'notarization_height': '2000',
'prev_notarization_h': 1998,
'next_notarization_h': 2008,
'hdrs_proof_low': '2000',
'hdrs_proof_high': '2100',
'numhdrs_expected': 113,
'tx_proof_id': '67ffe0eaecd6081de04675c492a59090b573ee78955c4e8a85b8ac0be0e8e418',
'tx_spent_height': 2681,
'tx_proof_height': '2690',
}
}


class TestNspvClient(unittest.TestCase):

def test_nspv_mempool(self):
print("testing nspv_mempool")
result = rpc_proxy.nspv_mempool("0", dest_address, "0")
self.assertEqual(result["result"], "success")
self.assertEqual(result["address"], dest_address)
self.assertEqual(result["isCC"], 0)
time.sleep(1)

def test_nspv_listtransactions(self):
print("testing nspv_listtransactions")
rpc_proxy.nspv_login(wif)
result = rpc_proxy.nspv_listtransactions()
self.assertEqual(result["result"], "success")
time.sleep(1)
result = rpc_proxy.nspv_listtransactions(chain_params.get(chain).get("tx_list_address"))
self.assertEqual(result["result"], "success")
self.assertEqual(result["address"], chain_params.get(chain).get("tx_list_address"))
rpc_proxy.nspv_logout()

def test_nspv_getinfo(self):
print("testing nspv_getinfo")
result = rpc_proxy.nspv_getinfo()
self.assertEqual(result["result"], "success")
self.assertGreater(result["height"], chain_params.get(chain).get("min_chain_height"))
time.sleep(1)

def test_nspv_notarizations(self):
print("testing nspv_notarizations")
result = rpc_proxy.nspv_notarizations(chain_params.get(chain).get("notarization_height"))
self.assertEqual(result["result"], "success")
self.assertEqual(result["prev"]["notarized_height"], chain_params.get(chain).get("prev_notarization_h"))
self.assertEqual(result["next"]["notarized_height"], chain_params.get(chain).get("next_notarization_h"))
time.sleep(1)

def test_nspv_hdrsproof(self):
print("testing nspv_hdrsproof")
result = rpc_proxy.nspv_hdrsproof(chain_params.get(chain).get("hdrs_proof_low"),
chain_params.get(chain).get("hdrs_proof_high"))
self.assertEqual(result["result"], "success")
self.assertEqual(result["numhdrs"], chain_params.get(chain).get("numhdrs_expected"))
time.sleep(1)

def test_nspv_login(self):
print("testing nspv_login")
result = rpc_proxy.nspv_login(wif)
self.assertEqual(result["result"], "success")
self.assertEqual(result["status"], "wif will expire in 777 seconds")
time.sleep(1)

def test_nspv_listunspent(self):
print("testing nspv_listunspent")
result = rpc_proxy.nspv_listunspent()
self.assertEqual(result["result"], "success")
time.sleep(1)
result = rpc_proxy.nspv_listunspent(chain_params.get(chain).get("tx_list_address"))
self.assertEqual(result["result"], "success")
self.assertEqual(result["address"], chain_params.get(chain).get("tx_list_address"))

def test_nspv_spend(self):
print("testing nspv_spend")
result = rpc_proxy.nspv_login(wif)
result = rpc_proxy.nspv_spend(dest_address, amount)
self.assertEqual(result["result"], "success")
self.assertEqual(result["vout"][0]["valueZat"], 1000000)
time.sleep(1)

def test_nspv_broadcast(self):
print("testing nspv_broadcast")
result = rpc_proxy.nspv_login(wif)
broadcast_hex = rpc_proxy.nspv_spend(dest_address, amount)["hex"]
time.sleep(1)
result = rpc_proxy.nspv_broadcast(broadcast_hex)
self.assertEqual(result["result"], "success")
self.assertEqual(result["retcode"], 1)
self.assertEqual(result["expected"], result["broadcast"])
print("Broadcast txid: " + result["broadcast"])
time.sleep(1)

def test_nspv_logout(self):
print("testing nspv_logout")
rpc_proxy.nspv_login(wif)
time.sleep(1)
rpc_proxy.nspv_logout()
time.sleep(1)
result = rpc_proxy.nspv_spend(dest_address, amount)
self.assertEqual(result["result"], "error")
self.assertEqual(result["error"], "wif expired")
time.sleep(1)

def test_nspv_spentinfo(self):
print("testing nspv_spent_info")
result = rpc_proxy.nspv_spentinfo(chain_params.get(chain).get("tx_proof_id"), "1")
self.assertEqual(result["result"], "success")
self.assertEqual(result["spentheight"], chain_params.get(chain).get("tx_spent_height"))
time.sleep(1)

def test_nspv_txproof(self):
print("testing nspv_txproof")
result = rpc_proxy.nspv_txproof(chain_params.get(chain).get("tx_proof_id"),
chain_params.get(chain).get("tx_proof_height"))
self.assertEqual(result["txid"], chain_params.get(chain).get("tx_proof_id"))
time.sleep(1)

def test_nspv_login_timout(self):
print("testing auto-logout in 777 seconds")
rpc_proxy.nspv_login(wif)
time.sleep(778)
result = rpc_proxy.nspv_spend(dest_address, amount)
self.assertEqual(result["result"], "error")
self.assertEqual(result["error"], "wif expired")
time.sleep(1)


if __name__ == '__main__':
unittest.main()
3 changes: 2 additions & 1 deletion src/assetchains
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -eo pipefail
source pubkey.txt
overide_args="$@"
seed_ip=`getent hosts zero.kolo.supernet.org | awk '{ print $1 }'`
komodo_binary='./komodod'

if [ -z "$delay" ]; then delay=20; fi

Expand All @@ -14,6 +15,6 @@ if [ -z "$delay" ]; then delay=20; fi
gen=" -gen -genproclimit=1"
fi

./komodod $gen $args $overide_args -pubkey=$pubkey -addnode=$seed_ip &
$komodo_binary $gen $args $overide_args -pubkey=$pubkey -addnode=$seed_ip &
sleep $delay
done
4 changes: 3 additions & 1 deletion src/assetchains_stop
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -eo pipefail
komodo_cli='./komodo-cli'

./listassetchains | while read chain; do
./komodo-cli --ac_name=$chain stop
$komodo_cli --ac_name=$chain stop
done

19 changes: 17 additions & 2 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,34 @@ void komodo_passport_iteration();
uint64_t komodo_interestsum();
int32_t komodo_longestchain();
void komodo_cbopretupdate(int32_t forceflag);
CBlockIndex *komodo_chainactive(int32_t height);

void WaitForShutdown(boost::thread_group* threadGroup)
{
int32_t i; bool fShutdown = ShutdownRequested();
int32_t i,height; CBlockIndex *pindex; bool fShutdown = ShutdownRequested(); const uint256 zeroid;
// Tell the main threads to shutdown.
if (komodo_currentheight()>KOMODO_EARLYTXID_HEIGHT && KOMODO_EARLYTXID!=zeroid && ((height=tx_height(KOMODO_EARLYTXID))==0 || height>KOMODO_EARLYTXID_HEIGHT))
{
fprintf(stderr,"error: earlytx must be before block height %d or tx does not exist\n",KOMODO_EARLYTXID_HEIGHT);
StartShutdown();
}
if ( ASSETCHAINS_STAKED == 0 && ASSETCHAINS_ADAPTIVEPOW == 0 && (pindex= komodo_chainactive(1)) != 0 )
{
if ( pindex->nTime > ADAPTIVEPOW_CHANGETO_DEFAULTON )
{
ASSETCHAINS_ADAPTIVEPOW = 1;
fprintf(stderr,"default activate adaptivepow\n");
} else fprintf(stderr,"height1 time %u vs %u\n",pindex->nTime,ADAPTIVEPOW_CHANGETO_DEFAULTON);
} //else fprintf(stderr,"cant find height 1\n");
if ( ASSETCHAINS_CBOPRET != 0 )
komodo_pricesinit();
while (!fShutdown)
{
//fprintf(stderr,"call passport iteration\n");
if ( ASSETCHAINS_SYMBOL[0] == 0 )
{
komodo_passport_iteration();
if ( KOMODO_NSPV <= 0 )
komodo_passport_iteration();
for (i=0; i<10; i++)
{
fShutdown = ShutdownRequested();
Expand Down
1 change: 0 additions & 1 deletion src/cc/CCImportGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ std::string ImportGatewayWithdraw(uint64_t txfee,uint256 bindtxid,std::string re
std::string ImportGatewayPartialSign(uint64_t txfee,uint256 lasttxid,std::string refcoin, std::string hex);
std::string ImportGatewayCompleteSigning(uint64_t txfee,uint256 lasttxid,std::string refcoin,std::string hex);
std::string ImportGatewayMarkDone(uint64_t txfee,uint256 completetxid,std::string refcoin);
UniValue ImportGatewayPendingDeposits(uint256 bindtxid,std::string refcoin);
UniValue ImportGatewayPendingWithdraws(uint256 bindtxid,std::string refcoin);
UniValue ImportGatewayProcessedWithdraws(uint256 bindtxid,std::string refcoin);
UniValue ImportGatewayExternalAddress(uint256 bindtxid,CPubKey pubkey);
Expand Down
11 changes: 10 additions & 1 deletion src/cc/CCPegs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
bool PegsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn);

// CCcustom
UniValue PegsInfo();
std::string PegsCreate(uint64_t txfee,int64_t amount,std::vector<uint256> bindtxids);
std::string PegsFund(uint64_t txfee,uint256 pegstxid, uint256 tokenid, int64_t amount);
std::string PegsGet(uint64_t txfee,uint256 pegstxid, uint256 tokenid, int64_t amount);
std::string PegsRedeem(uint64_t txfee,uint256 pegstxid, uint256 tokenid);
std::string PegsLiquidate(uint64_t txfee,uint256 pegstxid, uint256 tokenid, uint256 liquidatetxid);
std::string PegsExchange(uint64_t txfee,uint256 pegstxid, uint256 tokenid, int64_t amount);
UniValue PegsAccountHistory(uint256 pegstxid);
UniValue PegsAccountInfo(uint256 pegstxid);
UniValue PegsWorstAccounts(uint256 pegstxid);
UniValue PegsInfo(uint256 pegstxid);

#endif
12 changes: 6 additions & 6 deletions src/cc/CCassetstx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ UniValue AssetOrders(uint256 refassetid, CPubKey pk, uint8_t additionalEvalCode)

txid = it->first.txhash;
LOGSTREAM("ccassets", CCLOG_DEBUG2, stream << "addOrders() checking txid=" << txid.GetHex() << std::endl);
if ( GetTransaction(txid, ordertx, hashBlock, false) != 0 )
if ( myGetTransaction(txid, ordertx, hashBlock) != 0 )
{
// for logging: funcid = DecodeAssetOpRet(vintx.vout[vintx.vout.size() - 1].scriptPubKey, evalCode, assetid, assetid2, price, origpubkey);
if (ordertx.vout.size() > 0 && (funcid = DecodeAssetTokenOpRet(ordertx.vout[ordertx.vout.size()-1].scriptPubKey, evalCode, assetid, assetid2, price, origpubkey)) != 0)
Expand Down Expand Up @@ -273,7 +273,7 @@ std::string CreateBuyOffer(int64_t txfee, int64_t bidamount, uint256 assetid, in
fprintf(stderr,"negative bidamount %lld, pricetotal %lld\n", (long long)bidamount, (long long)pricetotal);
return("");
}
if (GetTransaction(assetid, vintx, hashBlock, false) == 0)
if (myGetTransaction(assetid, vintx, hashBlock) == 0)
{
fprintf(stderr,"cant find assetid\n");
return("");
Expand Down Expand Up @@ -470,7 +470,7 @@ std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid)
if (AddNormalinputs(mtx, mypk, txfee, 3) > 0)
{
mask = ~((1LL << mtx.vin.size()) - 1);
if (GetTransaction(bidtxid, vintx, hashBlock, false) != 0)
if (myGetTransaction(bidtxid, vintx, hashBlock) != 0)
{
std::vector<uint8_t> vopretNonfungible;
GetNonfungibleData(assetid, vopretNonfungible);
Expand Down Expand Up @@ -520,7 +520,7 @@ std::string CancelSell(int64_t txfee,uint256 assetid,uint256 asktxid)
if (AddNormalinputs(mtx, mypk, txfee, 3) > 0)
{
mask = ~((1LL << mtx.vin.size()) - 1);
if (GetTransaction(asktxid, vintx, hashBlock, false) != 0)
if (myGetTransaction(asktxid, vintx, hashBlock) != 0)
{
std::vector<uint8_t> vopretNonfungible;
GetNonfungibleData(assetid, vopretNonfungible);
Expand Down Expand Up @@ -594,7 +594,7 @@ std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t f
if (AddNormalinputs(mtx, mypk, 2*txfee, 3) > 0)
{
mask = ~((1LL << mtx.vin.size()) - 1);
if (GetTransaction(bidtxid, vintx, hashBlock, false) != 0)
if (myGetTransaction(bidtxid, vintx, hashBlock) != 0)
{
bidamount = vintx.vout[bidvout].nValue;
SetAssetOrigpubkey(origpubkey, origprice, vintx);
Expand Down Expand Up @@ -697,7 +697,7 @@ std::string FillSell(int64_t txfee, uint256 assetid, uint256 assetid2, uint256 a
//if (AddNormalinputs(mtx, mypk, 2*txfee, 3) > 0)
//{
//mask = ~((1LL << mtx.vin.size()) - 1);
if (GetTransaction(asktxid, vintx, hashBlock, false) != 0)
if (myGetTransaction(asktxid, vintx, hashBlock) != 0)
{
orig_assetoshis = vintx.vout[askvout].nValue;
SetAssetOrigpubkey(origpubkey, total_nValue, vintx);
Expand Down
14 changes: 11 additions & 3 deletions src/cc/CCinclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ one other technical note is that komodod has the insight-explorer extensions bui
#include "../utlist.h"
#include "../uthash.h"
#include "merkleblock.h"
#include "../komodo_nSPV_defs.h"

#define CC_BURNPUBKEY "02deaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddead"
#define CC_MAXVINS 1024
#define CC_REQUIREMENTS_MSG (KOMODO_NSPV!=0?"to use CC contracts you need to nspv_login first\n":"to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n")

#define SMALLVAL 0.000000000000001
#define SATOSHIDEN ((uint64_t)100000000L)
Expand All @@ -79,6 +81,7 @@ one other technical note is that komodod has the insight-explorer extensions bui
OPRETID_CHANNELSDATA = 0x14,
OPRETID_HEIRDATA = 0x15,
OPRETID_ROGUEGAMEDATA = 0x16,
OPRETID_PEGSDATA = 0x17,

// non cc contract data:
OPRETID_FIRSTNONCCDATA = 0x80,
Expand Down Expand Up @@ -126,7 +129,7 @@ struct CCcontract_info

// the same for tokens 1of2 keys cc
char tokens1of2addr[64];
CPubKey tokens1of2pk[2];
CPubKey tokens1of2pk[2]; uint8_t tokens1of2priv[32];

// this is for spending from two additional 'unspendable' CC addresses of other eval codes
// (that is, for spending from several cc contract 'unspendable' addresses):
Expand All @@ -150,6 +153,7 @@ struct oracleprice_info
};

typedef std::vector<uint8_t> vscript_t;
extern struct NSPV_CCmtxinfo NSPV_U;

#ifdef ENABLE_WALLET
extern CWallet* pwalletMain;
Expand All @@ -172,12 +176,14 @@ static const uint256 zeroid;
static uint256 ignoretxid;
static int32_t ignorevin;
bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock);
bool NSPV_myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, int32_t &txheight, int32_t &currentheight);
int32_t is_hexstr(char *str,int32_t n);
bool myAddtomempool(CTransaction &tx, CValidationState *pstate = NULL, bool fSkipExpiry = false);
int32_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag);
int64_t CCgettxout(uint256 txid,int32_t vout,int32_t mempoolflag,int32_t lockflag);
bool myIsutxo_spentinmempool(uint256 &spenttxid,int32_t &spentvini,uint256 txid,int32_t vout);
bool mytxid_inmempool(uint256 txid);
int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout);
int32_t myGet_mempool_txs(std::vector<CTransaction> &txs,uint8_t evalcode,uint8_t funcid);
int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex);
int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp);
int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp);
Expand Down Expand Up @@ -249,7 +255,7 @@ CC *MakeTokensCCcond1(uint8_t evalcode, CPubKey pk);
CC *MakeTokensCCcond1(uint8_t evalcode, uint8_t evalcode2, CPubKey pk);
bool GetTokensCCaddress(struct CCcontract_info *cp, char *destaddr, CPubKey pk);
bool GetTokensCCaddress1of2(struct CCcontract_info *cp, char *destaddr, CPubKey pk, CPubKey pk2);
void CCaddrTokens1of2set(struct CCcontract_info *cp, CPubKey pk1, CPubKey pk2, char *coinaddr);
void CCaddrTokens1of2set(struct CCcontract_info *cp, CPubKey pk1, CPubKey pk2, uint8_t *priv, char *coinaddr);
int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode);

bool IsCCInput(CScript const& scriptSig);
Expand Down Expand Up @@ -286,9 +292,11 @@ extern std::vector<CPubKey> NULL_pubkeys;
std::string FinalizeCCTx(uint64_t skipmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret,std::vector<CPubKey> pubkeys = NULL_pubkeys);
void SetCCunspents(std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs,char *coinaddr,bool CCflag = true);
void SetCCtxids(std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,char *coinaddr,bool CCflag = true);
int64_t NSPV_AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs,struct NSPV_CCmtxinfo *ptr);
int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs);
int64_t AddNormalinputs2(CMutableTransaction &mtx,int64_t total,int32_t maxinputs);
int64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout,int32_t CCflag);
bool NSPV_SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey,uint32_t nTime);

// curve25519 and sha256
bits256 curve25519_shared(bits256 privkey,bits256 otherpub);
Expand Down
Loading

0 comments on commit 4ebe46a

Please sign in to comment.