Skip to content

Commit

Permalink
Update tooling and fix new linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Mar 20, 2024
1 parent 34f2c2c commit 7a0c2b9
Show file tree
Hide file tree
Showing 18 changed files with 434 additions and 218 deletions.
510 changes: 359 additions & 151 deletions .golangci.yml

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ VERSION ?= $(shell git describe --tags)
# Flags appended to `go test` command in `make test`
TEST_FLAGS ?=

GOLANGCI_LINT_VERSION := v1.56.2
GOLANGCI_LINT_VERSION := v1.57.0
STATICCHECK_VERSION := v0.4.7
GOTESTSUM_VERSION := v1.11.0
GOSCALE_VERSION := v1.1.13

BUF_VERSION := 1.29.0
PROTOC_VERSION = 25.3
BUF_VERSION := 1.30.0
PROTOC_VERSION = 26.0

GRPC_JSON_PROXY_VERSION := v2.19.1
PROTOC_GO_VERSION := v1.33.0
PROTOC_GEN_GO_VERSION := v1.3.0
PROTOC_OPENAPI_VERSION := v2.19.1

# Everything below this line is meant to be static, i.e. only adjust the above variables. ###

Expand Down Expand Up @@ -51,13 +56,13 @@ GOLINES := $(GOBIN)/golines
FUZZTIME ?= "10s"

$(GOVULNCHECK):
@go install golang.org/x/vuln/cmd/govulncheck@latest
@go install golang.org/x/vuln/cmd/govulncheck@v1.0.4

$(GOLINES):
@go install github.com/segmentio/[email protected]

$(BIN_DIR)/mockgen:
go install go.uber.org/mock/mockgen@v0.4.0
@go install go.uber.org/mock/mockgen@$(MOCKGEN_VERSION)

install-buf:
@mkdir -p $(BIN_DIR)
Expand All @@ -78,10 +83,10 @@ endif

# Download protoc plugins
protoc-plugins:
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.18.0
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.18.0
@go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.31.0
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@$(GRPC_JSON_PROXY_VERSION)
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@$(PROTOC_OPENAPI_VERSION)
@go install google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GO_VERSION)
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@$(PROTOC_GEN_GO_VERSION)
.PHONY: protoc-plugins

