public void OutPointCacheCanRetrieveExisting() { const string CollectionName = "DummyCollection"; var dataFolder = new DataFolder(TestBase.CreateTestDir(this)); string dbPath = Path.Combine(dataFolder.RootPath, CollectionName); FileMode fileMode = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? FileMode.Exclusive : FileMode.Shared; var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Mode = fileMode }); var cache = new AddressIndexerOutpointsRepository(database, new ExtendedLoggerFactory()); var outPoint = new OutPoint(uint256.Parse("0000af9ab2c8660481328d0444cf167dfd31f24ca2dbba8e5e963a2434cffa93"), 0); var data = new OutPointData() { Outpoint = outPoint.ToString(), ScriptPubKeyBytes = new byte[] { 0, 0, 0, 0 }, Money = Money.Coins(1) }; cache.AddOutPointData(data); Assert.True(cache.TryGetOutPointData(outPoint, out OutPointData retrieved)); Assert.NotNull(retrieved); Assert.Equal(outPoint.ToString(), retrieved.Outpoint); }
public void OutPointCacheEvicts() { const string CollectionName = "OutputsData"; var dataFolder = new DataFolder(TestBase.CreateTestDir(this)); string dbPath = Path.Combine(dataFolder.RootPath, CollectionName); var database = new LiteDatabase(new ConnectionString() { Filename = dbPath, Upgrade = true }); var cache = new AddressIndexerOutpointsRepository(database, new ExtendedLoggerFactory(), 2); Assert.Equal(0, cache.Count); Assert.Equal(0, database.GetCollection <OutPointData>(CollectionName).Count()); var outPoint1 = new OutPoint(uint256.Parse("0000af9ab2c8660481328d0444cf167dfd31f24ca2dbba8e5e963a2434cffa93"), 1);; var pair1 = new OutPointData() { Outpoint = outPoint1.ToString(), ScriptPubKeyBytes = new byte[] { 0, 0, 0, 0 }, Money = Money.Coins(1) }; cache.AddOutPointData(pair1); Assert.Equal(1, cache.Count); Assert.Equal(0, database.GetCollection <OutPointData>(CollectionName).Count()); var outPoint2 = new OutPoint(uint256.Parse("cf8ce1419bbc4870b7d4f1c084534d91126dd3283b51ec379e0a20e27bd23633"), 2);; var pair2 = new OutPointData() { Outpoint = outPoint2.ToString(), ScriptPubKeyBytes = new byte[] { 1, 1, 1, 1 }, Money = Money.Coins(2) }; cache.AddOutPointData(pair2); Assert.Equal(2, cache.Count); Assert.Equal(0, database.GetCollection <OutPointData>(CollectionName).Count()); var outPoint3 = new OutPoint(uint256.Parse("126dd3283b51ec379e0a20e27bd23633cf8ce1419bbc4870b7d4f1c084534d91"), 3);; var pair3 = new OutPointData() { Outpoint = outPoint3.ToString(), ScriptPubKeyBytes = new byte[] { 2, 2, 2, 2 }, Money = Money.Coins(3) }; cache.AddOutPointData(pair3); Assert.Equal(2, cache.Count); // One of the cache items should have been evicted, and will therefore be persisted on disk. Assert.Equal(1, database.GetCollection <OutPointData>(CollectionName).Count()); // The evicted item should be pair1. Assert.Equal(pair1.ScriptPubKeyBytes, database.GetCollection <OutPointData>(CollectionName).FindAll().First().ScriptPubKeyBytes); // It should still be possible to retrieve pair1 from the cache (it will pull it from disk). Assert.True(cache.TryGetOutPointData(outPoint1, out OutPointData pair1AfterEviction)); Assert.NotNull(pair1AfterEviction); Assert.Equal(pair1.ScriptPubKeyBytes, pair1AfterEviction.ScriptPubKeyBytes); Assert.Equal(pair1.Money, pair1AfterEviction.Money); }
public void CanRewind() { var rewindDataBlockHash = new uint256(RandomUtils.GetUInt64()); var outPoint = new OutPoint(new uint256(RandomUtils.GetUInt64()), 1); var data = new OutPointData() { Outpoint = outPoint.ToString(), Money = 1, ScriptPubKeyBytes = RandomUtils.GetBytes(20) }; var rewindData = new AddressIndexerRewindData() { BlockHash = rewindDataBlockHash.ToString(), BlockHeight = 100, SpentOutputs = new List <OutPointData>() { data } }; this.repository.RecordRewindData(rewindData); Assert.False(this.repository.TryGetOutPointData(outPoint, out OutPointData dataOut)); this.repository.Rewind(rewindDataBlockHash); Assert.True(this.repository.TryGetOutPointData(outPoint, out dataOut)); // Now record and purge rewind data. this.repository.RecordRewindData(rewindData); this.repository.RemoveOutPointData(outPoint); Assert.False(this.repository.TryGetOutPointData(outPoint, out dataOut)); this.repository.PurgeOldRewindData(rewindData.BlockHeight + 1); Assert.Throws <Exception>(() => this.repository.Rewind(rewindDataBlockHash)); Assert.False(this.repository.TryGetOutPointData(outPoint, out dataOut)); }
public void CanAddAndRemoveOutpointData() { var outPoint = new OutPoint(new uint256(RandomUtils.GetUInt64()), 1); var data = new OutPointData() { Outpoint = outPoint.ToString(), Money = 1, ScriptPubKeyBytes = RandomUtils.GetBytes(20) }; this.repository.AddOutPointData(data); // Add more to trigger eviction. for (int i = 0; i < this.maxItems * 2; i++) { this.repository.AddOutPointData(new OutPointData() { Outpoint = this.RandomString(20) }); } Assert.True(this.repository.TryGetOutPointData(outPoint, out OutPointData dataOut)); Assert.True(data.ScriptPubKeyBytes.SequenceEqual(dataOut.ScriptPubKeyBytes)); }