def message(): message = Message( to=CANONICAL_ADDRESS_A, sender=CANONICAL_ADDRESS_B, value=100, data=b'', code=b'', gas=100, ) return message
def message(): message = Message( origin=CANONICAL_ADDRESS_B, to=CANONICAL_ADDRESS_A, sender=CANONICAL_ADDRESS_B, value=100, data=b'', code=b'', gas=100, gas_price=1, ) return message
def __init__(self): self.block_prevhash = 0 self.block_coinbase = 0 self.block_timestamp = 0 self.block_number = 0 self.block_difficulty = 0 self.block_gas_limit = 0 self.tx_origin = b'0' * 40 self.tx_gasprice = 0 self.storage = {} self.height = 1 self.uncommited_tx_count = 0 self.nonce = 5 result,gas,memory = vm_execute(self,Message(addr0,addr2),GenesisPermissionBytes) self.code = {addr2:bytes(memory)} result,gas,genesis_code = vm_execute(self,Message(addr0,addr1),GenesisParamsPrefix+addr_padding+addr2) self.code.update({addr1:bytes(genesis_code)}) result,gas,memory = vm_execute(self,Message(addr0,addr3),GenesisCheckTxBytes) self.code.update({addr3:bytes(memory)}) result,gas,memory = vm_execute(self,Message(addr0,addr4),GenesisDeliverTxBytes) self.code.update({addr4:bytes(memory)}) result,gas,memory = vm_execute(self,Message(addr0,addr1,data=check_tx_addr_set),bytes(genesis_code)) result,gas,memory = vm_execute(self,Message(addr0,addr1,data=deliver_tx_addr_set),bytes(genesis_code))
def execute_transaction(self, transaction): # get transaction data fromAddr = bytes.fromhex(transaction.sender) toAddr = bytes.fromhex(transaction.recipient) codeData = bytes.fromhex(transaction.code) # execute transaction if (toAddr == addr0): reply, gas, result = self.create_address(Message(fromAddr, toAddr), codeData) else: result = self.call_address(fromAddr, toAddr, codeData) return result
def test_is_origin_computation(computation, transaction_context): assert computation.is_origin_computation message2 = Message( to=CANONICAL_ADDRESS_A, # Different sender than the tx context origin sender=CANONICAL_ADDRESS_A, value=100, data=b'', code=b'', gas=100, ) computation2 = DummyComputation( vm_state=None, message=message2, transaction_context=transaction_context, ) assert not computation2.is_origin_computation
def fixture_to_computation(fixture, code, vm): message = Message( to=fixture['exec']['address'], sender=fixture['exec']['caller'], value=fixture['exec']['value'], data=fixture['exec']['data'], code=code, gas=fixture['exec']['gas'], ) transaction_context = BaseTransactionContext( origin=fixture['exec']['origin'], gas_price=fixture['exec']['gasPrice'], ) return vm.state.get_computation(message, transaction_context).apply_computation( vm.state, message, transaction_context, )
def deliver_tx(self,data): # get deliver_tx address result = self.call_address(addr0,addr1,deliver_tx_addr_req) delivertxaddr = bytes(result[12:32]) # call deliver_tx bdata = bytearray.fromhex(data.decode("utf-8")) call_data = deliver_tx_call_prefix+utils.encode_int32(len(bdata))+utils.encode_int32(math.ceil(len(bdata)/32))+bdata result = self.call_address(addr0,delivertxaddr,call_data) # get transaction data fromAddr = bytes(result[0:20]) toAddr = bytes(result[32:52]) length = utils.bytearray_to_int(result[96:128]) codeData = bytes(result[160:160+length]) # execute transaction if(toAddr==addr0): reply,gas,result = self.create_address(Message(fromAddr,toAddr),codeData) else: result = self.call_address(fromAddr,toAddr,codeData) self.uncommited_tx_count += 1 return result
def call_address(self, fromAddr, toAddr, data): print("running",toAddr.hex()) code = self.code.get(toAddr,None) if(code != None): result,gas,memory = vm_execute(self,Message(fromAddr,toAddr,data=data),code) return memory
def test_vm_fixtures(fixture, vm_class): chaindb = ChainDB(get_db_backend()) header = BlockHeader( coinbase=fixture['env']['currentCoinbase'], difficulty=fixture['env']['currentDifficulty'], block_number=fixture['env']['currentNumber'], gas_limit=fixture['env']['currentGasLimit'], timestamp=fixture['env']['currentTimestamp'], ) vm = vm_class(header=header, chaindb=chaindb) vm_state = vm.state with vm_state.state_db() as state_db: setup_state_db(fixture['pre'], state_db) code = state_db.get_code(fixture['exec']['address']) # Update state_root manually vm.block.header.state_root = vm_state.state_root message = Message( to=fixture['exec']['address'], sender=fixture['exec']['caller'], value=fixture['exec']['value'], data=fixture['exec']['data'], code=code, gas=fixture['exec']['gas'], ) transaction_context = BaseTransactionContext( origin=fixture['exec']['origin'], gas_price=fixture['exec']['gasPrice'], ) computation = vm.state.get_computation( message, transaction_context).apply_computation( vm.state, message, transaction_context, ) # Update state_root manually vm.block.header.state_root = computation.vm_state.state_root if 'post' in fixture: # # Success checks # assert not computation.is_error log_entries = computation.get_log_entries() if 'logs' in fixture: actual_logs_hash = hash_log_entries(log_entries) expected_logs_hash = fixture['logs'] assert expected_logs_hash == actual_logs_hash elif log_entries: raise AssertionError("Got log entries: {0}".format(log_entries)) expected_output = fixture['out'] assert computation.output == expected_output gas_meter = computation.gas_meter expected_gas_remaining = fixture['gas'] actual_gas_remaining = gas_meter.gas_remaining gas_delta = actual_gas_remaining - expected_gas_remaining assert gas_delta == 0, "Gas difference: {0}".format(gas_delta) call_creates = fixture.get('callcreates', []) assert len(computation.children) == len(call_creates) call_creates = fixture.get('callcreates', []) for child_computation, created_call in zip(computation.children, call_creates): to_address = created_call['destination'] data = created_call['data'] gas_limit = created_call['gasLimit'] value = created_call['value'] assert child_computation.msg.to == to_address assert data == child_computation.msg.data or child_computation.msg.code assert gas_limit == child_computation.msg.gas assert value == child_computation.msg.value post_state = fixture['post'] else: # # Error checks # assert computation.is_error assert isinstance(computation._error, VMError) post_state = fixture['pre'] with vm.state.state_db(read_only=True) as state_db: verify_state_db(post_state, state_db)
def test_vm_fixtures(fixture_name, fixture): db = get_db_backend() header = BlockHeader( coinbase=fixture['env']['currentCoinbase'], difficulty=fixture['env']['currentDifficulty'], block_number=fixture['env']['currentNumber'], gas_limit=fixture['env']['currentGasLimit'], timestamp=fixture['env']['currentTimestamp'], ) chain = ChainForTesting(db=db, header=header) vm = chain.get_vm() with vm.state_db() as state_db: setup_state_db(fixture['pre'], state_db) code = state_db.get_code(fixture['exec']['address']) message = Message( origin=fixture['exec']['origin'], to=fixture['exec']['address'], sender=fixture['exec']['caller'], value=fixture['exec']['value'], data=fixture['exec']['data'], code=code, gas=fixture['exec']['gas'], gas_price=fixture['exec']['gasPrice'], ) computation = vm.apply_computation(message) if 'post' in fixture: # # Success checks # assert computation.error is None expected_logs = [{ 'address': log_entry[0], 'topics': log_entry[1], 'data': log_entry[2], } for log_entry in computation.get_log_entries()] expected_logs == fixture['logs'] expected_output = fixture['out'] assert computation.output == expected_output gas_meter = computation.gas_meter expected_gas_remaining = fixture['gas'] actual_gas_remaining = gas_meter.gas_remaining gas_delta = actual_gas_remaining - expected_gas_remaining assert gas_delta == 0, "Gas difference: {0}".format(gas_delta) call_creates = fixture.get('callcreates', []) assert len(computation.children) == len(call_creates) call_creates = fixture.get('callcreates', []) for child_computation, created_call in zip(computation.children, call_creates): to_address = created_call['destination'] data = created_call['data'] gas_limit = created_call['gasLimit'] value = created_call['value'] assert child_computation.msg.to == to_address assert data == child_computation.msg.data or child_computation.msg.code assert gas_limit == child_computation.msg.gas assert value == child_computation.msg.value post_state = fixture['post'] else: # # Error checks # assert computation.error assert isinstance(computation.error, VMError) post_state = fixture['pre'] with vm.state_db(read_only=True) as state_db: verify_state_db(post_state, state_db)
def test_vm_fixtures(fixture_name, fixture): db = MemoryDB() header = BlockHeader( coinbase=fixture['env']['currentCoinbase'], difficulty=fixture['env']['currentDifficulty'], block_number=fixture['env']['currentNumber'], gas_limit=fixture['env']['currentGasLimit'], timestamp=fixture['env']['currentTimestamp'], ) meta_evm = EVMForTesting.configure(db=db)(header=header) evm = meta_evm.get_evm() setup_state_db(fixture['pre'], evm.state_db) message = Message( origin=fixture['exec']['origin'], to=fixture['exec']['address'], sender=fixture['exec']['caller'], value=fixture['exec']['value'], data=fixture['exec']['data'], code=evm.state_db.get_code(fixture['exec']['address']), gas=fixture['exec']['gas'], gas_price=fixture['exec']['gasPrice'], ) computation = evm.apply_computation(message) if 'post' in fixture: # # Success checks # assert computation.error is None expected_logs = [{ 'address': log_entry[0], 'topics': log_entry[1], 'data': log_entry[2], } for log_entry in computation.get_log_entries()] expected_logs == fixture['logs'] expected_output = fixture['out'] assert computation.output == expected_output gas_meter = computation.gas_meter expected_gas_remaining = fixture['gas'] actual_gas_remaining = gas_meter.gas_remaining gas_delta = actual_gas_remaining - expected_gas_remaining assert gas_delta == 0, "Gas difference: {0}".format(gas_delta) call_creates = fixture.get('callcreates', []) assert len(computation.children) == len(call_creates) for child_computation, created_call in zip( computation.children, fixture.get('callcreates', [])): to_address = created_call['destination'] data = created_call['data'] gas_limit = created_call['gasLimit'] value = created_call['value'] assert child_computation.msg.to == to_address assert data == child_computation.msg.data or child_computation.msg.code assert gas_limit == child_computation.msg.gas assert value == child_computation.msg.value post_state = fixture['post'] else: # # Error checks # assert computation.error assert isinstance(computation.error, VMError) post_state = fixture['pre'] for account, account_data in post_state.items(): for slot, expected_storage_value in account_data['storage'].items(): actual_storage_value = evm.state_db.get_storage(account, slot) assert actual_storage_value == expected_storage_value expected_nonce = account_data['nonce'] expected_code = account_data['code'] expected_balance = account_data['balance'] actual_nonce = evm.state_db.get_nonce(account) actual_code = evm.state_db.get_code(account) actual_balance = evm.state_db.get_balance(account) assert actual_nonce == expected_nonce assert actual_code == expected_code assert actual_balance == expected_balance