Ejemplo n.º 1
0
 private string Sign(string address, string message)
 {
     // TODO: implement message signing
     //var addressUint160 = address.HexToUInt160();
     return(Web3DataFormatUtils.Web3Data("".HexToBytes()));
     //throw new ApplicationException("Not implemented yet");
 }
Ejemplo n.º 2
0
        public JObject GetChildrenByHash(string nodeHash)
        {
            IHashTrieNode?node = _nodeRetrieval.TryGetNode(HexUtils.HexToBytes(nodeHash), out var childrenHash);

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

            JArray children = new JArray {
            };

            foreach (var childHash in childrenHash)
            {
                JObject child = GetNodeByHash(Web3DataFormatUtils.Web3Data(childHash));

                if (child.Count == 0)
                {
                    return new JObject {
                    }
                }
                ;
                children.Add(child);
            }

            JObject nodeHashWithChildren = new JObject {
            };

            nodeHashWithChildren[Web3DataFormatUtils.Web3Data(node.Hash)] = children;
            return(nodeHashWithChildren);
        }
Ejemplo n.º 3
0
        public string GetStorageAt(string address, string position, string blockTag)
        {
            var blockNumber        = GetBlockNumberByTag(blockTag);
            var blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber !);
            var value = blockchainSnapshot.Storage.GetValue(address.HexToUInt160(), position.HexToUInt256());

            return(Web3DataFormatUtils.Web3Data(value.ToHex().HexToBytes()));
        }
Ejemplo n.º 4
0
 private JArray GetWork()
 {
     return(new JArray(
                Web3DataFormatUtils.Web3Data(new UInt256()),
                Web3DataFormatUtils.Web3Data(new UInt256()),
                Web3DataFormatUtils.Web3Data(new UInt256())
                ));
 }
Ejemplo n.º 5
0
        public JObject GetNodeByHash(string nodeHash)
        {
            IHashTrieNode?node = _nodeRetrieval.TryGetNode(HexUtils.HexToBytes(nodeHash), out var childrenHash);

            if (node == null)
            {
                return new JObject {
                }
            }
            ;
            return(Web3DataFormatUtils.Web3NodeWithChildrenHash(node, childrenHash));
        }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
        private JArray GetCurrentValidators()
        {
            var    allValidators = _stateManager.CurrentSnapshot.Validators.GetValidatorsPublicKeys().ToArray();
            JArray validators    = new JArray {
            };

            foreach (var validator in allValidators)
            {
                validators.Add(Web3DataFormatUtils.Web3Data(validator.Buffer.ToByteArray()));
            }
            return(validators);
        }
Ejemplo n.º 8
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)));
        }
Ejemplo n.º 9
0
        public static JObject Web3Trie(IDictionary <ulong, IHashTrieNode> trie)
        {
            var jsonTrie = new JObject {
            };

            foreach (var item in trie)
            {
                var version = item.Key;
                var node    = item.Value;
                jsonTrie[Web3Number(version)] = Web3DataFormatUtils.Web3Node(node);
            }
            return(jsonTrie);
        }
Ejemplo n.º 10
0
        public JObject?GetStateHashFromTrieRootsRange(string startBlockTag, string endBlockTag)
        {
            ulong startBlock = startBlockTag.HexToUlong(), endBlock = endBlockTag.HexToUlong();
            var   stateHash = new JObject {
            };

            for (ulong curBlock = startBlock; curBlock <= endBlock; curBlock++)
            {
                stateHash[curBlock.ToHex(false)] =
                    Web3DataFormatUtils.Web3Data(SingleNodeHashFromRoot(curBlock.ToHex(false)));
            }

            return(stateHash);
        }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
0
        public JObject?GetAllTrieRootsHash(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);
            IBlockchainSnapshot blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber);
            var trieRootsHash = new JObject {
            };

            ISnapshot[] snapshots     = blockchainSnapshot.GetAllSnapshot();
            string[]    snapshotNames = new string[] { "Balances", "Contracts", "Storage", "Transactions", "Blocks", "Events", "Validators" };
            for (int i = 0; i < snapshots.Length; i++)
            {
                trieRootsHash[snapshots[i] + "Hash"] = Web3DataFormatUtils.Web3Data(snapshots[i].Hash);
            }
            return(trieRootsHash);
        }
Ejemplo n.º 13
0
        public string?GetBlockRawByNumber(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return(null);
            }
            Block?block = _blockManager.GetByHeight((ulong)blockNumber);

            if (block == null)
            {
                return(null);
            }
            return(Web3DataFormatUtils.Web3BlockRaw(block));
        }
Ejemplo n.º 14
0
        public string GetRootHashByTrieName(string trieName, string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

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

            if (snapshot == null)
            {
                return("0x");
            }
            return(Web3DataFormatUtils.Web3Data(snapshot.Hash));
        }
