Esempio 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");
 }
Esempio 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);
        }
Esempio 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()));
        }
Esempio n. 4
0
 private JArray GetWork()
 {
     return(new JArray(
                Web3DataFormatUtils.Web3Data(new UInt256()),
                Web3DataFormatUtils.Web3Data(new UInt256()),
                Web3DataFormatUtils.Web3Data(new UInt256())
                ));
 }
Esempio 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));
        }
Esempio 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));
        }
Esempio 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);
        }
Esempio 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)));
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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));
        }
Esempio 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);
        }
Esempio 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));
        }
Esempio 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));
        }
Esempio 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));
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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));
            }
        }
Esempio 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));
        }
Esempio 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));
        }
Esempio 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);
        }
Esempio n. 22
0
 private string CompileSerpent(string sourceCode)
 {
     return(Web3DataFormatUtils.Web3Data("".HexToBytes()));
     //throw new ApplicationException("Not implemented");
 }
Esempio 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()));
            }
        }
Esempio 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));
        }
Esempio n. 25
0
 public string GetDownloadedNodesTillNow()
 {
     return(Web3DataFormatUtils.Web3Number(_nodeRetrieval.GetDownloadedNodeCount()));
 }
Esempio n. 26
0
 public string GetBlockNumber()
 {
     return(Web3DataFormatUtils.Web3Number(_blockManager.GetHeight()));
 }
Esempio n. 27
0
 public string GetStateHashFromTrieRoots(string blockTag)
 {
     return(Web3DataFormatUtils.Web3Data(SingleNodeHashFromRoot(blockTag)));
 }
Esempio n. 28
0
 public JArray GetAccounts()
 {
     return(new JArray {
         Web3DataFormatUtils.Web3Data(_systemContractReader.NodeAddress())
     });
 }