Skip to content

Commit

Permalink
Merge pull request #175 from OffchainLabs/unit-cond-osp
Browse files Browse the repository at this point in the history
Add unit test for conditional OSP
  • Loading branch information
gvladika authored Apr 26, 2024
2 parents 82e9425 + 7867bae commit 6d86f7a
Showing 1 changed file with 99 additions and 10 deletions.
109 changes: 99 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,118 @@ 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"
);

/// check hashes are different
assertNotEq(legacySegment0, newSegment0, "Start machine hash should be different");
assertNotEq(legacySegment1, newSegment1, "End machine hash should be different");
}

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 6d86f7a

Please sign in to comment.