bitcore-node
Version:
A blockchain indexing node with extended capabilities using bitcore
193 lines • 12.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bson_1 = require("bson");
const chai_1 = require("chai");
const cache_1 = require("../../../src/models/cache");
const helpers_1 = require("../../helpers");
const integration_1 = require("../../helpers/integration");
const transaction_1 = require("../../../src/providers/chain-state/evm/models/transaction");
const test_ops_1 = require("../../data/test-ops");
describe('Cache Model', function () {
const key = 'key';
const value = { hello: 'world' };
const walletKey = 'wallet-key';
const walletValue = { walletThing: 1 };
const wallet = new bson_1.ObjectId();
const otherAddress = '0x9527a556f0D9AA753dE0D1f9Ad024a17dd281B1D';
const otherKey = `getBalanceForAddress-ETH-testnet-${otherAddress.toLowerCase()}`;
const suite = this;
this.timeout(30000);
before(integration_1.intBeforeHelper);
after(async () => (0, integration_1.intAfterHelper)(suite));
beforeEach(async () => {
await (0, helpers_1.resetDatabase)();
});
it('should cache a value', async () => {
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
const found = await cache_1.CacheStorage.collection.findOne({ key });
const got = await cache_1.CacheStorage.getGlobal(key);
(0, chai_1.expect)(found).to.exist;
(0, chai_1.expect)(found.expireTime).to.eq(cache_1.CacheStorage.Times.Hour);
(0, chai_1.expect)(got).to.deep.eq(found.value);
(0, chai_1.expect)(got).to.deep.eq(value);
});
it('should get the value', async () => {
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
const got = await cache_1.CacheStorage.getGlobal(key);
(0, chai_1.expect)(got).to.deep.eq(value);
});
it('should expire the key', async () => {
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.expire(key);
const got = await cache_1.CacheStorage.getGlobal(key);
(0, chai_1.expect)(got).to.eq(null);
});
it('should cache the value returned by fn', async () => {
const got = await cache_1.CacheStorage.getGlobalOrRefresh(key, async () => value, cache_1.CacheStorage.Times.Hour);
const found = await cache_1.CacheStorage.collection.findOne({ key });
(0, chai_1.expect)(found).to.exist;
(0, chai_1.expect)(found.expireTime).to.eq(cache_1.CacheStorage.Times.Hour);
(0, chai_1.expect)(got).to.deep.eq(found.value);
});
it('should cache the wallet value', async () => {
await cache_1.CacheStorage.setForWallet(wallet, walletKey, walletValue, cache_1.CacheStorage.Times.Day);
const found = await cache_1.CacheStorage.collection.findOne({ key: walletKey, wallet });
const global = await cache_1.CacheStorage.getGlobal(walletKey);
(0, chai_1.expect)(global).to.eq(null);
const got = await cache_1.CacheStorage.getForWallet(wallet, walletKey);
(0, chai_1.expect)(found).to.exist;
(0, chai_1.expect)(found.expireTime).to.eq(cache_1.CacheStorage.Times.Day);
(0, chai_1.expect)(got).to.deep.eq(found.value);
(0, chai_1.expect)(got).to.deep.eq(walletValue);
});
it('should cache the value returned by fn for wallet', async () => {
const got = await cache_1.CacheStorage.getForWalletOrRefresh(wallet, walletKey, async () => walletValue, cache_1.CacheStorage.Times.Hour);
const found = await cache_1.CacheStorage.collection.findOne({ key: walletKey, wallet });
(0, chai_1.expect)(found).to.exist;
(0, chai_1.expect)(found.expireTime).to.eq(cache_1.CacheStorage.Times.Hour);
(0, chai_1.expect)(got).to.deep.eq(found.value);
});
it('should expire balance cache for ETH to ETH', async () => {
const chain = 'ETH';
const network = 'testnet';
const address1 = '0x60a0c2f0f36020dca97f6214b3c8ff72d50d76db'; // ETH 1 address
const address2 = '0x3Ec3dA6E14BE9518A9a6e92DdCC6ACfF2CEFf4ef'; // ETH 2 address
const otherAddress = '0x9527a556f0D9AA753dE0D1f9Ad024a17dd281B1D';
const otherKey = `getBalanceForAddress-ETH-testnet-${otherAddress.toLowerCase()}`;
const key1 = `getBalanceForAddress-${chain}-${network}-${address1.toLowerCase()}`;
const key2 = `getBalanceForAddress-${chain}-${network}-${address2.toLowerCase()}`;
const op1 = test_ops_1.TEST_OP_ETH_TO_ETH1;
const op2 = test_ops_1.TEST_OP_ETH_TO_ETH2;
await cache_1.CacheStorage.setGlobal(key1, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key2, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(otherKey, value, cache_1.CacheStorage.Times.Hour);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op1);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op2);
const got1 = await cache_1.CacheStorage.getGlobal(key1);
const got2 = await cache_1.CacheStorage.getGlobal(key2);
const gotOther = await cache_1.CacheStorage.getGlobal(otherKey);
(0, chai_1.expect)(got1).to.eq(null);
(0, chai_1.expect)(got2).to.eq(null);
(0, chai_1.expect)(gotOther).to.deep.eq(value);
});
it('should expire balance cache for transfer ERC20Token to ERC20Token', async () => {
const chain = 'ETH';
const network = 'testnet';
const address1 = '0x60a0c2f0f36020dca97f6214b3c8ff72d50d76db'; // Linked ETH1 address
const address2 = '0x3Ec3dA6E14BE9518A9a6e92DdCC6ACfF2CEFf4ef'; // Linked ETH2 address
const tokenAddress1 = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'; // 1 Token address
const tokenAddress2 = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'; // 2 Token address
const keyToken1 = `getBalanceForAddress-${chain}-${network}-${address1.toLowerCase()}-${tokenAddress1.toLowerCase()}`;
const keyToken2 = `getBalanceForAddress-${chain}-${network}-${address2.toLowerCase()}-${tokenAddress2.toLowerCase()}`;
const key1 = `getBalanceForAddress-${chain}-${network}-${address1.toLowerCase()}`;
const key2 = `getBalanceForAddress-${chain}-${network}-${address2.toLowerCase()}`;
const op1 = test_ops_1.TEST_OP_TRANSFER_ERC20_TO_ERC201;
const op2 = test_ops_1.TEST_OP_TRANSFER_ERC20_TO_ERC202;
await cache_1.CacheStorage.setGlobal(keyToken1, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(keyToken2, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key1, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key2, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(otherKey, value, cache_1.CacheStorage.Times.Hour);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op1);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op2);
const gotToken1 = await cache_1.CacheStorage.getGlobal(keyToken1);
const gotToken2 = await cache_1.CacheStorage.getGlobal(keyToken2);
const got1 = await cache_1.CacheStorage.getGlobal(key1);
const got2 = await cache_1.CacheStorage.getGlobal(key2);
const gotOther = await cache_1.CacheStorage.getGlobal(otherKey);
(0, chai_1.expect)(gotToken1).to.eq(null);
(0, chai_1.expect)(gotToken2).to.eq(null);
(0, chai_1.expect)(got1).to.eq(null);
(0, chai_1.expect)(got2).to.deep.eq(value); // Nothing to update in Linked ETH2
(0, chai_1.expect)(gotOther).to.deep.eq(value);
});
it('should expire balance cache for swaps ERC20Token to ERC20Token', async () => {
const chain = 'ETH';
const network = 'testnet';
const address = '0x60a0c2f0f36020dca97f6214b3c8ff72d50d76db'; // Linked ETH address
const tokenAddress1 = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'; // 1 Token address
const tokenAddress2 = '0xaaf64bfcc32d0f15873a02163e7e500671a4ffcd'; // 2 Token address
const keyToken1 = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}-${tokenAddress1.toLowerCase()}`;
const keyToken2 = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}-${tokenAddress2.toLowerCase()}`;
const key = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}`;
const op1 = test_ops_1.TEST_OP_ERC20_TO_ERC201;
const op2 = test_ops_1.TEST_OP_ERC20_TO_ERC202;
await cache_1.CacheStorage.setGlobal(keyToken1, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(keyToken2, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(otherKey, value, cache_1.CacheStorage.Times.Hour);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op1);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op2);
const gotToken1 = await cache_1.CacheStorage.getGlobal(keyToken1);
const gotToken2 = await cache_1.CacheStorage.getGlobal(keyToken2);
const got = await cache_1.CacheStorage.getGlobal(key);
const gotOther = await cache_1.CacheStorage.getGlobal(otherKey);
(0, chai_1.expect)(gotToken1).to.eq(null);
(0, chai_1.expect)(gotToken2).to.eq(null);
(0, chai_1.expect)(got).to.eq(null);
(0, chai_1.expect)(gotOther).to.deep.eq(value);
});
it('should expire balance cache for swaps ERC20Token to ETH', async () => {
const chain = 'ETH';
const network = 'testnet';
const address = '0x60a0c2f0f36020dca97f6214b3c8ff72d50d76db'; // Linked ETH address
const tokenAddress = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'; // Token address
const keyToken = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}-${tokenAddress.toLowerCase()}`;
const key = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}`;
const op1 = test_ops_1.TEST_OP_ERC20Token_TO_ETH1;
const op2 = test_ops_1.TEST_OP_ERC20Token_TO_ETH2;
await cache_1.CacheStorage.setGlobal(keyToken, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(otherKey, value, cache_1.CacheStorage.Times.Hour);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op1);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op2);
const gotToken = await cache_1.CacheStorage.getGlobal(keyToken);
const got = await cache_1.CacheStorage.getGlobal(key);
const gotOther = await cache_1.CacheStorage.getGlobal(otherKey);
(0, chai_1.expect)(gotToken).to.eq(null);
(0, chai_1.expect)(got).to.eq(null);
(0, chai_1.expect)(gotOther).to.deep.eq(value);
});
it('should expire balance cache for swaps ETH to ERC20Token', async () => {
const chain = 'ETH';
const network = 'testnet';
const address = '0x60a0c2f0f36020dca97f6214b3c8ff72d50d76db'; // Linked ETH address
const tokenAddress = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'; // Token address
const keyToken = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}-${tokenAddress.toLowerCase()}`;
const key = `getBalanceForAddress-${chain}-${network}-${address.toLowerCase()}`;
const op1 = test_ops_1.TEST_OP_ETH_TO_ERC201;
const op2 = test_ops_1.TEST_OP_ETH_TO_ERC202;
await cache_1.CacheStorage.setGlobal(keyToken, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(key, value, cache_1.CacheStorage.Times.Hour);
await cache_1.CacheStorage.setGlobal(otherKey, value, cache_1.CacheStorage.Times.Hour);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op1);
await transaction_1.EVMTransactionStorage.expireBalanceCache(op2);
const gotToken = await cache_1.CacheStorage.getGlobal(keyToken);
const got = await cache_1.CacheStorage.getGlobal(key);
const gotOther = await cache_1.CacheStorage.getGlobal(otherKey);
(0, chai_1.expect)(gotToken).to.eq(null);
(0, chai_1.expect)(got).to.eq(null);
(0, chai_1.expect)(gotOther).to.deep.eq(value);
});
});
//# sourceMappingURL=cache.spec.js.map