def test_data_feeds(): k, v, k2, v2 = accounts() scode3 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[1001] = msg.sender return(0) elif msg.sender == contract.storage[1001] and msg.datasize == 2: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(contract.storage[msg.data[0]]) ''' code3 = serpent_compile(scode3) logger.debug("AST", serpent.rewrite(serpent.parse(scode3))) blk = b.genesis({v: 10**18, v2: 10**18}) tx10 = t.contract(0, gasprice, startgas, 0, code3).sign(k) s, addr = pb.apply_transaction(blk, tx10) tx11 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx11) tx12 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx12) assert serpent.decode_datalist(o) == [0] tx13 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 726])).sign(k) s, o = pb.apply_transaction(blk, tx13) assert serpent.decode_datalist(o) == [1] tx14 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx14) assert serpent.decode_datalist(o) == [726] return blk, addr
def test_data_feeds(): k, v, k2, v2 = accounts() scode3 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[1001] = msg.sender return(0) elif msg.sender == contract.storage[1001] and msg.datasize == 2: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(contract.storage[msg.data[0]]) ''' code3 = serpent.compile(scode3) logger.debug("AST", serpent.rewrite(serpent.parse(scode3))) blk = b.genesis({v: 10 ** 18, v2: 10 ** 18}) tx10 = t.contract(0, gasprice, startgas, 0, code3).sign(k) s, addr = pb.apply_transaction(blk, tx10) tx11 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx11) tx12 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx12) assert serpent.decode_datalist(o) == [0] tx13 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 726])).sign(k) s, o = pb.apply_transaction(blk, tx13) assert serpent.decode_datalist(o) == [1] tx14 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx14) assert serpent.decode_datalist(o) == [726] return blk, addr
def test_namecoin(): k, v, k2, v2 = accounts() blk = b.genesis({v: u.denoms.ether * 1}) code1 = serpent.compile(namecoin_code) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_transaction(blk, tx1) snapshot = blk.snapshot() # tx2 tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_transaction(blk, tx2) assert serpent.decode_datalist(o) == [1] # tx3 tx3 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 20])).sign(k) s, o = pb.apply_transaction(blk, tx3) assert serpent.decode_datalist(o) == [0] # tx4 tx4 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist(['harry', 60])).sign(k) s, o = pb.apply_transaction(blk, tx4) assert serpent.decode_datalist(o) == [1] blk.revert(snapshot) assert blk.to_dict() assert blk.to_dict()['state'][addr]['code']
def test_namecoin(): k, v, k2, v2 = accounts() blk = b.genesis({v: u.denoms.ether * 1}) code1 = serpent_compile(namecoin_code) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_transaction(blk, tx1) snapshot = blk.snapshot() # tx2 tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_transaction(blk, tx2) assert serpent.decode_datalist(o) == [1] # tx3 tx3 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 20])).sign(k) s, o = pb.apply_transaction(blk, tx3) assert serpent.decode_datalist(o) == [0] # tx4 tx4 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist(['harry', 60])).sign(k) s, o = pb.apply_transaction(blk, tx4) assert serpent.decode_datalist(o) == [1] blk.revert(snapshot) assert blk.to_dict() assert blk.to_dict()['state'][addr]['code']
def tag_content(content_hash, tag, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_tag = transactions.Transaction( nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([2, content_hash, tag])).sign(key) ans = processblock.apply_tx(genesis, tx_tag)
def send_money(to, amount, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_money = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([to, amount ])).sign(key) ans = processblock.apply_tx(genesis, tx_money)
def gen_test(code, val, data): while 1: s = t.state(1) c = s.contract(code) pre = s.block.to_dict()['state'] env = { "currentCoinbase": s.block.coinbase, "currentDifficulty": str(s.block.difficulty), "currentGasLimit": str(s.block.gas_limit), "currentNumber": str(s.block.number), "currentTimestamp": str(s.block.timestamp), "previousHash": s.block.prevhash.encode('hex') } apply_message_calls = [] orig_apply_msg = pb.apply_msg def apply_msg_wrapper(_block, _tx, msg, code): pb.enable_debug() apply_message_calls.append( dict(gasLimit=msg.gas, value=msg.value, desgination=msg.to, data=msg.data.encode('hex'))) result, gas_rem, data = orig_apply_msg(_block, _tx, msg, code) pb.disable_debug() return result, gas_rem, data pb.apply_msg = apply_msg_wrapper d = serpent.encode_datalist(map(int, data)) tx = pyethereum.transactions.Transaction(1, 10**12, 10000, c, val, data)\ .sign(t.keys[0]) msg = pb.Message(t.accounts[0], c, val, 10000, d) exek = { "address": msg.to, "caller": msg.sender, "code": '0x' + s.block.get_code(c).encode('hex'), "data": '0x' + d.encode('hex'), "gas": str(10000), "gasPrice": str(10**12), "origin": tx.sender, "value": str(val) } success, gas, o = pb.apply_msg(s.block, tx, msg, s.block.get_code(c)) post = s.block.to_dict()['state'] callcreates = apply_message_calls[1:] if success: break return { "callcreates": callcreates, "env": env, "pre": pre, "post": post, "exec": exek, "gas": str(gas), "out": '0x' + ''.join(map(chr, o)).encode('hex') }
def register_name(name, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) # register eth-address to a name. recover name from blockchain. names are not unique. but names + first 4 digits of address probably are.... tx_register_name = transactions.Transaction( nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([5, name])).sign(key) ans = processblock.apply_tx(genesis, tx_register_name)
def __init__(self, to, donate_to, value=0): self.to = to self.value = value self.donate_to = donate_to self.data = serpent.encode_datalist([self._data_code(), donate_to]) self.tx = lambda user: transactions.Transaction( NonceSingleton.inc(user), self.DEFAULT_GASPRICE, self. DEFAULT_STARTGAS, to, value, self.data)
def vote_tag(content_hash, tag, vote, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) #vote on a tag. tx_vote = transactions.Transaction( nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([3, content_hash, tag, vote])).sign(key) ans = processblock.apply_tx(genesis, tx_vote)
def tx(self, frm, to, value, data, gas=STARTGAS): _tx = transactions.Transaction(nonce=self.nonce[frm], gasprice=self.GASPRICE, startgas=gas, to=to, value=value, data=serpent.encode_datalist(data)).sign(frm.key) result, ans = processblock.apply_transaction(self.genesis, _tx) assert result self.nonce[frm] += 1 return serpent.decode_datalist(ans)
def test_currency(): k, v, k2, v2 = accounts() scode2 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[0x%s] = 1000 return(1) elif msg.datasize == 1: addr = msg.data[0] return(contract.storage[addr]) else: from = msg.sender fromvalue = contract.storage[from] to = msg.data[0] value = msg.data[1] if fromvalue >= value: contract.storage[from] = fromvalue - value contract.storage[to] = contract.storage[to] + value return(1) else: return(0) ''' % v code2 = serpent.compile(scode2) blk = b.genesis({v: 10 ** 18}) tx4 = t.contract(0, gasprice, startgas, 0, code2).sign(k) s, addr = pb.apply_transaction(blk, tx4) tx5 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx5) assert serpent.decode_datalist(o) == [1] tx6 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 200])).sign(k) s, o = pb.apply_transaction(blk, tx6) assert serpent.decode_datalist(o) == [1] tx7 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 900])).sign(k) s, o = pb.apply_transaction(blk, tx7) assert serpent.decode_datalist(o) == [0] tx8 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([v])).sign(k) s, o = pb.apply_transaction(blk, tx8) assert serpent.decode_datalist(o) == [800] tx9 = t.Transaction(5, gasprice, startgas, addr, 0, serpent.encode_datalist([v2])).sign(k) s, o = pb.apply_transaction(blk, tx9) assert serpent.decode_datalist(o) == [200]
def test_currency(): k, v, k2, v2 = accounts() scode2 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[0x%s] = 1000 return(1) elif msg.datasize == 1: addr = msg.data[0] return(contract.storage[addr]) else: from = msg.sender fromvalue = contract.storage[from] to = msg.data[0] value = msg.data[1] if fromvalue >= value: contract.storage[from] = fromvalue - value contract.storage[to] = contract.storage[to] + value return(1) else: return(0) ''' % v code2 = serpent_compile(scode2) blk = b.genesis({v: 10**18}) tx4 = t.contract(0, gasprice, startgas, 0, code2).sign(k) s, addr = pb.apply_transaction(blk, tx4) tx5 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx5) assert serpent.decode_datalist(o) == [1] tx6 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 200])).sign(k) s, o = pb.apply_transaction(blk, tx6) assert serpent.decode_datalist(o) == [1] tx7 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 900])).sign(k) s, o = pb.apply_transaction(blk, tx7) assert serpent.decode_datalist(o) == [0] tx8 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([v])).sign(k) s, o = pb.apply_transaction(blk, tx8) assert serpent.decode_datalist(o) == [800] tx9 = t.Transaction(5, gasprice, startgas, addr, 0, serpent.encode_datalist([v2])).sign(k) s, o = pb.apply_transaction(blk, tx9) assert serpent.decode_datalist(o) == [200]
def gen_test(code, val, data): while 1: s = t.state(1) c = s.contract(code) pre = s.block.to_dict()['state'] env = { "currentCoinbase": s.block.coinbase, "currentDifficulty": str(s.block.difficulty), "currentGasLimit": str(s.block.gas_limit), "currentNumber": str(s.block.number), "currentTimestamp": str(s.block.timestamp), "previousHash": s.block.prevhash.encode('hex') } apply_message_calls = [] orig_apply_msg = pb.apply_msg def apply_msg_wrapper(_block, _tx, msg, code): pb.enable_debug() apply_message_calls.append(dict(gasLimit=msg.gas, value=msg.value, desgination=msg.to, data=msg.data.encode('hex'))) result, gas_rem, data = orig_apply_msg(_block, _tx, msg, code) pb.disable_debug() return result, gas_rem, data pb.apply_msg = apply_msg_wrapper d = serpent.encode_datalist(map(int, data)) tx = pyethereum.transactions.Transaction(1, 10**12, 10000, c, val, data)\ .sign(t.keys[0]) msg = pb.Message(t.accounts[0], c, val, 10000, d) exek = { "address": msg.to, "caller": msg.sender, "code": '0x'+s.block.get_code(c).encode('hex'), "data": '0x'+d.encode('hex'), "gas": str(10000), "gasPrice": str(10**12), "origin": tx.sender, "value": str(val) } success, gas, o = pb.apply_msg(s.block, tx, msg, s.block.get_code(c)) post = s.block.to_dict()['state'] callcreates = apply_message_calls[1:] if success: break return { "callcreates": callcreates, "env": env, "pre": pre, "post": post, "exec": exek, "gas": str(gas), "out": '0x'+''.join(map(chr, o)).encode('hex') }
def _add_dc_to_dcp(self, to_addr, from_addr, sig, msghash, msgsize, data): assert(self.verify_dc_sig(sig, msghash, msgsize, from_addr, data)) tx = self.dcp_list[to_addr] txdata = serpent.decode_datalist(tx.data) txdata[0] += 1 #increment dc count #append dc data to dcp tx strings = byte_arrays_to_string(data) # data contains from_addr, msg_size, [args] map(txdata.append, strings) self.dcp_list[to_addr].data = serpent.encode_datalist(txdata) self.dcp_list[to_addr].sign(privkey)
def init_dcp(self, addr): nonce = self.miner.block.get_nonce(self_addr) #self.config.get('wallet', 'coinbase')) print 'init nonce for dcp', nonce GASPRICE = 100 print 'addr!' print addr new_dcp = Transaction(nonce, GASPRICE, 10**5, addr, 0, serpent.encode_datalist([0])).sign(privkey) #encode datalist... print 'sender', new_dcp.sender print 'self addr', self_addr self.dcp_list[addr] = new_dcp
def _add_dc_to_dcp(self, to_addr, from_addr, sig, msghash, msgsize, data): assert (self.verify_dc_sig(sig, msghash, msgsize, from_addr, data)) tx = self.dcp_list[to_addr] txdata = serpent.decode_datalist(tx.data) txdata[0] += 1 #increment dc count #append dc data to dcp tx strings = byte_arrays_to_string( data) # data contains from_addr, msg_size, [args] map(txdata.append, strings) self.dcp_list[to_addr].data = serpent.encode_datalist(txdata) self.dcp_list[to_addr].sign(privkey)
def transact(self, dest, secret=constants.DEFAULT_KEY, data="", gas=constants.DEFAULT_GAS, gas_price=constants.GAS_PRICE, value=0): if data: data = "0x" + serpent.encode_datalist(data).encode('hex') params = { 'aDest': dest, 'bData': data, 'sec': secret, 'xGas': hex(gas), 'xGasPrice': hex(gas_price), 'xValue': hex(value)} return self._rpc_post('transact', params)
def get_all_content(genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_v = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([7, 'kjsdhg'])).sign(key) ans = processblock.apply_tx(genesis, tx_v) decoded = serpent.decode_datalist(ans) hexd = map(hex, decoded) f = lambda x : x[2:-1] hexd = map(f, hexd) f = lambda x: x.decode('hex') hexd = map(f, hexd) return hexd
def __init__(self, to, donate_to, value=0): self.to = to self.value = value self.donate_to = donate_to self.data = serpent.encode_datalist([self._data_code(), donate_to]) self.tx = lambda user: transactions.Transaction(NonceSingleton.inc(user), self.DEFAULT_GASPRICE, self.DEFAULT_STARTGAS, to, value, self.data)
def init_system(genesis, key): code = serpent.compile(open('serp/root.se').read()) tx_make_root = transactions.Transaction.contract(0, 0, 10**12, 10000, code).sign(key) root_contract = processblock.apply_tx(genesis, tx_make_root) root_hash = root_contract.encode('hex') f = lambda x: write_owner(root_hash, x) map(f, ['serp/data.se', 'serp/tag.se', 'serp/users.se', 'serp/currency.se']) code = serpent.compile(open('serp/data.se').read()) tx_make_data = transactions.Transaction.contract(1, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/tag.se').read()) tx_make_tag = transactions.Transaction.contract(2, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/users.se').read()) tx_make_users = transactions.Transaction.contract(3, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/currency.se').read()) tx_make_currency = transactions.Transaction.contract( 4, 0, 10**12, 10000, code).sign(key) data_contract = processblock.apply_tx(genesis, tx_make_data) tag_contract = processblock.apply_tx(genesis, tx_make_tag) users_contract = processblock.apply_tx(genesis, tx_make_users) currency_contract = processblock.apply_tx(genesis, tx_make_currency) d_contract = data_contract.encode('hex') t_contract = tag_contract.encode('hex') u_contract = users_contract.encode('hex') c_contract = currency_contract.encode('hex') #init root tx_init_root = transactions.Transaction( 5, 0, 10**12, 10000, root_contract, serpent.encode_datalist( [d_contract, t_contract, u_contract, c_contract])).sign(key) ans = processblock.apply_tx(genesis, tx_init_root) adresses = { root_hash: 'root', d_contract: 'data', t_contract: 'tags', u_contract: 'users', utils.privtoaddr(key): 'me', c_contract: 'currency' } return adresses
def init_dcp(self, addr): nonce = self.miner.block.get_nonce( self_addr) #self.config.get('wallet', 'coinbase')) print 'init nonce for dcp', nonce GASPRICE = 100 print 'addr!' print addr new_dcp = Transaction(nonce, GASPRICE, 10**5, addr, 0, serpent.encode_datalist([0])).sign( privkey) #encode datalist... print 'sender', new_dcp.sender print 'self addr', self_addr self.dcp_list[addr] = new_dcp
def push_content(content, title, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) content_hash = utils.sha3(content) # push a transaction with a title. recover title from blockchain tx_push = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([1, content_hash, title])).sign(key) ans = processblock.apply_tx(genesis, tx_push) f = open('data/%s'%content_hash.encode('hex'), 'w') f.write(content) f.close() return content_hash
def tx(self, frm, to, value, data, gas=STARTGAS): _tx = transactions.Transaction(nonce=self.nonce[frm], gasprice=self.GASPRICE, startgas=gas, to=to, value=value, data=encode_datalist(data)).sign( frm.key) result, ans = processblock.apply_transaction(self.genesis, _tx) assert result self.nonce[frm] += 1 return decode_datalist(ans)
def get_all_content(genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_v = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([7, 'kjsdhg' ])).sign(key) ans = processblock.apply_tx(genesis, tx_v) decoded = serpent.decode_datalist(ans) hexd = map(hex, decoded) f = lambda x: x[2:-1] hexd = map(f, hexd) f = lambda x: x.decode('hex') hexd = map(f, hexd) return hexd
def push_content(content, title, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) content_hash = utils.sha3(content) # push a transaction with a title. recover title from blockchain tx_push = transactions.Transaction( nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([1, content_hash, title])).sign(key) ans = processblock.apply_tx(genesis, tx_push) f = open('data/%s' % content_hash.encode('hex'), 'w') f.write(content) f.close() return content_hash
def init_system(genesis, key): code = serpent.compile(open('serp/root.se').read()) tx_make_root = transactions.Transaction.contract(0,0,10**12, 10000, code).sign(key) root_contract = processblock.apply_tx(genesis, tx_make_root) root_hash = root_contract.encode('hex') f = lambda x: write_owner(root_hash, x) map(f, ['serp/data.se', 'serp/tag.se', 'serp/users.se', 'serp/currency.se']) code = serpent.compile(open('serp/data.se').read()) tx_make_data = transactions.Transaction.contract(1,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/tag.se').read()) tx_make_tag = transactions.Transaction.contract(2,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/users.se').read()) tx_make_users = transactions.Transaction.contract(3,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/currency.se').read()) tx_make_currency = transactions.Transaction.contract(4,0,10**12, 10000, code).sign(key) data_contract = processblock.apply_tx(genesis, tx_make_data) tag_contract = processblock.apply_tx(genesis, tx_make_tag) users_contract = processblock.apply_tx(genesis, tx_make_users) currency_contract = processblock.apply_tx(genesis, tx_make_currency) d_contract = data_contract.encode('hex') t_contract = tag_contract.encode('hex') u_contract = users_contract.encode('hex') c_contract = currency_contract.encode('hex') #init root tx_init_root = transactions.Transaction(5, 0, 10**12, 10000, root_contract, serpent.encode_datalist([d_contract, t_contract, u_contract, c_contract])).sign(key) ans = processblock.apply_tx(genesis, tx_init_root) adresses = {root_hash:'root', d_contract:'data', t_contract:'tags', u_contract:'users', utils.privtoaddr(key):'me', c_contract:'currency'} return adresses
def test_hedge(): k, v, k2, v2 = accounts() blk, addr = test_data_feeds() scode4 = ''' if !contract.storage[1000]: contract.storage[1000] = msg.sender contract.storage[1002] = msg.value contract.storage[1003] = msg.data[0] return(1) elif !contract.storage[1001]: ethvalue = contract.storage[1002] if msg.value >= ethvalue: contract.storage[1001] = msg.sender othervalue = ethvalue * call(0x%s,[contract.storage[1003]],1) contract.storage[1004] = othervalue contract.storage[1005] = block.timestamp + 86400 return([2,othervalue],2) else: othervalue = contract.storage[1004] ethvalue = othervalue / call(0x%s,contract.storage[1003]) if ethvalue >= contract.balance: send(contract.storage[1000],contract.balance) return(3) elif block.timestamp > contract.storage[1005]: send(contract.storage[1001],contract.balance - ethvalue) send(contract.storage[1000],ethvalue) return(4) else: return(5) ''' % (addr, addr) code4 = serpent.compile(scode4) logger.debug("AST", serpent.rewrite(serpent.parse(scode4))) # important: no new genesis block tx15 = t.contract(5, gasprice, startgas, 0, code4).sign(k) s, addr2 = pb.apply_transaction(blk, tx15) tx16 = t.Transaction(6, gasprice, startgas, addr2, 10 ** 17, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx16) assert serpent.decode_datalist(o) == [1] tx17 = t.Transaction(0, gasprice, startgas, addr2, 10 ** 17, serpent.encode_datalist([500])).sign(k2) s, o = pb.apply_transaction(blk, tx17) assert serpent.decode_datalist(o) == [2, 72600000000000000000L] snapshot = blk.snapshot() tx18 = t.Transaction(7, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx18) assert serpent.decode_datalist(o) == [5] tx19 = t.Transaction(8, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 300])).sign(k) s, o = pb.apply_transaction(blk, tx19) assert serpent.decode_datalist(o) == [1] tx20 = t.Transaction(9, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx20) assert serpent.decode_datalist(o) == [3] blk.revert(snapshot) blk.timestamp += 200000 tx21 = t.Transaction(7, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 1452])).sign(k) s, o = pb.apply_transaction(blk, tx21) assert serpent.decode_datalist(o) == [1] tx22 = t.Transaction(8, gasprice, 2000, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx22) assert serpent.decode_datalist(o) == [4]
def tag_content(content_hash, tag, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_tag = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([2, content_hash, tag])).sign(key) ans = processblock.apply_tx(genesis, tx_tag)
def register_name(name, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) # register eth-address to a name. recover name from blockchain. names are not unique. but names + first 4 digits of address probably are.... tx_register_name = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([5, name])).sign(key) ans = processblock.apply_tx(genesis, tx_register_name)
def test_hedge(): k, v, k2, v2 = accounts() blk, addr = test_data_feeds() scode4 = ''' if !contract.storage[1000]: contract.storage[1000] = msg.sender contract.storage[1002] = msg.value contract.storage[1003] = msg.data[0] return(1) elif !contract.storage[1001]: ethvalue = contract.storage[1002] if msg.value >= ethvalue: contract.storage[1001] = msg.sender othervalue = ethvalue * call(0x%s,[contract.storage[1003]],1) contract.storage[1004] = othervalue contract.storage[1005] = block.timestamp + 86400 return([2,othervalue],2) else: othervalue = contract.storage[1004] ethvalue = othervalue / call(0x%s,contract.storage[1003]) if ethvalue >= contract.balance: send(contract.storage[1000],contract.balance) return(3) elif block.timestamp > contract.storage[1005]: send(contract.storage[1001],contract.balance - ethvalue) send(contract.storage[1000],ethvalue) return(4) else: return(5) ''' % (addr, addr) code4 = serpent_compile(scode4) logger.debug("AST", serpent.rewrite(serpent.parse(scode4))) # important: no new genesis block tx15 = t.contract(5, gasprice, startgas, 0, code4).sign(k) s, addr2 = pb.apply_transaction(blk, tx15) tx16 = t.Transaction(6, gasprice, startgas, addr2, 10**17, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx16) assert serpent.decode_datalist(o) == [1] tx17 = t.Transaction(0, gasprice, startgas, addr2, 10**17, serpent.encode_datalist([500])).sign(k2) s, o = pb.apply_transaction(blk, tx17) assert serpent.decode_datalist(o) == [2, 72600000000000000000L] snapshot = blk.snapshot() tx18 = t.Transaction(7, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx18) assert serpent.decode_datalist(o) == [5] tx19 = t.Transaction(8, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 300])).sign(k) s, o = pb.apply_transaction(blk, tx19) assert serpent.decode_datalist(o) == [1] tx20 = t.Transaction(9, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx20) assert serpent.decode_datalist(o) == [3] blk.revert(snapshot) blk.timestamp += 200000 tx21 = t.Transaction(7, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 1452])).sign(k) s, o = pb.apply_transaction(blk, tx21) assert serpent.decode_datalist(o) == [1] tx22 = t.Transaction(8, gasprice, 2000, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx22) assert serpent.decode_datalist(o) == [4]
def send_money(to, amount, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) tx_money = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([to, amount])).sign(key) ans = processblock.apply_tx(genesis, tx_money)
def vote_tag(content_hash, tag, vote, genesis, root_contract, usr): key, addr = usr nonce = get_nonce(genesis, addr) #vote on a tag. tx_vote = transactions.Transaction(nonce, 0, 10**12, 10000, root_contract, serpent.encode_datalist([3, content_hash, tag, vote])).sign(key) ans = processblock.apply_tx(genesis, tx_vote)
blk = b.genesis({v: u.denoms.ether * 1}) scode1 = ''' if !contract.storage[msg.data[0]]: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(0) ''' code1 = serpent.compile(scode1) # print("AST", serpent.rewrite(serpent.parse(scode1))) # print("Assembly", serpent.compile_to_assembly(scode1)) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_tx(blk, tx1) snapshot = blk.snapshot() print("Contract address", addr) tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_tx(blk, tx2) print("Result of registering george:45: ", serpent.decode_datalist(o)) assert serpent.decode_datalist(o) == [1] tx3 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 20])).sign(k) s, o = pb.apply_tx(blk, tx3) print("Result of registering george:20: ", serpent.decode_datalist(o)) assert serpent.decode_datalist(o) == [0] tx4 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist(['harry', 60])).sign(k) s, o = pb.apply_tx(blk, tx4) print("Result of registering harry:60: ", serpent.decode_datalist(o)) assert serpent.decode_datalist(o) == [1] scode2 = ''' if !contract.storage[1000]:
code = serpent.assemble(assembly) print code msg_hash = utils.sha3('heres a message') v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pub = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pub) print verified tx_make_root = transactions.contract(0,10,10**30, 10**30, code).sign(key) success, root_contract = processblock.apply_tx(gen, tx_make_root) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([msg_hash, v, r, s])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist(['hi', 'bye'])).sign(key) tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([2, '139dcd5cc79e260272e05147c349ab5f2db3f102', 1])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([2, 1])).sign(key) print assembly success, ans = processblock.apply_tx(gen, tx_init_root) print ans data = serpent.decode_datalist(ans) print data print hex(data[0]) quit() print ans.encode('hex') data = serpent.decode_datalist(ans) print 'raw decoded data:', data print 'data as hex:' print map(hex, data) #print ('%02x'%data).decode('hex') print assembly
nargs = pad32(1) d0 = pad32('hi') print nargs, d0 msg_hash = utils.sha3(nargs + d0) v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pubkey = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pubkey) gen = blocks.genesis({addr: 10**18}) print serpent.compile_to_assembly(open("DAOist frame.se").read()) DAOcode = serpent.compile(open("DAOist frame.se").read()) DAOcontract = transactions.contract(0, 1, 10**12, 100, DAOcode) DAOcontract.sign(key) success, contract_address = processblock.apply_tx(gen, DAOcontract) DCP = transactions.Transaction( 1, 10**12, 10000, contract_address, 0, serpent.encode_datalist([1, 1, v, r, s, 1, 'hi'])) DCP.sign(key) success, result = processblock.apply_tx(gen, DCP) print "success: ", success print serpent.decode_datalist(result)
blk = b.Block.genesis({ v: 10**18 }) scode1 = ''' if !contract.storage[msg.data[0]]: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(0) ''' code1 = serpent.compile(scode1) print "AST", serpent.rewrite(serpent.parse(scode1)) print "Assembly", serpent.compile_to_assembly(scode1) tx1 = t.Transaction.contract(0,0,10**12,1000,code1).sign(k) addr = pb.apply_tx(blk,tx1) snapshot = blk.snapshot() print "Contract address", addr.encode('hex') tx2 = t.Transaction(1,0,10**12,1000,addr,serpent.encode_datalist(['george',45])) tx2.sign(k) o = pb.apply_tx(blk,tx2) print "Result of registering george:45: ", serpent.decode_datalist(o) tx3 = t.Transaction(2,0,10**12,1000,addr,serpent.encode_datalist(['george',20])).sign(k) o = pb.apply_tx(blk,tx3) print "Result of registering george:20: ", serpent.decode_datalist(o) tx4 = t.Transaction(3,0,10**12,1000,addr,serpent.encode_datalist(['harry',60])).sign(k) o = pb.apply_tx(blk,tx4) print "Result of registering harry:60: ", serpent.decode_datalist(o) scode2 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[0x%s] = 1000 elif msg.datasize == 1:
for x in keys: endowment[x] = 10**18 genesis = blocks.genesis(endowment) tx1 = transactions.contract(0, 10**12, 100000, 0, root_code).sign(root_key) result, contract = processblock.apply_transaction(genesis, tx1) nonce = 1 for address in keys: longitude = random.randint(1000, 110000) latitude = random.randint(1000, 110000) datalist = serpent.encode_datalist([0, longitude, latitude]) tx = transactions.Transaction(0, 10**12, 10000, contract, 1000, datalist).sign(keys[address]) #nonce = nonce + 1 result, ans = processblock.apply_transaction(genesis, tx) storage = genesis.to_dict()["state"][contract]["storage"] integers = {} for k in storage: integers[int(k, 16)] = int(storage[k], 16) print(json.dumps(integers, sort_keys=True, indent=4, separators=(',', ': ')))
def sha3(data): return serpent.decode_datalist(utils.sha3(serpent.encode_datalist(*data)))[0]
for x in keys: endowment[x] = 10**18 genesis = blocks.genesis(endowment) tx1 = transactions.contract(0, 10**12, 100000, 0, root_code).sign(root_key) result, contract = processblock.apply_transaction(genesis, tx1) nonce=1 for address in keys: longitude = random.randint(1000, 110000) latitude = random.randint(1000, 110000) datalist = serpent.encode_datalist([0, longitude, latitude]) tx = transactions.Transaction(0, 10**12, 10000, contract, 1000, datalist).sign(keys[address]) #nonce = nonce + 1 result, ans = processblock.apply_transaction(genesis, tx) storage = genesis.to_dict()["state"][contract]["storage"] integers = {}
h = "%02x"%n l = len(h) return "0"*(32-l)+h nargs = pad32(1) d0 = pad32('hi') print nargs, d0 msg_hash = utils.sha3(nargs+d0) v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pubkey = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v,r,s), pubkey) gen = blocks.genesis({addr: 10**18}) print serpent.compile_to_assembly(open("DAOist frame.se").read()) DAOcode = serpent.compile(open("DAOist frame.se").read()) DAOcontract = transactions.contract(0, 1, 10**12, 100, DAOcode) DAOcontract.sign(key) success, contract_address = processblock.apply_tx(gen, DAOcontract) DCP = transactions.Transaction(1,10**12, 10000, contract_address, 0, serpent.encode_datalist([1,1,v,r,s,1,'hi'])) DCP.sign(key) success, result = processblock.apply_tx(gen, DCP) print "success: ", success print serpent.decode_datalist(result)
scode1 = ''' if !contract.storage[msg.data[0]]: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(0) ''' code1 = serpent.compile(scode1) print "AST", serpent.rewrite(serpent.parse(scode1)) print "Assembly", serpent.compile_to_assembly(scode1) tx1 = t.Transaction.contract(0, 0, 10**12, 1000, code1).sign(k) addr = pb.apply_tx(blk, tx1) snapshot = blk.snapshot() print "Contract address", addr.encode('hex') tx2 = t.Transaction(1, 0, 10**12, 1000, addr, serpent.encode_datalist(['george', 45])) tx2.sign(k) o = pb.apply_tx(blk, tx2) print "Result of registering george:45: ", serpent.decode_datalist(o) tx3 = t.Transaction(2, 0, 10**12, 1000, addr, serpent.encode_datalist(['george', 20])).sign(k) o = pb.apply_tx(blk, tx3) print "Result of registering george:20: ", serpent.decode_datalist(o) tx4 = t.Transaction(3, 0, 10**12, 1000, addr, serpent.encode_datalist(['harry', 60])).sign(k) o = pb.apply_tx(blk, tx4) print "Result of registering harry:60: ", serpent.decode_datalist(o) scode2 = ''' if !contract.storage[1000]: contract.storage[1000] = 1
print code msg_hash = utils.sha3('heres a message') v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pub = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pub) print verified tx_make_root = transactions.contract(0, 10, 10**30, 10**30, code).sign(key) success, root_contract = processblock.apply_tx(gen, tx_make_root) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([msg_hash, v, r, s])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist(['hi', 'bye'])).sign(key) tx_init_root = transactions.Transaction( 1, 100, 10**40, root_contract, 0, serpent.encode_datalist([2, '139dcd5cc79e260272e05147c349ab5f2db3f102', 1])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([2, 1])).sign(key) print assembly success, ans = processblock.apply_tx(gen, tx_init_root) print ans data = serpent.decode_datalist(ans) print data print hex(data[0]) quit() print ans.encode('hex') data = serpent.decode_datalist(ans) print 'raw decoded data:', data print 'data as hex:' print map(hex, data) #print ('%02x'%data).decode('hex') print assembly
recipient_key = utils.sha3('recipient') recipient_addr = utils.privtoaddr(recipient_key) host_key = utils.sha3('host') host_addr = utils.privtoaddr(host_key) #initialize the block genesis = blocks.genesis({sender_addr: 10**18, recipient_addr: 10**18, host_addr: 10**18}) #initialize the contract tx1 = transactions.contract(0, 10**12, 10000, 0, code).sign(host_key) result, contract = processblock.apply_transaction(genesis, tx1) #execute escrow transaction #nonce, gasprice, startgas, to, value, data price = int(sys.argv[1]) #user supplied price tx2 = transactions.Transaction(0, 10**12, 10000, contract, price, serpent.encode_datalist([1,recipient_addr])).sign(sender_key) result, ans = processblock.apply_transaction(genesis, tx2) tx3 = transactions.Transaction(0, 10**12, 10000, contract, 0, serpent.encode_datalist([int(sys.argv[2])])).sign(recipient_key) result, ans = processblock.apply_transaction(genesis, tx3) tx4 = transactions.Transaction(1, 10**12, 10000, contract, 0, serpent.encode_datalist([int(sys.argv[3])])).sign(sender_key) result, ans = processblock.apply_transaction(genesis, tx4) print('Service address: %s ' %str(hex(genesis.get_storage_data(contract, 0)))) print('Sender address: %s' %str(hex(genesis.get_storage_data(contract, 1)))) print('Reciever address: %s\n' %str(hex(genesis.get_storage_data(contract, 3)))) print('Price: %s' %str(genesis.get_storage_data(contract, 2))) #mine serveral blocks for i in range(5): genesis.finalize() t = (genesis.timestamp or int(time.time())) + 60
key2 = utils.sha3('cow2') addr2 = utils.privtoaddr(key2) key_host = utils.sha3('host') add_host = utils.privtoaddr(key_host) #initialize the block genesis = blocks.genesis({addr: 10**18, addr2: 10**18, add_host: 10**18}) #This is to initialize the contract tx1 = transactions.contract(0,10**12,10000,0,code).sign(key_host) result, contract = processblock.apply_transaction(genesis, tx1) #start buying tickets #nonce, gasprice, startgas, to, value, data tx2 = transactions.Transaction(0, 10**12, 10000, contract, 10**15, serpent.encode_datalist([])).sign(key) result, ans = processblock.apply_transaction(genesis,tx2) tx3 = transactions.Transaction(0, 10**12, 10000, contract, 10**15, serpent.encode_datalist([])).sign(key2) result, ans = processblock.apply_transaction(genesis,tx3) print('Check no. of buyers: %s ' %str(genesis.get_storage_data(contract, 1))) print('Check address of ticket 1: %s ' %str(hex(genesis.get_storage_data(contract, 4)))) print 'balance ' + str(genesis.get_balance(addr)) print('Check address of ticket 2: %s ' %str(hex(genesis.get_storage_data(contract, 5)))) print 'balance ' + str(genesis.get_balance(addr2)) #Mine serveral blocks for i in range(5): genesis.finalize() t = (genesis.timestamp or int(time.time())) + 60