Exemplo n.º 1
0
def test_counter():
    params = ((0, 2), (-1, 0), (1.1, 9))
    for i in params:
        my_counter = count(*i)
        of_counter = itertools.count(*i)
        for _ in range(10):
            assert next(my_counter) == next(of_counter)
Exemplo n.º 2
0
def easy_add_transaction(tx_orig, privkey='default'):
    tx = copy.deepcopy(tx_orig)
    if privkey in ['default', 'Default']:
        if tools.db_existence('privkey'):
            privkey = tools.db_get('privkey')
        else:
            return 'No private key is known, so the tx cannot be signed. '

    pubkey = tools.privtopub(privkey)
    address = tools.make_address([pubkey], 1)

    if 'count' not in tx:
        try:
            tx['count'] = tools.count(address, {})
        except:
            tx['count'] = 1

    # add our pubkey
    if 'pubkeys' not in tx:
        tx['pubkeys'] = [pubkey]

    # this is IMPORTANT
    # when adding new transaction which is signed by us,
    # this procedure is applied. tx without signatures is signed with our privkey.
    if 'signatures' not in tx:
        tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    return blockchain.add_tx(tx)
Exemplo n.º 3
0
def make_mint(pubkey):
    address = tools.make_address([pubkey], 1)
    return {
        'type': 'mint',
        'pubkeys': [pubkey],
        'signatures': ['first_sig'],
        'count': tools.count(address, custom.queues)
    }
Exemplo n.º 4
0
def easy_add_transaction(tx_orig, DB):
    tx = copy.deepcopy(tx_orig)
    if 'pubkeys' not in tx:
        tx['pubkeys']=[DB['pubkey']]
    try:
        tx['count'] = tools.count(DB['address'], DB)
    except:
        tx['count'] = 1
    tx['signatures'] = [tools.sign(tools.det_hash(tx), DB['privkey'])]
    return(blockchain.add_tx(tx, DB))
Exemplo n.º 5
0
def easy_add_transaction(tx_orig, DB):
    tx = copy.deepcopy(tx_orig)
    if 'pubkeys' not in tx:
        tx['pubkeys'] = [DB['pubkey']]
    try:
        tx['count'] = tools.count(DB['address'], DB)
    except:
        tx['count'] = 1
    tx['signatures'] = [tools.sign(tools.det_hash(tx), DB['privkey'])]
    return (blockchain.add_tx(tx, DB))
Exemplo n.º 6
0
def easy_add_transaction(tx_orig, DB, privkey='default'):
    tx = copy.deepcopy(tx_orig)
    try:
        tx['count'] = tools.count(DB['address'], DB)
    except:
        tx['count'] = 1
    if privkey=='default':
        if 'privkey' in DB:
            privkey=DB['privkey']
        else:
            return('no private key is known, so the tx cannot be signed. Here is the tx: \n'+str(tools.package(tx_orig).encode('base64').replace('\n', '')))
    if 'pubkeys' not in tx:
        tx['pubkeys']=[tools.privtopub(privkey)]
    tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    tools.log('collect winnings 3')
    return(blockchain.add_tx(tx, DB))
Exemplo n.º 7
0
def common_buy_shares(tx, num_states, brainwallet):
    privkey = tools.det_hash(brainwallet)
    pubkey = tools.privtopub(privkey)
    address = tools.make_address([pubkey], 1)
    tx['pubkeys'] = [pubkey]
    tx['count'] = tools.count(address, {})
    cost = txs_tools.cost_to_buy_shares(tx)
    tx['price_limit'] = int(cost * 1.01) + 1
    print(
        'now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.'
    )
    tx = tools.unpackage(tools.package(tx))
    tx = tools.POW(tx)
    tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    print('tx for copy/pasting into pushtx: ' +
          tools.package(tx).encode('base64'))
    return tx
Exemplo n.º 8
0
def trade_shares(DB, args):  #args = [ PM_id, buy ]
    privkey = tools.db_get('privkey')
    pubkey = tools.privtopub(privkey)
    address = tools.make_address([pubkey], 1)
    tx = {
        'type': 'buy_shares',
        'PM_id': args[0],
        'buy': csv2vec(args[1]),
        'pubkeys': [pubkey],
        'count': tools.count(address, {})
    }
    cost = txs_tools.cost_to_buy_shares(tx)
    tx['price_limit'] = int(cost * 1.01) + 1
    tx = tools.unpackage(tools.package(tx))
    tx = tools.POW(tx)
    tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    return easy_add_transaction(tx, DB, privkey)
Exemplo n.º 9
0
def easy_add_transaction(tx_orig, DB, privkey='default'):
    tx = copy.deepcopy(tx_orig)
    if privkey in ['default', 'Default']:
        if tools.db_existence('privkey'):
            privkey=tools.db_get('privkey')
        else:
            return('no private key is known, so the tx cannot be signed. Here is the tx: \n'+str(tools.package(tx_orig).encode('base64').replace('\n', '')))
    pubkey=tools.privtopub(privkey)
    address=tools.make_address([pubkey], 1)
    if 'count' not in tx:
        try:
            tx['count'] = tools.count(address, {})
        except:
            tx['count'] = 1
    if 'pubkeys' not in tx:
        tx['pubkeys']=[pubkey]
    if 'signatures' not in tx:
        tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    return(blockchain.add_tx(tx, DB))
