Skip to content
Permalink
Browse files

Basic service impl

  • Loading branch information...
moshababo committed Mar 9, 2019
1 parent 2e28e8f commit ab3c51f712fc3ed223d5650dfaeb370528e5c132
Showing with 445 additions and 51 deletions.
  1. +23 −24 config.go
  2. +3 −4 go.mod
  3. +9 −0 go.sum
  4. +32 −0 internal/log.go
  5. +9 −7 internal/prover.go
  6. +1 −2 internal/store.go
  7. +13 −5 log.go
  8. +8 −3 rpc/rpcserver.go
  9. +12 −6 server.go
  10. +32 −0 service/log.go
  11. +107 −0 service/round.go
  12. +140 −0 service/service.go
  13. +56 −0 service/service_test.go
@@ -8,26 +8,29 @@ import (
"fmt"
"github.com/btcsuite/btcutil"
"github.com/jessevdk/go-flags"
"github.com/spacemeshos/poet-ref/service"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"time"
)

const (
defaultConfigFilename = "poet.conf"
defaultDataDirname = "data"
defaultLogLevel = "debug"
defaultLogDirname = "logs"
defaultLogFilename = "poet.log"
defaultMaxLogFiles = 3
defaultMaxLogFileSize = 10
defaultRPCPort = 50002
defaultRESTPort = 8080
defaultCoreServiceMode = false
defaultN = 3
defaultHashFunction = "sha256"
defaultConfigFilename = "poet.conf"
defaultDataDirname = "data"
defaultLogLevel = "debug"
defaultLogDirname = "logs"
defaultLogFilename = "poet.log"
defaultMaxLogFiles = 3
defaultMaxLogFileSize = 10
defaultRPCPort = 50002
defaultRESTPort = 8080
defaultN = 15
defaultHashFunction = "sha256"
defaultInitialRoundDuration = 2 * time.Second
defaultExecuteEmpty = true
)

