Skip to content

Commit

Permalink
feat: add fuel chain state setter (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
DefiCake authored Aug 16, 2024
1 parent 510e462 commit c1ba7a9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-doors-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuel-bridge/solidity-contracts': minor
---

Added FuelChainState setter to FuelMessagePortalV3
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ contract FuelMessagePortalV3 is FuelMessagePortalV2 {
using FuelBlockHeaderLib for FuelBlockHeader;
using FuelBlockHeaderLiteLib for FuelBlockHeaderLite;

event FuelChainStateUpdated(address indexed sender, address indexed oldValue, address indexed newValue);

error MessageBlacklisted();
error WithdrawalsPaused();
error MessageRelayFailed();
Expand Down Expand Up @@ -140,10 +142,16 @@ contract FuelMessagePortalV3 is FuelMessagePortalV2 {
emit MessageRelayed(messageId, message.sender, message.recipient, message.amount);
}

function setFuelChainState(address newFuelChainState) external onlyRole(DEFAULT_ADMIN_ROLE) {
emit FuelChainStateUpdated(msg.sender, address(_fuelChainState), newFuelChainState);
_fuelChainState = FuelChainState(newFuelChainState);
}

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}

67 changes: 65 additions & 2 deletions packages/solidity-contracts/test/messagesIncomingV3.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { calcRoot, constructTree, getProof } from '@fuel-ts/merkle';
import type { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers';
import { expect } from 'chai';
import { MaxUint256, parseEther, toBeHex, type Provider } from 'ethers';
import {
MaxUint256,
parseEther,
toBeHex,
type Provider,
Wallet,
ZeroAddress,
} from 'ethers';
import { deployments, ethers, upgrades } from 'hardhat';

import type BlockHeader from '../protocol/blockHeader';
Expand Down Expand Up @@ -309,7 +316,7 @@ describe('FuelMessagePortalV3 - Incoming messages', () => {
expect(await fuelMessagePortal.withdrawalsPaused()).to.be.true;
});

describe('Behaves like V3 - Blacklisting', () => {
describe('Behaves like V3', () => {
beforeEach('fixture', async () => {
const fixt = await fixture();
const { V2Implementation, V3Implementation } = fixt;
Expand Down Expand Up @@ -530,6 +537,62 @@ describe('FuelMessagePortalV3 - Incoming messages', () => {
}
});
});

describe('setFuelChainState()', () => {
it('can only be called by DEFAULT_ADMIN_ROLE', async () => {
const [deployer] = await ethers.getSigners();

const mallory = Wallet.createRandom(provider);
deployer.sendTransaction({ to: mallory, value: parseEther('1') });

const defaultAdminRole = await fuelMessagePortal.DEFAULT_ADMIN_ROLE();

const rogueTx = fuelMessagePortal
.connect(mallory)
.setFuelChainState(ZeroAddress);
const expectedErrorMsg =
`AccessControl: account ${(
await mallory.getAddress()
).toLowerCase()}` + ` is missing role ${defaultAdminRole}`;

await expect(rogueTx).to.be.revertedWith(expectedErrorMsg);

await fuelMessagePortal
.connect(deployer)
.grantRole(defaultAdminRole, mallory);

const tx = fuelMessagePortal
.connect(mallory)
.setFuelChainState(ZeroAddress);

await expect(tx).not.to.be.reverted;
});

it('changes the fuel chain state address', async () => {
const [deployer] = await ethers.getSigners();
const newFuelChainStateAddress = Wallet.createRandom().address;
const oldFuelChainStateAddress =
await fuelMessagePortal.fuelChainStateContract();

const receipt = await fuelMessagePortal
.setFuelChainState(newFuelChainStateAddress)
.then((tx) => tx.wait());

const [event] = await fuelMessagePortal.queryFilter(
fuelMessagePortal.filters.FuelChainStateUpdated,
receipt.blockNumber,
receipt.blockNumber
);

expect(event.args.sender).to.equal(deployer.address);
expect(event.args.oldValue).to.equal(oldFuelChainStateAddress);
expect(event.args.newValue).to.equal(newFuelChainStateAddress);

expect(await fuelMessagePortal.fuelChainStateContract()).to.equal(
newFuelChainStateAddress
);
});
});
});

describe('Behaves like V2 - Accounting', () => {
Expand Down

0 comments on commit c1ba7a9

Please sign in to comment.