public void Test_Web3Data()
        {
            Assert.AreEqual("0x", Web3DataFormatUtils.Web3Data(Enumerable.Empty <byte>()));
            Assert.AreEqual("0x41", Web3DataFormatUtils.Web3Data(new byte[] { 65 }));
            Assert.AreEqual("0x004200", Web3DataFormatUtils.Web3Data(new byte[] { 0, 66, 0 }));
            Assert.AreEqual("0x0f0f0f", Web3DataFormatUtils.Web3Data(new byte[] { 15, 15, 15 }));

            Assert.AreEqual(
                "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
                Web3DataFormatUtils.Web3Data(new byte[] { 0xc0 }.Keccak())
                );
            Assert.AreEqual(
                "0x0000000000000000000000000000000000000000000000000000000000000001",
                Web3DataFormatUtils.Web3Data(BigInteger.One.ToUInt256())
                );
            Assert.AreEqual(
                "0x0000000000000000000000000000000000000003",
                Web3DataFormatUtils.Web3Data(ContractRegisterer.StakingContract)
                );
            var address = "023f7d80bc1c1f2a93bca97e81b9f3073150e15cef78b8d37a7ec4c947993ad5e7"
                          .HexToBytes()
                          .ToPublicKey()
                          .GetAddress();

            Assert.AreEqual("0xa4aee5f4599fa96cfca74f339c0544635e70d152", Web3DataFormatUtils.Web3Data(address));
        }
示例#2
0
        public JArray GetLogs(ulong start, ulong finish, List <UInt160> addresses, List <List <UInt256> > allTopics)
        {
            var jArray = new JArray();

            for (var blockNumber = start; blockNumber <= finish; blockNumber++)
            {
                var block = _blockManager.GetByHeight(blockNumber);
                if (block == null)
                {
                    continue;
                }
                var txs = block !.TransactionHashes;
                foreach (var tx in txs)
                {
                    var txEvents = _stateManager.LastApprovedSnapshot.Events.GetTotalTransactionEvents(tx);
                    for (var i = 0; i < txEvents; i++)
                    {
                        var txEventObj = _stateManager.LastApprovedSnapshot.Events.GetEventByTransactionHashAndIndex(tx,
                                                                                                                     (uint)i);
                        var txEvent = txEventObj.Event;
                        if (txEvent is null)
                        {
                            continue;
                        }

                        if (!addresses.Any(a => txEvent.Contract.Equals(a)))
                        {
                            continue;
                        }

                        var txTopics = new List <UInt256>();
                        txTopics.Add(txEvent.SignatureHash);
                        if (txEventObj.Topics != null)
                        {
                            foreach (var topic in txEventObj.Topics)
                            {
                                txTopics.Add(topic);
                            }
                        }

                        if (!MatchTopics(allTopics, txTopics))
                        {
                            Logger.LogInformation($"Skip event with signature [{txEvent.SignatureHash.ToHex()}]");
                            continue;
                        }

                        if (txEvent.BlockHash is null || txEvent.BlockHash.IsZero())
                        {
                            txEvent.BlockHash = block.Hash;
                        }

                        jArray.Add(Web3DataFormatUtils.Web3Event(txEventObj, blockNumber, block.Hash));
                    }
                }
            }

            return(jArray);
        }
示例#3
0
        IDictionary <ulong, IHashTrieNode> DownloadChild(ulong version)
        {
            Logger.LogWarning($"downloding childs with of node with version {version}");
            JObject?receivedInfo = (JObject?)_CallJsonRPCAPI("la_getChildByVersion", new JArray {
                Web3DataFormatUtils.Web3Number(version)
            }, _peerURL);
            IDictionary <ulong, IHashTrieNode> childs = Web3DataFormatUtils.TrieFromJson(receivedInfo);

            Logger.LogWarning($"Completed downloding childs with of node with version {version}");
            return(childs);
        }
