Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Service to actor pattern #172

Merged
merged 9 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ all: build
.PHONY: all

test:
$(GOTESTSUM) -- -timeout 5m -p 1 ./...
$(GOTESTSUM) -- -race -timeout 5m -p 1 ./...
.PHONY: test

install: install-buf install-protoc
Expand Down
2 changes: 1 addition & 1 deletion gateway/challenge_verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (a *caching) Verify(ctx context.Context, challenge, signature []byte) (*Res
logger := logging.FromContext(ctx).WithFields(log.Field(zap.Binary("challenge", challengeHash[:])))
if result, ok := a.cache.Get(challengeHash); ok {
logger.Debug("retrieved challenge verifier result from the cache")
// SAFETY: type assertion will never panic as we insert only `*ATX` values.
// SAFETY: type assertion will never panic as we insert only `*challengeVerifierResult` values.
result := result.(*challengeVerifierResult)
return result.Result, result.err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
golang.org/x/sync v0.1.0
google.golang.org/genproto v0.0.0-20221114212237-e4508ebdbee1
google.golang.org/grpc v1.51.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
8 changes: 4 additions & 4 deletions rpc/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *rpcServer) Start(ctx context.Context, in *api.StartRequest) (*api.Start

verifier, err := service.CreateChallengeVerifier(gtwManager.Connections())
if err != nil {
return nil, fmt.Errorf("failed to create ATX provider: %w", err)
return nil, fmt.Errorf("failed to create challenge verifier: %w", err)
}

// Swap the new and old gateway managers.
Expand Down Expand Up @@ -136,7 +136,7 @@ func (r *rpcServer) UpdateGateway(ctx context.Context, in *api.UpdateGatewayRequ

verifier, err := service.CreateChallengeVerifier(gtwManager.Connections())
if err != nil {
return nil, fmt.Errorf("failed to create ATX provider: %w", err)
return nil, fmt.Errorf("failed to create challenge verifier: %w", err)
}

// Swap the new and old gateway managers.
Expand All @@ -163,13 +163,13 @@ func (r *rpcServer) Submit(ctx context.Context, in *api.SubmitRequest) (*api.Sub
}

out := new(api.SubmitResponse)
out.RoundId = round.ID
out.RoundId = round
out.Hash = hash
return out, nil
}

func (r *rpcServer) GetInfo(ctx context.Context, in *api.GetInfoRequest) (*api.GetInfoResponse, error) {
info, err := r.s.Info()
info, err := r.s.Info(ctx)
if err != nil {
return nil, err
}
Expand Down
12 changes: 5 additions & 7 deletions service/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"strconv"
"sync"
"time"

"github.com/spacemeshos/merkle-tree"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/opt"

"github.com/spacemeshos/poet/hash"
"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/prover"
"github.com/spacemeshos/poet/shared"
)
Expand Down Expand Up @@ -63,8 +63,6 @@ type round struct {
teardownChan chan struct{}

stateCache *roundState

submitMtx sync.Mutex
}

func (r *round) Epoch() uint32 {
Expand Down Expand Up @@ -141,8 +139,6 @@ func (r *round) submit(key, challenge []byte) error {
return errors.New("round is not open")
}

r.submitMtx.Lock()
defer r.submitMtx.Unlock()
if has, err := r.challengesDb.Has(key); err != nil {
return err
} else if has {
Expand Down Expand Up @@ -170,20 +166,21 @@ func (r *round) isEmpty() bool {
}

func (r *round) execute(ctx context.Context, end time.Time, minMemoryLayer uint) error {
logger := logging.FromContext(ctx).WithFields(log.String("round", r.ID))
logger.Info("executing until %v...", end)

r.executionStarted = time.Now()
if err := r.saveState(); err != nil {
return err
}

close(r.executionStartedChan)

r.submitMtx.Lock()
var err error
r.execution.Members, r.execution.Statement, err = r.calcMembersAndStatement()
if err != nil {
return err
}
r.submitMtx.Unlock()

if err := r.saveState(); err != nil {
return err
Expand All @@ -208,6 +205,7 @@ func (r *round) execute(ctx context.Context, end time.Time, minMemoryLayer uint)

close(r.executionEndedChan)

logger.Info("execution ended, phi=%x, duration %v", r.execution.NIP.Root, time.Since(r.executionStarted))
return nil
}

Expand Down
16 changes: 15 additions & 1 deletion service/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"crypto/rand"
"fmt"
"path/filepath"
"testing"
Expand All @@ -12,6 +13,19 @@ import (
"github.com/spacemeshos/poet/prover"
)

func genChallenges(num int) ([][]byte, error) {
ch := make([][]byte, num)
for i := 0; i < num; i++ {
ch[i] = make([]byte, 32)
_, err := rand.Read(ch[i])
if err != nil {
return nil, err
}
}

return ch, nil
}

// TestRound_Recovery test round recovery functionality.
// The scenario proceeds as follows:
// - Execute r1 as a reference round.
Expand Down Expand Up @@ -183,7 +197,7 @@ func TestRound_State(t *testing.T) {
req.Equal(prevState, state)

// Recover execution.
req.NoError(r.recoverExecution(ctx, state.Execution, time.Now().Add(100*time.Microsecond)))
req.NoError(r.recoverExecution(ctx, state.Execution, time.Now().Add(200*time.Millisecond)))

req.False(r.executionStarted.IsZero())
proof, err := r.proof(false)
Expand Down
Loading