예제 #1
0
        public Synchroniser(SyncState syncState,
                            IPeerSyncManager peerSyncManager,
                            IDeltaCache deltaCache,
                            IDeltaHeightWatcher deltaHeightWatcher,
                            IDeltaHashProvider deltaHashProvider,
                            IDeltaDfsReader deltaDfsReader,
                            IDeltaIndexService deltaIndexService,
                            IMapperProvider mapperProvider,
                            IUserOutput userOutput,
                            ILogger logger,
                            int rangeSize        = 20, //cannot go over 20 until udp network fragmentation is fixed
                            IScheduler scheduler = null)
        {
            State               = syncState;
            _peerSyncManager    = peerSyncManager;
            _deltaHeightWatcher = deltaHeightWatcher;
            DeltaCache          = deltaCache;
            _rangeSize          = rangeSize;
            _deltaIndexService  = deltaIndexService;
            _mapperProvider     = mapperProvider;
            _userOutput         = userOutput;

            _deltaHashProvider = deltaHashProvider;

            _logger = logger;

            _syncCompletedReplaySubject = new ReplaySubject <ulong>(1, scheduler ?? Scheduler.Default);
            SyncCompleted = _syncCompletedReplaySubject.AsObservable();
        }
예제 #2
0
        public DeltaDfsReaderTests()
        {
            var hashingAlgorithm = HashingAlgorithm.GetAlgorithmMetadata("blake2b-256");

            _hashProvider = new HashProvider(hashingAlgorithm);
            _dfs          = Substitute.For <IDfs>();
            _logger       = Substitute.For <ILogger>();

            _dfsReader = new DeltaDfsReader(_dfs, _logger);
        }
예제 #3
0
        public void Init()
        {
            var hashingAlgorithm = HashingAlgorithm.GetAlgorithmMetadata("keccak-256");

            _hashProvider = new HashProvider(hashingAlgorithm);
            _dfsService   = Substitute.For <IDfsService>();
            _logger       = Substitute.For <ILogger>();

            _dfsReader = new DeltaDfsReader(_dfsService, _logger);
        }
        public DeltaCacheTests()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"));
            _memoryCache  = Substitute.For <IMemoryCache>();
            _dfsReader    = Substitute.For <IDeltaDfsReader>();
            _logger       = Substitute.For <ILogger>();

            var tokenProvider = Substitute.For <IDeltaCacheChangeTokenProvider>();

            tokenProvider.GetChangeToken().Returns(Substitute.For <IChangeToken>());

            _deltaCache = new DeltaCache(_hashProvider, _memoryCache, _dfsReader, tokenProvider, _logger);
        }
예제 #5
0
        public void Init()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            _memoryCache  = Substitute.For <IMemoryCache>();
            _dfsReader    = Substitute.For <IDeltaDfsReader>();
            _logger       = Substitute.For <ILogger>();

            var tokenProvider = Substitute.For <IDeltaCacheChangeTokenProvider>();

            tokenProvider.GetChangeToken().Returns(Substitute.For <IChangeToken>());

            var storageProvider = Substitute.For <IStorageProvider>();
            var stateProvider   = Substitute.For <IStateProvider>();

            stateProvider.StateRoot.Returns(Keccak.Zero);

            _deltaCache = new DeltaCache(_hashProvider, _memoryCache, _dfsReader, tokenProvider, storageProvider, stateProvider, new StateDb(), Substitute.For <IDeltaIndexService>(), _logger);
        }
예제 #6
0
        public DeltaCache(IHashProvider hashProvider,
                          IMemoryCache memoryCache,
                          IDeltaDfsReader dfsReader,
                          IDeltaCacheChangeTokenProvider changeTokenProvider,
                          IStorageProvider storageProvider,
                          IStateProvider stateProvider,
                          ISnapshotableDb stateDb,
                          IDeltaIndexService deltaIndexService,
                          ILogger logger)
        {
            _deltaIndexService = deltaIndexService;

            stateProvider.CreateAccount(TruffleTestAccount, 1_000_000_000.Kat());
            stateProvider.CreateAccount(CatalystTruffleTestAccount, 1_000_000_000.Kat());

            storageProvider.Commit();
            stateProvider.Commit(CatalystGenesisSpec.Instance);

            storageProvider.CommitTrees();
            stateProvider.CommitTree();

            stateDb.Commit();

            var genesisDelta = new Delta
            {
                TimeStamp = Timestamp.FromDateTime(DateTime.UnixEpoch),
                StateRoot = ByteString.CopyFrom(stateProvider.StateRoot.Bytes),
            };


            GenesisHash = hashProvider.ComputeMultiHash(genesisDelta).ToCid();

            _dfsReader    = dfsReader;
            _logger       = logger;
            _entryOptions = () => new MemoryCacheEntryOptions()
                            .AddExpirationToken(changeTokenProvider.GetChangeToken())
                            .RegisterPostEvictionCallback(EvictionCallback);

            _memoryCache = memoryCache;
            _memoryCache.Set(GenesisHash, genesisDelta);
        }
