From 85838ca6ddac6ab3f4c48d6e23a8c47924e9901b Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Mon, 17 Jul 2023 11:47:15 +1000 Subject: [PATCH] op-challenger: Add tests for Position attack and defend. --- op-challenger/fault/position_test.go | 54 ++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/op-challenger/fault/position_test.go b/op-challenger/fault/position_test.go index dba505f92c0e..7ab6d6d8d1b4 100644 --- a/op-challenger/fault/position_test.go +++ b/op-challenger/fault/position_test.go @@ -36,27 +36,29 @@ type testNodeInfo struct { Depth int IndexAtDepth int TraceIndex uint64 + AttackGIndex uint64 // 0 indicates attack is not possible from this node + DefendGIndex uint64 // 0 indicates defend is not possible from this node } var treeNodesMaxDepth4 = []testNodeInfo{ - {GIndex: 1, Depth: 0, IndexAtDepth: 0, TraceIndex: 15}, + {GIndex: 1, Depth: 0, IndexAtDepth: 0, TraceIndex: 15, AttackGIndex: 2}, - {GIndex: 2, Depth: 1, IndexAtDepth: 0, TraceIndex: 7}, - {GIndex: 3, Depth: 1, IndexAtDepth: 1, TraceIndex: 15}, + {GIndex: 2, Depth: 1, IndexAtDepth: 0, TraceIndex: 7, AttackGIndex: 4, DefendGIndex: 6}, + {GIndex: 3, Depth: 1, IndexAtDepth: 1, TraceIndex: 15, AttackGIndex: 6}, - {GIndex: 4, Depth: 2, IndexAtDepth: 0, TraceIndex: 3}, - {GIndex: 5, Depth: 2, IndexAtDepth: 1, TraceIndex: 7}, - {GIndex: 6, Depth: 2, IndexAtDepth: 2, TraceIndex: 11}, - {GIndex: 7, Depth: 2, IndexAtDepth: 3, TraceIndex: 15}, + {GIndex: 4, Depth: 2, IndexAtDepth: 0, TraceIndex: 3, AttackGIndex: 8, DefendGIndex: 10}, + {GIndex: 5, Depth: 2, IndexAtDepth: 1, TraceIndex: 7, AttackGIndex: 10}, + {GIndex: 6, Depth: 2, IndexAtDepth: 2, TraceIndex: 11, AttackGIndex: 12, DefendGIndex: 14}, + {GIndex: 7, Depth: 2, IndexAtDepth: 3, TraceIndex: 15, AttackGIndex: 14}, - {GIndex: 8, Depth: 3, IndexAtDepth: 0, TraceIndex: 1}, - {GIndex: 9, Depth: 3, IndexAtDepth: 1, TraceIndex: 3}, - {GIndex: 10, Depth: 3, IndexAtDepth: 2, TraceIndex: 5}, - {GIndex: 11, Depth: 3, IndexAtDepth: 3, TraceIndex: 7}, - {GIndex: 12, Depth: 3, IndexAtDepth: 4, TraceIndex: 9}, - {GIndex: 13, Depth: 3, IndexAtDepth: 5, TraceIndex: 11}, - {GIndex: 14, Depth: 3, IndexAtDepth: 6, TraceIndex: 13}, - {GIndex: 15, Depth: 3, IndexAtDepth: 7, TraceIndex: 15}, + {GIndex: 8, Depth: 3, IndexAtDepth: 0, TraceIndex: 1, AttackGIndex: 16, DefendGIndex: 18}, + {GIndex: 9, Depth: 3, IndexAtDepth: 1, TraceIndex: 3, AttackGIndex: 18}, + {GIndex: 10, Depth: 3, IndexAtDepth: 2, TraceIndex: 5, AttackGIndex: 20, DefendGIndex: 22}, + {GIndex: 11, Depth: 3, IndexAtDepth: 3, TraceIndex: 7, AttackGIndex: 22}, + {GIndex: 12, Depth: 3, IndexAtDepth: 4, TraceIndex: 9, AttackGIndex: 24, DefendGIndex: 26}, + {GIndex: 13, Depth: 3, IndexAtDepth: 5, TraceIndex: 11, AttackGIndex: 26}, + {GIndex: 14, Depth: 3, IndexAtDepth: 6, TraceIndex: 13, AttackGIndex: 28, DefendGIndex: 30}, + {GIndex: 15, Depth: 3, IndexAtDepth: 7, TraceIndex: 15, AttackGIndex: 30}, {GIndex: 16, Depth: 4, IndexAtDepth: 0, TraceIndex: 0}, {GIndex: 17, Depth: 4, IndexAtDepth: 1, TraceIndex: 1}, @@ -95,3 +97,25 @@ func TestTraceIndex(t *testing.T) { require.Equal(t, test.TraceIndex, result) } } + +func TestAttack(t *testing.T) { + for _, test := range treeNodesMaxDepth4 { + if test.AttackGIndex == 0 { + continue + } + pos := NewPosition(test.Depth, test.IndexAtDepth) + result := pos.Attack() + require.Equalf(t, test.AttackGIndex, result.ToGIndex(), "Attack from GIndex %v", pos.ToGIndex()) + } +} + +func TestDefend(t *testing.T) { + for _, test := range treeNodesMaxDepth4 { + if test.DefendGIndex == 0 { + continue + } + pos := NewPosition(test.Depth, test.IndexAtDepth) + result := pos.Defend() + require.Equalf(t, test.DefendGIndex, result.ToGIndex(), "Defend from GIndex %v", pos.ToGIndex()) + } +}