Commit 5d0c3b3e authored by Andreas Bigger's avatar Andreas Bigger

Localized positions

parent 181c74a3
......@@ -35,13 +35,17 @@ func NewTraceProvider(logger log.Logger, providers []types.TraceProvider, depthT
}
}
func (s *SplitTraceProvider) providerForDepth(depth uint64) types.TraceProvider {
func (s *SplitTraceProvider) providerForDepth(depth uint64) (uint64, types.TraceProvider) {
reduced := uint64(0)
for i, tier := range s.depthTiers {
if depth <= tier {
return s.providers[i]
return reduced, s.providers[i]
}
if i < len(s.providers)-1 {
reduced += tier
}
}
return s.providers[len(s.providers)-1]
return reduced, s.providers[len(s.providers)-1]
}
// Get routes the Get request to the internal [types.TraceProvider] that
......@@ -50,7 +54,10 @@ func (s *SplitTraceProvider) Get(ctx context.Context, pos types.Position) (commo
if len(s.providers) == 0 {
return common.Hash{}, NoProvidersErr
}
return s.providerForDepth(uint64(pos.Depth())).Get(ctx, pos.TraceIndex(pos.Depth()))
reduced, provider := s.providerForDepth(uint64(pos.Depth()))
localizedPosition := pos.Localize(reduced)
// todo(refcell): we should just pass the localized position once `Get` is updated to accept a Position
return provider.Get(ctx, localizedPosition.ToGIndex())
}
// AbsolutePreStateCommitment returns the absolute prestate from the lowest internal [types.TraceProvider]
......
......@@ -2,7 +2,7 @@ package split
import (
"context"
"fmt"
"errors"
"testing"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
......@@ -14,7 +14,7 @@ import (
)
var (
mockGetError = fmt.Errorf("mock get error")
mockGetError = errors.New("mock get error")
mockOutput = common.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
mockCommitment = common.HexToHash("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
)
......@@ -38,9 +38,10 @@ func TestGet(t *testing.T) {
providers: []types.TraceProvider{&mockOutputProvider},
depthTiers: []uint64{40, 20},
}
output, err := splitProvider.Get(context.Background(), types.NewPosition(1, 0))
output, err := splitProvider.Get(context.Background(), types.NewPosition(6, 3))
require.NoError(t, err)
require.Equal(t, mockOutput, output)
expectedGIndex := types.NewPosition(6, 3).ToGIndex()
require.Equal(t, common.BytesToHash([]byte{byte(expectedGIndex)}), output)
})
t.Run("ReturnsCorrectOutputWithMultipleProviders", func(t *testing.T) {
......@@ -51,9 +52,10 @@ func TestGet(t *testing.T) {
providers: []types.TraceProvider{&firstOutputProvider, &secondOutputProvider},
depthTiers: []uint64{40, 20},
}
output, err := splitProvider.Get(context.Background(), types.NewPosition(41, 0))
output, err := splitProvider.Get(context.Background(), types.NewPosition(42, 17))
require.NoError(t, err)
require.Equal(t, mockOutput, output)
expectedGIndex := types.NewPosition(2, 1).ToGIndex()
require.Equal(t, common.BytesToHash([]byte{byte(expectedGIndex)}), output)
})
}
......@@ -121,7 +123,7 @@ func (m *mockTraceProvider) Get(ctx context.Context, i uint64) (common.Hash, err
if m.getError != nil {
return common.Hash{}, m.getError
}
return m.getOutput, nil
return common.BytesToHash([]byte{byte(i)}), nil
}
func (m *mockTraceProvider) AbsolutePreStateCommitment(ctx context.Context) (hash common.Hash, err error) {
......
......@@ -25,6 +25,15 @@ func (p Position) MoveRight() Position {
}
}
// Localize returns a new position for a subtree.
// reduced is the number of levels to reduce the depth by.
func (p Position) Localize(reduced uint64) Position {
newPosDepth := uint64(p.depth) - reduced
nodesAtDepth := 1 << newPosDepth
newIndexAtDepth := p.indexAtDepth % nodesAtDepth
return NewPosition(int(newPosDepth), newIndexAtDepth)
}
func (p Position) Depth() int {
return p.depth
}
......
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