Commit 07b0dc29 authored by Joshua Gutow's avatar Joshua Gutow Committed by GitHub

op-challenger API Cleanups (#6108)

* Use uint64 as trace index

* Return new Position object when attacking & defending

* Use the new API with the solver
parent 38003570
...@@ -11,11 +11,11 @@ func PositionExampleOne() { ...@@ -11,11 +11,11 @@ func PositionExampleOne() {
// go left to d, then right to f, then left to e // go left to d, then right to f, then left to e
p := fault.Position{} p := fault.Position{}
p.Print(3) p.Print(3)
p.Attack() p = p.Attack()
p.Print(3) p.Print(3)
p.Defend() p = p.Defend()
p.Print(3) p.Print(3)
p.Attack() p = p.Attack()
p.Print(3) p.Print(3)
} }
...@@ -26,10 +26,10 @@ func PositionExampleTwo() { ...@@ -26,10 +26,10 @@ func PositionExampleTwo() {
// go left r, then left to b, then right to q // go left r, then left to b, then right to q
p := fault.Position{} p := fault.Position{}
p.Print(3) p.Print(3)
p.Attack() p = p.Attack()
p.Print(3) p.Print(3)
p.Attack() p = p.Attack()
p.Print(3) p.Print(3)
p.Defend() p = p.Defend()
p.Print(3) p.Print(3)
} }
...@@ -28,11 +28,11 @@ func (p *Position) IndexAtDepth() int { ...@@ -28,11 +28,11 @@ func (p *Position) IndexAtDepth() int {
// TraceIndex calculates the what the index of the claim value would be inside the trace. // TraceIndex calculates the what the index of the claim value would be inside the trace.
// It is equivalent to going right until the final depth has been reached. // It is equivalent to going right until the final depth has been reached.
func (p *Position) TraceIndex(maxDepth int) int { func (p *Position) TraceIndex(maxDepth int) uint64 {
// When we go right, we do a shift left and set the bottom bit to be 1. // When we go right, we do a shift left and set the bottom bit to be 1.
// To do this in a single step, do all the shifts at once & or in all 1s for the bottom bits. // To do this in a single step, do all the shifts at once & or in all 1s for the bottom bits.
rd := maxDepth - p.depth rd := maxDepth - p.depth
return p.indexAtDepth<<rd | ((1 << rd) - 1) return uint64(p.indexAtDepth<<rd | ((1 << rd) - 1))
} }
// move goes to the left or right child. // move goes to the left or right child.
...@@ -55,16 +55,20 @@ func (p *Position) parent() { ...@@ -55,16 +55,20 @@ func (p *Position) parent() {
p.indexAtDepth = p.indexAtDepth >> 1 p.indexAtDepth = p.indexAtDepth >> 1
} }
// Attack moves this position to a position to the left which disagrees with this position. // Attack creates a new position which is the attack position of this one.
func (p *Position) Attack() { func (p *Position) Attack() Position {
p.move(false) p2 := NewPosition(p.depth, p.indexAtDepth)
p2.move(false)
return p2
} }
// Defend moves this position to the right which agrees with this position. Note: // Defend creates a new position which is the defend position of this one.
func (p *Position) Defend() { func (p *Position) Defend() Position {
p.parent() p2 := NewPosition(p.depth, p.indexAtDepth)
p.move(true) p2.parent()
p.move(false) p2.move(true)
p2.move(false)
return p2
} }
func (p *Position) Print(maxDepth int) { func (p *Position) Print(maxDepth int) {
......
...@@ -35,7 +35,7 @@ type testNodeInfo struct { ...@@ -35,7 +35,7 @@ type testNodeInfo struct {
GIndex uint64 GIndex uint64
Depth int Depth int
IndexAtDepth int IndexAtDepth int
TraceIndex int TraceIndex uint64
} }
var treeNodesMaxDepth4 = []testNodeInfo{ var treeNodesMaxDepth4 = []testNodeInfo{
......
...@@ -58,8 +58,7 @@ func (s *Solver) doNothing() (*Response, error) { ...@@ -58,8 +58,7 @@ func (s *Solver) doNothing() (*Response, error) {
// attack returns a response that attacks the claim. // attack returns a response that attacks the claim.
func (s *Solver) attack(claim Claim) (*Response, error) { func (s *Solver) attack(claim Claim) (*Response, error) {
claim.Position.Attack() value, err := s.traceAtPosition(claim.Position.Attack())
value, err := s.traceAtPosition(&claim.Position)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -68,8 +67,7 @@ func (s *Solver) attack(claim Claim) (*Response, error) { ...@@ -68,8 +67,7 @@ func (s *Solver) attack(claim Claim) (*Response, error) {
// defend returns a response that defends the claim. // defend returns a response that defends the claim.
func (s *Solver) defend(claim Claim) (*Response, error) { func (s *Solver) defend(claim Claim) (*Response, error) {
claim.Position.Defend() value, err := s.traceAtPosition(claim.Position.Defend())
value, err := s.traceAtPosition(&claim.Position)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -78,14 +76,13 @@ func (s *Solver) defend(claim Claim) (*Response, error) { ...@@ -78,14 +76,13 @@ func (s *Solver) defend(claim Claim) (*Response, error) {
// agreeWithClaim returns true if the [Claim] is correct according to the internal [TraceProvider]. // agreeWithClaim returns true if the [Claim] is correct according to the internal [TraceProvider].
func (s *Solver) agreeWithClaim(claim Claim) (bool, error) { func (s *Solver) agreeWithClaim(claim Claim) (bool, error) {
ourValue, err := s.traceAtPosition(&claim.Position) ourValue, err := s.traceAtPosition(claim.Position)
return ourValue == claim.Value, err return ourValue == claim.Value, err
} }
// traceAtPosition returns the [common.Hash] from internal [TraceProvider] at the given [Position]. // traceAtPosition returns the [common.Hash] from internal [TraceProvider] at the given [Position].
func (s *Solver) traceAtPosition(p *Position) (common.Hash, error) { func (s *Solver) traceAtPosition(p Position) (common.Hash, error) {
// todo: update TraceIndex to return a uint64 to keep types consistent
index := p.TraceIndex(s.gameDepth) index := p.TraceIndex(s.gameDepth)
hash, err := s.Get(uint64(index)) hash, err := s.Get(index)
return hash, err return hash, err
} }
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