Пример #1
0
        public LedgerTests()
        {
            _testScheduler  = new TestScheduler();
            _fakeRepository = Substitute.For <IAccountRepository>();
            _hashProvider   = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            _mapperProvider = new TestMapperProvider();

            _logger            = Substitute.For <ILogger>();
            _mempool           = Substitute.For <IMempool <PublicEntryDao> >();
            _deltaHashProvider = Substitute.For <IDeltaHashProvider>();
            _receipts          = Substitute.For <ITransactionRepository>();
            _synchroniser      = Substitute.For <ISynchroniser>();
            _genesisHash       = _hashProvider.ComputeUtf8MultiHash("genesis").ToCid();
            _synchroniser.DeltaCache.GenesisHash.Returns(_genesisHash);
            _executor        = Substitute.For <IDeltaExecutor>();
            _stateProvider   = Substitute.For <IStateProvider>();
            _storageProvider = Substitute.For <IStorageProvider>();
            _cryptoContext   = new FfiWrapper();
            _signingContext  = new SigningContext
            {
                NetworkType   = NetworkType.Devnet,
                SignatureType = SignatureType.TransactionPublic
            };

            _deltaIndexService = new DeltaIndexService(new InMemoryRepository <DeltaIndexDao, string>());
        }
Пример #2
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();
        }
Пример #3
0
        public DeltaHashProvider(IDeltaCache deltaCache,
                                 IDeltaIndexService deltaIndexService,
                                 ILogger logger,
                                 int capacity = 10_000)
        {
            _deltaCache = deltaCache;
            _logger     = logger;
            _deltaHashUpdatesSubject = new ReplaySubject <Cid>(0);
            var comparer = ComparerBuilder.For <Timestamp>().OrderBy(u => u, descending: true);

            _capacity = capacity;
            _hashesByTimeDescending = new SortedList <Timestamp, Cid>(comparer)
            {
                Capacity = _capacity
            };

            var latestDeltaIndex = deltaIndexService.LatestDeltaIndex();

            if (deltaIndexService.LatestDeltaIndex() != null)
            {
                var foundDelta = _deltaCache.TryGetOrAddConfirmedDelta(latestDeltaIndex.Cid, out var delta);
                _hashesByTimeDescending.Add(delta.TimeStamp, latestDeltaIndex.Cid);
                return;
            }

            _hashesByTimeDescending.Add(Timestamp.FromDateTime(DateTime.UnixEpoch), _deltaCache.GenesisHash);
        }
Пример #4
0
 public CandidateDeltaObserver(IDeltaVoter deltaVoter, IDeltaIndexService deltaIndexService, SyncState syncState, IPeerRepository peerRepository, IHashProvider provider, ILogger logger)
     : base(logger)
 {
     _deltaVoter        = deltaVoter;
     _deltaIndexService = deltaIndexService;
     _syncState         = syncState;
     _peerRepository    = peerRepository;
     _hashProvider      = provider;
 }
Пример #5
0
 public DeltaHistoryRequestObserver(IPeerSettings peerSettings,
                                    IDeltaIndexService deltaIndexService,
                                    IMapperProvider mapperProvider,
                                    ILogger logger)
     : base(logger, peerSettings)
 {
     _deltaIndexService = deltaIndexService;
     _mapperProvider    = mapperProvider;
 }
 public DeltaHeightRequestObserver(IPeerSettings peerSettings,
                                   IDeltaIndexService deltaIndexService,
                                   IMapperProvider mapperProvider,
                                   SyncState syncState,
                                   ILogger logger)
     : base(logger, peerSettings)
 {
     _deltaIndexService = deltaIndexService;
     _mapperProvider    = mapperProvider;
     _syncState         = syncState;
 }
Пример #7
0
        public Ledger(IDeltaExecutor deltaExecutor,
                      IStateProvider stateProvider,
                      IStorageProvider storageProvider,
                      ISnapshotableDb stateDb,
                      IDb codeDb,
                      IAccountRepository accounts,
                      IDeltaIndexService deltaIndexService,
                      ITransactionRepository receipts,
                      IDeltaHashProvider deltaHashProvider,
                      ISynchroniser synchroniser,
                      IMempool <PublicEntryDao> mempool,
                      IMapperProvider mapperProvider,
                      IHashProvider hashProvider,
                      ILogger logger)
        {
            Accounts         = accounts;
            _deltaExecutor   = deltaExecutor;
            _stateProvider   = stateProvider;
            _storageProvider = storageProvider;

            _stateDb        = stateDb;
            _codeDb         = codeDb;
            _mempool        = mempool;
            _mapperProvider = mapperProvider;
            _hashProvider   = hashProvider;
            _logger         = logger;
            _receipts       = receipts;
            _synchroniser   = synchroniser;

            _deltaUpdatesSubscription = deltaHashProvider.DeltaHashUpdates.Subscribe(Update);

            _deltaIndexService = deltaIndexService;

            var latestDeltaIndex = _deltaIndexService.LatestDeltaIndex();

            if (latestDeltaIndex != null)
            {
                _latestKnownDelta       = latestDeltaIndex.Cid;
                _latestKnownDeltaNumber = (long)latestDeltaIndex.Height;
                return;
            }

            _latestKnownDelta = _synchroniser.DeltaCache.GenesisHash;
            WriteLatestKnownDelta(_latestKnownDelta);
        }
Пример #8
0
 public DeltaBuilder(IDeltaTransactionRetriever transactionRetriever,
                     IDeterministicRandomFactory randomFactory,
                     IHashProvider hashProvider,
                     IPeerSettings peerSettings,
                     IDeltaCache deltaCache,
                     IDateTimeProvider dateTimeProvider,
                     IDeltaIndexService deltaIndexService,
                     ILogger logger)
 {
     _deltaIndexService    = deltaIndexService;
     _transactionRetriever = transactionRetriever;
     _randomFactory        = randomFactory;
     _hashProvider         = hashProvider;
     _producerUniqueId     = peerSettings.PeerId;
     _deltaCache           = deltaCache;
     _dateTimeProvider     = dateTimeProvider;
     _logger = logger;
 }
Пример #9
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);
        }
Пример #10
0
 public DeltaResolver(IDeltaIndexService deltaIndexService, ILedger ledger)
 {
     _deltaIndexService = deltaIndexService;
     _ledger            = ledger;
 }
Пример #11
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);
        }