Commit c04d3959 authored by clabby's avatar clabby

Remove concept of valid / invalid from alphabet game

parent a0f1d5d9
This diff is collapsed.
......@@ -9,11 +9,11 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/solc"
)
const AlphabetVMStorageLayoutJSON = "{\"storage\":[{\"astId\":1000,\"contract\":\"test/FaultDisputeGame.t.sol:AlphabetVM\",\"label\":\"oracle\",\"offset\":0,\"slot\":\"0\",\"type\":\"t_contract(IPreimageOracle)1002\"},{\"astId\":1001,\"contract\":\"test/FaultDisputeGame.t.sol:AlphabetVM\",\"label\":\"traceLength\",\"offset\":20,\"slot\":\"0\",\"type\":\"t_uint8\"}],\"types\":{\"t_contract(IPreimageOracle)1002\":{\"encoding\":\"inplace\",\"label\":\"contract IPreimageOracle\",\"numberOfBytes\":\"20\"},\"t_uint8\":{\"encoding\":\"inplace\",\"label\":\"uint8\",\"numberOfBytes\":\"1\"}}}"
const AlphabetVMStorageLayoutJSON = "{\"storage\":[{\"astId\":1000,\"contract\":\"test/FaultDisputeGame.t.sol:AlphabetVM\",\"label\":\"oracle\",\"offset\":0,\"slot\":\"0\",\"type\":\"t_contract(IPreimageOracle)1001\"}],\"types\":{\"t_contract(IPreimageOracle)1001\":{\"encoding\":\"inplace\",\"label\":\"contract IPreimageOracle\",\"numberOfBytes\":\"20\"}}}"
var AlphabetVMStorageLayout = new(solc.StorageLayout)
var AlphabetVMDeployedBin = "0x608060405234801561001057600080fd5b506004361061004c5760003560e01c80637dc0d1d014610051578063a13060eb1461009b578063a8a8b5eb146100d2578063f8e0cb961461012e575b600080fd5b6000546100719073ffffffffffffffffffffffffffffffffffffffff1681565b60405173ffffffffffffffffffffffffffffffffffffffff90911681526020015b60405180910390f35b6000546100c09074010000000000000000000000000000000000000000900460ff1681565b60405160ff9091168152602001610092565b61012c6100e036600461028f565b6000805460ff90921674010000000000000000000000000000000000000000027fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff909216919091179055565b005b61014161013c366004610302565b61014f565b604051908152602001610092565b600080600060087f0000000000000000000000000000000000000000000000000000000000000000901b6008888860405161018b92919061036e565b6040518091039020901b036101b157600091506101aa8688018861037e565b90506101d0565b6101bd86880188610397565b9092509050816101cc816103e8565b9250505b816101dc826001610420565b60408051602081019390935282015260600160405160208183030381529060405280519060200120925060006001600060149054906101000a900460ff166102249190610438565b60ff16830361023557506001610275565b60005461025f9060019074010000000000000000000000000000000000000000900460ff16610438565b60ff1683101561027157506003610275565b5060025b8060f81b60ff60f81b198516179350505050949350505050565b6000602082840312156102a157600080fd5b813560ff811681146102b257600080fd5b9392505050565b60008083601f8401126102cb57600080fd5b50813567ffffffffffffffff8111156102e357600080fd5b6020830191508360208285010111156102fb57600080fd5b9250929050565b6000806000806040858703121561031857600080fd5b843567ffffffffffffffff8082111561033057600080fd5b61033c888389016102b9565b9096509450602087013591508082111561035557600080fd5b50610362878288016102b9565b95989497509550505050565b8183823760009101908152919050565b60006020828403121561039057600080fd5b5035919050565b600080604083850312156103aa57600080fd5b50508035926020909101359150565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610419576104196103b9565b5060010190565b60008219821115610433576104336103b9565b500190565b600060ff821660ff841680821015610452576104526103b9565b9003939250505056fea164736f6c634300080f000a"
var AlphabetVMDeployedBin = "0x608060405234801561001057600080fd5b50600436106100365760003560e01c80637dc0d1d01461003b578063f8e0cb9614610085575b600080fd5b60005461005b9073ffffffffffffffffffffffffffffffffffffffff1681565b60405173ffffffffffffffffffffffffffffffffffffffff90911681526020015b60405180910390f35b610098610093366004610212565b6100a6565b60405190815260200161007c565b600080600060087f0000000000000000000000000000000000000000000000000000000000000000901b600888886040516100e292919061027e565b6040518091039020901b0361010857600091506101018688018861028e565b9050610127565b610114868801886102a7565b909250905081610123816102f8565b9250505b81610133826001610330565b604080516020810193909352820152606001604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe081840301815291905280516020909101207effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f010000000000000000000000000000000000000000000000000000000000000017979650505050505050565b60008083601f8401126101db57600080fd5b50813567ffffffffffffffff8111156101f357600080fd5b60208301915083602082850101111561020b57600080fd5b9250929050565b6000806000806040858703121561022857600080fd5b843567ffffffffffffffff8082111561024057600080fd5b61024c888389016101c9565b9096509450602087013591508082111561026557600080fd5b50610272878288016101c9565b95989497509550505050565b8183823760009101908152919050565b6000602082840312156102a057600080fd5b5035919050565b600080604083850312156102ba57600080fd5b50508035926020909101359150565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610329576103296102c9565b5060010190565b60008219821115610343576103436102c9565b50019056fea164736f6c634300080f000a"
func init() {
if err := json.Unmarshal([]byte(AlphabetVMStorageLayoutJSON), AlphabetVMStorageLayout); err != nil {
......
......@@ -59,7 +59,7 @@ func (ap *AlphabetTraceProvider) Get(ctx context.Context, i uint64) (common.Hash
if err != nil {
return common.Hash{}, err
}
return ap.alphabetStateHash(claimBytes), nil
return alphabetStateHash(claimBytes), nil
}
// AbsolutePreState returns the absolute pre-state for the alphabet trace.
......@@ -78,7 +78,7 @@ func (ap *AlphabetTraceProvider) AbsolutePreStateCommitment(ctx context.Context)
}
func (ap *AlphabetTraceProvider) StateHash(ctx context.Context, state []byte) (common.Hash, error) {
return ap.alphabetStateHash(state), nil
return alphabetStateHash(state), nil
}
// BuildAlphabetPreimage constructs the claim bytes for the index and state item.
......@@ -86,18 +86,9 @@ func BuildAlphabetPreimage(i uint64, letter string) []byte {
return append(IndexToBytes(i), LetterToBytes(letter)...)
}
func (ap *AlphabetTraceProvider) alphabetStateHash(state []byte) common.Hash {
func alphabetStateHash(state []byte) common.Hash {
h := crypto.Keccak256Hash(state)
// instead of the state containing an "exited" boolean, we just check if the index reached the end
i := new(big.Int).SetBytes(state[:32])
finalStateIdx := uint64(len(ap.state) - 1)
if !i.IsUint64() || i.Uint64() > finalStateIdx {
h[0] = mipsevm.VMStatusPanic // this state should never be reached, if we increment by 1 per step
} else if i.Uint64() == finalStateIdx {
h[0] = mipsevm.VMStatusInvalid
} else {
h[0] = mipsevm.VMStatusUnfinished
}
h[0] = mipsevm.VMStatusInvalid
return h
}
......
......@@ -10,7 +10,7 @@ import (
)
func alphabetClaim(index uint64, letter string, provider *AlphabetTraceProvider) common.Hash {
return provider.alphabetStateHash(BuildAlphabetPreimage(index, letter))
return alphabetStateHash(BuildAlphabetPreimage(index, letter))
}
// TestAlphabetProvider_Get_ClaimsByTraceIndex tests the [fault.AlphabetProvider] Get function.
......
......@@ -88,16 +88,16 @@ FaucetTest:test_nonAdmin_drip_fails() (gas: 262520)
FaucetTest:test_receive_succeeds() (gas: 17401)
FaucetTest:test_withdraw_nonAdmin_reverts() (gas: 13145)
FaucetTest:test_withdraw_succeeds() (gas: 78359)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot1:test_resolvesCorrectly_succeeds() (gas: 509652)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot2:test_resolvesCorrectly_succeeds() (gas: 516910)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot3:test_resolvesCorrectly_succeeds() (gas: 513211)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot4:test_resolvesCorrectly_succeeds() (gas: 516451)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot5:test_resolvesCorrectly_succeeds() (gas: 514790)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot1:test_resolvesCorrectly_succeeds() (gas: 508417)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot2:test_resolvesCorrectly_succeeds() (gas: 515675)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot3:test_resolvesCorrectly_succeeds() (gas: 511976)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot4:test_resolvesCorrectly_succeeds() (gas: 513216)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot5:test_resolvesCorrectly_succeeds() (gas: 511555)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot1:test_resolvesCorrectly_succeeds() (gas: 499197)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot2:test_resolvesCorrectly_succeeds() (gas: 506057)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot3:test_resolvesCorrectly_succeeds() (gas: 502738)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot4:test_resolvesCorrectly_succeeds() (gas: 505955)
FaultDisputeGame_ResolvesCorrectly_CorrectRoot5:test_resolvesCorrectly_succeeds() (gas: 505224)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot1:test_resolvesCorrectly_succeeds() (gas: 497962)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot2:test_resolvesCorrectly_succeeds() (gas: 504822)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot3:test_resolvesCorrectly_succeeds() (gas: 501503)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot4:test_resolvesCorrectly_succeeds() (gas: 502720)
FaultDisputeGame_ResolvesCorrectly_IncorrectRoot5:test_resolvesCorrectly_succeeds() (gas: 501989)
FaultDisputeGame_Test:test_addLocalData_static_succeeds() (gas: 640504)
FaultDisputeGame_Test:test_createdAt_succeeds() (gas: 10342)
FaultDisputeGame_Test:test_extraData_succeeds() (gas: 32377)
......
......@@ -515,19 +515,19 @@ contract GamePlayer {
(,, Claim grandparentClaim, Position grandparentPos,) = gameProxy.claimData(grandparentIndex);
Claim ourGrandparentClaim = claimAt(grandparentPos);
if (Claim.unwrap(ourParentClaim) << 8 != Claim.unwrap(parentClaim) << 8) {
if (Claim.unwrap(ourParentClaim) != Claim.unwrap(parentClaim)) {
// Attack parent.
movePos = parentPos.move(true);
// If we also disagree with the grandparent, attack it as well.
if (Claim.unwrap(ourGrandparentClaim) << 8 != Claim.unwrap(grandparentClaim) << 8) {
if (Claim.unwrap(ourGrandparentClaim) != Claim.unwrap(grandparentClaim)) {
movePos2 = grandparentPos.move(true);
}
// Flag the move as an attack.
isAttack = true;
} else if (
Claim.unwrap(ourParentClaim) << 8 == Claim.unwrap(parentClaim) << 8
&& Claim.unwrap(ourGrandparentClaim) << 8 == Claim.unwrap(grandparentClaim) << 8
Claim.unwrap(ourParentClaim) == Claim.unwrap(parentClaim)
&& Claim.unwrap(ourGrandparentClaim) == Claim.unwrap(grandparentClaim)
) {
movePos = parentPos.move(false);
}
......@@ -611,16 +611,8 @@ contract GamePlayer {
function claimAt(uint256 _traceIndex) public view returns (Claim claim_) {
bytes32 hash =
keccak256(abi.encode(_traceIndex >= trace.length ? trace.length - 1 : _traceIndex, traceAt(_traceIndex)));
uint256 status;
if (_traceIndex == trace.length - 1) {
status = 1;
} else if (_traceIndex < trace.length - 1) {
status = 3;
} else {
status = 2;
}
assembly {
claim_ := or(and(hash, not(shl(248, 0xFF))), shl(248, status))
claim_ := or(and(hash, not(shl(248, 0xFF))), shl(248, 1))
}
}
......@@ -635,14 +627,13 @@ contract OneVsOne_Arena is FaultDisputeGame_Init {
bytes ABSOLUTE_PRESTATE = abi.encode(15);
/// @dev The absolute prestate claim.
Claim internal constant ABSOLUTE_PRESTATE_CLAIM =
Claim.wrap(bytes32((uint256(3) << 248) | uint256(keccak256(abi.encode(15)))));
Claim.wrap(bytes32((uint256(3) << 248) | (~uint256(0xFF << 248) & uint256(keccak256(abi.encode(15))))));
/// @dev The defender.
GamePlayer internal defender;
/// @dev The challenger.
GamePlayer internal challenger;
function init(
bool defenderCorrect,
GamePlayer _defender,
GamePlayer _challenger,
uint256 _finalTraceIndex
......@@ -658,11 +649,6 @@ contract OneVsOne_Arena is FaultDisputeGame_Init {
defender.init(gameProxy, challenger, vm);
challenger.init(gameProxy, defender, vm);
// Set the expected trace length to the defender's trace length.
AlphabetVM(address(gameProxy.VM())).setTraceLength(
uint8((defenderCorrect ? _defender : _challenger).trace().length)
);
// Label actors for trace.
vm.label(address(challenger), "Challenger");
vm.label(address(defender), "Defender");
......@@ -673,7 +659,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRoot1 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 0);
super.init(false, dishonest, honest, 15);
super.init(dishonest, honest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -694,7 +680,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRoot1 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 0);
super.init(true, honest, dishonest, 15);
super.init(honest, dishonest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -715,7 +701,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRoot2 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 7);
super.init(false, dishonest, honest, 15);
super.init(dishonest, honest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -736,7 +722,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRoot2 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 7);
super.init(true, honest, dishonest, 15);
super.init(honest, dishonest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -757,7 +743,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRoot3 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 2);
super.init(false, dishonest, honest, 15);
super.init(dishonest, honest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -778,7 +764,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRoot3 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 16, 2);
super.init(true, honest, dishonest, 15);
super.init(honest, dishonest, 15);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -799,7 +785,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRoot4 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer_HalfTrace(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 8, 5);
super.init(false, dishonest, honest, 7);
super.init(dishonest, honest, 7);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -820,7 +806,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRoot4 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer_HalfTrace(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 8, 5);
super.init(true, honest, dishonest, 7);
super.init(honest, dishonest, 7);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -841,7 +827,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRoot5 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer_QuarterTrace(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 4, 3);
super.init(false, dishonest, honest, 3);
super.init(dishonest, honest, 3);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -862,7 +848,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRoot5 is OneVsOne_Arena {
function setUp() public override {
GamePlayer honest = new HonestPlayer_QuarterTrace(ABSOLUTE_PRESTATE);
GamePlayer dishonest = new VariableDivergentPlayer(ABSOLUTE_PRESTATE, 4, 3);
super.init(true, honest, dishonest, 3);
super.init(honest, dishonest, 3);
}
function test_resolvesCorrectly_succeeds() public {
......@@ -892,7 +878,7 @@ contract FaultDisputeGame_ResolvesCorrectly_IncorrectRootFuzz is OneVsOne_Arena
_dishonestTraceLength,
i
);
super.init(false, dishonest, honest, _dishonestTraceLength - 1);
super.init(dishonest, honest, _dishonestTraceLength - 1);
// Play the game until a step is forced.
challenger.play(0);
......@@ -922,7 +908,7 @@ contract FaultDisputeGame_ResolvesCorrectly_CorrectRootFuzz is OneVsOne_Arena {
_dishonestTraceLength,
i
);
super.init(true, honest, dishonest, 15);
super.init(honest, dishonest, 15);
// Play the game until a step is forced.
challenger.play(0);
......@@ -997,18 +983,11 @@ contract AlphabetVM is IBigStepper {
Claim internal immutable ABSOLUTE_PRESTATE;
IPreimageOracle public oracle;
uint8 public traceLength = 15;
constructor(Claim _absolutePrestate) {
ABSOLUTE_PRESTATE = _absolutePrestate;
oracle = new PreimageOracle();
}
/// @notice Sets the expected trace length.
function setTraceLength(uint8 _traceLength) public {
traceLength = _traceLength;
}
/// @inheritdoc IBigStepper
function step(bytes calldata _stateData, bytes calldata) external view returns (bytes32 postState_) {
uint256 traceIndex;
......@@ -1024,16 +1003,8 @@ contract AlphabetVM is IBigStepper {
}
// STF: n -> n + 1
postState_ = keccak256(abi.encode(traceIndex, claim + 1));
VMStatus status;
if (traceIndex == traceLength - 1) {
status = VMStatuses.INVALID;
} else if (traceIndex < traceLength - 1) {
status = VMStatuses.UNFINISHED;
} else {
status = VMStatuses.PANIC;
}
assembly {
postState_ := or(and(postState_, not(shl(248, 0xFF))), shl(248, status))
postState_ := or(and(postState_, not(shl(248, 0xFF))), shl(248, 1))
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment