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() {
// go left to d, then right to f, then left to e
p := fault.Position{}
p.Print(3)
p.Attack()
p = p.Attack()
p.Print(3)
p.Defend()
p = p.Defend()
p.Print(3)
p.Attack()
p = p.Attack()
p.Print(3)
}
......@@ -26,10 +26,10 @@ func PositionExampleTwo() {
// go left r, then left to b, then right to q
p := fault.Position{}
p.Print(3)
p.Attack()
p = p.Attack()
p.Print(3)
p.Attack()
p = p.Attack()
p.Print(3)
p.Defend()
p = p.Defend()
p.Print(3)
}
......@@ -28,11 +28,11 @@ func (p *Position) IndexAtDepth() int {
// 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.
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.
// 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
return p.indexAtDepth<<rd | ((1 << rd) - 1)
return uint64(p.indexAtDepth<<rd | ((1 << rd) - 1))
}
// move goes to the left or right child.
......@@ -55,16 +55,20 @@ func (p *Position) parent() {
p.indexAtDepth = p.indexAtDepth >> 1
}
// Attack moves this position to a position to the left which disagrees with this position.
func (p *Position) Attack() {
p.move(false)
// Attack creates a new position which is the attack position of this one.
func (p *Position) Attack() Position {
p2 := NewPosition(p.depth, p.indexAtDepth)
p2.move(false)
return p2
}
// Defend moves this position to the right which agrees with this position. Note:
func (p *Position) Defend() {
p.parent()
p.move(true)
p.move(false)
// Defend creates a new position which is the defend position of this one.
func (p *Position) Defend() Position {
p2 := NewPosition(p.depth, p.indexAtDepth)
p2.parent()
p2.move(true)
p2.move(false)
return p2
}
func (p *Position) Print(maxDepth int) {
......
......@@ -35,7 +35,7 @@ type testNodeInfo struct {
GIndex uint64
Depth int
IndexAtDepth int
TraceIndex int
TraceIndex uint64
}
var treeNodesMaxDepth4 = []testNodeInfo{
......
......@@ -58,8 +58,7 @@ func (s *Solver) doNothing() (*Response, error) {
// attack returns a response that attacks the claim.
func (s *Solver) attack(claim Claim) (*Response, error) {
claim.Position.Attack()
value, err := s.traceAtPosition(&claim.Position)
value, err := s.traceAtPosition(claim.Position.Attack())
if err != nil {
return nil, err
}
......@@ -68,8 +67,7 @@ func (s *Solver) attack(claim Claim) (*Response, error) {
// defend returns a response that defends the claim.
func (s *Solver) defend(claim Claim) (*Response, error) {
claim.Position.Defend()
value, err := s.traceAtPosition(&claim.Position)
value, err := s.traceAtPosition(claim.Position.Defend())
if err != nil {
return nil, err
}
......@@ -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].
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
}
// traceAtPosition returns the [common.Hash] from internal [TraceProvider] at the given [Position].
func (s *Solver) traceAtPosition(p *Position) (common.Hash, error) {
// todo: update TraceIndex to return a uint64 to keep types consistent
func (s *Solver) traceAtPosition(p Position) (common.Hash, error) {
index := p.TraceIndex(s.gameDepth)
hash, err := s.Get(uint64(index))
hash, err := s.Get(index)
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