Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge branch 'crude-taker-recovery2'
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-belcher committed Sep 23, 2015
2 parents f0724f8 + 79caac7 commit ac24975
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 133 deletions.
27 changes: 19 additions & 8 deletions lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import sys, datetime, json, time, pprint, threading, getpass
import random
import blockchaininterface, slowaes
from ConfigParser import SafeConfigParser, NoSectionError
from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
import os, io, itertools

JM_VERSION = 2
nickname = ''
DUST_THRESHOLD = 546
bc_interface = None
ordername_list = ["absorder", "relorder"]
maker_timeout_sec = 30

debug_file_lock = threading.Lock()
debug_file_handle = None
Expand Down Expand Up @@ -50,6 +51,7 @@
#port = 6697
#usessl = true
#socks5 = true
maker_timeout_sec = 30
[POLICY]
#for dust sweeping, try merge_algorithm = gradual
Expand All @@ -73,7 +75,13 @@ def load_program_config():
for o in v:
if o not in config.options(k):
raise Exception("Config file does not contain the required option: "+o)


try:
global maker_timeout_sec
maker_timeout_sec = config.getint('MESSAGING', 'maker_timeout_sec')
except NoOptionError:
debug('maker_timeout_sec not found in .cfg file, using default value')

#configure the interface to the blockchain on startup
global bc_interface
bc_interface = blockchaininterface.get_blockchain_interface_instance(config)
Expand Down Expand Up @@ -109,7 +117,7 @@ def rand_norm_array(mu, sigma, n):

def rand_exp_array(lamda, n):
#'lambda' is reserved (in case you are triggered by spelling errors)
return [random.expovariate(lamda) for i in range(n)]
return [random.expovariate(1.0 / lamda) for i in range(n)]

def rand_pow_array(power, n):
#rather crude in that uses a uniform sample which is a multiple of 1e-4
Expand Down Expand Up @@ -399,7 +407,6 @@ def get_utxos_by_mixdepth(self):
'''
returns a list of utxos sorted by different mix levels
'''
debug('get_utxos_by_mixdepth wallet.unspent = \n' + pprint.pformat(self.unspent))
mix_utxo_list = {}
for m in range(self.max_mix_depth):
mix_utxo_list[m] = {}
Expand All @@ -408,6 +415,7 @@ def get_utxos_by_mixdepth(self):
if mixdepth not in mix_utxo_list:
mix_utxo_list[mixdepth] = {}
mix_utxo_list[mixdepth][utxo] = addrvalue
debug('get_utxos_by_mixdepth = \n' + pprint.pformat(mix_utxo_list))
return mix_utxo_list

class BitcoinCoreWallet(AbstractWallet):
Expand Down Expand Up @@ -505,10 +513,11 @@ def pick_order(orders, n, feekey):
pickedOrderIndex = -1


def choose_order(db, cj_amount, n, chooseOrdersBy):
def choose_orders(db, cj_amount, n, chooseOrdersBy, ignored_makers=[]):
sqlorders = db.execute('SELECT * FROM orderbook;').fetchall()
orders = [(o['counterparty'], o['oid'], calc_cj_fee(o['ordertype'], o['cjfee'], cj_amount), o['txfee'])
for o in sqlorders if cj_amount >= o['minsize'] and cj_amount <= o['maxsize']]
for o in sqlorders if cj_amount >= o['minsize'] and cj_amount <= o['maxsize'] and o['counterparty']
not in ignored_makers]
counterparties = set([o[0] for o in orders])
if n > len(counterparties):
debug('ERROR not enough liquidity in the orderbook n=%d suitable-counterparties=%d amount=%d totalorders=%d'
Expand All @@ -527,7 +536,7 @@ def choose_order(db, cj_amount, n, chooseOrdersBy):
chosen_orders = [o[:2] for o in chosen_orders]
return dict(chosen_orders), total_cj_fee

def choose_sweep_order(db, my_total_input, my_tx_fee, n, chooseOrdersBy):
def choose_sweep_orders(db, my_total_input, my_tx_fee, n, chooseOrdersBy, ignored_makers=[]):
'''
choose an order given that we want to be left with no change
i.e. sweep an entire group of utxos
Expand Down Expand Up @@ -560,7 +569,8 @@ def is_amount_in_range(ordercombo, cjamount):

sqlorders = db.execute('SELECT * FROM orderbook;').fetchall()
orderkeys = ['counterparty', 'oid', 'ordertype', 'minsize', 'maxsize', 'txfee', 'cjfee']
orderlist = [dict([(k, o[k]) for k in orderkeys]) for o in sqlorders]
orderlist = [dict([(k, o[k]) for k in orderkeys]) for o in sqlorders
if o['counterparty'] not in ignored_makers]

ordercombos = [combo for combo in itertools.combinations(orderlist, n)]

Expand All @@ -582,3 +592,4 @@ def is_amount_in_range(ordercombo, cjamount):
debug('cj amount = ' + str(cjamount))
return orders, cjamount


24 changes: 17 additions & 7 deletions lib/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ def __pubmsg(self, message):
def __privmsg(self, nick, cmd, message):
debug('>>privmsg ' + 'nick=' + nick + ' cmd=' + cmd + ' msg=' + message)
#should we encrypt?
box = self.__get_encryption_box(cmd, nick)
box, encrypt = self.__get_encryption_box(cmd, nick)
#encrypt before chunking
if box:
if encrypt:
if not box:
debug('error, dont have encryption box object for ' + nick + ', dropping message')
return
message = enc_wrapper.encrypt_encode(message, box)

header = "PRIVMSG " + nick + " :"
Expand Down Expand Up @@ -302,9 +305,9 @@ def __get_encryption_box(self, cmd, nick):
to check which command strings correspond to which
type of object (maker/taker).''' #old doc, dont trust
if cmd in plaintext_commands:
return None
return None, False
else:
return self.cjpeer.get_crypto_box_from_nick(nick)
return self.cjpeer.get_crypto_box_from_nick(nick), True

def __handle_privmsg(self, source, target, message):
nick = get_irc_nick(source)
Expand All @@ -330,15 +333,22 @@ def __handle_privmsg(self, source, target, message):
self.built_privmsg[nick] = [cmd_string, message[:-2]]
else:
self.built_privmsg[nick][1] += message[:-2]
box = self.__get_encryption_box(self.built_privmsg[nick][0], nick)
box, encrypt = self.__get_encryption_box(self.built_privmsg[nick][0], nick)
if message[-1]==';':
self.waiting[nick]=True
elif message[-1]=='~':
self.waiting[nick]=False
if box:
if encrypt:
if not box:
debug('error, dont have encryption box object for ' + nick + ', dropping message')
return
#need to decrypt everything after the command string
to_decrypt = ''.join(self.built_privmsg[nick][1].split(' ')[1])
decrypted = enc_wrapper.decode_decrypt(to_decrypt, box)
try:
decrypted = enc_wrapper.decode_decrypt(to_decrypt, box)
except ValueError as e:
debug('valueerror when decrypting, skipping: ' + repr(e))
return
parsed = self.built_privmsg[nick][1].split(' ')[0] + ' ' + decrypted
else:
parsed = self.built_privmsg[nick][1]
Expand Down
Loading

0 comments on commit ac24975

Please sign in to comment.