public EthController(IWeb3EthApi web3EthApi, IWeb3HandlerResolver handlerResolver, IJsonSerializer jsonSerializer, IEthRpcService ethRpcService) { _ethRpcService = ethRpcService; _web3EthApi = web3EthApi ?? throw new ArgumentNullException(nameof(web3EthApi)); _handlerResolver = handlerResolver ?? throw new ArgumentNullException(nameof(handlerResolver)); _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer)); }
protected override UInt256 Handle(Address address, IWeb3EthApi api) { Delta delta = api.GetLatestDeltaWithCid().Delta; var stateRoot = delta.StateRoot.ToKeccak(); return(api.StateReader.GetBalance(stateRoot, address)); }
public static bool TryGetDeltaWithCid(this IWeb3EthApi api, BlockParameter block, out DeltaWithCid delta) { Cid cid; switch (block.Type) { case BlockParameterType.Earliest: cid = api.DeltaCache.GenesisHash; break; case BlockParameterType.Latest: cid = api.DeltaResolver.LatestDelta; break; case BlockParameterType.Pending: cid = api.DeltaResolver.LatestDelta; break; case BlockParameterType.BlockNumber: var blockNumber = block.BlockNumber.Value; if (!api.DeltaResolver.TryResolve(blockNumber, out cid)) { delta = default; return(false); } break; default: throw new ArgumentOutOfRangeException(); } delta = api.GetDeltaWithCid(cid); return(true); }
protected override long?Handle(Keccak deltaHash, IWeb3EthApi api) { if (api.DeltaCache.TryGetOrAddConfirmedDelta(deltaHash.ToCid(), out var delta)) { return(delta.PublicEntries.Count); } return(default);
protected override long?Handle(BlockParameter block, IWeb3EthApi api) { if (api.TryGetDeltaWithCid(block, out var delta)) { return(delta.Delta.PublicEntries.Count); } return(default);
protected override byte[] Handle(Address address, IWeb3EthApi api) { Delta delta = api.GetLatestDeltaWithCid().Delta; var stateRoot = delta.StateRoot.ToKeccak(); byte[] byteCode = api.StateReader.GetCode(stateRoot, address); return(byteCode); }
protected override long Handle(TransactionForRpc transactionCall, IWeb3EthApi api) { var deltaWithCid = api.GetLatestDeltaWithCid(); var callOutputTracer = api.CallAndRestore(transactionCall, deltaWithCid); return(callOutputTracer.GasSpent); }
protected override UInt256 Handle(Address address, IWeb3EthApi api) { // change to appropriate hash Delta delta = api.DeltaResolver.Latest; // Keccak stateRoot = api.StateRootResolver.Resolve(delta.Hash); <-- we need a delta hash // return api.StateReader.GetBalance(stateRoot, address); return(new UInt256(1000000)); }
protected override Keccak Handle(TransactionForRpc transaction, IWeb3EthApi api) { var deltaWithCid = api.GetLatestDeltaWithCid(); var parentDelta = deltaWithCid.Delta; Keccak root = parentDelta.StateRoot.ToKeccak(); var publicEntry = api.ToPublicEntry(transaction, root); return(api.SendTransaction(publicEntry)); }
protected override TransactionForRpc Handle(Keccak transactionHash, IWeb3EthApi api) { if (api.FindTransactionData(transactionHash, out var deltaHash, out var delta, out var index)) { return(api.ToTransactionForRpc(new DeltaWithCid { Cid = deltaHash, Delta = delta }, index)); } return(default);
protected override object Handle(IWeb3EthApi api) { var syncState = api.SyncState; return(syncState.IsSynchronized?(object)false:new { StartingBlock = string.Format("0x{0:X}", syncState.StartingBlock), CurrentBlock = string.Format("0x{0:X}", syncState.CurrentBlock), HighestBlock = string.Format("0x{0:X}", syncState.HighestBlock), }); }
protected override UInt256 Handle(Address address, BlockParameter block, IWeb3EthApi api) { if (api.TryGetDeltaWithCid(block, out var deltaWithCid)) { Keccak stateRoot = deltaWithCid.Delta.StateRoot.ToKeccak(); Account account = api.StateReader.GetAccount(stateRoot, address); return(account?.Nonce ?? 0); } throw new InvalidOperationException($"Delta not found: '{block}'"); }
public static DeltaWithCid GetDeltaWithCid(this IWeb3EthApi api, Cid cid) { if (!api.DeltaCache.TryGetOrAddConfirmedDelta(cid, out Delta delta)) { throw new Exception($"Delta not found '{cid}'"); } return(new DeltaWithCid { Delta = delta, Cid = cid }); }
public static PublicEntry ToPublicEntry(this IWeb3EthApi api, TransactionForRpc transactionCall, Keccak root) { return(new PublicEntry { Nonce = (ulong)api.StateReader.GetNonce(root, transactionCall.From), SenderAddress = transactionCall.From.Bytes.ToByteString(), ReceiverAddress = transactionCall.To?.Bytes.ToByteString() ?? ByteString.Empty, GasLimit = (ulong)transactionCall.Gas.GetValueOrDefault(), GasPrice = transactionCall.GasPrice.GetValueOrDefault().ToUint256ByteString(), Amount = transactionCall.Value.GetValueOrDefault().ToUint256ByteString(), Data = transactionCall.Data?.ToByteString() ?? ByteString.Empty }); }
protected override BlockForRpc Handle(BlockParameter block, IWeb3EthApi api) { Cid deltaHash; long blockNumber; IDeltaCache deltaCache = api.DeltaCache; IDeltaResolver deltaResolver = api.DeltaResolver; switch (block.Type) { case BlockParameterType.Earliest: deltaHash = deltaCache.GenesisHash; blockNumber = 0; break; case BlockParameterType.Latest: deltaHash = deltaResolver.LatestDelta; blockNumber = deltaResolver.LatestDeltaNumber; break; case BlockParameterType.Pending: deltaHash = deltaResolver.LatestDelta; blockNumber = deltaResolver.LatestDeltaNumber; break; case BlockParameterType.BlockNumber: blockNumber = block.BlockNumber.Value; if (!deltaResolver.TryResolve(blockNumber, out deltaHash)) { return(null); } break; default: throw new ArgumentOutOfRangeException(); } DeltaWithCid deltaWithCid = api.GetDeltaWithCid(deltaHash); return(BuildBlock(deltaWithCid, blockNumber, api.HashProvider)); }
protected override Keccak Handle(byte[] transaction, IWeb3EthApi api) { PublicEntry publicEntry; try { Transaction tx = Rlp.Decode <Transaction>(transaction); EthereumEcdsa ecdsa = new EthereumEcdsa(MainnetSpecProvider.Instance, LimboLogs.Instance); tx.SenderAddress = ecdsa.RecoverAddress(tx, MainnetSpecProvider.IstanbulBlockNumber); tx.Timestamp = (UInt256)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); publicEntry = new PublicEntry { Data = (tx.Data ?? tx.Init).ToByteString(), GasLimit = (ulong)tx.GasLimit, GasPrice = tx.GasPrice.ToUint256ByteString(), Nonce = (ulong)tx.Nonce, SenderAddress = tx.SenderAddress.Bytes.ToByteString(), ReceiverAddress = tx.To?.Bytes.ToByteString() ?? ByteString.Empty, Amount = tx.Value.ToUint256ByteString(), Signature = new Protocol.Cryptography.Signature { RawBytes = ByteString.CopyFrom((byte)1) } }; } catch { try { TransactionBroadcast transactionBroadcast = TransactionBroadcast.Parser.ParseFrom(transaction); publicEntry = transactionBroadcast.PublicEntry; } catch (Exception) { throw new InvalidDataException($"Transaction data could not be deserialized into a {nameof(PublicEntry)}"); } } return(api.SendTransaction(publicEntry)); }
public static CallOutputTracer CallAndRestore(this IWeb3EthApi api, TransactionForRpc transactionCall, DeltaWithCid deltaWithCid) { var parentDelta = deltaWithCid.Delta; Keccak root = parentDelta.StateRoot.ToKeccak(); if (transactionCall.Gas == null) { transactionCall.Gas = parentDelta.GasLimit; } var publicEntry = api.ToPublicEntry(transactionCall, root); var newDelta = deltaWithCid.CreateOneOffDelta(publicEntry); CallOutputTracer callOutputTracer = new CallOutputTracer(); api.StateProvider.StateRoot = root; api.Executor.CallAndReset(newDelta, callOutputTracer); api.StateProvider.Reset(); api.StorageProvider.Reset(); return(callOutputTracer); }
private IEnumerable <TransactionForRpc> ConvertMempoolTransactions(IWeb3EthApi api, IList <PublicEntry> publicEntries) { foreach (var publicEntry in publicEntries) { yield return(new TransactionForRpc { GasPrice = publicEntry.GasPrice.ToUInt256(), BlockHash = null, BlockNumber = (UInt256)0x0, Nonce = publicEntry.Nonce, To = Web3EthApiExtensions.ToAddress(publicEntry.ReceiverAddress), From = Web3EthApiExtensions.ToAddress(publicEntry.SenderAddress), Value = publicEntry.Amount.ToUInt256(), Hash = publicEntry.GetHash(api.HashProvider), Data = publicEntry.Data.ToByteArray(), R = new byte[0], S = new byte[0], V = UInt256.Zero, Gas = publicEntry.GasLimit, TransactionIndex = (UInt256)0x0 }); } }
public static TransactionForRpc ToTransactionForRpc(this IWeb3EthApi api, DeltaWithCid deltaWithCid, int transactionIndex) { var(delta, deltaCid) = deltaWithCid; var publicEntry = delta.PublicEntries[transactionIndex]; var deltaNumber = delta.DeltaNumber; return(new TransactionForRpc { GasPrice = publicEntry.GasPrice.ToUInt256(), BlockHash = deltaCid, BlockNumber = (UInt256)deltaNumber, Nonce = publicEntry.Nonce, To = ToAddress(publicEntry.ReceiverAddress), From = ToAddress(publicEntry.SenderAddress), Value = publicEntry.Amount.ToUInt256(), Hash = publicEntry.GetHash(api.HashProvider), Data = publicEntry.Data.ToByteArray(), R = new byte[0], S = new byte[0], V = UInt256.Zero, Gas = publicEntry.GasLimit, TransactionIndex = (UInt256)transactionIndex }); }
protected override ReceiptForRpc Handle(Keccak txHash, IWeb3EthApi api) { if (api == null) { throw new ArgumentNullException(nameof(api)); } if (txHash == null) { return(null); } TransactionReceipt receipt = api.FindReceipt(txHash); if (receipt == null) { return(null); } ReceiptForRpc receiptForRpc = new ReceiptForRpc { TransactionHash = txHash, TransactionIndex = receipt.Index, BlockHash = receipt.DeltaHash, BlockNumber = receipt.DeltaNumber, CumulativeGasUsed = receipt.GasUsedTotal, GasUsed = receipt.GasUsed, From = new Address(receipt.Sender), To = receipt.Recipient == null ? null : new Address(receipt.Recipient), ContractAddress = receipt.ContractAddress == null ? null : new Address(receipt.ContractAddress), Logs = receipt.Logs.Select((l, idx) => new LogEntryForRpc(receipt, l, idx)).ToArray(), LogsBloom = new Bloom(receipt.Logs), Status = receipt.StatusCode, }; return(receiptForRpc); }
protected override BlockForRpc Handle(BlockParameter blockParameter, UInt256 positionIndex, IWeb3EthApi api) => null;
protected override BlockForRpc Handle(BlockParameter address, IWeb3EthApi api) { // this needs some kind of delta repository that knows current delta and is also able to retrieve past deltas throw new NotImplementedException(); }
protected override string Handle(IWeb3EthApi api) { return(string.Format("0x{0:X}", 2)); }
protected override UInt256?Handle(IWeb3EthApi api) => UInt256.Zero;
protected override long Handle(IWeb3EthApi api) { return((long)NetworkType.Devnet); }
protected override Keccak Handle(TransactionForRpc param1, IWeb3EthApi api) { throw new System.NotImplementedException(); }
protected abstract TResult Handle(IWeb3EthApi api);
public override object Handle(string[] parameters, IWeb3EthApi api, IJsonSerializer serializer) { return(Handle(api)); }
public abstract object Handle(string[] parameters, IWeb3EthApi api, IJsonSerializer serializer);
protected override Keccak Handle(byte[] param1, IWeb3EthApi api) { throw new System.NotImplementedException(); }