Exemplo n.º 1
0
 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));
 }
Exemplo n.º 2
0
        protected override UInt256 Handle(Address address, IWeb3EthApi api)
        {
            Delta delta     = api.GetLatestDeltaWithCid().Delta;
            var   stateRoot = delta.StateRoot.ToKeccak();

            return(api.StateReader.GetBalance(stateRoot, address));
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        protected override long?Handle(Keccak deltaHash, IWeb3EthApi api)
        {
            if (api.DeltaCache.TryGetOrAddConfirmedDelta(deltaHash.ToCid(), out var delta))
            {
                return(delta.PublicEntries.Count);
            }

            return(default);
Exemplo n.º 5
0
        protected override long?Handle(BlockParameter block, IWeb3EthApi api)
        {
            if (api.TryGetDeltaWithCid(block, out var delta))
            {
                return(delta.Delta.PublicEntries.Count);
            }

            return(default);
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        protected override long Handle(TransactionForRpc transactionCall, IWeb3EthApi api)
        {
            var deltaWithCid = api.GetLatestDeltaWithCid();

            var callOutputTracer = api.CallAndRestore(transactionCall, deltaWithCid);

            return(callOutputTracer.GasSpent);
        }
Exemplo n.º 8
0
        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);
Exemplo n.º 11
0
        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}'");
        }
Exemplo n.º 13
0
        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
            });
        }
Exemplo n.º 14
0
 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));
        }
Exemplo n.º 17
0
        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
         });
     }
 }
Exemplo n.º 19
0
        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
            });
        }
Exemplo n.º 20
0
        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;
Exemplo n.º 22
0
 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();
 }
Exemplo n.º 23
0
 protected override string Handle(IWeb3EthApi api)
 {
     return(string.Format("0x{0:X}", 2));
 }
Exemplo n.º 24
0
 protected override UInt256?Handle(IWeb3EthApi api) => UInt256.Zero;
Exemplo n.º 25
0
 protected override long Handle(IWeb3EthApi api)
 {
     return((long)NetworkType.Devnet);
 }
Exemplo n.º 26
0
 protected override Keccak Handle(TransactionForRpc param1, IWeb3EthApi api)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 27
0
 protected abstract TResult Handle(IWeb3EthApi api);
Exemplo n.º 28
0
 public override object Handle(string[] parameters, IWeb3EthApi api, IJsonSerializer serializer)
 {
     return(Handle(api));
 }
Exemplo n.º 29
0
 public abstract object Handle(string[] parameters, IWeb3EthApi api, IJsonSerializer serializer);
Exemplo n.º 30
0
 protected override Keccak Handle(byte[] param1, IWeb3EthApi api)
 {
     throw new System.NotImplementedException();
 }