Example #1
0
        public void AddressCacheCanRetrieveExisting()
        {
            const string CollectionName = "DummyCollection";
            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 AddressIndexRepository(database, new ExtendedLoggerFactory());

            string address        = "xyz";
            var    balanceChanges = new List <AddressBalanceChange>();

            balanceChanges.Add(new AddressBalanceChange()
            {
                BalanceChangedHeight = 1, Deposited = true, Satoshi = 1
            });

            var data = new AddressIndexerData()
            {
                Address = address, BalanceChanges = balanceChanges
            };

            cache.AddOrUpdate(data.Address, data, data.BalanceChanges.Count + 1);

            AddressIndexerData retrieved = cache.GetOrCreateAddress("xyz");

            Assert.NotNull(retrieved);
            Assert.Equal("xyz", retrieved.Address);
            Assert.Equal(1, retrieved.BalanceChanges.First().BalanceChangedHeight);
            Assert.True(retrieved.BalanceChanges.First().Deposited);
            Assert.Equal(1, retrieved.BalanceChanges.First().Satoshi);
        }
Example #2
0
        public void AddressCacheEvicts()
        {
            const string CollectionName = "AddrData";
            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 AddressIndexRepository(database, new ExtendedLoggerFactory(), 4);

            // Recall, each index entry counts as 1 and each balance change associated with it is an additional 1.
            Assert.Equal(0, database.GetCollection <AddressIndexerData>(CollectionName).Count());

            string address1        = "xyz";
            var    balanceChanges1 = new List <AddressBalanceChange>();

            balanceChanges1.Add(new AddressBalanceChange()
            {
                BalanceChangedHeight = 1, Deposited = true, Satoshi = 1
            });
            var data1 = new AddressIndexerData()
            {
                Address = address1, BalanceChanges = balanceChanges1
            };

            cache.AddOrUpdate(data1.Address, data1, data1.BalanceChanges.Count + 1);

            Assert.Equal(0, database.GetCollection <AddressIndexerData>(CollectionName).Count());

            string address2        = "abc";
            var    balanceChanges2 = new List <AddressBalanceChange>();

            balanceChanges2.Add(new AddressBalanceChange()
            {
                BalanceChangedHeight = 2, Deposited = false, Satoshi = 2
            });

            cache.AddOrUpdate(address2, new AddressIndexerData()
            {
                Address = address2, BalanceChanges = balanceChanges2
            }, balanceChanges2.Count + 1);

            Assert.Equal(0, database.GetCollection <AddressIndexerData>(CollectionName).Count());

            string address3        = "def";
            var    balanceChanges3 = new List <AddressBalanceChange>();

            balanceChanges3.Add(new AddressBalanceChange()
            {
                BalanceChangedHeight = 3, Deposited = true, Satoshi = 3
            });
            cache.AddOrUpdate(address3, new AddressIndexerData()
            {
                Address = address3, BalanceChanges = balanceChanges3
            }, balanceChanges3.Count + 1);

            // One of the cache items should have been evicted, and will therefore be persisted on disk.
            Assert.Equal(1, database.GetCollection <AddressIndexerData>(CollectionName).Count());

            // The evicted item should be data1.
            Assert.Equal(data1.Address, database.GetCollection <AddressIndexerData>(CollectionName).FindAll().First().Address);
            Assert.Equal(1, database.GetCollection <AddressIndexerData>(CollectionName).FindAll().First().BalanceChanges.First().BalanceChangedHeight);
            Assert.True(database.GetCollection <AddressIndexerData>(CollectionName).FindAll().First().BalanceChanges.First().Deposited);
            Assert.Equal(1, database.GetCollection <AddressIndexerData>(CollectionName).FindAll().First().BalanceChanges.First().Satoshi);

            // Check that the first address can still be retrieved, it should come from disk in this case.
            AddressIndexerData retrieved = cache.GetOrCreateAddress("xyz");

            Assert.NotNull(retrieved);
            Assert.Equal("xyz", retrieved.Address);
            Assert.Equal(1, retrieved.BalanceChanges.First().BalanceChangedHeight);
            Assert.True(retrieved.BalanceChanges.First().Deposited);
            Assert.Equal(1, retrieved.BalanceChanges.First().Satoshi);
        }