public ResultWrapper <BigInteger?> eth_estimateGas(TransactionForRpc transactionCall) { try { _readerWriterLockSlim.EnterWriteLock(); Block headBlock = _blockchainBridge.RetrieveHeadBlock(); if (transactionCall.Gas == null) { transactionCall.Gas = headBlock.GasLimit; } long result = _blockchainBridge.EstimateGas(headBlock, transactionCall.ToTransaction()); return(ResultWrapper <BigInteger?> .Success(result)); } finally { _readerWriterLockSlim.ExitWriteLock(); } }
public void Eth_get_block_by_number_empty_param() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); bridge.FindBlock(Arg.Any <Keccak>(), Arg.Any <bool>()).Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.RetrieveHeadBlock().Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.Head.Returns(Build.A.BlockHeader.TestObject); IEthModule module = new EthModule(NullLogManager.Instance, bridge); string serialized = RpcTest.TestSerializedRequest(module, "eth_getBlockByNumber", "", "true"); Assert.AreEqual("{\"id\":\"0x43\",\"jsonrpc\":\"2.0\",\"result\":null,\"error\":{\"code\":-32602,\"message\":\"Incorrect parameters count, expected: 2, actual: 1\",\"data\":null}}", serialized); }
public void Eth_get_block_by_number() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); bridge.FindBlock(Arg.Any <Keccak>(), Arg.Any <bool>()).Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.RetrieveHeadBlock().Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.Head.Returns(Build.A.BlockHeader.TestObject); IEthModule module = new EthModule(NullLogManager.Instance, bridge); string serialized = RpcTest.TestSerializedRequest(module, "eth_getBlockByNumber", "latest", "true"); Assert.AreEqual("{\"id\":\"0x43\",\"jsonrpc\":\"2.0\",\"result\":{\"number\":\"0x0\",\"hash\":\"0xa2a9f03b9493046696099d27b2612b99497aa1f392ec966716ab393c715a5bb6\",\"parentHash\":\"0xff483e972a04a9a62bb4b7d04ae403c615604e4090521ecc5bb7af67f71be09c\",\"nonce\":\"0x3e8\",\"mixHash\":\"0x2ba5557a4c62a513c7e56d1bf13373e0da6bec016755483e91589fe1c6d212e2\",\"sha3Uncles\":\"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347\",\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"transactionsRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"stateRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"receiptsRoot\":\"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\",\"miner\":\"0x0000000000000000000000000000000000000000\",\"difficulty\":\"0xf4240\",\"totalDifficulty\":\"0x0\",\"extraData\":\"0x010203\",\"size\":\"0x0\",\"gasLimit\":\"0x3d0900\",\"gasUsed\":\"0x0\",\"timestamp\":\"0xf4240\",\"transactions\":[{\"hash\":null,\"nonce\":\"0x0\",\"blockHash\":\"0xa2a9f03b9493046696099d27b2612b99497aa1f392ec966716ab393c715a5bb6\",\"blockNumber\":\"0x0\",\"transactionIndex\":\"0x0\",\"from\":null,\"to\":\"0x0000000000000000000000000000000000000000\",\"value\":\"0x1\",\"gasPrice\":\"0x1\",\"gas\":\"0x5208\",\"data\":\"0x\"}],\"transactionHashes\":null,\"uncles\":[]}}", serialized); }
public void Eth_get_block_by_number_with_number_bad_number() { IBlockchainBridge bridge = Substitute.For <IBlockchainBridge>(); bridge.FindBlock(Arg.Any <UInt256>()).Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.RetrieveHeadBlock().Returns(Build.A.Block.WithTotalDifficulty(0).WithTransactions(Build.A.Transaction.TestObject).TestObject); bridge.Head.Returns(Build.A.BlockHeader.TestObject); IEthModule module = new EthModule(new EthereumJsonSerializer(), Substitute.For <IConfigProvider>(), NullLogManager.Instance, bridge); string serialized = RpcTest.TestSerializedRequest(module, "eth_getBlockByNumber", "'0x1234567890123456789012345678901234567890123456789012345678901234567890'", "true"); Assert.AreEqual("{\"id\":67,\"jsonrpc\":\"2.0\",\"result\":null}", serialized); }
private ResultWrapper <Core.Block> GetBlock(BlockParameter blockParameter) { switch (blockParameter.Type) { case BlockParameterType.Pending: var pending = _blockchainBridge.FindBlock(_blockchainBridge.BestSuggested.Hash, false); return(ResultWrapper <Core.Block> .Success(pending)); // TODO: a pending block for sealEngine, work in progress case BlockParameterType.Latest: return(ResultWrapper <Core.Block> .Success(_blockchainBridge.RetrieveHeadBlock())); case BlockParameterType.Earliest: var genesis = _blockchainBridge.RetrieveGenesisBlock(); return(ResultWrapper <Core.Block> .Success(genesis)); case BlockParameterType.BlockId: if (blockParameter.BlockId?.Value == null) { return(ResultWrapper <Core.Block> .Fail($"Block id is required for {BlockParameterType.BlockId}", ErrorType.InvalidParams)); } var value = blockParameter.BlockId.AsNumber(); if (!value.HasValue) { return(ResultWrapper <Core.Block> .Fail("Invalid block id", ErrorType.InvalidParams)); } var block = _blockchainBridge.FindBlock(new UInt256(value.Value)); if (block == null) { return(ResultWrapper <Core.Block> .Fail($"Cannot find block for {value.Value}", ErrorType.NotFound)); } return(ResultWrapper <Core.Block> .Success(block)); default: throw new Exception($"BlockParameterType not supported: {blockParameter.Type}"); } }
private ResultWrapper <Core.Block> GetBlock(BlockParameter blockParameter) { switch (blockParameter.Type) { case BlockParameterType.Pending: var pending = _blockchainBridge.FindBlock(_blockchainBridge.BestSuggested.Hash, false); return(ResultWrapper <Core.Block> .Success(pending)); // TODO: a pending block for sealEngine, work in progress case BlockParameterType.Latest: return(ResultWrapper <Core.Block> .Success(_blockchainBridge.RetrieveHeadBlock())); case BlockParameterType.Earliest: var genesis = _blockchainBridge.RetrieveGenesisBlock(); return(ResultWrapper <Core.Block> .Success(genesis)); case BlockParameterType.BlockId: if (blockParameter.BlockId?.Value == null) { return(ResultWrapper <Core.Block> .Fail($"Block id is required for {BlockParameterType.BlockId}", ErrorType.InvalidParams)); } var value = blockParameter.BlockId.GetValue(); if (!value.HasValue) { return(ResultWrapper <Core.Block> .Fail("Invalid block id", ErrorType.InvalidParams)); } throw new NotImplementedException(); // TODO: TKS - discuss with me later, there was a rebuilt of BlockStore / Blockchain, work in progress now, so just commenting it out // var block = _blockTree.FindBlock(value.Value); // if (block == null) // { // return ResultWrapper<Core.Block>.Fail($"Cannot find block for {value.Value}", ErrorType.NotFound); // } // return ResultWrapper<Core.Block>.Success(block); default: throw new Exception($"BlockParameterType not supported: {blockParameter.Type}"); } }