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) # print("AST", serpent.rewrite(serpent.parse(scode3))) # print("Assembly", serpent.compile_to_assembly(scode3)) blk = b.genesis({v: 10**18, v2: 10**18}) tx10 = t.contract(0, gasprice, startgas, 0, code3).sign(k) s, addr = pb.apply_tx(blk, tx10) tx11 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_tx(blk, tx11) tx12 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_tx(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_tx(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_tx(blk, tx14) assert serpent.decode_datalist(o) == [726] return blk, addr
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 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 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 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_transaction(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) tx = get_transaction() assert not tx in blk.get_transactions() success, res = processblock.apply_tx(blk, tx) assert tx in blk.get_transactions() assert blk.get_balance(v) == utils.denoms.finney * 990 assert blk.get_balance(v2) == utils.denoms.finney * 10
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 test_transaction(): k, v, k2, v2 = accounts() set_db() blk = mkgenesis({v: utils.denoms.ether * 1}) db_store(blk) blk = mine_next_block(blk) tx = get_transaction() assert not tx in blk.get_transactions() success, res = processblock.apply_tx(blk, tx) assert tx in blk.get_transactions() assert blk.get_balance(v) == utils.denoms.finney * 990 assert blk.get_balance(v2) == utils.denoms.finney * 10
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 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 test_namecoin(): k, v, k2, v2 = accounts() 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) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_tx(blk, tx1) snapshot = blk.snapshot() tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_tx(blk, tx2) 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) 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) assert serpent.decode_datalist(o) == [1]
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_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_tx(blk, tx4) tx5 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_tx(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_tx(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_tx(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_tx(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_tx(blk, tx9) assert serpent.decode_datalist(o) == [200]
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
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)
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 * msg(0x%s,0,tx.gas-100,[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 / msg(0x%s,0,tx.gas-100,[contract.storage[1003]],1) if ethvalue >= contract.balance: send(contract.storage[1000],contract.balance,tx.gas-100) return(3) elif block.timestamp > contract.storage[1005]: send(contract.storage[1001],contract.balance - ethvalue,tx.gas-100) send(contract.storage[1000],ethvalue,tx.gas-100) return(4) else: return(5) ''' % (addr, addr) code4 = serpent.compile(scode4) # print("AST", serpent.rewrite(serpent.parse(scode4))) # print("Assembly", serpent.compile_to_assembly(scode4)) # important: no new genesis block tx15 = t.contract(5, gasprice, startgas, 0, code4).sign(k) s, addr2 = pb.apply_tx(blk, tx15) tx16 = t.Transaction(6, gasprice, startgas, addr2, 10**17, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_tx(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_tx(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_tx(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_tx(blk, tx19) assert serpent.decode_datalist(o) == [1] tx20 = t.Transaction(9, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_tx(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_tx(blk, tx21) assert serpent.decode_datalist(o) == [1] tx22 = t.Transaction(8, gasprice, 2000, addr2, 0, '').sign(k) s, o = pb.apply_tx(blk, tx22) assert serpent.decode_datalist(o) == [4]
def apply_tx(self, tx): return processblock.apply_tx(self.genesis, tx)
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 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)
assembly = serpent.compile_to_assembly(open('tester.se').read()) print assembly 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
gen = blocks.genesis({addr: 10**60}) assembly = serpent.compile_to_assembly(open('tester.se').read()) print assembly 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()
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)
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)