Exemplo n.º 10
0
def easy_add_transaction(tx_orig, DB, privkey='default'):
    tx = copy.deepcopy(tx_orig)
    if privkey in ['default', 'Default']:
        if tools.db_existence('privkey'):
            privkey = tools.db_get('privkey')
        else:
            return ('no private key is known, so the tx cannot be signed. Here is the tx: \n' + str(
                tools.package(tx_orig).encode('base64').replace('\n', '')))
    pubkey = tools.privtopub(privkey)
    address = tools.make_address([pubkey], 1)
    if 'count' not in tx:
        try:
            tx['count'] = tools.count(address, {})
        except:
            tx['count'] = 1
    if 'pubkeys' not in tx:
        tx['pubkeys'] = [pubkey]
    if 'signatures' not in tx:
        tx['signatures'] = [tools.sign(tools.det_hash(tx), privkey)]
    return (blockchain.add_tx(tx, DB))
Exemplo n.º 11
0
def build_buy_shares():
    tx={'type':'buy_shares', 'PM_id':str(raw_input('What is the unique name for this prediction market?\n>'))}
    num_states=int(raw_input('how many states does this pm have?\n>'))
    tx['buy']=[]
    for i in range(num_states):
        tx['buy'].append(int(raw_input('how many shares do you want to buy of state '+str(i)+'? To sell states, use negative numbers.\n>')))
    brainwallet=str(raw_input('What is your brainwallet\n>'))
    privkey=tools.det_hash(brainwallet)
    pubkey=tools.privtopub(privkey)
    address=tools.make_address([pubkey], 1)
    tx['pubkeys']=[pubkey]
    tx['count'] = tools.count(address, {})
    cost=txs_tools.cost_to_buy_shares(tx)
    tx['price_limit']=int(cost*1.01)
    print('now for a little proof of work. This may take several minutes. The purpose of this pow is to make it more difficult for a front runner to steal your trade.')
    tx=tools.unpackage(tools.package(tx))
    tx=tools.POW(tx)
    tx['signatures']=[tools.sign(tools.det_hash(tx), privkey)]
    print('tx for copy/pasting into pushtx: '+tools.package(tx).encode('base64'))
    return tx
Exemplo n.º 12
0
 def verify_count(tx, txs):
     return tx['count'] != tools.count(address, DB)
Exemplo n.º 13
0
 def verify_count(tx, txs):
     return tx['count'] != tools.count(address, DB)
Exemplo n.º 14
0
def make_mint(pubkey, DB):
    address = tools.make_address([pubkey], 1)
    return {'type': 'mint',
            'pubkeys': [pubkey],
            'signatures': ['first_sig'],
            'count': tools.count(address, DB)}
Exemplo n.º 15
0
def test_cluesCorrect():
	
	types = {}	
	for app in apps:
		for t in apps[app]:
			tools.count(types, t)
	
	# check if all apps have a category
	assert types['cats'] == len(apps)

	# check if all categories listed in app are defined 
	assert (set(reduce(list.__add__, [apps[a]['cats'] for a in apps])) <= 
		set([int(x) for x in categories.keys()]))
			
	# check if only expected fields are defined for apps
	assert (set(types.keys()) == 
		set(['implies', 'script', 'scripts', 'url', 'cats', 'headers', 'html', 'meta', 'env', 'confidence']))

	types = {}	
	for t in [k for a in apps for k in apps[a]]:
		tools.count(types, t)
	logging.debug(types)
	# {u'confidence': 4, u'implies': 124, u'script': 100, u'url': 22, u'html': 148, u'headers': 88, u'cats': 380, u'meta': 97, u'env': 121}	

	# check if numbers of entries are as expected
	assert types['implies'] > 120
	assert types['script'] > 95
	assert types['scripts'] > 0
	assert types['url'] > 20
	assert types['headers'] > 80
	assert types['html'] > 140
	assert types['meta'] > 90
	assert types['env'] > 115
	
	# check if all implies are lists of unicodes, headers and meta are dictionaries of str/unicode,
	# and others (including app names) are str/unicode
	assert set([type(a) for a in apps]) <= set([str, unicode])
	assert set([type(apps[a]['script'])  for a in apps if apps[a].has_key('script') ]) <= set([str, unicode])
	assert set([type(apps[a]['url'])     for a in apps if apps[a].has_key('url')    ]) <= set([str, unicode])
	assert set([type(apps[a]['html'])    for a in apps if apps[a].has_key('html')   ]) <= set([str, unicode])
	assert set([type(apps[a]['env'])     for a in apps if apps[a].has_key('env')    ]) <= set([str, unicode])
	assert set([type(apps[a]['implies']) for a in apps if apps[a].has_key('implies')]) == set([list])
	assert set([type(apps[a]['meta'])    for a in apps if apps[a].has_key('meta')   ]) == set([dict])
	assert set([type(apps[a]['headers']) for a in apps if apps[a].has_key('headers')]) == set([dict])
	assert set([
			type(v) 
			for a in apps if apps[a].has_key('implies') 
			for v in apps[a]['implies']]) <= set([str, unicode])	
	assert set([
			type(x) 
			for a in apps if apps[a].has_key('headers') 
			for k in apps[a]['headers'] 
			for x in [k, apps[a]['headers'][k]]]) <= set([str, unicode])
	assert set([
			type(x) 
			for a in apps if apps[a].has_key('meta') 
			for k in apps[a]['meta'] 
			for x in [k, apps[a]['meta'][k]]]) <= set([str, unicode])

	for h in [apps[a]["headers"] for a in apps if apps[a].has_key("headers")]:
		for k in h:
			logging.debug(type(k), k)
				
	# check if all 'implies' references exist
	assert set(reduce(list.__add__, [apps[a]['implies'] 
			for a in apps if apps[a].has_key('implies')])) - set(apps.keys()) == set()