Exemplo n.º 1
0
 public BlockRequestManager(IBlockSnapshot blockSnapshot, ulong maxBlock, NodeStorage nodeStorage)
 {
     _nodeStorage   = nodeStorage;
     _blockSnapshot = blockSnapshot;
     _done          = 0;
     _maxBlock      = maxBlock;
     for (ulong i = 1; i <= _maxBlock; i++)
     {
         nextBlocksToDownload.Add(i);
     }
 }
Exemplo n.º 2
0
        public void DownloadBlocks(NodeStorage nodeStorage, IBlockSnapshot blockSnapshot)
        {
            _blockRequestManager = new BlockRequestManager(blockSnapshot, Convert.ToUInt64(_blockNumber, 16), nodeStorage);

            while (!_blockRequestManager.Done())
            {
                if (!_peerManager.TryGetPeer(out var peer))
                {
                    Thread.Sleep(100);
                    continue;
                }
                if (!_blockRequestManager.TryGetBatch(out var batch))
                {
                    _peerManager.TryFreePeer(peer);
                    Thread.Sleep(100);
                    continue;
                }
                HandleRequest(peer, batch, 2);
            }
        }
Exemplo n.º 3
0
        public string GetTrie(string trieName, NodeStorage _nodeStorage)
        {
            string rootHash = DownloadRootHashByTrieName(trieName, _blockNumber);

            System.Console.WriteLine("Inside Get Trie. rootHash: " + rootHash);
            if (!rootHash.Equals(EmptyHash))
            {
                _requestManager.AddHash(rootHash);
            }
            while (!_requestManager.Done())
            {
                //    Console.WriteLine("GetTrie........");
                if (!_peerManager.TryGetPeer(out var peer))
                {
                    Thread.Sleep(100);
                    continue;
                }
                //    Console.WriteLine("GetTrie after TryGetPeer........");
                if (!_requestManager.TryGetHashBatch(out var hashBatch))
                {
                    _peerManager.TryFreePeer(peer);
                    Thread.Sleep(100);
                    continue;
                }
                //    Console.WriteLine("GetTrie after TryGetHashBatch........");
                HandleRequest(peer, hashBatch, 1);
            }
            _nodeStorage.CommitIds();
            _nodeStorage.CommitNodes();
            if (!rootHash.Equals(EmptyHash))
            {
                //            bool res =_nodeStorage.GetIdByHash(rootHash,out ulong id);
                //            bool flag = _requestManager.CheckConsistency(id);
                //            System.Console.WriteLine(trieName + " : consistency: " + flag);
            }
            return(rootHash);
        }
Exemplo n.º 4
0
        public static void StartSync(IStateManager stateManager,
                                     IRocksDbContext dbContext,
                                     ISnapshotIndexRepository snapshotIndexRepository,
                                     VersionFactory versionFactory,
                                     ulong blockNumber)
        {
            dbContext.Save(EntryPrefix.NodesDownloadedTillNow.BuildPrefix(), UInt64Utils.ToBytes(0));
            List <string> devnetNodes = new List <string>
            {
                "http://157.245.160.201:7070",
                "http://95.217.6.171:7070",
                "http://88.99.190.191:7070",
                "http://94.130.78.183:7070",
                "http://94.130.24.163:7070",
                "http://94.130.110.127:7070",
                "http://94.130.110.95:7070",
                "http://94.130.58.63:7070",
                "http://88.99.86.166:7070",
                "http://88.198.78.106:7070",
                "http://88.198.78.141:7070",
                "http://88.99.126.144:7070",
                "http://88.99.87.58:7070",
                "http://95.217.6.234:7070"
            };
//            List <string> onlyonenode = new List<string>
            List <string> localnetNodes = new List <string>
            {
                "http://127.0.0.1:7070",
                "http://127.0.0.1:7071",
                "http://127.0.0.1:7072"
            };

            var snapshot = stateManager.NewSnapshot();

            ISnapshot[] snapshots = new ISnapshot[] { snapshot.Balances,
                                                      snapshot.Contracts,
                                                      snapshot.Storage,
                                                      snapshot.Transactions,
                                                      snapshot.Events,
                                                      snapshot.Validators, };

            List <string>  urls           = devnetNodes;
            HybridQueue    hybridQueue    = new HybridQueue(dbContext);
            PeerManager    peerManager    = new PeerManager(urls);
            NodeStorage    nodeStorage    = new NodeStorage(dbContext, versionFactory);
            RequestManager requestManager = new RequestManager(nodeStorage, hybridQueue);
            Downloader     downloader     = new Downloader(peerManager, requestManager, blockNumber);

            string[] trieNames = new string[]
            {
                "Balances", "Contracts", "Storage", "Transactions", "Events", "Validators"
            };


            downloader.DownloadBlocks(nodeStorage, snapshot.Blocks);

            for (int i = 0; i < trieNames.Length; i++)
            {
                Logger.LogWarning($"Starting trie {trieNames[i]}");
                string rootHash  = downloader.GetTrie(trieNames[i], nodeStorage);
                bool   foundRoot = nodeStorage.GetIdByHash(rootHash, out ulong curTrieRoot);
                snapshots[i].SetCurrentVersion(curTrieRoot);
                Logger.LogWarning($"Ending trie {trieNames[i]} : {curTrieRoot}");
                Logger.LogWarning($"Max Queue Size {requestManager.maxQueueSize}");
                Logger.LogWarning($"Total Nodes downloaded: {versionFactory.CurrentVersion}");
            }

            blockNumber = Convert.ToUInt64(downloader.GetBlockNumber(), 16);
            stateManager.Approve();
            stateManager.Commit();
            snapshotIndexRepository.SaveSnapshotForBlock(blockNumber, snapshot);
            Logger.LogWarning($"Set state to block {blockNumber} complete");
        }
Exemplo n.º 5
0
 public RequestManager(NodeStorage nodeStorage, HybridQueue hybridQueue)
 {
     _nodeStorage = nodeStorage;
     _hybridQueue = hybridQueue;
 }