def main(): svcoin = '''\ def init(): sstore(tx.origin, 21*10**9) def sendSVCoins(to, amount): with my_bal = sload(msg.sender): if amount < my_bal: sstore(msg.sender, my_bal - amount) sstore(to, sload(to) + amount) return(1) return(-1) def mySVCoinBalance(): return(sload(msg.sender)) def getSVCoinBalance(address): return(sload(address)) ''' evm = '0x' + serpent.compile(svcoin).encode('hex') fullsig = json.loads(serpent.mk_full_signature(svcoin)) node = TestNode(log=open(os.devnull, 'w'), verbose=False) node.start() rpc = RPC_Client((node.rpchost, node.rpcport), 0) password = os.urandom(32).encode('hex') account = rpc.personal_newAccount(password)['result'] rpc.personal_unlockAccount(account, password, hex(500)) rpc.miner_start(2) balance = 0 while balance < int(MAXGAS, 16): balance = int(rpc.eth_getBalance(account)['result'], 16) txhash = rpc.eth_sendTransaction(sender=account, data=evm, gas=MAXGAS)['result'] while True: response = rpc.eth_getTransactionReceipt(txhash) receipt = response.get('result', False) if receipt: blocknumber = receipt.get('blockNumber', False) if blocknumber: address = receipt['contractAddress'] break contract = Contract(address, fullsig, rpc) print 'My balance is', contract.mySVCoinBalance(call=True) receipt = contract.sendSVCoins(2, 10000, send=True, receipt=True) print 'Sent coins to address 2, receipt:' print json.dumps(receipt, indent=4, sort_keys=True) print 'Balance at address 2 is', contract.getSVCoinBalance(2, call=True)
def main(): svcoin = '''\ def init(): sstore(tx.origin, 21*10**9) def sendSVCoins(to, amount): with my_bal = sload(msg.sender): if amount < my_bal: sstore(msg.sender, my_bal - amount) sstore(to, sload(to) + amount) return(1) return(-1) def mySVCoinBalance(): return(sload(msg.sender)) def getSVCoinBalance(address): return(sload(address)) ''' evm = '0x' + serpent.compile(svcoin).encode('hex') fullsig = json.loads(serpent.mk_full_signature(svcoin)) node = TestNode(log=open(os.devnull, 'w'), verbose=False) node.start() rpc = RPC_Client((node.rpchost, node.rpcport), 0) password = os.urandom(32).encode('hex') account = rpc.personal_newAccount(password)['result'] rpc.personal_unlockAccount(account, password, hex(500)) rpc.miner_start(2) balance = 0 while balance < int(MAXGAS, 16): balance = int(rpc.eth_getBalance(account)['result'], 16) txhash = rpc.eth_sendTransaction(sender=account, data=evm, gas=MAXGAS)['result'] while True: response = rpc.eth_getTransactionReceipt(txhash) receipt = response.get('result', False) if receipt: blocknumber = receipt.get('blockNumber', False) if blocknumber: address = receipt['contractAddress'] break contract = Contract(address, fullsig, rpc) print 'My balance is', contract.mySVCoinBalance(call=True) receipt = contract.sendSVCoins(2, 10000, send=True, receipt=True) print 'Sent coins to address 2, receipt:' print json.dumps(receipt, indent=4, sort_keys=True) print 'Balance at address 2 is', contract.getSVCoinBalance(2, call=True)
def test_whitelist(): top_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) whitelist_code = open( os.path.join(top_dir, 'src', 'data and api', 'reporting.se.whitelist')).read().split('\n') # change the period length of votes so testing is feasible old_period = whitelist_code.index('macro PERIOD: 1800') whitelist_code[old_period] = 'macro PERIOD: 100' whitelist_code = '\n'.join(whitelist_code) # start the geth node node = TestNode(verbose=False) node.start() # create rpc client and initialize accounts rpc = RPC_Client((node.rpchost, node.rpcport), 0) accounts = setup_accounts(rpc, 10, int(MAXGAS, 16), 60 * 60) # compile code print 'compiling and submitting code' evm = '0x' + serpent.compile(whitelist_code).encode('hex') fullsig = json.loads(serpent.mk_full_signature(whitelist_code)) response = rpc.eth_sendTransaction(sender=accounts[0], data=evm, gas=MAXGAS) txhash = response['result'] while True: response = rpc.eth_getTransactionReceipt(txhash) receipt = response.get('result', False) if receipt: blocknumber = receipt.get('blockNumber', False) if blocknumber: address = receipt['contractAddress'] break print 'done.' contract = Contract(address, fullsig, rpc) for account in accounts: while True: try: contract.addReporter( 1010101, int(account, 16), send=True, sender=account, receipt=True ) #this option forces blocking until included in a block except AssertionError as exc: error = json.loads(exc.message)['error'] code = error['code'] if code != -32603: raise exc print 'nonce too low for account', account print 'trying again' time.sleep(10) else: break print 'account', account, 'added as reporter' index = contract.repIDToIndex(1010101, int(account, 16), call=True) contract.setRep(1010101, index, 10000 * 2**64, send=True, sender=account, receipt=True) contract.setWhitelist(2, [1, 3, 4, 5], send=True, receipt=True) ballot_hash = contract.propose_replacement(5, 6, call=True) contract.propose_replacement(5, 6, send=True, receipt=True) for account, _ in zip(accounts, range(6)): contract.whitelistVote(ballot_hash, sender=account) last_period = contract.getPeriod() while contract.getPeriod() == last_period: time.sleep(1) if contract.getWhitelist(2) == [1, 3, 4, 6]: print 'TEST PASSED' else: print 'TEST FAILED'
def test_whitelist(): top_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) whitelist_code = open(os.path.join(top_dir, "src", "data and api", "reporting.se.whitelist")).read().split("\n") # change the period length of votes so testing is feasible old_period = whitelist_code.index("macro PERIOD: 1800") whitelist_code[old_period] = "macro PERIOD: 100" whitelist_code = "\n".join(whitelist_code) # start the geth node node = TestNode(verbose=False) node.start() # create rpc client and initialize accounts rpc = RPC_Client((node.rpchost, node.rpcport), 0) accounts = setup_accounts(rpc, 10, int(MAXGAS, 16), 60 * 60) # compile code print "compiling and submitting code" evm = "0x" + serpent.compile(whitelist_code).encode("hex") fullsig = json.loads(serpent.mk_full_signature(whitelist_code)) response = rpc.eth_sendTransaction(sender=accounts[0], data=evm, gas=MAXGAS) txhash = response["result"] while True: response = rpc.eth_getTransactionReceipt(txhash) receipt = response.get("result", False) if receipt: blocknumber = receipt.get("blockNumber", False) if blocknumber: address = receipt["contractAddress"] break print "done." contract = Contract(address, fullsig, rpc) for account in accounts: while True: try: contract.addReporter( 1010101, int(account, 16), send=True, sender=account, receipt=True ) # this option forces blocking until included in a block except AssertionError as exc: error = json.loads(exc.message)["error"] code = error["code"] if code != -32603: raise exc print "nonce too low for account", account print "trying again" time.sleep(10) else: break print "account", account, "added as reporter" index = contract.repIDToIndex(1010101, int(account, 16), call=True) contract.setRep(1010101, index, 10000 * 2 ** 64, send=True, sender=account, receipt=True) contract.setWhitelist(2, [1, 3, 4, 5], send=True, receipt=True) ballot_hash = contract.propose_replacement(5, 6, call=True) contract.propose_replacement(5, 6, send=True, receipt=True) for account, _ in zip(accounts, range(6)): contract.whitelistVote(ballot_hash, sender=account) last_period = contract.getPeriod() while contract.getPeriod() == last_period: time.sleep(1) if contract.getWhitelist(2) == [1, 3, 4, 6]: print "TEST PASSED" else: print "TEST FAILED"