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 5 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 rpc/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ 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
}
Expand Down
7 changes: 0 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 Down Expand Up @@ -63,8 +62,6 @@ type round struct {
teardownChan chan struct{}

stateCache *roundState

submitMtx sync.Mutex
}

func (r *round) Epoch() uint32 {
Expand Down Expand Up @@ -141,8 +138,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 @@ -177,13 +172,11 @@ func (r *round) execute(ctx context.Context, end time.Time, minMemoryLayer uint)

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 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