示例#4
0
        private IHashTrieNode DownloadNode(ulong version)
        {
            Logger.LogWarning($"downloding node with version {version}");
            JObject?receivedInfo = (JObject?)_CallJsonRPCAPI("la_getNodeByVersion", new JArray {
                Web3DataFormatUtils.Web3Number(version)
            }, _peerURL);
            IHashTrieNode node = Web3DataFormatUtils.NodeFromJson(receivedInfo);

            Logger.LogWarning($"Completed downloding node with version {version}");
            return(node);
        }
示例#5
0
        public ulong DownloadRoot(string trieName)
        {
            Logger.LogWarning($"downloading root version of {trieName} trie");
            ulong root = Convert.ToUInt64((string)_CallJsonRPCAPI("la_getRootVersionByTrieName",
                                                                  new JArray {
                trieName, Web3DataFormatUtils.Web3Number(_blockNumber)
            }, _peerURL), 16);

            Logger.LogWarning($"Completed downloading root version of {trieName} trie, root = {root}");
            return(root);
        }
示例#6
0
 public JObject FormatResult(string msg, UInt256 hash, int?v)
 {
     if (v is null)
     {
         v = 0;
     }
     return(new JObject
     {
         ["message"] = msg,
         ["hash"] = Web3DataFormatUtils.Web3Data(hash),
         ["v"] = Web3DataFormatUtils.Web3Number((ulong)v),
     });
 }
示例#7
0
        public Downloader(PeerManager peerManager, RequestManager requestManager, ulong blockNumber)
        {
            _peerManager    = peerManager;
            _requestManager = requestManager;
            if (blockNumber == 0)
            {
                _blockNumber = DownloadLatestBlockNumber();
            }
            else
            {
                _blockNumber = Web3DataFormatUtils.Web3Number(blockNumber);
            }

            System.Console.WriteLine("blocknumber: " + Convert.ToUInt64(_blockNumber, 16));
        }
示例#8
0
        // Changed GetBalance to public
        public void TestGetBalancePending()
        {
            var tx = TestUtils.GetRandomTransaction(false);

            _stateManager.LastApprovedSnapshot.Balances.AddBalance(tx.Transaction.From, Money.Parse("1000"));
            var result = _transactionPool.Add(tx);

            Assert.AreEqual(OperatingError.Ok, result);

            var beforeBalance     = _stateManager.LastApprovedSnapshot.Balances.GetBalance(tx.Transaction.From);
            var sentValue         = new Money(tx.Transaction.Value);
            var afterBalance      = beforeBalance - sentValue - new Money(tx.Transaction.GasLimit * tx.Transaction.GasPrice);
            var knownAfterBalance = Money.Parse("1000") - sentValue - new Money(tx.Transaction.GasLimit * tx.Transaction.GasPrice);

            Assert.AreEqual(knownAfterBalance, afterBalance);
            var balance = _apiService.GetBalance(tx.Transaction.From.ToHex(), "pending");

            Assert.IsTrue(Web3DataFormatUtils.Web3Number(afterBalance.ToWei().ToUInt256()) == balance);
        }
示例#9
0
        public string SignMessage(string message, bool useNewChainId)
        {
            if (_privateWallet.IsLocked())
            {
                throw new Exception("wallet is locked");
            }
            var keyPair = _privateWallet.EcdsaKeyPair;

            Logger.LogInformation($"public key: {keyPair.PublicKey.ToHex()}, address: {keyPair.PublicKey.GetAddress().ToHex()}");
            try
            {
                var msg       = message.HexToBytes();
                var signature = Crypto.Sign(msg, keyPair.PrivateKey.Encode(), useNewChainId);
                return(Web3DataFormatUtils.Web3Data(signature));
            }
            catch (Exception exception)
            {
                Logger.LogWarning($"Got exception trying to sign message: {exception}");
                throw;
            }
        }
