Skip to content

Commit

Permalink
op-challenger: Trigger verification of completed preimages (ethereum-…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsutton authored Jan 19, 2024
1 parent 8c8fa15 commit 78d2cdd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 14 deletions.
31 changes: 21 additions & 10 deletions op-challenger/game/keccak/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ import (
"github.com/ethereum/go-ethereum/log"
)

type Verifier interface {
Verify(ctx context.Context, oracle types.LargePreimageOracle, preimage types.LargePreimageMetaData)
}

type LargePreimageScheduler struct {
log log.Logger
ch chan common.Hash
oracles []types.LargePreimageOracle
cancel func()
wg sync.WaitGroup
log log.Logger
ch chan common.Hash
oracles []types.LargePreimageOracle
verifier Verifier
cancel func()
wg sync.WaitGroup
}

func NewLargePreimageScheduler(logger log.Logger, oracles []types.LargePreimageOracle) *LargePreimageScheduler {
func NewLargePreimageScheduler(logger log.Logger, oracles []types.LargePreimageOracle, verifier Verifier) *LargePreimageScheduler {
return &LargePreimageScheduler{
log: logger,
ch: make(chan common.Hash, 1),
oracles: oracles,
log: logger,
ch: make(chan common.Hash, 1),
oracles: oracles,
verifier: verifier,
}
}

Expand Down Expand Up @@ -71,6 +77,11 @@ func (s *LargePreimageScheduler) verifyPreimages(ctx context.Context, blockHash
}

func (s *LargePreimageScheduler) verifyOraclePreimages(ctx context.Context, oracle types.LargePreimageOracle, blockHash common.Hash) error {
_, err := oracle.GetActivePreimages(ctx, blockHash)
preimages, err := oracle.GetActivePreimages(ctx, blockHash)
for _, preimage := range preimages {
if preimage.ShouldVerify() {
s.verifier.Verify(ctx, oracle, preimage)
}
}
return err
}
56 changes: 53 additions & 3 deletions op-challenger/game/keccak/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keccak

import (
"context"
"math/big"
"sync"
"testing"
"time"
Expand All @@ -16,21 +17,51 @@ import (
func TestScheduleNextCheck(t *testing.T) {
ctx := context.Background()
logger := testlog.Logger(t, log.LvlInfo)
oracle := &stubOracle{}
scheduler := NewLargePreimageScheduler(logger, []types.LargePreimageOracle{oracle})
preimage1 := types.LargePreimageMetaData{ // Incomplete so won't be verified
LargePreimageIdent: types.LargePreimageIdent{
Claimant: common.Address{0xab},
UUID: big.NewInt(111),
},
}
preimage2 := types.LargePreimageMetaData{ // Already countered so won't be verified
LargePreimageIdent: types.LargePreimageIdent{
Claimant: common.Address{0xab},
UUID: big.NewInt(222),
},
Timestamp: 1234,
Countered: true,
}
preimage3 := types.LargePreimageMetaData{
LargePreimageIdent: types.LargePreimageIdent{
Claimant: common.Address{0xdd},
UUID: big.NewInt(333),
},
Timestamp: 1234,
}
oracle := &stubOracle{
images: []types.LargePreimageMetaData{preimage1, preimage2, preimage3},
}
verifier := &stubVerifier{}
scheduler := NewLargePreimageScheduler(logger, []types.LargePreimageOracle{oracle}, verifier)
scheduler.Start(ctx)
defer scheduler.Close()
err := scheduler.Schedule(common.Hash{0xaa}, 3)
require.NoError(t, err)
require.Eventually(t, func() bool {
return oracle.GetPreimagesCount() == 1
}, 10*time.Second, 10*time.Millisecond)
require.Eventually(t, func() bool {
verified := verifier.Verified()
t.Logf("Verified preimages: %v", verified)
return len(verified) == 1 && verified[0] == preimage3
}, 10*time.Second, 10*time.Millisecond, "Did not verify preimage")
}

type stubOracle struct {
m sync.Mutex
addr common.Address
getPreimagesCount int
images []types.LargePreimageMetaData
}

func (s *stubOracle) Addr() common.Address {
Expand All @@ -41,11 +72,30 @@ func (s *stubOracle) GetActivePreimages(_ context.Context, _ common.Hash) ([]typ
s.m.Lock()
defer s.m.Unlock()
s.getPreimagesCount++
return nil, nil
return s.images, nil
}

func (s *stubOracle) GetPreimagesCount() int {
s.m.Lock()
defer s.m.Unlock()
return s.getPreimagesCount
}

type stubVerifier struct {
m sync.Mutex
verified []types.LargePreimageMetaData
}

func (s *stubVerifier) Verify(_ context.Context, _ types.LargePreimageOracle, image types.LargePreimageMetaData) {
s.m.Lock()
defer s.m.Unlock()
s.verified = append(s.verified, image)
}

func (s *stubVerifier) Verified() []types.LargePreimageMetaData {
s.m.Lock()
defer s.m.Unlock()
v := make([]types.LargePreimageMetaData, len(s.verified))
copy(v, s.verified)
return v
}
22 changes: 22 additions & 0 deletions op-challenger/game/keccak/verifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keccak

import (
"context"

"github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum/go-ethereum/log"
)

type PreimageVerifier struct {
log log.Logger
}

func NewPreimageVerifier(logger log.Logger) *PreimageVerifier {
return &PreimageVerifier{
log: logger,
}
}

func (v *PreimageVerifier) Verify(ctx context.Context, oracle types.LargePreimageOracle, preimage types.LargePreimageMetaData) {
// No verification currently performed.
}
3 changes: 2 additions & 1 deletion op-challenger/game/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func (s *Service) initScheduler(cfg *config.Config) error {
}

func (s *Service) initLargePreimages() error {
s.preimages = keccak.NewLargePreimageScheduler(s.logger, s.registry.Oracles())
verifier := keccak.NewPreimageVerifier(s.logger)
s.preimages = keccak.NewLargePreimageScheduler(s.logger, s.registry.Oracles(), verifier)
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions op-challenger/game/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type LargePreimageMetaData struct {
Countered bool
}

// ShouldVerify returns true if the preimage upload is complete and has not yet been countered.
// Note that the challenge period for the preimage may have expired but the image not yet been finalized.
func (m LargePreimageMetaData) ShouldVerify() bool {
return m.Timestamp > 0 && !m.Countered
}

type LargePreimageOracle interface {
Addr() common.Address
GetActivePreimages(ctx context.Context, blockHash common.Hash) ([]LargePreimageMetaData, error)
Expand Down

0 comments on commit 78d2cdd

Please sign in to comment.