Skip to content

Commit

Permalink
Add unit test to test conditional OSP
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Apr 25, 2024
1 parent 82e9425 commit 0ab2b78
Showing 1 changed file with 95 additions and 10 deletions.
105 changes: 95 additions & 10 deletions test/foundry/ChallengeManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity ^0.8.4;
import "forge-std/Test.sol";
import "./util/TestUtil.sol";
import "../../src/challenge/ChallengeManager.sol";
import "../../src/osp/OneStepProofEntry.sol";
import "forge-std/console.sol";

contract ChallengeManagerTest is Test {
IChallengeResultReceiver resultReceiver = IChallengeResultReceiver(address(137));
Expand All @@ -23,31 +25,114 @@ contract ChallengeManagerTest is Test {
);
chalman.initialize(resultReceiver, sequencerInbox, bridge, osp);
assertEq(
address(chalman.resultReceiver()),
address(resultReceiver),
"Result receiver not set"
address(chalman.resultReceiver()), address(resultReceiver), "Result receiver not set"
);
assertEq(
address(chalman.sequencerInbox()),
address(sequencerInbox),
"Sequencer inbox not set"
address(chalman.sequencerInbox()), address(sequencerInbox), "Sequencer inbox not set"
);
assertEq(address(chalman.bridge()), address(bridge), "Bridge not set");
assertEq(address(chalman.osp()), address(osp), "OSP not set");
return chalman;
}

function testCondOsp() public {
ChallengeManager chalman = deploy();

/// legacy root and OSP that will be used as conditional
IOneStepProofEntry legacyOSP = IOneStepProofEntry(
address(
new OneStepProofEntry(
IOneStepProver(makeAddr("0")),
IOneStepProver(makeAddr("mem")),
IOneStepProver(makeAddr("math")),
IOneStepProver(makeAddr("hostio"))
)
)
);
bytes32 legacyRoot = keccak256(abi.encodePacked("legacyRoot"));

// legacy hashes
bytes32 legacySegment0 = legacyOSP.getStartMachineHash(
keccak256(abi.encodePacked("globalStateHash[0]")), legacyRoot
);
bytes32 legacySegment1 = legacyOSP.getEndMachineHash(
MachineStatus.FINISHED, keccak256(abi.encodePacked("globalStateHashes[1]"))
);

/// new OSP
IOneStepProofEntry _newOSP = IOneStepProofEntry(
address(
new OneStepProofEntry(
IOneStepProver(makeAddr("0")),
IOneStepProver(makeAddr("mem")),
IOneStepProver(makeAddr("math")),
IOneStepProver(makeAddr("hostio"))
)
)
);

// new hashes
bytes32 newSegment0 = _newOSP.getStartMachineHash(
keccak256(abi.encodePacked("globalStateHash[0]")), randomRoot
);
bytes32 newSegment1 = _newOSP.getEndMachineHash(
MachineStatus.FINISHED, keccak256(abi.encodePacked("new_globalStateHashes[1]"))
);

/// do upgrade
vm.prank(proxyAdmin);
TransparentUpgradeableProxy(payable(address(chalman))).upgradeToAndCall(
address(chalmanImpl),
abi.encodeWithSelector(
ChallengeManager.postUpgradeInit.selector, _newOSP, legacyRoot, legacyOSP
)
);

/// check cond osp
IOneStepProofEntry _condOsp = chalman.getOsp(legacyRoot);
assertEq(address(_condOsp), address(legacyOSP), "Legacy osp not set");
assertEq(
_condOsp.getStartMachineHash(
keccak256(abi.encodePacked("globalStateHash[0]")), legacyRoot
),
legacySegment0,
"Unexpected start machine hash"
);
assertEq(
_condOsp.getEndMachineHash(
MachineStatus.FINISHED, keccak256(abi.encodePacked("globalStateHashes[1]"))
),
legacySegment1,
"Unexpected end machine hash"
);

/// check new osp
IOneStepProofEntry _newOsp = chalman.getOsp(randomRoot);
assertEq(address(_newOsp), address(_newOSP), "New osp not set");
assertEq(
_newOsp.getStartMachineHash(
keccak256(abi.encodePacked("globalStateHash[0]")), randomRoot
),
newSegment0,
"Unexpected start machine hash"
);
assertEq(
_newOsp.getEndMachineHash(
MachineStatus.FINISHED, keccak256(abi.encodePacked("new_globalStateHashes[1]"))
),
newSegment1,
"Unexpected end machine hash"
);
}

function testPostUpgradeInit() public {
ChallengeManager chalman = deploy();

vm.prank(proxyAdmin);
TransparentUpgradeableProxy(payable(address(chalman))).upgradeToAndCall(
address(chalmanImpl),
abi.encodeWithSelector(
ChallengeManager.postUpgradeInit.selector,
newOsp,
randomRoot,
condOsp
ChallengeManager.postUpgradeInit.selector, newOsp, randomRoot, condOsp
)
);

Expand Down

0 comments on commit 0ab2b78

Please sign in to comment.