示例#1
0
        public bool GetIdByHash(string nodeHash, out ulong id)
        {
            id = 0;
            if (nodeHash.Equals(EmptyHash))
            {
                return(true);
            }
            if (_idCache.TryGetValue(nodeHash, out id))
            {
                return(true);
            }
            var idByte = _dbContext.Get(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(nodeHash)));

            if (!(idByte is null))
            {
                id = UInt64Utils.FromBytes(idByte);
                return(true);
            }
            id = _versionFactory.NewVersion();
            _idCache[nodeHash] = id;
            if (_idCache.Count >= _idCacheCapacity)
            {
                CommitIds();
            }
            return(false);
        }
示例#2
0
        public void SetDbShrinkDepth(ulong depth)
        {
            var prefix = EntryPrefix.DbShrinkDepth.BuildPrefix();

            Save(prefix, UInt64Utils.ToBytes(depth), false);
            Commit();
        }
示例#3
0
        public IHashTrieNode?TryGetNode(byte[] nodeHash, out List <byte[]> childrenHash)
        {
            childrenHash = new List <byte[]>();
            var prefix = EntryPrefix.VersionByHash.BuildPrefix(nodeHash);
            var idByte = _rocksDbContext.Get(prefix);

            if (idByte == null)
            {
                return(null);
            }
            ulong         id   = UInt64Utils.FromBytes(idByte);
            IHashTrieNode?node = TryGetNode(id);

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

            if (node.Type == NodeType.Internal)
            {
                foreach (var childId in node.Children)
                {
                    var child = TryGetNode(childId);
                    if (child == null)
                    {
                        return(null);
                    }
                    childrenHash.Add(child.Hash);
                }
            }
            return(node);
        }
示例#4
0
        public void WriteNodeToBatch(ulong id, IHashTrieNode node, RocksDbAtomicWrite tx)
        {
            var prefix = EntryPrefix.PersistentHashMap.BuildPrefix(id);

            tx.Put(prefix, NodeSerializer.ToBytes(node));
            var hashPrefix = EntryPrefix.VersionByHash.BuildPrefix(node.Hash);

            tx.Put(hashPrefix, UInt64Utils.ToBytes(id));
        }
示例#5
0
        public ulong GetOldestSnapshotInDb()
        {
            var prefix = EntryPrefix.OldestSnapshotInDb.BuildPrefix();
            var block  = Get(prefix);

            if (block is null)
            {
                return(0);
            }
            return(UInt64Utils.FromBytes(block));
        }
示例#6
0
        public ulong?GetDbShrinkDepth()
        {
            var prefix = EntryPrefix.DbShrinkDepth.BuildPrefix();
            var depth  = Get(prefix);

            if (depth is null)
            {
                return(null);
            }
            return(UInt64Utils.FromBytes(depth));
        }
示例#7
0
        public void CommitNodes()
        {
            RocksDbAtomicWrite tx = new RocksDbAtomicWrite(_dbContext);

            foreach (var item in _nodeCache)
            {
                tx.Put(EntryPrefix.PersistentHashMap.BuildPrefix(item.Key), NodeSerializer.ToBytes(item.Value));
                Console.WriteLine("Adding node to DB : " + item.Key);
            }
            ulong nodesCnt = UInt64Utils.FromBytes(_dbContext.Get(EntryPrefix.NodesDownloadedTillNow.BuildPrefix()));

            nodesCnt += (ulong)_nodeCache.Count;
            tx.Put(EntryPrefix.NodesDownloadedTillNow.BuildPrefix(), UInt64Utils.ToBytes(nodesCnt));
            tx.Commit();
            _nodeCache.Clear();
        }
示例#8
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");
        }
示例#9
0
        public void SetOldestSnapshotInDb(ulong block)
        {
            var prefix = EntryPrefix.OldestSnapshotInDb.BuildPrefix();

            Save(prefix, UInt64Utils.ToBytes(block));
        }
示例#10
0
        public void CommitIds()
        {
            RocksDbAtomicWrite tx = new RocksDbAtomicWrite(_dbContext);

            foreach (var item in _idCache)
            {
                tx.Put(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(item.Key)), UInt64Utils.ToBytes(item.Value));
            }
            tx.Commit();
            _idCache.Clear();
        }
示例#11
0
 public ulong GetDownloadedNodeCount()
 {
     return(UInt64Utils.FromBytes(_rocksDbContext.Get(EntryPrefix.NodesDownloadedTillNow.BuildPrefix())));
 }