Skip to content

Commit

Permalink
Ring buffer tests (#184)
Browse files Browse the repository at this point in the history
* ran generate

* added ring buffer tests and formatted repo

* fixed snake case formatting

* fixed linting

* removed white space

* ran make generate
  • Loading branch information
z-j-lin committed Aug 19, 2022
1 parent a877bc0 commit b9ceed3
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT-open-group
pragma solidity ^0.8.11;

import "contracts/libraries/snapshots/SnapshotRingBuffer.sol";
import "contracts/libraries/parsers/BClaimsParserLibrary.sol";

contract SnapshotsRingBufferMock is SnapshotRingBuffer {
using EpochLib for Epoch;
using RingBuffer for SnapshotBuffer;
uint256 internal constant _EPOCH_LENGTH = 1024;
//epoch counter wrapped in a struct
Epoch internal _epoch;
//new snapshot ring buffer
SnapshotBuffer internal _snapshots;

function setSnapshot(bytes calldata bClaims_) public returns (uint32) {
BClaimsParserLibrary.BClaims memory blockClaims = BClaimsParserLibrary.extractBClaims(
bClaims_
);
return _setSnapshot(Snapshot(block.number, blockClaims));
}

function setEpoch(uint32 epoch_) public {
_epoch.set(epoch_);
}

function getEpoch() public view returns (uint32) {
return _epoch.get();
}

function getSnapshot(uint32 epoch_) public view returns (Snapshot memory) {
return _getSnapshot(epoch_);
}

function getLatestSnapshot() public view returns (Snapshot memory) {
return _getLatestSnapshot();
}

function getSnapshots() public view returns (SnapshotBuffer memory) {
return _getSnapshots();
}

function getEpochRegister() public view returns (Epoch memory) {
return _epochRegister();
}

function _getSnapshots() internal view override returns (SnapshotBuffer storage) {
return _snapshots;
}

function _epochRegister() internal view override returns (Epoch storage) {
return _epoch;
}

function _getEpochFromHeight(uint32 height_) internal pure override returns (uint32) {
if (height_ <= _EPOCH_LENGTH) {
return 1;
}
if (height_ % _EPOCH_LENGTH == 0) {
return uint32(height_ / _EPOCH_LENGTH);
}
return uint32((height_ / _EPOCH_LENGTH) + 1);
}
}

This file was deleted.

67 changes: 67 additions & 0 deletions bridge/test/ringBuffer/ring-buffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect } from "chai";
import { ethers } from "hardhat";
import { SnapshotsRingBufferMock } from "../../typechain-types";
import { signedData1 } from "../snapshots/assets/4-validators-snapshots-100-Group1";
describe("Ring Buffer Library", async () => {
let ringBuffer: SnapshotsRingBufferMock;
beforeEach(async () => {
const ringBufferBase = await ethers.getContractFactory(
"SnapshotsRingBufferMock"
);
ringBuffer = await ringBufferBase.deploy();
});

it("sets the epoch", async () => {
let epoch = await ringBuffer.getEpoch();
expect(epoch).to.equal(0);
const txResponse = await ringBuffer.setEpoch(1);
await txResponse.wait();
epoch = await ringBuffer.getEpoch();
expect(epoch).to.equal(1);
});

it("stores a snapshot on the ring buffer", async () => {
let txResponse = await ringBuffer.setSnapshot(signedData1[0].BClaims);
await txResponse.wait();
txResponse = await ringBuffer.setEpoch(1);
await txResponse.wait();
const snapshot = await ringBuffer.getLatestSnapshot();
expect(snapshot.blockClaims.height).to.eq(1024);
});

it("stores 7 snapshot on the ring buffer", async () => {
for (let i = 1; i <= 7; i++) {
await ringBuffer.setSnapshot(signedData1[i - 1].BClaims);
await ringBuffer.setEpoch(i);
}
const epoch = await ringBuffer.getEpoch();
expect(epoch).to.equal(7);
const snapshot = await ringBuffer.getSnapshot(7);
expect(snapshot.blockClaims.height).to.eq(1024 * 7);
});

it("attempts to get a snapshot that is no longer in the ring buffer", async () => {
for (let i = 1; i <= 7; i++) {
await ringBuffer.setSnapshot(signedData1[i - 1].BClaims);
await ringBuffer.setEpoch(i);
}
const epoch = await ringBuffer.getEpoch();
expect(epoch).to.equal(7);
const snapshot = ringBuffer.getSnapshot(1);
await expect(snapshot)
.to.be.revertedWithCustomError(ringBuffer, "SnapshotsNotInBuffer")
.withArgs(1);
});
it("get the 0 snapshot", async () => {
for (let i = 1; i <= 7; i++) {
await ringBuffer.setSnapshot(signedData1[i - 1].BClaims);
await ringBuffer.setEpoch(i);
}
const epoch = await ringBuffer.getEpoch();
expect(epoch).to.equal(7);
const snapshot = await ringBuffer.getSnapshot(0);
expect(snapshot.committedAt).to.eq(0);
expect(snapshot.blockClaims.height).to.eq(0);
expect(snapshot.blockClaims.chainId).to.eq(0);
});
});

0 comments on commit b9ceed3

Please sign in to comment.