all: build
Expand Down
16 changes: 8 additions & 8 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@ import (

func TestGenLabelHashFunc(t *testing.T) {
t.Parallel()
r := require.New(t)

aChallenge, bChallenge := []byte("a"), []byte("b")
data, other := []byte("data"), []byte("other")

// same challenge and data -> same hash
r.Equal(GenLabelHashFunc(aChallenge)(data), GenLabelHashFunc(aChallenge)(data))
aHash := GenLabelHashFunc(aChallenge)(data)
require.Equal(t, aHash, GenLabelHashFunc(aChallenge)(data))

// different challenge -> different hash
r.NotEqual(GenLabelHashFunc(aChallenge)(data), GenLabelHashFunc(bChallenge)(data))
require.NotEqual(t, aHash, GenLabelHashFunc(bChallenge)(data))

// different data -> different hash
r.NotEqual(GenLabelHashFunc(aChallenge)(data), GenLabelHashFunc(aChallenge)(other))
require.NotEqual(t, aHash, GenLabelHashFunc(aChallenge)(other))
}

func TestGenMerkleHashFunc(t *testing.T) {
t.Parallel()
r := require.New(t)

aChallenge, bChallenge := []byte("a"), []byte("b")
lChild, rChild := []byte("l"), []byte("r")

// same challenge and children -> same hash
r.Equal(GenMerkleHashFunc(aChallenge)(nil, lChild, rChild), GenMerkleHashFunc(aChallenge)(nil, lChild, rChild))
aHash := GenMerkleHashFunc(aChallenge)(nil, lChild, rChild)
require.Equal(t, aHash, GenMerkleHashFunc(aChallenge)(nil, lChild, rChild))

// different challenge -> different hash
r.NotEqual(GenMerkleHashFunc(aChallenge)(nil, lChild, rChild), GenMerkleHashFunc(bChallenge)(nil, lChild, rChild))
require.NotEqual(t, aHash, GenMerkleHashFunc(bChallenge)(nil, lChild, rChild))

// different children (e.g. different order) -> different hash
r.NotEqual(GenMerkleHashFunc(aChallenge)(nil, lChild, rChild), GenMerkleHashFunc(aChallenge)(nil, rChild, lChild))
require.NotEqual(t, aHash, GenMerkleHashFunc(aChallenge)(nil, rChild, lChild))
}

func TestGenLabelHashFuncHash(t *testing.T) {
Expand Down
11 changes: 5 additions & 6 deletions poetcore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/spacemeshos/poet/hash"
Expand Down Expand Up @@ -67,7 +66,7 @@ func TestNip(t *testing.T) {
time.Now().Add(1*time.Second),
securityParam,
)
assert.NoError(t, err)
require.NoError(t, err)
fmt.Printf("Dag root label: %x\n", merkleProof.Root)

err = verifier.Validate(
Expand All @@ -77,15 +76,15 @@ func TestNip(t *testing.T) {
numLeaves,
securityParam,
)
assert.NoError(t, err, "failed to verify proof")
require.NoError(t, err, "failed to verify proof")
}

func BenchmarkProofEx(t *testing.B) {
for j := 0; j < 10; j++ {
// generate random commitment
challenge := make([]byte, 32)
_, err := rand.Read(challenge)
assert.NoError(t, err)
require.NoError(t, err)

securityParam := shared.T

Expand All @@ -97,7 +96,7 @@ func BenchmarkProofEx(t *testing.B) {
time.Now().Add(time.Second),
securityParam,
)
assert.NoError(t, err)
require.NoError(t, err)
fmt.Printf("Dag root label: %x\n", merkleProof.Root)

err = verifier.Validate(
Expand All @@ -107,6 +106,6 @@ func BenchmarkProofEx(t *testing.B) {
numLeaves,
securityParam,
)
assert.NoError(t, err, "failed to verify proof")
require.NoError(t, err, "failed to verify proof")
}
}
6 changes: 3 additions & 3 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func makeRecoveryProofTree(

// Validate that layer 0 exists.
if _, ok := layersFiles[0]; !ok {
return nil, nil, fmt.Errorf("layer 0 cache file is missing")
return nil, nil, errors.New("layer 0 cache file is missing")

Check warning on line 167 in prover/prover.go

View check run for this annotation

Codecov / codecov/patch

prover/prover.go#L167

Added line #L167 was not covered by tests
}

var topLayer uint
Expand Down Expand Up @@ -245,8 +245,8 @@ func makeRecoveryProofTree(
}
parkedNodes = append(parkedNodes, memCachedParkedNodes...)

logging.FromContext(ctx).
Debug("recovered parked nodes", zap.Array("nodes", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error {
logging.FromContext(ctx).Debug("recovered parked nodes",
zap.Array("nodes", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error {
for _, node := range parkedNodes {
enc.AppendString(fmt.Sprintf("%X", node))
}
Expand Down
1 change: 1 addition & 0 deletions registration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func DefaultConfig() Config {
}
}

//nolint:lll
type Config struct {
// FIXME: remove deprecated PoW
PowDifficulty uint `long:"pow-difficulty" description:"(DEPRECATED) PoW difficulty (in the number of leading zero bits)"`
Expand Down
7 changes: 4 additions & 3 deletions registration/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ Package registration provides functionality for registering PoET challenges, sch
Each round starts by being open for receiving challenges (byte arrays) from miners.
Once finalized (according to the configured duration), a hash digest is created from the received challenges,
and is used as the input for the proof generation. The hash is the root of a merkle tree constructed from registered challenges
sorted lexicographically.
The proof generation is done by a worker, which can be a separate process that communicates with the registration service.
and is used as the input for the proof generation. The hash is the root of a merkle tree constructed from registered
challenges sorted lexicographically.
The proof generation is done by a worker, which can be a separate process that communicates with the registration
service.
Once completed, the proof is stored in a database and available for nodes via a GRPC query.
In addition to the PoET, the proof also contains the list of received challenges, so that the membership
Expand Down
2 changes: 1 addition & 1 deletion registration/pow_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PowVerifier interface {
// PoW hash is ripemd256(powChallenge || nodeID || poetChallenge || nonce)
Verify(poetChallenge, nodeID []byte, nonce uint64) error
Params() PowParams
SetParams(PowParams)
SetParams(params PowParams)
}

type PowParams struct {
Expand Down
9 changes: 6 additions & 3 deletions registration/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ func (r *Registration) closeRound(ctx context.Context) error {
if err != nil {
return fmt.Errorf("calculating membership root: %w", err)
}
logging.FromContext(ctx).
Info("closing round", zap.Uint("epoch", r.openRound.epoch), zap.Binary("root", root), zap.Int("members", r.openRound.members))
logging.FromContext(ctx).Info("closing round",
zap.Uint("epoch", r.openRound.epoch),
zap.Binary("root", root),
zap.Int("members", r.openRound.members),
)

if err := r.openRound.Close(); err != nil {
logging.FromContext(ctx).Error("failed to close the open round", zap.Error(err))
Expand Down Expand Up @@ -375,7 +378,7 @@ func (r *Registration) Submit(
}
}
case errors.Is(err, ErrChallengeAlreadySubmitted):
case err != nil:
default: // err != nil
return 0, time.Time{}, err
}

Expand Down
6 changes: 5 additions & 1 deletion registration/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ func (r *round) submit(ctx context.Context, key, challenge []byte) (<-chan error
if r.batch.Len() >= r.maxBatchSize {
r.flushPendingSubmitsLocked()
} else if r.batch.Len() == 1 {
logging.FromContext(ctx).Debug("scheduling flush of pending submits", zap.Uint("round", r.epoch), zap.Duration("interval", r.flushInterval))
logging.FromContext(ctx).Debug(
"scheduling flush of pending submits",
zap.Uint("round", r.epoch),
zap.Duration("interval", r.flushInterval),
)
time.AfterFunc(r.flushInterval, r.flushPendingSubmits)
}

Expand Down
2 changes: 1 addition & 1 deletion release/proto/go/rpc/api/v1/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions release/proto/go/rpc/api/v1/api.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions release/proto/openapiv2/rpc/api/v1/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@
}
}
},
"apiv1PowParams": {
"type": "object",
"properties": {
"difficulty": {
"type": "integer",
"format": "int64",
"title": "Difficulty of the PoW challenge (in terms of leading zero bits in the hash)"
},
"challenge": {
"type": "string",
"format": "byte",
"title": "The challenge to be used for the PoW"
}
}
},
"protobufAny": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -237,26 +252,11 @@
}
}
},
"v1PowParams": {
"type": "object",
"properties": {
"difficulty": {
"type": "integer",
"format": "int64",
"title": "Difficulty of the PoW challenge (in terms of leading zero bits in the hash)"
},
"challenge": {
"type": "string",
"format": "byte",
"title": "The challenge to be used for the PoW"
}
}
},
"v1PowParamsResponse": {
"type": "object",
"properties": {
"powParams": {
"$ref": "#/definitions/v1PowParams"
"$ref": "#/definitions/apiv1PowParams"
}
}
},
Expand All @@ -281,7 +281,7 @@
"title": "Proof of Work nonce\ndeprecated - use certificate instead"
},
"powParams": {
"$ref": "#/definitions/v1PowParams",
"$ref": "#/definitions/apiv1PowParams",
"title": "Proof of Work parameters that were used to generate the nonce\ndeprecated - use certificate instead"
},
"prefix": {
Expand Down
4 changes: 2 additions & 2 deletions rpc/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Submit_DoesNotPanicOnMissingPubKey(t *testing.T) {
// Assert
require.Nil(t, out)
require.Error(t, err)
require.Equal(t, status.Code(err), codes.InvalidArgument)
require.Equal(t, codes.InvalidArgument, status.Code(err))
require.ErrorContains(t, err, "invalid public key")
}

Expand All @@ -48,6 +48,6 @@ func Test_Submit_DoesNotPanicOnMissingSignature(t *testing.T) {
// Assert
require.Nil(t, out)
require.Error(t, err)
require.Equal(t, status.Code(err), codes.InvalidArgument)
require.Equal(t, codes.InvalidArgument, status.Code(err))
require.ErrorContains(t, err, "invalid signature")
}
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
defaultCycleGap = 10 * time.Second
)

//nolint:lll
type Config struct {
Genesis Genesis `long:"genesis-time" description:"Genesis timestamp in RFC3339 format"`
PoetDir string `long:"poetdir" description:"The base directory that contains poet's data, logs, configuration file, etc."`
Expand Down
4 changes: 3 additions & 1 deletion service/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package service

const (
defaultMemoryLayers = 26 // Up to (1 << 26) * 2 - 1 Merkle tree cache nodes (32 bytes each) will be held in-memory
// Up to (1 << 26) * 2 - 1 Merkle tree cache nodes (32 bytes each) will be held in-memory.
defaultMemoryLayers = 26
defaultTreeFileBufferSize = 4096
defaultEstimatedLeavesPerSecond = 78000
)

//nolint:lll
type Config struct {
// Merkle-Tree related configuration:
EstimatedLeavesPerSecond uint `long:"lps" description:"Estimated number of leaves generated per second"`
Expand Down
2 changes: 1 addition & 1 deletion shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func MakeLabelFunc() func(hash LabelHash, labelID uint64, leftSiblings [][]byte)
// Scale encoding is implemented by hand to be able to limit [][]byte slices to a maximum size (inner and outer slices).
type MerkleProof struct {
Root []byte `scale:"max=32"`
ProvenLeaves [][]byte `scale:"max=150"` // the max. size of this slice is T (security param), and each element is exactly 32 bytes
ProvenLeaves [][]byte `scale:"max=150"` // max. size is T (security param), and each element is exactly 32 bytes
ProofNodes [][]byte `scale:"max=5400"` // 36 nodes per leaf and each node is exactly 32 bytes
}

Expand Down
2 changes: 1 addition & 1 deletion shared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestFiatShamir(t *testing.T) {
deviationFromExpected := float64(int(occurrences[uint64(i)])-expectedIndicesPerBucket) /
float64(expectedIndicesPerBucket)
// fmt.Printf("%d %d %+0.3f%%\n", i, occurrences[uint64(i)], 100*deviationFromExpected)
assert.True(t, math.Abs(deviationFromExpected) < 0.005,
assert.Less(t, math.Abs(deviationFromExpected), 0.005,
"deviation from expected cannot exceed 0.5%% (for bucket %d it was %+0.3f%%)", i,
100*deviationFromExpected)
}
Expand Down

0 comments on commit 7a0c2b9

Please sign in to comment.