Skip to content

Commit

Permalink
Refactor / move mock edge so it can be used by other packages (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon authored Jul 20, 2023
1 parent 9c9d8a9 commit 9201c47
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 251 deletions.
1 change: 1 addition & 0 deletions challenge-manager/challenge-tree/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_test(
embed = [":challenge-tree"],
deps = [
"//chain-abstraction:protocol",
"//challenge-manager/challenge-tree/mock",
"//containers/option",
"//containers/threadsafe",
"//layer2-state-provider",
Expand Down
113 changes: 57 additions & 56 deletions challenge-manager/challenge-tree/ancestors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

protocol "github.com/OffchainLabs/challenge-protocol-v2/chain-abstraction"
"github.com/OffchainLabs/challenge-protocol-v2/challenge-manager/challenge-tree/mock"
"github.com/OffchainLabs/challenge-protocol-v2/containers/option"
"github.com/OffchainLabs/challenge-protocol-v2/containers/threadsafe"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -200,10 +201,10 @@ func Test_findOriginEdge(t *testing.T) {
require.Equal(t, got.Id(), protocol.EdgeId(common.BytesToHash([]byte("blk-0.b-4.b"))))
}

func buildEdges(allEdges ...*edge) map[edgeId]*edge {
m := make(map[edgeId]*edge)
func buildEdges(allEdges ...*mock.Edge) map[mock.EdgeId]*mock.Edge {
m := make(map[mock.EdgeId]*mock.Edge)
for _, e := range allEdges {
m[e.id] = e
m[e.ID] = e
}
return m
}
Expand Down Expand Up @@ -242,23 +243,23 @@ func setupBlockChallengeTreeSnapshot(t *testing.T, tree *HonestChallengeTree) {
)
// Child-relationship linking.
// Alice.
aliceEdges["blk-0.a-16.a"].lowerChildId = "blk-0.a-8.a"
aliceEdges["blk-0.a-16.a"].upperChildId = "blk-8.a-16.a"
aliceEdges["blk-0.a-8.a"].lowerChildId = "blk-0.a-4.a"
aliceEdges["blk-0.a-8.a"].upperChildId = "blk-4.a-8.a"
aliceEdges["blk-4.a-8.a"].lowerChildId = "blk-4.a-6.a"
aliceEdges["blk-4.a-8.a"].upperChildId = "blk-6.a-8.a"
aliceEdges["blk-4.a-6.a"].lowerChildId = "blk-4.a-5.a"
aliceEdges["blk-4.a-6.a"].upperChildId = "blk-5.a-6.a"
aliceEdges["blk-0.a-16.a"].LowerChildID = "blk-0.a-8.a"
aliceEdges["blk-0.a-16.a"].UpperChildID = "blk-8.a-16.a"
aliceEdges["blk-0.a-8.a"].LowerChildID = "blk-0.a-4.a"
aliceEdges["blk-0.a-8.a"].UpperChildID = "blk-4.a-8.a"
aliceEdges["blk-4.a-8.a"].LowerChildID = "blk-4.a-6.a"
aliceEdges["blk-4.a-8.a"].UpperChildID = "blk-6.a-8.a"
aliceEdges["blk-4.a-6.a"].LowerChildID = "blk-4.a-5.a"
aliceEdges["blk-4.a-6.a"].UpperChildID = "blk-5.a-6.a"
// Bob.
bobEdges["blk-0.a-16.b"].lowerChildId = "blk-0.a-8.b"
bobEdges["blk-0.a-16.b"].upperChildId = "blk-8.b-16.b"
bobEdges["blk-0.a-8.b"].lowerChildId = "blk-0.a-4.a"
bobEdges["blk-0.a-8.b"].upperChildId = "blk-4.a-8.b"
bobEdges["blk-4.a-8.b"].lowerChildId = "blk-4.a-6.b"
bobEdges["blk-4.a-8.b"].upperChildId = "blk-6.b-6.8"
bobEdges["blk-4.a-6.b"].lowerChildId = "blk-4.a-5.b"
bobEdges["blk-4.a-6.b"].upperChildId = "blk-5.b-6.b"
bobEdges["blk-0.a-16.b"].LowerChildID = "blk-0.a-8.b"
bobEdges["blk-0.a-16.b"].UpperChildID = "blk-8.b-16.b"
bobEdges["blk-0.a-8.b"].LowerChildID = "blk-0.a-4.a"
bobEdges["blk-0.a-8.b"].UpperChildID = "blk-4.a-8.b"
bobEdges["blk-4.a-8.b"].LowerChildID = "blk-4.a-6.b"
bobEdges["blk-4.a-8.b"].UpperChildID = "blk-6.b-6.8"
bobEdges["blk-4.a-6.b"].LowerChildID = "blk-4.a-5.b"
bobEdges["blk-4.a-6.b"].UpperChildID = "blk-5.b-6.b"

transformedEdges := make(map[protocol.EdgeId]protocol.SpecEdge)
for _, v := range aliceEdges {
Expand Down Expand Up @@ -327,7 +328,7 @@ func setupBlockChallengeTreeSnapshot(t *testing.T, tree *HonestChallengeTree) {
mutuals.Put(b.Id(), creationTime(bCreation))
}

func id(eId edgeId) protocol.EdgeId {
func id(eId mock.EdgeId) protocol.EdgeId {
return protocol.EdgeId(common.BytesToHash([]byte(eId)))
}

Expand All @@ -340,8 +341,8 @@ func id(eId edgeId) protocol.EdgeId {
// and then inserts the respective edges into a challenge tree.
func setupBigStepChallengeSnapshot(t *testing.T, tree *HonestChallengeTree, claimId string) {
t.Helper()
originEdge := tree.edges.Get(id(edgeId(claimId))).(*edge)
origin := originId(originEdge.computeMutualId())
originEdge := tree.edges.Get(id(mock.EdgeId(claimId))).(*mock.Edge)
origin := mock.OriginId(originEdge.ComputeMutualId())
aliceEdges := buildEdges(
// Alice.
newEdge(&newCfg{t: t, edgeId: "big-0.a-16.a", originId: origin, claimId: claimId, createdAt: 11}),
Expand All @@ -367,23 +368,23 @@ func setupBigStepChallengeSnapshot(t *testing.T, tree *HonestChallengeTree, clai
)
// Child-relationship linking.
// Alice.
aliceEdges["big-0.a-16.a"].lowerChildId = "big-0.a-8.a"
aliceEdges["big-0.a-16.a"].upperChildId = "big-8.a-16.a"
aliceEdges["big-0.a-8.a"].lowerChildId = "big-0.a-4.a"
aliceEdges["big-0.a-8.a"].upperChildId = "big-4.a-8.a"
aliceEdges["big-4.a-8.a"].lowerChildId = "big-4.a-6.a"
aliceEdges["big-4.a-8.a"].upperChildId = "big-6.a-8.a"
aliceEdges["big-4.a-6.a"].lowerChildId = "big-4.a-5.a"
aliceEdges["big-4.a-6.a"].upperChildId = "big-5.a-6.a"
aliceEdges["big-0.a-16.a"].LowerChildID = "big-0.a-8.a"
aliceEdges["big-0.a-16.a"].UpperChildID = "big-8.a-16.a"
aliceEdges["big-0.a-8.a"].LowerChildID = "big-0.a-4.a"
aliceEdges["big-0.a-8.a"].UpperChildID = "big-4.a-8.a"
aliceEdges["big-4.a-8.a"].LowerChildID = "big-4.a-6.a"
aliceEdges["big-4.a-8.a"].UpperChildID = "big-6.a-8.a"
aliceEdges["big-4.a-6.a"].LowerChildID = "big-4.a-5.a"
aliceEdges["big-4.a-6.a"].UpperChildID = "big-5.a-6.a"
// Bob.
bobEdges["big-0.a-16.b"].lowerChildId = "big-0.a-8.b"
bobEdges["big-0.a-16.b"].upperChildId = "big-8.b-16.b"
bobEdges["big-0.a-8.b"].lowerChildId = "big-0.a-4.a"
bobEdges["big-0.a-8.b"].upperChildId = "big-4.a-8.b"
bobEdges["big-4.a-8.b"].lowerChildId = "big-4.a-6.b"
bobEdges["big-4.a-8.b"].upperChildId = "big-6.b-6.8"
bobEdges["big-4.a-6.b"].lowerChildId = "big-4.a-5.b"
bobEdges["big-4.a-6.b"].upperChildId = "big-5.b-6.b"
bobEdges["big-0.a-16.b"].LowerChildID = "big-0.a-8.b"
bobEdges["big-0.a-16.b"].UpperChildID = "big-8.b-16.b"
bobEdges["big-0.a-8.b"].LowerChildID = "big-0.a-4.a"
bobEdges["big-0.a-8.b"].UpperChildID = "big-4.a-8.b"
bobEdges["big-4.a-8.b"].LowerChildID = "big-4.a-6.b"
bobEdges["big-4.a-8.b"].UpperChildID = "big-6.b-6.8"
bobEdges["big-4.a-6.b"].LowerChildID = "big-4.a-5.b"
bobEdges["big-4.a-6.b"].UpperChildID = "big-5.b-6.b"

for _, v := range aliceEdges {
tree.edges.Put(v.Id(), v)
Expand Down Expand Up @@ -462,8 +463,8 @@ func setupBigStepChallengeSnapshot(t *testing.T, tree *HonestChallengeTree, clai
// and then inserts the respective edges into a challenge tree.
func setupSmallStepChallengeSnapshot(t *testing.T, tree *HonestChallengeTree, claimId string) {
t.Helper()
originEdge := tree.edges.Get(id(edgeId(claimId))).(*edge)
origin := originId(originEdge.computeMutualId())
originEdge := tree.edges.Get(id(mock.EdgeId(claimId))).(*mock.Edge)
origin := mock.OriginId(originEdge.ComputeMutualId())
aliceEdges := buildEdges(
// Alice.
newEdge(&newCfg{t: t, edgeId: "smol-0.a-16.a", originId: origin, claimId: claimId, createdAt: 21}),
Expand All @@ -489,23 +490,23 @@ func setupSmallStepChallengeSnapshot(t *testing.T, tree *HonestChallengeTree, cl
)
// Child-relationship linking.
// Alice.
aliceEdges["smol-0.a-16.a"].lowerChildId = "smol-0.a-8.a"
aliceEdges["smol-0.a-16.a"].upperChildId = "smol-8.a-16.a"
aliceEdges["smol-0.a-8.a"].lowerChildId = "smol-0.a-4.a"
aliceEdges["smol-0.a-8.a"].upperChildId = "smol-4.a-8.a"
aliceEdges["smol-4.a-8.a"].lowerChildId = "smol-4.a-6.a"
aliceEdges["smol-4.a-8.a"].upperChildId = "smol-6.a-8.a"
aliceEdges["smol-4.a-6.a"].lowerChildId = "smol-4.a-5.a"
aliceEdges["smol-4.a-6.a"].upperChildId = "smol-5.a-6.a"
aliceEdges["smol-0.a-16.a"].LowerChildID = "smol-0.a-8.a"
aliceEdges["smol-0.a-16.a"].UpperChildID = "smol-8.a-16.a"
aliceEdges["smol-0.a-8.a"].LowerChildID = "smol-0.a-4.a"
aliceEdges["smol-0.a-8.a"].UpperChildID = "smol-4.a-8.a"
aliceEdges["smol-4.a-8.a"].LowerChildID = "smol-4.a-6.a"
aliceEdges["smol-4.a-8.a"].UpperChildID = "smol-6.a-8.a"
aliceEdges["smol-4.a-6.a"].LowerChildID = "smol-4.a-5.a"
aliceEdges["smol-4.a-6.a"].UpperChildID = "smol-5.a-6.a"
// Bob.
bobEdges["smol-0.a-16.b"].lowerChildId = "smol-0.a-8.b"
bobEdges["smol-0.a-16.b"].upperChildId = "smol-8.b-16.b"
bobEdges["smol-0.a-8.b"].lowerChildId = "smol-0.a-4.a"
bobEdges["smol-0.a-8.b"].upperChildId = "smol-4.a-8.b"
bobEdges["smol-4.a-8.b"].lowerChildId = "smol-4.a-6.b"
bobEdges["smol-4.a-8.b"].upperChildId = "smol-6.b-6.8"
bobEdges["smol-4.a-6.b"].lowerChildId = "smol-4.a-5.b"
bobEdges["smol-4.a-6.b"].upperChildId = "smol-5.b-6.b"
bobEdges["smol-0.a-16.b"].LowerChildID = "smol-0.a-8.b"
bobEdges["smol-0.a-16.b"].UpperChildID = "smol-8.b-16.b"
bobEdges["smol-0.a-8.b"].LowerChildID = "smol-0.a-4.a"
bobEdges["smol-0.a-8.b"].UpperChildID = "smol-4.a-8.b"
bobEdges["smol-4.a-8.b"].LowerChildID = "smol-4.a-6.b"
bobEdges["smol-4.a-8.b"].UpperChildID = "smol-6.b-6.8"
bobEdges["smol-4.a-6.b"].LowerChildID = "smol-4.a-5.b"
bobEdges["smol-4.a-6.b"].UpperChildID = "smol-5.b-6.b"

for _, v := range aliceEdges {
tree.edges.Put(v.Id(), v)
Expand Down
14 changes: 14 additions & 0 deletions challenge-manager/challenge-tree/mock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "mock",
testonly = True,
srcs = ["edge.go"],
importpath = "github.com/OffchainLabs/challenge-protocol-v2/challenge-manager/challenge-tree/mock",
visibility = ["//visibility:public"],
deps = [
"//chain-abstraction:protocol",
"//containers/option",
"@com_github_ethereum_go_ethereum//common",
],
)
156 changes: 156 additions & 0 deletions challenge-manager/challenge-tree/mock/edge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package mock

import (
"context"
"errors"
"fmt"

protocol "github.com/OffchainLabs/challenge-protocol-v2/chain-abstraction"
"github.com/OffchainLabs/challenge-protocol-v2/containers/option"
"github.com/ethereum/go-ethereum/common"
)

var _ = protocol.ReadOnlyEdge(&Edge{})

type EdgeId string
type Commit string
type OriginId string

// Mock Edge for challenge tree specific tests, making it easier for test ergonomics.
type Edge struct {
ID EdgeId
EdgeType protocol.EdgeType
StartHeight uint64
StartCommit Commit
EndHeight uint64
EndCommit Commit
OriginID OriginId
ClaimID string
LowerChildID EdgeId
UpperChildID EdgeId
CreationBlock uint64
}

func (e *Edge) Id() protocol.EdgeId {
return protocol.EdgeId(common.BytesToHash([]byte(e.ID)))
}

func (e *Edge) GetType() protocol.EdgeType {
return e.EdgeType
}

func (e *Edge) StartCommitment() (protocol.Height, common.Hash) {
return protocol.Height(e.StartHeight), common.BytesToHash([]byte(e.StartCommit))
}

func (e *Edge) EndCommitment() (protocol.Height, common.Hash) {
return protocol.Height(e.EndHeight), common.BytesToHash([]byte(e.EndCommit))
}

func (e *Edge) CreatedAtBlock() (uint64, error) {
return e.CreationBlock, nil
}

func (e *Edge) OriginId() protocol.OriginId {
return protocol.OriginId(common.BytesToHash([]byte(e.OriginID)))
}

func (e *Edge) MutualId() protocol.MutualId {
return protocol.MutualId(common.BytesToHash([]byte(e.ComputeMutualId())))
}

func (e *Edge) ComputeMutualId() string {
return fmt.Sprintf(
"%d-%s-%d-%s-%d",
e.EdgeType,
e.OriginID,
e.StartHeight,
e.StartCommit,
e.EndHeight,
)
}

// The claim id of the edge, if any
func (e *Edge) ClaimId() option.Option[protocol.ClaimId] {
if e.ClaimID == "" {
return option.None[protocol.ClaimId]()
}
return option.Some(protocol.ClaimId(common.BytesToHash([]byte(e.ClaimID))))
}

// The lower child of the edge, if any.
func (e *Edge) LowerChild(_ context.Context) (option.Option[protocol.EdgeId], error) {
if e.LowerChildID == "" {
return option.None[protocol.EdgeId](), nil
}
return option.Some(protocol.EdgeId(common.BytesToHash([]byte(e.LowerChildID)))), nil
}

// The upper child of the edge, if any.
func (e *Edge) UpperChild(_ context.Context) (option.Option[protocol.EdgeId], error) {
if e.UpperChildID == "" {
return option.None[protocol.EdgeId](), nil
}
return option.Some(protocol.EdgeId(common.BytesToHash([]byte(e.UpperChildID)))), nil
}

func (e *Edge) HasChildren(ctx context.Context) (bool, error) {
return e.LowerChildID != "" && e.UpperChildID != "", nil
}

// The ministaker of an edge. Only existing for level zero edges.
func (*Edge) MiniStaker() option.Option[common.Address] {
return option.None[common.Address]()
}

// The assertion hash of the parent assertion that originated the challenge
// at the top-level.
func (*Edge) AssertionHash(_ context.Context) (protocol.AssertionHash, error) {
return protocol.AssertionHash{}, nil
}

// The time in seconds an edge has been unrivaled.
func (*Edge) TimeUnrivaled(_ context.Context) (uint64, error) {
return 0, nil
}

// The status of an edge.
func (*Edge) Status(_ context.Context) (protocol.EdgeStatus, error) {
return 0, nil
}

// Whether or not an edge has rivals.
func (*Edge) HasRival(_ context.Context) (bool, error) {
return false, nil
}

// Checks if an edge has a length one rival.
func (*Edge) HasLengthOneRival(_ context.Context) (bool, error) {
return false, nil
}

// The history commitment for the top-level edge the current edge's challenge is made upon.
// This is used at subchallenge creation boundaries.
func (*Edge) TopLevelClaimHeight(_ context.Context) (protocol.OriginHeights, error) {
return protocol.OriginHeights{}, nil
}

func (*Edge) Bisect(
_ context.Context,
_ common.Hash,
_ []byte,
) (protocol.SpecEdge, protocol.SpecEdge, error) {
return nil, nil, errors.New("unimplemented")
}

func (*Edge) ConfirmByTimer(_ context.Context, _ []protocol.EdgeId) error {
return errors.New("unimplemented")
}

func (*Edge) ConfirmByClaim(_ context.Context, _ protocol.ClaimId) error {
return errors.New("unimplemented")
}

func (*Edge) ConfirmByChildren(_ context.Context) error {
return errors.New("unimplemented")
}
Loading

0 comments on commit 9201c47

Please sign in to comment.