Skip to content
Permalink
Browse files

Service test (#26)

* Adding basic gRPC layer

* PR fixes

* Add test

* Minor types refactoring
  • Loading branch information...
moshababo committed Mar 19, 2019
1 parent 5cc4ab2 commit fc2c24f7a98867b31b849ccfcbbf508c177de411
Showing with 138 additions and 142 deletions.
  1. +3 −3 rpc/api/api.pb.go
  2. +17 −6 rpc/rpcserver.go
  3. +15 −14 service/round.go
  4. +55 −57 service/service.go
  5. +48 −62 service/service_test.go

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -25,23 +25,34 @@ func NewRPCServer(service *service.Service) *rpcServer {
}

func (r *rpcServer) SubmitCommitment(ctx context.Context, in *api.SubmitCommitmentRequest) (*api.SubmitCommitmentResponse, error) {
res, err := r.s.SubmitCommitment(in.Commitment)
res, err := r.s.Submit(in.Commitment)
if err != nil {
return nil, err
}

out := new(api.SubmitCommitmentResponse)
out.RoundId = int32(res.RoundId)
out.RoundId = int32(res.Id)
return out, nil
}

func (r *rpcServer) GetInfo(ctx context.Context, in *api.GetInfoRequest) (*api.GetInfoResponse, error) {
info := r.s.Info()

out := new(api.GetInfoResponse)
out.OpenRoundId = info.OpenRoundId
out.ExecutingRoundsIds = info.ExecutingRoundsIds
out.ExecutedRoundsIds = info.ExecutedRoundsIds
out.OpenRoundId = int32(info.OpenRoundId)

ids := make([]int32, len(info.ExecutingRoundsIds))
for i, id := range info.ExecutingRoundsIds {
ids[i] = int32(id)
}
out.ExecutingRoundsIds = ids

ids = make([]int32, len(info.ExecutedRoundsIds))
for i, id := range info.ExecutedRoundsIds {
ids[i] = int32(id)
}
out.ExecutedRoundsIds = ids

return out, nil
}

@@ -60,7 +71,7 @@ func (r *rpcServer) GetRoundInfo(ctx context.Context, in *api.GetRoundInfoReques
out.Opened = info.Opened.UnixNano() / int64(time.Millisecond)
out.ExecuteStart = info.ExecuteStart.UnixNano() / int64(time.Millisecond)
out.ExecuteEnd = info.ExecuteEnd.UnixNano() / int64(time.Millisecond)
out.NumOfcommitments = int32(info.NumOfCommitments)
out.NumOfcommitments = int32(info.NumOfCommits)
out.MerkleRoot = info.MerkleRoot
out.Nip = nip

@@ -12,15 +12,15 @@ import (

type round struct {
cfg *Config
id int
Id int
opened time.Time
executeStart time.Time
executeEnd time.Time

commitments [][]byte
merkleTree *merkle.Tree
merkleRoot []byte
nip *shared.Proof
commits [][]byte
merkleTree *merkle.Tree
merkleRoot []byte
nip *shared.Proof

closedChan chan struct{}
executedChan chan struct{}
@@ -29,24 +29,24 @@ type round struct {
func newRound(cfg *Config, id int) *round {
r := new(round)
r.cfg = cfg
r.id = id
r.Id = id
r.opened = time.Now()
r.closedChan = make(chan struct{})
r.executedChan = make(chan struct{})

return r
}

func (r *round) submitCommitment(c []byte) error {
func (r *round) submit(data []byte) error {
// TODO(moshababo): check for duplications?
r.commitments = append(r.commitments, c)
r.commits = append(r.commits, data)

return nil
}

func (r *round) close() error {
r.merkleTree = merkle.NewTree(merkle.GetSha256Parent)
for _, c := range r.commitments {
for _, c := range r.commits {
err := r.merkleTree.AddLeaf(c)
if err != nil {
return err
@@ -55,8 +55,9 @@ func (r *round) close() error {

root, err := r.merkleTree.Root()
if err != nil {
return nil
return err
}

r.merkleRoot = root

close(r.closedChan)
@@ -89,19 +90,19 @@ func (r *round) execute() error {
func (r *round) membershipProof(c []byte) ([][]byte, error) {
// TODO(moshababo): change this temp inefficient implementation
ci := -1
for i, commitment := range r.commitments {
if bytes.Equal(c, commitment) {
for i, commit := range r.commits {
if bytes.Equal(c, commit) {
ci = i
break
}
}

if ci == -1 {
return nil, errors.New("commitment not found")
return nil, errors.New("commit not found")
}

t := merkle.NewProvingTree(merkle.GetSha256Parent, []uint64{uint64(ci)})
for _, c := range r.commitments {
for _, c := range r.commits {
err := t.AddLeaf(c)
if err != nil {
return nil, err
@@ -11,7 +11,7 @@ type Config struct {
HashFunction string `long:"hashfunction" description:"PoET hash function"`
RoundsDuration time.Duration `long:"duration" description:"duration of the opening time for each round. If not specified, rounds duration will be determined by its previous round end of PoET execution"`
InitialRoundDuration time.Duration `long:"initialduration" description:"duration of the opening time for the initial round. if rounds duration isn't specified, this param is necessary"`
ExecuteEmpty bool `long:"empty" description:"whether to execution empty rounds, without any submitted commitments"`
ExecuteEmpty bool `long:"empty" description:"whether to execution empty rounds, without any submitted commits"`
}

type Service struct {
@@ -21,40 +21,38 @@ type Service struct {
rounds map[int]*round
executingRounds map[int]*round
executedRounds map[int]*round

errChan chan error
}

type InfoResponse struct {
OpenRoundId int32
ExecutingRoundsIds []int32
ExecutedRoundsIds []int32
OpenRoundId int
ExecutingRoundsIds []int
ExecutedRoundsIds []int
}

type RoundInfoResponse struct {
Opened time.Time
ExecuteStart time.Time
ExecuteEnd time.Time
NumOfCommitments int
MerkleRoot []byte
Nip *shared.Proof
}

type SubmitCommitmentResponse struct {
RoundId int
Opened time.Time
ExecuteStart time.Time
ExecuteEnd time.Time
NumOfCommits int
MerkleRoot []byte
Nip *shared.Proof
}

var (
ErrRoundNotFound = errors.New("round not found")
)

func NewService(cfg *Config) (*Service, error) {
time.Now()
s := new(Service)
s.cfg = cfg
s.rounds = make(map[int]*round)
s.executingRounds = make(map[int]*round)
s.executedRounds = make(map[int]*round)
s.errChan = make(chan error)

roundId := int(0)
roundId := 1
s.openRound = s.newRound(roundId)
log.Infof("round %v opened", roundId)

@@ -67,7 +65,7 @@ func NewService(cfg *Config) (*Service, error) {
case <-s.roundsTicker():
}

if len(s.openRound.commitments) == 0 && !s.cfg.ExecuteEmpty {
if len(s.openRound.commits) == 0 && !s.cfg.ExecuteEmpty {
continue
}

@@ -81,45 +79,52 @@ func NewService(cfg *Config) (*Service, error) {
go func() {
// TODO(moshababo): apply safe concurrency
r := s.prevRound
s.executingRounds[r.id] = r
s.executingRounds[r.Id] = r

err := r.close()
if err != nil {
s.errChan <- err
log.Error(err)
}
log.Infof("round %v closed, executing...", r.id)
log.Infof("round %v closed, executing...", r.Id)
err = r.execute()
if err != nil {
s.errChan <- err
log.Error(err)
}

delete(s.executingRounds, r.id)
s.executedRounds[r.id] = r
log.Infof("round %v executed, phi=%v", r.id, r.nip.Phi)
delete(s.executingRounds, r.Id)
s.executedRounds[r.Id] = r
log.Infof("round %v executed, phi=%v", r.Id, r.nip.Phi)
}()
}
}()

return s, nil
}

func (s *Service) Info() *InfoResponse {
res := new(InfoResponse)
res.OpenRoundId = int32(s.openRound.id)
func (s *Service) Submit(data []byte) (*round, error) {
r := s.openRound
err := r.submit(data)
if err != nil {
return nil, err
}

ids := make([]int32, 0, len(s.executingRounds))
for id := range s.executingRounds {
ids = append(ids, int32(id))
return r, nil
}

func (s *Service) MembershipProof(roundId int, c []byte) ([][]byte, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}
res.ExecutingRoundsIds = ids

ids = make([]int32, 0, len(s.executedRounds))
for id := range s.executedRounds {
ids = append(ids, int32(id))
proof, err := r.membershipProof(c)
if err != nil {
return nil, err
}
res.ExecutedRoundsIds = ids

return res
return proof, nil
}

func (s *Service) RoundInfo(roundId int) (*RoundInfoResponse, error) {
@@ -132,46 +137,39 @@ func (s *Service) RoundInfo(roundId int) (*RoundInfoResponse, error) {
res.Opened = r.opened
res.ExecuteStart = r.executeStart
res.ExecuteEnd = r.executeEnd
res.NumOfCommitments = len(r.commitments)
res.NumOfCommits = len(r.commits)
res.MerkleRoot = r.merkleRoot
res.Nip = r.nip

return res, nil
}

func (s *Service) round(roundId int) (*round, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}
func (s *Service) Info() *InfoResponse {
res := new(InfoResponse)
res.OpenRoundId = s.openRound.Id

return r, nil
}
ids := make([]int, 0, len(s.executingRounds))
for id := range s.executingRounds {
ids = append(ids, id)
}
res.ExecutingRoundsIds = ids

func (s *Service) SubmitCommitment(c []byte) (*SubmitCommitmentResponse, error) {
r := s.openRound
err := r.submitCommitment(c)
if err != nil {
return nil, err
ids = make([]int, 0, len(s.executedRounds))
for id := range s.executedRounds {
ids = append(ids, id)
}
res.ExecutedRoundsIds = ids

res := new(SubmitCommitmentResponse)
res.RoundId = r.id
return res, nil
return res
}

func (s *Service) MembershipProof(roundId int, c []byte) ([][]byte, error) {
func (s *Service) round(roundId int) (*round, error) {
r := s.rounds[roundId]
if r == nil {
return nil, ErrRoundNotFound
}

proof, err := r.membershipProof(c)
if err != nil {
return nil, err
}

return proof, nil
return r, nil
}

func (s *Service) newRound(id int) *round {
Oops, something went wrong.

0 comments on commit fc2c24f

Please sign in to comment.
You can’t perform that action at this time.