var (
@@ -37,14 +40,9 @@ var (
defaultLogDir = filepath.Join(defaultPoetDir, defaultLogDirname)
)

type serviceConfig struct {
N int `long:"n" description:"time parameter"`
HashFunction string `long:"hashfunction" description:"hash function"`
}

type coreServiceConfig struct {
N int `long:"n" description:"time parameter"`
HashFunction string `long:"hashfunction" description:"hash function"`
N int `long:"n" description:"PoET time parameter"`
HashFunction string `long:"hashfunction" description:"PoET hash function"`
}

// config defines the configuration options for poet.
@@ -70,7 +68,7 @@ type config struct {
CoreServiceMode bool `long:"core" description:"Enable poet in core service mode"`

CoreService *coreServiceConfig `group:"Core Service" namespace:"core"`
Service *serviceConfig `group:"Service"`
Service *service.Config `group:"Service"`
}

// loadConfig initializes and parses the config using a config file and command
@@ -92,10 +90,11 @@ func loadConfig() (*config, error) {
MaxLogFileSize: defaultMaxLogFileSize,
RawRPCListener: fmt.Sprintf("localhost:%d", defaultRPCPort),
RawRESTListener: fmt.Sprintf("localhost:%d", defaultRESTPort),
CoreServiceMode: defaultCoreServiceMode,
Service: &serviceConfig{
N: defaultN,
HashFunction: defaultHashFunction,
Service: &service.Config{
N: defaultN,
HashFunction: defaultHashFunction,
InitialRoundDuration: defaultInitialRoundDuration,
ExecuteEmpty: defaultExecuteEmpty,
},
CoreService: &coreServiceConfig{
N: defaultN,
7 go.mod
@@ -4,15 +4,14 @@ require (
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32 // indirect
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803
github.com/davecgh/go-spew v0.0.0-20180830191138-d8f796af33cc // indirect
github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff
github.com/golang/protobuf v1.2.0
github.com/grpc-ecosystem/grpc-gateway v0.0.0-20170724004829-f2862b476edc
github.com/jessevdk/go-flags v1.4.0
github.com/jrick/logrotate v1.0.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spacemeshos/sha256-simd v0.0.0-20190106153923-a8fce312d27f
github.com/stretchr/testify v0.0.0-20181014000028-04af85275a5c
github.com/spacemeshos/merkle-tree v0.0.0-20190303120439-11079fe33c42
github.com/spacemeshos/sha256-simd v0.0.0-20190111104731-8575aafc88c9
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 // indirect
9 go.sum
@@ -15,6 +15,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v0.0.0-20180830191138-d8f796af33cc h1:yjqBI4w+3eJOAftNV/wJMd1dlOBQwHZWQYrnJpYpoBg=
github.com/davecgh/go-spew v0.0.0-20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -39,10 +41,17 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spacemeshos/merkle-tree v0.0.0-20190303120439-11079fe33c42 h1:rCf5SLMolpeg1yCaCK6v5fM2RSterniXkt7S5UkzpMY=
github.com/spacemeshos/merkle-tree v0.0.0-20190303120439-11079fe33c42/go.mod h1:mPxjt4RONPxSUhxOq4bhSJyKVGQJ0VMSyRiE51dDLgE=
github.com/spacemeshos/sha256-simd v0.0.0-20190106153923-a8fce312d27f h1:eFILkWSPoM2aUkP66gud+JAL2Mu0J9CZdQvZLs4NCps=
github.com/spacemeshos/sha256-simd v0.0.0-20190106153923-a8fce312d27f/go.mod h1:Pz5LyRghtiEuSixOVGO0da5AxKBZa6+Yq8ZJfjTAPI0=
github.com/spacemeshos/sha256-simd v0.0.0-20190111104731-8575aafc88c9 h1:Cc+np6ORem5wrvO+YHQ1sdu71ItiQVRiYB4ugazpgxM=
github.com/spacemeshos/sha256-simd v0.0.0-20190111104731-8575aafc88c9/go.mod h1:Pz5LyRghtiEuSixOVGO0da5AxKBZa6+Yq8ZJfjTAPI0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v0.0.0-20181014000028-04af85275a5c h1:SN73kZVhfLxiGMU59RXp4eXCPRwMo9y8nnAqSnW97k0=
github.com/stretchr/testify v0.0.0-20181014000028-04af85275a5c/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -0,0 +1,32 @@
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Copyright (c) 2017-2019 The Spacemesh developers

package internal

import (
"github.com/btcsuite/btclog"
)

// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var log btclog.Logger

// The default amount of logging is none.
func init() {
DisableLog()
}

// DisableLog disables all library log output. Logging output is disabled
// by default until UseLogger is called.
func DisableLog() {
UseLogger(btclog.Disabled)
}

// UseLogger uses a specified Logger to output package logging info.
// This should be used in preference to SetLogWriter if the caller is also
// using btclog.
func UseLogger(logger btclog.Logger) {
log = logger
}
@@ -58,7 +58,7 @@ func NewProver(x []byte, n uint, h HashFunc) (shared.IProver, error) {
}

fileName := fmt.Sprintf("./poet-%d.bin", binary.BigEndian.Uint64(d))
fmt.Printf("Dag store: %s\n", path.Join(dir, fileName))
log.Tracef("Dag store: %s", path.Join(dir, fileName))

store, err := NewKvFileStore(fileName, n)
if err != nil {
@@ -181,11 +181,13 @@ func (p *SMProver) ComputeDag() (phi shared.Label, err error) {
N := math.Pow(2, float64(p.n+1)) - 1
L := math.Pow(2, float64(p.n))

fmt.Printf("DAG(%d):\n", p.n)
fmt.Printf("> Nodes: %d\n", uint64(N))
fmt.Printf("> Leaves: %d\n", uint64(L))
fmt.Printf("> Commitment: %x\n", p.x)
fmt.Printf("> Commitment hash: %x\n", p.h.Hash(p.x))
var msg string
msg += fmt.Sprintf("DAG(%d):\n", p.n)
msg += fmt.Sprintf("> Nodes: %d\n", uint64(N))
msg += fmt.Sprintf("> Leaves: %d\n", uint64(L))
msg += fmt.Sprintf("> Commitment: %x\n", p.x)
msg += fmt.Sprintf("> Commitment hash: %x", p.h.Hash(p.x))
log.Trace(msg)

p.t = time.Now()

@@ -301,7 +303,7 @@ func (p *SMProver) computeLeafLabel(leafId Identifier) (shared.Label, error) {
i := bs.GetValue()
N := math.Pow(2, float64(p.n))
r := math.Min(100.0, 100*float64(i)/N)
fmt.Printf("Leaf %s %d %.2v%% %0.2f leaves/sec \n", leafId, i, r, freq)
log.Tracef("Leaf %s %d %.2v%% %0.2f leaves/sec", leafId, i, r, freq)
p.t = time.Now()

// PrintMemUsage()
@@ -2,7 +2,6 @@ package internal

import (
"errors"
"fmt"
"github.com/spacemeshos/poet-ref/shared"
"math"
"os"
@@ -100,7 +99,7 @@ func (d *KVFileStore) Finalize() error {
// flush buffered ata to the underling writer
err := d.bw.Flush()

fmt.Printf("Finalizing store. Wrote %d leaves. Total bytes: %d\n", d.c, d.sz)
log.Tracef("Finalizing store. Wrote %d leaves. Total bytes: %d", d.c, d.sz)

return err
}
18 log.go
@@ -8,8 +8,10 @@ import (
"fmt"
"github.com/btcsuite/btclog"
"github.com/jrick/logrotate/rotator"
"github.com/spacemeshos/poet-ref/internal"
"github.com/spacemeshos/poet-ref/rpc"
"github.com/spacemeshos/poet-ref/rpccore"
"github.com/spacemeshos/poet-ref/service"
"github.com/spacemeshos/poet-ref/signal"
"os"
"path/filepath"
@@ -43,21 +45,27 @@ var (
// application shutdown.
logRotator *rotator.Rotator

poetLog = backendLog.Logger("POET")
rpcsLog = backendLog.Logger("RPCS")
poetLog = backendLog.Logger("POET") // global
rpcServerLog = backendLog.Logger("RPCS")
serviceLog = backendLog.Logger("SRVC")
proverLog = backendLog.Logger("PROV")
)

// Initialize package-global logger variables.
func init() {
rpc.UseLogger(rpcsLog)
rpccore.UseLogger(rpcsLog)
signal.UseLogger(poetLog)
rpc.UseLogger(rpcServerLog)
rpccore.UseLogger(rpcServerLog)
service.UseLogger(serviceLog)
internal.UseLogger(proverLog)
}

// subsystemLoggers maps each subsystem identifier to its associated logger.
var subsystemLoggers = map[string]btclog.Logger{
"POET": poetLog,
"RPCS": rpcsLog,
"RPCS": rpcServerLog,
"SRVC": serviceLog,
"PROV": proverLog,
}

// initLogRotator initializes the logging rotater to write logs to logFile and
@@ -2,19 +2,24 @@ package rpc

import (
"github.com/spacemeshos/poet-ref/rpc/api"
"github.com/spacemeshos/poet-ref/service"
"golang.org/x/net/context"
)

// rpcServer is a gRPC, RPC front end to poet
type rpcServer struct{}
type rpcServer struct {
service *service.Service
}

// A compile time check to ensure that rpcService fully implements
// the PoetServer gRPC rpc.
var _ api.PoetServer = (*rpcServer)(nil)

// NewRPCServer creates and returns a new instance of the rpcServer.
func NewRPCServer() *rpcServer {
return &rpcServer{}
func NewRPCServer(service *service.Service) *rpcServer {
return &rpcServer{
service: service,
}
}

func (r *rpcServer) GetServiceInfo(ctx context.Context, in *api.GetServiceInfoRequest) (*api.GetServiceInfoResponse, error) {
@@ -8,6 +8,7 @@ import (
"github.com/spacemeshos/poet-ref/rpc/api"
"github.com/spacemeshos/poet-ref/rpccore"
apicore "github.com/spacemeshos/poet-ref/rpccore/api"
"github.com/spacemeshos/poet-ref/service"
"github.com/spacemeshos/poet-ref/shared"
"github.com/spacemeshos/poet-ref/signal"
"golang.org/x/net/context"
@@ -40,7 +41,12 @@ func startServer() error {
proxyRegstr = append(proxyRegstr, apicore.RegisterPoetCoreProverHandlerFromEndpoint)
proxyRegstr = append(proxyRegstr, apicore.RegisterPoetVerifierHandlerFromEndpoint)
} else {
rpcServer := rpc.NewRPCServer()
s, err := service.NewService(cfg.Service)
if err != nil {
return nil
}

rpcServer := rpc.NewRPCServer(s)
grpcServer = grpc.NewServer(options...)

api.RegisterPoetServer(grpcServer, rpcServer)
@@ -55,7 +61,7 @@ func startServer() error {
defer lis.Close()

go func() {
rpcsLog.Infof("RPC server listening on %s", lis.Addr())
rpcServerLog.Infof("RPC server listening on %s", lis.Addr())
grpcServer.Serve(lis)
}()

@@ -69,9 +75,9 @@ func startServer() error {
}

go func() {
rpcsLog.Infof("REST proxy start listening on %s", cfg.RESTListener.String())
rpcServerLog.Infof("REST proxy start listening on %s", cfg.RESTListener.String())
err := http.ListenAndServe(cfg.RESTListener.String(), mux)
rpcsLog.Errorf("REST proxy failed listening: %s\n", err)
rpcServerLog.Errorf("REST proxy failed listening: %s\n", err)
}()

// Wait for shutdown signal from either a graceful server stop or from
@@ -93,12 +99,12 @@ func loggerInterceptor() func(ctx context.Context, req interface{}, info *grpc.U
} else {
reqDispStr = reqStr
}
rpcsLog.Tracef("%v: %v %v\n", peer.Addr.String(), info.FullMethod, reqDispStr)
rpcServerLog.Tracef("%v: %v %v\n", peer.Addr.String(), info.FullMethod, reqDispStr)

resp, err := handler(ctx, req)

if err != nil {
rpcsLog.Tracef("%v: FAILURE %v %s", peer.Addr.String(), info.FullMethod, err)
rpcServerLog.Tracef("%v: FAILURE %v %s", peer.Addr.String(), info.FullMethod, err)
}
return resp, err
}
@@ -0,0 +1,32 @@
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Copyright (c) 2017-2019 The Spacemesh developers

package service

import (
"github.com/btcsuite/btclog"
)

// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var log btclog.Logger

// The default amount of logging is none.
func init() {
DisableLog()
}

// DisableLog disables all library log output. Logging output is disabled
// by default until UseLogger is called.
func DisableLog() {
UseLogger(btclog.Disabled)
}

// UseLogger uses a specified Logger to output package logging info.
// This should be used in preference to SetLogWriter if the caller is also
// using btclog.
func UseLogger(logger btclog.Logger) {
log = logger
}
Oops, something went wrong.

0 comments on commit ab3c51f

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