Ejemplo n.º 15
0
        public JObject GetWithdrawStakeTransaction(JObject opts)
        {
            var from = opts["from"]?.ToString().HexToBytes().ToUInt160() ??
                       throw new Exception($"\"from\" {opts["from"]} is not valid");

            var validatorPubKey = opts["validatorPublicKey"]?.ToString().HexToBytes() ??
                                  throw new Exception($"\"validatorPublicKey\" {opts["validatorPublicKey"]} is not valid");

            var tx = _transactionBuilder.InvokeTransaction(
                from,
                ContractRegisterer.StakingContract,
                Money.Zero,
                StakingInterface.MethodWithdrawStake,
                validatorPubKey
                );

            return(Web3DataFormatUtils.Web3UnsignedTransaction(tx, true));
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public JArray GetBlockRawByNumberBatch(List <string> blockTagList)
        {
            JArray blockRawList = new JArray {
            };

            foreach (var blockTag in blockTagList)
            {
                var blockNumber = GetBlockNumberByTag(blockTag);
                if (blockNumber == null)
                {
                    return(null);
                }
                Block?block = _blockManager.GetByHeight((ulong)blockNumber);
                if (block != null)
                {
                    blockRawList.Add(Web3DataFormatUtils.Web3BlockRaw(block));
                }
            }
            return(blockRawList);
        }
Ejemplo n.º 18
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));
            }
        }
Ejemplo n.º 19
0
        public JObject GetNodeByVersion(string versionTag)
        {
            var version = GetVersionNumberByTag(versionTag);

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

            if (node == null)
            {
                return new JObject {
                }
            }
            ;
            return(Web3DataFormatUtils.Web3Node(node));
        }
Ejemplo n.º 20
0
        public JObject GetStakeTransaction(JObject opts)
        {
            var staker = opts["stakerAddress"]?.ToString().HexToBytes().ToUInt160() ??
                         throw new Exception($"\"stakerAddress\" {opts["stakerAddress"]} is not valid");

            var validatorPubKey = opts["validatorPublicKey"]?.ToString().HexToBytes() ??
                                  throw new Exception($"\"validatorPublicKey\" {opts["validatorPublicKey"]} is not valid");

            var stakeAmount = Money.Parse(opts["stakeAmount"]?.ToString() ??
                                          throw new Exception($"\"stakeAmount\" {opts["stakeAmount"]} is not valid")
                                          );
            var tx = _transactionBuilder.InvokeTransaction(
                staker,
                ContractRegisterer.StakingContract,
                Money.Zero,
                StakingInterface.MethodBecomeStaker,
                validatorPubKey,
                (object)stakeAmount.ToUInt256()
                );

            return(Web3DataFormatUtils.Web3UnsignedTransaction(tx, true));
        }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
0
 private string CompileSerpent(string sourceCode)
 {
     return(Web3DataFormatUtils.Web3Data("".HexToBytes()));
     //throw new ApplicationException("Not implemented");
 }
Ejemplo n.º 23
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()));
            }
        }
Ejemplo n.º 24
0
        public JObject?GetBlockByNumber(string blockTag, bool fullTx)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

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

            if (block == null)
            {
                return(null);
            }

            ulong gasUsed = 0;
            var   txArray = new JArray();

            if (block.TransactionHashes.Count <= 0)
            {
                return(Web3DataFormatUtils.Web3Block(block !, gasUsed, txArray));
            }
            List <TransactionReceipt> txs = new List <TransactionReceipt>();

            try
            {
                foreach (var txHash in block.TransactionHashes)
                {
                    Logger.LogInformation($"Transaction hash {txHash.ToHex()} in block {blockNumber}");
                    TransactionReceipt?tx = _transactionManager.GetByHash(txHash);
                    if (tx is null)
                    {
                        Logger.LogWarning($"Transaction not found in DB {txHash.ToHex()}");
                    }
                    else
                    {
                        txs.Add(tx);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogWarning($"Exception {e}");
                Logger.LogWarning($"block {block!.Hash},  {block.Header.Index}, {block.TransactionHashes.Count}");
                foreach (var txhash in block.TransactionHashes)
                {
                    Logger.LogWarning($"txhash {txhash.ToHex()}");
                }
            }

            try
            {
                gasUsed = txs.Aggregate(gasUsed, (current, tx) => current + tx.GasUsed);
            }
            catch (Exception e)
            {
                Logger.LogWarning($"Exception {e}");
                Logger.LogWarning($"txs {txs}");
                foreach (var tx in txs)
                {
                    if (tx is null)
                    {
                        continue;
                    }
                    Logger.LogWarning($"tx {tx.Hash.ToHex()} {tx.GasUsed} {tx.Status} {tx.IndexInBlock}");
                }
            }

            if (fullTx)
            {
                txArray = Web3DataFormatUtils.Web3BlockTransactionArray(txs, block !.Hash, block !.Header.Index);
            }
            else
            {
                foreach (var tx in txs)
                {
                    txArray.Add(Web3DataFormatUtils.Web3Data(tx.Hash));
                }
            }
            return(Web3DataFormatUtils.Web3Block(block !, gasUsed, txArray));
        }
Ejemplo n.º 25
0
 public string GetDownloadedNodesTillNow()
 {
     return(Web3DataFormatUtils.Web3Number(_nodeRetrieval.GetDownloadedNodeCount()));
 }
Ejemplo n.º 26
0
 public string GetBlockNumber()
 {
     return(Web3DataFormatUtils.Web3Number(_blockManager.GetHeight()));
 }
Ejemplo n.º 27
0
 public string GetStateHashFromTrieRoots(string blockTag)
 {
     return(Web3DataFormatUtils.Web3Data(SingleNodeHashFromRoot(blockTag)));
 }
Ejemplo n.º 28
0
 public JArray GetAccounts()
 {
     return(new JArray {
         Web3DataFormatUtils.Web3Data(_systemContractReader.NodeAddress())
     });
 }