solana.js: added loadHistory test

This commit is contained in:
Conner Gallagher 2022-12-09 12:03:08 -07:00
parent 62a4878682
commit 5c6be89d3e
2 changed files with 49 additions and 4 deletions

View File

@ -56,7 +56,7 @@ export class AggregatorHistoryBuffer extends Account<
} }
/** /**
* Decode an aggregators history buffer and return an array of historical samples * Decode an aggregators history buffer and return an array of historical samples in ascending order by timestamp.
* @params historyBuffer the historyBuffer AccountInfo stored on-chain * @params historyBuffer the historyBuffer AccountInfo stored on-chain
* @return the array of {@linkcode types.AggregatorHistoryRow} samples * @return the array of {@linkcode types.AggregatorHistoryRow} samples
*/ */
@ -74,13 +74,15 @@ export class AggregatorHistoryBuffer extends Account<
const front: Array<types.AggregatorHistoryRow> = []; const front: Array<types.AggregatorHistoryRow> = [];
const tail: Array<types.AggregatorHistoryRow> = []; const tail: Array<types.AggregatorHistoryRow> = [];
for (let i = 12; i < historyBuffer.length; i += ROW_SIZE) { const buffer = historyBuffer.slice(12);
if (i + ROW_SIZE > historyBuffer.length) {
for (let i = 0; i < buffer.length; i += ROW_SIZE) {
if (i + ROW_SIZE > buffer.length) {
break; break;
} }
const row = types.AggregatorHistoryRow.fromDecoded( const row = types.AggregatorHistoryRow.fromDecoded(
types.AggregatorHistoryRow.layout().decode(historyBuffer, i) types.AggregatorHistoryRow.layout().decode(buffer, i)
); );
if (row.timestamp.eq(new anchor.BN(0))) { if (row.timestamp.eq(new anchor.BN(0))) {

View File

@ -0,0 +1,43 @@
import 'mocha';
import assert from 'assert';
import { AggregatorAccount, SwitchboardProgram } from '../src';
import { clusterApiUrl, Connection } from '@solana/web3.js';
describe('History Tests', () => {
let program: SwitchboardProgram;
before(async () => {
program = await SwitchboardProgram.load(
'devnet',
new Connection(process.env.SOLANA_DEVNET_RPC ?? clusterApiUrl('devnet'))
);
});
/** History buffer should be returned with the oldest elements (lowest timestamps) first */
it('Verifies a history buffer is decoded in order', async () => {
const [aggregatorAccount, aggregator] = await AggregatorAccount.load(
program,
'GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'
);
const history = await aggregatorAccount.loadHistory();
let lastTimestamp: number | undefined = undefined;
for (const [n, row] of history.entries()) {
if (lastTimestamp === undefined) {
lastTimestamp = row.timestamp.toNumber();
continue;
}
const currentTimestamp = row.timestamp.toNumber();
assert(
lastTimestamp < currentTimestamp,
`Aggregator History is out of order at element ${n}, prev ${lastTimestamp}, curr ${currentTimestamp}`
);
lastTimestamp = currentTimestamp;
}
});
});