Esempio n. 1
0
        public string?GetBlockTransactionsCountByNumber(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return(Web3DataFormatUtils.Web3Number(_transactionPool.Size()));
            }
            var block = _blockManager.GetByHeight((ulong)blockNumber);

            return(block == null ? null : Web3DataFormatUtils.Web3Number((ulong)block !.TransactionHashes.Count));
        }
Esempio n. 2
0
        public string CheckNodeHashes(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);
            IBlockchainSnapshot blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber);
            bool res = true;

            ISnapshot[] snapshots = blockchainSnapshot.GetAllSnapshot();
            foreach (var snapshot in snapshots)
            {
                res &= snapshot.IsTrieNodeHashesOk();
            }
            return(Web3DataFormatUtils.Web3Number(Convert.ToUInt64(res)));
        }
Esempio n. 3
0
        public string GetTransactionCount(string from, string blockId)
        {
            ulong nonce;

            if (blockId.Equals("pending"))
            {
                nonce = _transactionPool.GetNextNonceForAddress(from.HexToUInt160());
            }
            else
            {
                nonce = GetSnapshotByTag(blockId) !.Transactions.GetTotalTransactionCount(from.HexToUInt160());
            }
            return(Web3DataFormatUtils.Web3Number(nonce));
        }
Esempio n. 4
0
        public JObject GetChildrenByVersion(string versionTag)
        {
            var version = GetVersionNumberByTag(versionTag);

            if (version == null)
            {
                return new JObject {
                }
            }
            ;
            var node = _nodeRetrieval.TryGetNode((ulong)version);

            if (node == null)
            {
                return new JObject {
                }
            }
            ;

            JArray children = new JArray {
            };

            if (node.Type == NodeType.Internal)
            {
                foreach (var childId in node.Children)
                {
                    var child = _nodeRetrieval.TryGetNode(childId);

                    if (child == null)
                    {
                        return new JObject {
                        }
                    }
                    ;
                    children.Add(Web3DataFormatUtils.Web3Node(child));
                }
            }
            JObject nodeWithChildren = new JObject {
            };

            nodeWithChildren[Web3DataFormatUtils.Web3Number((ulong)version)] = children;
            return(nodeWithChildren);
        }
Esempio n. 5
0
        public string GetRootVersionByTrieName(string trieName, string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return("0x");
            }
            IBlockchainSnapshot blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber);
            ISnapshot?          snapshot           = blockchainSnapshot.GetSnapshot(trieName);

            if (snapshot is null)
            {
                return("0x");
            }
            else
            {
                return(Web3DataFormatUtils.Web3Number(snapshot.Version));
            }
        }
Esempio n. 6
0
        public JObject?GetStateByNumber(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return(null);
            }
            IBlockchainSnapshot blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber);
            var state = new JObject {
            };

            string[]    trieNames = new string[] { "Balances", "Contracts", "Storage", "Transactions", "Blocks", "Events", "Validators" };
            ISnapshot[] snapshots = blockchainSnapshot.GetAllSnapshot();
            for (var i = 0; i < trieNames.Length; i++)
            {
                state[trieNames[i]]          = Web3DataFormatUtils.Web3Trie(snapshots[i].GetState());
                state[trieNames[i] + "Root"] = Web3DataFormatUtils.Web3Number(snapshots[i].Version);
            }

            return(state);
        }
Esempio n. 7
0
 public string GetDownloadedNodesTillNow()
 {
     return(Web3DataFormatUtils.Web3Number(_nodeRetrieval.GetDownloadedNodeCount()));
 }
Esempio n. 8
0
 public string GetBlockNumber()
 {
     return(Web3DataFormatUtils.Web3Number(_blockManager.GetHeight()));
 }
Esempio n. 9
0
        public string GetBalance(string address, string tag)
        {
            var addressUint160 = address.HexToUInt160();

            if (tag == "pending")
            {
                // Get all transaction from pool
                var transactions = _transactionPool.Transactions;

                List <TransactionReceipt> txReceipts = new List <TransactionReceipt>();

                foreach (var tx in transactions)
                {
                    var from = tx.Value.Transaction.From.ToHex();

                    if (address == from)
                    {
                        txReceipts.Add(tx.Value);
                    }
                }

                // Sort on the basis of nonce
                txReceipts = txReceipts.OrderBy(receipt => receipt, new ReceiptComparer()).ToList();

                // Get current address nonce
                var transactionRepository = _stateManager.CurrentSnapshot.Transactions;
                var currNonce             = transactionRepository.GetTotalTransactionCount(addressUint160);

                // Virtually execute the txs in nonce order
                var availableBalance = GetSnapshotByTag("latest") !.Balances.GetBalance(addressUint160);

                foreach (var tx in txReceipts)
                {
                    var from = tx.Transaction.From.ToHex();

                    if (currNonce == tx.Transaction.Nonce)
                    {
                        // Executing the transaction
                        var gasp   = new Money(tx.Transaction.GasPrice);
                        var gasl   = new Money(tx.Transaction.GasLimit);
                        var txamnt = new Money(tx.Transaction.Value);

                        if (availableBalance - txamnt - (gasl * gasp) >= Money.Parse("0"))
                        {
                            // If transaction can be executed
                            availableBalance = availableBalance - txamnt - (gasl * gasp);
                            currNonce       += 1;
                        }
                        else if (availableBalance - (gasl * gasp) >= Money.Parse("0"))
                        {
                            // If balance is not enough for transaction
                            availableBalance = availableBalance - (gasl * gasp);
                            currNonce       += 1;
                        }
                    }
                }

                return(Web3DataFormatUtils.Web3Number(availableBalance.ToWei().ToUInt256()));
            }
            else
            {
                var availableBalance = GetSnapshotByTag(tag) !.Balances.GetBalance(addressUint160);
                return(Web3DataFormatUtils.Web3Number(availableBalance.ToWei().ToUInt256()));
            }
        }