예제 #7
0
        public DeltaCache(IHashProvider hashProvider,
                          IMemoryCache memoryCache,
                          IDeltaDfsReader dfsReader,
                          IDeltaCacheChangeTokenProvider changeTokenProvider,
                          ILogger logger)
        {
            var genesisDelta = new Delta {
                TimeStamp = Timestamp.FromDateTime(DateTime.MinValue.ToUniversalTime())
            };

            GenesisHash = hashProvider.ComputeMultiHash(genesisDelta).CreateCid();

            _dfsReader    = dfsReader;
            _logger       = logger;
            _entryOptions = () => new MemoryCacheEntryOptions()
                            .AddExpirationToken(changeTokenProvider.GetChangeToken())
                            .RegisterPostEvictionCallback(EvictionCallback);

            _memoryCache = memoryCache;
            _memoryCache.Set(GenesisHash, genesisDelta);
        }
예제 #8
0
        public void Init()
        {
            _cancellationToken = new CancellationToken();

            _manualResetEventSlim = new ManualResetEventSlim(false);

            _testScheduler = new TestScheduler();
            _hashProvider  = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));

            _peerSettings = Substitute.For <IPeerSettings>();
            _peerSettings.PeerId.Returns(PeerIdHelper.GetPeerId());

            _deltaDfsReader = Substitute.For <IDeltaDfsReader>();
            _deltaDfsReader.TryReadDeltaFromDfs(Arg.Any <Cid>(), out Arg.Any <Delta>()).Returns(x => true);

            _deltaCache = Substitute.For <IDeltaCache>();
            _deltaCache.GenesisHash.Returns("bafk2bzacecji5gcdd6lxsoazgnbg46c3vttjwwkptiw27enachziizhhkir2w".ToCid());

            _ledger = Substitute.For <ILedger>();

            _peerService = Substitute.For <IPeerService>();

            _deltaHashProvider = new DeltaHashProvider(_deltaCache, Substitute.For <IDeltaIndexService>(), Substitute.For <ILogger>());

            _deltaIndexService = new DeltaIndexService(new InMemoryRepository <DeltaIndexDao, string>());
            _deltaIndexService.Add(new DeltaIndexDao {
                Cid = _hashProvider.ComputeUtf8MultiHash("0").ToCid(), Height = 0
            });

            _peerClient = Substitute.For <IPeerClient>();
            ModifyPeerClient <LatestDeltaHashRequest>((request, senderPeerIdentifier) =>
            {
                var deltaHeightResponse = new LatestDeltaHashResponse
                {
                    DeltaIndex = new DeltaIndex
                    {
                        Cid = _hashProvider.ComputeUtf8MultiHash(_syncTestHeight.ToString()).ToCid().ToArray()
                              .ToByteString(),
                        Height = (uint)_syncTestHeight
                    }
                };

                _deltaHeightReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(),
                                                                 deltaHeightResponse.ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId())));
            });

            ModifyPeerClient <DeltaHistoryRequest>((request, senderPeerIdentifier) =>
            {
                var data = GenerateSampleData((int)request.Height, (int)request.Range, (int)_syncTestHeight);
                _deltaIndexService.Add(data.DeltaIndex.Select(x => DeltaIndexDao.ToDao <DeltaIndex>(x, _mapperProvider)));

                _deltaHistoryReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(),
                                                                  data
                                                                  .ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId())));
            });

            _peerRepository = new PeerRepository(new InMemoryRepository <Peer, string>());
            Enumerable.Repeat(new Peer {
                PeerId = PeerIdHelper.GetPeerId()
            }, 5).ToList().ForEach(_peerRepository.Add);

            _deltaHeightReplaySubject  = new ReplaySubject <IObserverDto <ProtocolMessage> >(1);
            _deltaHistoryReplaySubject = new ReplaySubject <IObserverDto <ProtocolMessage> >(1);

            var mergeMessageStreams = _deltaHeightReplaySubject.AsObservable()
                                      .Merge(_deltaHistoryReplaySubject.AsObservable());

            _peerService.MessageStream.Returns(mergeMessageStreams);

            _deltaHashProvider = Substitute.For <IDeltaHashProvider>();
            _deltaHashProvider.TryUpdateLatestHash(Arg.Any <Cid>(), Arg.Any <Cid>()).Returns(true);

            _mapperProvider = new TestMapperProvider();

            _userOutput = Substitute.For <IUserOutput>();

            _deltaHeightWatcher = new DeltaHeightWatcher(_peerClient, _peerRepository, _peerService, minimumPeers: 0);

            var dfsService = Substitute.For <IDfsService>();

            _peerSyncManager = new PeerSyncManager(_peerClient, _peerRepository,
                                                   _peerService, _userOutput, _deltaHeightWatcher, Substitute.For <IDfsService>(), 0.7, 0);
        }