private ResultWrapper <Data> GetAccountCode(Address address, BlockParameter blockParameter) { if (blockParameter.Type == BlockParameterType.Pending) { var code = _blockchainBridge.GetCode(address); return(ResultWrapper <Data> .Success(new Data(code))); } var block = GetBlock(blockParameter); if (block.Result.ResultType == ResultType.Failure) { return(ResultWrapper <Data> .Fail(block.Result.Error)); } return(GetAccountCode(address, block.Data.Header.StateRoot)); }
public ResultWrapper <byte[]> eth_getCode(Address address, BlockParameter blockParameter = null) { SearchResult <BlockHeader> searchResult = _blockchainBridge.SearchForHeader(blockParameter); if (searchResult.IsError) { return(ResultWrapper <byte[]> .Fail(searchResult)); } BlockHeader header = searchResult.Object; if (!HasStateForBlock(header)) { return(ResultWrapper <byte[]> .Fail($"No state available for block {header.Hash}", ErrorCodes.ResourceUnavailable)); } Account account = _blockchainBridge.GetAccount(address, header.StateRoot); if (account == null) { return(ResultWrapper <byte[]> .Success(Bytes.Empty)); } var code = _blockchainBridge.GetCode(account.CodeHash); return(ResultWrapper <byte[]> .Success(code)); }
public void ValidateContractAddress(Address contractAddress) { if (contractAddress != _contractAddress) { throw new InvalidDataException($"Contract address {contractAddress} is different than configured {_contractAddress}"); } if (_blockchainBridge.GetCode(contractAddress).Length == 0) { throw new InvalidDataException($"No contract code at address {contractAddress}."); } if (!Bytes.AreEqual(_blockchainBridge.GetCode(contractAddress), Bytes.FromHexString(ContractData.DeployedCode))) { throw new InvalidDataException($"Code at address {contractAddress} is different than expected."); } }
public void Ok_when_code_is_valid() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); DepositService depositService = new DepositService(bridge, _txPool, _abiEncoder, _wallet, _contractAddress, LimboLogs.Instance); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.FromHexString(ContractData.DeployedCode)); depositService.ValidateContractAddress(contractAddress); }
public void Throws_when_unexpected_code() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); DepositService depositService = new DepositService(bridge, _txPool, _abiEncoder, _wallet, _contractAddress, LimboLogs.Instance); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.FromHexString("0xa234")); Assert.Throws <InvalidDataException>(() => depositService.ValidateContractAddress(contractAddress)); }
public void Throws_when_no_code_deployed() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); DepositService depositService = new DepositService(bridge, _abiEncoder, _wallet, _ndmConfig, LimboLogs.Instance); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.Empty); Assert.Throws <InvalidDataException>(() => depositService.ValidateContractAddress(contractAddress)); }
public async Task get_code_should_invoke_blockchain_bridge_get_code() { var code = new byte[] { 0, 1, 2 }; var address = TestItem.AddressA; _blockchainBridge.GetCode(address).Returns(code); var result = await _ndmBridge.GetCodeAsync(address); _blockchainBridge.Received().GetCode(address); result.Should().BeSameAs(code); }
public async Task Ok_when_code_is_valid() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); _ndmBridge = new NdmBlockchainBridge(bridge, _txPool); DepositService depositService = new DepositService(_ndmBridge, _abiEncoder, _wallet, _contractAddress); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.FromHexString(ContractData.DeployedCode)); await depositService.ValidateContractAddressAsync(contractAddress); }
public void Throws_when_no_code_deployed() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); _ndmBridge = new NdmBlockchainBridge(bridge, _txPool); DepositService depositService = new DepositService(_ndmBridge, _abiEncoder, _wallet, _contractAddress); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.Empty); Assert.ThrowsAsync <InvalidDataException>(async() => await depositService.ValidateContractAddressAsync(contractAddress)); }
public void Throws_when_unexpected_code() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); _ndmBridge = new NdmBlockchainBridge(Substitute.For <ITxPoolBridge>(), bridge, _txPool); DepositService depositService = new DepositService(_ndmBridge, _abiEncoder, _wallet, _contractAddress); Address contractAddress = new Address(_ndmConfig.ContractAddress); bridge.GetCode(contractAddress).Returns(Bytes.FromHexString("0xa234")); Assert.ThrowsAsync <InvalidDataException>(async() => await depositService.ValidateContractAddressAsync(contractAddress)); }
private ResultWrapper <byte[]> GetAccountCode(Address address, Keccak stateRoot) { Account account = _blockchainBridge.GetAccount(address, stateRoot); if (account == null) { return(ResultWrapper <byte[]> .Success(Bytes.Empty)); } var code = _blockchainBridge.GetCode(account.CodeHash); return(ResultWrapper <byte[]> .Success(code)); }
public ResultWrapper <byte[]> eth_getCode(Address address, BlockParameter blockParameter = null) { SearchResult <BlockHeader> searchResult = _blockchainBridge.SearchForHeader(blockParameter); if (searchResult.IsError) { return(ResultWrapper <byte[]> .Fail(searchResult)); } BlockHeader header = searchResult.Object; Account account = _blockchainBridge.GetAccount(address, header.StateRoot); if (account == null) { return(ResultWrapper <byte[]> .Success(Bytes.Empty)); } var code = _blockchainBridge.GetCode(account.CodeHash); return(ResultWrapper <byte[]> .Success(code)); }
public Task <byte[]> GetCodeAsync(Address address) => Task.FromResult(_blockchainBridge.GetCode(address));