示例#10
0
        public JArray SyncPendingTransaction(ulong id, BlockchainEventFilterParams filter, bool poll)
        {
            var results = new JArray();

            var pendingTx = filter.PendingTransactionList;
            var poolTx    = _transactionPool.Transactions.Keys.ToArray();

            Array.Sort(poolTx, (x, y) => UInt256Utils.Compare(x, y));

            // comparing two sorted list listA and listB in O(listA.Count + listB.Count)

            int iter = 0;

            foreach (var txHash in poolTx)
            {
                if (txHash is null)
                {
                    continue;
                }

                while (iter < pendingTx.Count && UInt256Utils.Compare(txHash, pendingTx[iter]) > 0)
                {
                    iter++;
                }
                if (iter == pendingTx.Count || UInt256Utils.Compare(txHash, pendingTx[iter]) < 0)
                {
                    results.Add(Web3DataFormatUtils.Web3Data(txHash));
                }
            }

            if (poll)
            {
                // Pending transaction list in filter must be sorted for further optimization
                filter.PendingTransactionList = new List <UInt256>(poolTx.ToList());
                filter.PollingTime            = TimeUtils.CurrentTimeMillis();
                _filters[id] = filter;
            }
            return(results);
        }
示例#11
0
        public bool CheckConsistency(ulong rootId)
        {
            Queue <ulong> queue = new Queue <ulong>();

            queue.Enqueue(rootId);
            while (queue.Count > 0)
            {
                ulong cur = queue.Dequeue();
                System.Console.WriteLine("id: " + cur);
                //    System.Console.WriteLine($"id: {_nodeStorage.GetIdByHash(cur)}");
                if (_nodeStorage.TryGetNode(cur, out IHashTrieNode? trieNode))
                {
                    JObject node = Web3DataFormatUtils.Web3Node(trieNode);
                    //    Console.WriteLine("printing Node");
                    //    Console.WriteLine(node);
                    var nodeType = (string)node["NodeType"];
                    //        if (nodeType == null) return false;

                    if (nodeType.Equals("0x1")) // internal node
                    {
                        var jsonChildren = (JArray)node["Children"];
                        foreach (var jsonChild in jsonChildren)
                        {
                            ulong childId = Convert.ToUInt64((string)jsonChild, 16);
                            Console.WriteLine("Enqueueing child: " + childId);
                            queue.Enqueue(childId);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Not Found: " + cur);
                    return(false);
                }
            }
            return(true);
        }
示例#12
0
        public void Test_Web3Number()
        {
            Assert.AreEqual("0x0", Web3DataFormatUtils.Web3Number(0));
            Assert.AreEqual("0x1", Web3DataFormatUtils.Web3Number(1));
            Assert.AreEqual("0xa", Web3DataFormatUtils.Web3Number(10));
            Assert.AreEqual("0x10", Web3DataFormatUtils.Web3Number(16));
            Assert.AreEqual("0x41", Web3DataFormatUtils.Web3Number(65));
            Assert.AreEqual("0xff", Web3DataFormatUtils.Web3Number(255));
            Assert.AreEqual("0x400", Web3DataFormatUtils.Web3Number(1024));

            Assert.AreEqual("0x0", Web3DataFormatUtils.Web3Number(UInt256Utils.Zero));
            Assert.AreEqual("0x1", Web3DataFormatUtils.Web3Number(BigInteger.Parse("1").ToUInt256()));
            Assert.AreEqual("0xa", Web3DataFormatUtils.Web3Number(BigInteger.Parse("10").ToUInt256()));
            Assert.AreEqual("0x10", Web3DataFormatUtils.Web3Number(BigInteger.Parse("16").ToUInt256()));
            Assert.AreEqual("0x41", Web3DataFormatUtils.Web3Number(BigInteger.Parse("65").ToUInt256()));
            Assert.AreEqual("0xff", Web3DataFormatUtils.Web3Number(BigInteger.Parse("255").ToUInt256()));
            Assert.AreEqual("0x400", Web3DataFormatUtils.Web3Number(BigInteger.Parse("1024").ToUInt256()));
            // 2 ** 252 - 1
            Assert.AreEqual(
                "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
                Web3DataFormatUtils.Web3Number(
                    BigInteger
                    .Parse("7237005577332262213973186563042994240829374041602535252466099000494570602495")
                    .ToUInt256()
                    )
                );
            // 2 ** 256 - 1
            Assert.AreEqual(
                "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
                Web3DataFormatUtils.Web3Number(
                    BigInteger
                    .Parse("115792089237316195423570985008687907853269984665640564039457584007913129639935")
                    .ToUInt256()
                    )
                );
        }