Skip to content

Commit

Permalink
test(e2e): adds an e2e test to ensure cache is invalidated on epoch r…
Browse files Browse the repository at this point in the history
…ollover
  • Loading branch information
iccicci committed Nov 22, 2022
1 parent b94f372 commit 48a1b21
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions packages/e2e/test/long-running/cache-invalidation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* eslint-disable max-statements */
import { AddressType, KeyRole } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
import { KeyAgentFactoryProps, TestWallet, getWallet } from '../../src';
import { env } from '../environment';
import {
getTxConfirmationEpoch,
submitCertificate,
waitForEpoch,
waitForWalletStateSettle,
walletReady
} from '../util';
import { logger } from '@cardano-sdk/util-dev';

const vrf = Cardano.VrfVkHex('2ee5a4c423224bb9c42107fc18a60556d6a83cec1d9dd37a71f56af7198fc759');

const wallet1Params: KeyAgentFactoryProps = {
accountIndex: 0,
mnemonic:
// eslint-disable-next-line max-len
'phrase raw learn suspect inmate powder combine apology regular hero gain chronic fruit ritual short screen goddess odor keen creek brand today kit machine',
networkId: Cardano.NetworkId.testnet,
password: 'some_password'
};

describe('cache', () => {
let wallet1: TestWallet;

beforeAll(async () => {
jest.setTimeout(180_000);

wallet1 = await getWallet({
customKeyParams: wallet1Params,
env,
idx: 0,
logger,
name: 'Pool Wallet 1',
polling: { interval: 500 }
});

await waitForWalletStateSettle(wallet1.wallet);
});

afterAll(() => wallet1.wallet.shutdown());

test('cache is invalidated on epoch rollover', async () => {
const wallet = wallet1.wallet;

await walletReady(wallet);

const poolKeyAgent = wallet.keyAgent;

const poolPubKey = await poolKeyAgent.derivePublicKey({
index: 0,
role: KeyRole.External
});

const poolKeyHash = Cardano.Ed25519KeyHash.fromKey(poolPubKey);
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
const poolRewardAccount = (
await poolKeyAgent.deriveAddress({
index: 0,
type: AddressType.External
})
).rewardAccount;

const registrationCert: Cardano.PoolRegistrationCertificate = {
__typename: Cardano.CertificateType.PoolRegistration,
poolParameters: {
cost: 1000n,
id: poolId,
margin: { denominator: 5, numerator: 1 },
owners: [poolRewardAccount],
pledge: 50_000_000n,
relays: [{ __typename: 'RelayByAddress', ipv4: '127.0.0.1', port: 6000 }],
rewardAccount: poolRewardAccount,
vrf
}
};

// This loads the data in the cache
const resultBeforeRegistration = await wallet1.providers.stakePoolProvider.queryStakePools({
filters: { identifier: { values: [{ id: poolId }] } },
pagination: { limit: 1, startAt: 0 }
});

expect(resultBeforeRegistration.totalResultCount).toBe(0);

const signedTx = await submitCertificate(registrationCert, wallet1);

const resultAfterRegistration = await wallet1.providers.stakePoolProvider.queryStakePools({
filters: { identifier: { values: [{ id: poolId }] } },
pagination: { limit: 1, startAt: 0 }
});

// Here we still expect 0 as the cache is doing its job
expect(resultAfterRegistration.totalResultCount).toBe(0);

const registrationTxConfirmedAtEpoch = await getTxConfirmationEpoch(wallet1.wallet, signedTx);

await waitForEpoch(wallet1.wallet, registrationTxConfirmedAtEpoch + 1);

const resultAfterOneEpoch = await wallet1.providers.stakePoolProvider.queryStakePools({
filters: { identifier: { values: [{ id: poolId }] } },
pagination: { limit: 1, startAt: 0 }
});

expect(resultAfterOneEpoch.totalResultCount).toBe(1);
expect(resultAfterOneEpoch.pageResults[0].status).toBe('activating');

await waitForEpoch(wallet1.wallet, registrationTxConfirmedAtEpoch + 2);

const resultAfterTwoEpochs = await wallet1.providers.stakePoolProvider.queryStakePools({
filters: { identifier: { values: [{ id: poolId }] } },
pagination: { limit: 1, startAt: 0 }
});

expect(resultAfterTwoEpochs.totalResultCount).toBe(1);
expect(resultAfterTwoEpochs.pageResults[0].status).toBe('active');
});
});

0 comments on commit 48a1b21

Please sign in to comment.