-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ran generate * added ring buffer tests and formatted repo * fixed snake case formatting * fixed linting * removed white space * ran make generate
- Loading branch information
Showing
3 changed files
with
131 additions
and
62 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
bridge/test/contract-mocks/snapshotRingBuffer/SnapshotsRingBufferMock.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
bridge/test/contract-mocks/snapshotRingBuffer/snapshotStorageMock.sol
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |