Skip to content

Commit

Permalink
chore: fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jtieri committed Oct 28, 2024
1 parent 167408b commit 3628b42
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 55 deletions.
4 changes: 4 additions & 0 deletions cmd/horcrux/cmd/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func createCosignerEd25519ShardsCmd() *cobra.Command {
}

// createCosignerECIESShardsCmd is a cobra command for creating cosigner-to-cosigner encryption secp256k1 keys.
//
//nolint:dupl
func createCosignerECIESShardsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-ecies-shards",
Expand Down Expand Up @@ -206,6 +208,8 @@ func createCosignerECIESShardsCmd() *cobra.Command {
}

// createCosignerRSAShardsCmd is a cobra command for creating cosigner-to-cosigner encryption RSA keys.
//
//nolint:dupl
func createCosignerRSAShardsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-rsa-shards",
Expand Down
4 changes: 2 additions & 2 deletions signer/cond/cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRace(t *testing.T) {
c.L.Lock()
x = 1
c.Wait()
require.Equal(t, 2, x)
require.Equal(t, 2, x) //nolint:testifylint // go-require
x = 3
c.Broadcast()
c.L.Unlock()
Expand All @@ -44,7 +44,7 @@ func TestRace(t *testing.T) {
for {
if x == 2 {
c.Wait()
require.Equal(t, 3, x)
require.Equal(t, 3, x) //nolint:testifylint // go-require
break
}
if x == 3 {
Expand Down
2 changes: 0 additions & 2 deletions signer/cosigner_key_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func makeRSAKeys(num int) (rsaKeys []*rsa.PrivateKey, pubKeys []*rsa.PublicKey,
var eg errgroup.Group
bitSize := 4096
for i := 0; i < num; i++ {
i := i
eg.Go(func() error {
rsaKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
Expand All @@ -137,7 +136,6 @@ func makeECIESKeys(num int) ([]*ecies.PrivateKey, []*ecies.PublicKey, error) {
pubKeys := make([]*ecies.PublicKey, num)
var eg errgroup.Group
for i := 0; i < num; i++ {
i := i
eg.Go(func() error {
eciesKey, err := ecies.GenerateKey(rand.Reader, secp256k1.S256(), nil)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions signer/cosigner_nonce_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ func (cnc *CosignerNonceCache) LoadN(ctx context.Context, n int) {
expiration := time.Now().Add(cnc.nonceExpiration)

for i, p := range cnc.cosigners {
i := i
p := p
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, cnc.getNoncesTimeout)
Expand Down
25 changes: 13 additions & 12 deletions signer/cosigner_nonce_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ func TestMovingAverage(t *testing.T) {

ma.add(3*time.Second, 500)
require.Len(t, ma.items, 1)
require.Equal(t, float64(500), ma.average())
require.InEpsilon(t, float64(500), ma.average(), 0)

ma.add(3*time.Second, 100)
require.Len(t, ma.items, 2)
require.Equal(t, float64(300), ma.average())
require.InEpsilon(t, float64(300), ma.average(), 0)

ma.add(6*time.Second, 600)
require.Len(t, ma.items, 3)
require.Equal(t, float64(450), ma.average())
require.InEpsilon(t, float64(450), ma.average(), 0)

// should kick out the first one
ma.add(3*time.Second, 500)
require.Len(t, ma.items, 3)
require.Equal(t, float64(450), ma.average())
require.InEpsilon(t, float64(450), ma.average(), 0)

// should kick out the second one
ma.add(6*time.Second, 500)
require.Len(t, ma.items, 3)
require.Equal(t, float64(540), ma.average())
require.InEpsilon(t, float64(540), ma.average(), 0)

for i := 0; i < 5; i++ {
ma.add(2500*time.Millisecond, 1000)
}

require.Len(t, ma.items, 5)
require.Equal(t, float64(1000), ma.average())
require.InEpsilon(t, float64(1000), ma.average(), 0)
}

func TestClearNonces(t *testing.T) {
Expand Down Expand Up @@ -173,20 +173,20 @@ func TestNonceCacheDemand(t *testing.T) {
_, err := nonceCache.GetNonces([]Cosigner{cosigners[0], cosigners[1]})
require.NoError(t, err)
time.Sleep(10 * time.Millisecond)
require.Greater(t, nonceCache.cache.Size(), 0)
require.Positive(t, nonceCache.cache.Size())
}

size := nonceCache.cache.Size()

require.Greater(t, size, 0)
require.Positive(t, size)

cancel()

require.LessOrEqual(t, size, nonceCache.target(nonceCache.movingAverage.average()))

count, pruned := mp.Result()

require.Greater(t, count, 0, "count of pruning calls must be greater than 0")
require.Positive(t, count, "count of pruning calls must be greater than 0")
require.Equal(t, 0, pruned, "no nonces should have been pruned")
}

Expand Down Expand Up @@ -218,6 +218,7 @@ func TestNonceCacheExpiration(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

const loadN = 100

// Load first set of 100 nonces
nonceCache.LoadN(ctx, loadN)

Expand All @@ -240,7 +241,7 @@ func TestNonceCacheExpiration(t *testing.T) {

// we should have pruned only the first set of nonces
// The second set of nonces should not have expired yet and we should not have load any more
require.Equal(t, pruned, loadN)
require.Equal(t, loadN, pruned)

cancel()

Expand Down Expand Up @@ -425,7 +426,7 @@ func TestNonceCacheDemandSlow(t *testing.T) {

for i := 0; i < 10; i++ {
time.Sleep(200 * time.Millisecond)
require.Greater(t, nonceCache.cache.Size(), 0)
require.Positive(t, nonceCache.cache.Size())
_, err := nonceCache.GetNonces([]Cosigner{cosigners[0], cosigners[1]})
require.NoError(t, err)
}
Expand Down Expand Up @@ -462,7 +463,7 @@ func TestNonceCacheDemandSlowDefault(t *testing.T) {

for i := 0; i < 10; i++ {
time.Sleep(7 * time.Second)
require.Greater(t, nonceCache.cache.Size(), 0)
require.Positive(t, nonceCache.cache.Size())
_, err := nonceCache.GetNonces([]Cosigner{cosigners[0], cosigners[1]})
require.NoError(t, err)
}
Expand Down
6 changes: 2 additions & 4 deletions signer/cosigner_security_ecies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func TestCosignerECIES(t *testing.T) {
}

func testCosignerSecurity(t *testing.T, securities []CosignerSecurity) error {
t.Helper()

var (
mockPub = []byte("mock_pub")
mockShare = []byte("mock_share")
Expand Down Expand Up @@ -104,16 +106,12 @@ func TestConcurrentIterateCosignerECIES(t *testing.T) {
for i := 0; i < 5000; i++ {
var eg errgroup.Group
for i, security := range securities {
security := security
i := i
eg.Go(func() error {
var nestedEg errgroup.Group
for j, security2 := range securities {
if i == j {
continue
}
security2 := security2
j := j
nestedEg.Go(func() error {
n, err := security.EncryptAndSign(j+1, []byte("mock_pub"), []byte("mock_share"))
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions signer/cosigner_security_rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,12 @@ func TestConcurrentIterateCosignerRSA(t *testing.T) {
for i := 0; i < 100; i++ {
var eg errgroup.Group
for i, security := range securities {
security := security
i := i
eg.Go(func() error {
var nestedEg errgroup.Group
for j, security2 := range securities {
if i == j {
continue
}
security2 := security2
j := j
nestedEg.Go(func() error {
n, err := security.EncryptAndSign(j+1, []byte("mock_pub"), []byte("mock_share"))
if err != nil {
Expand Down
9 changes: 0 additions & 9 deletions signer/local_cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ func (cosigner *LocalCosigner) GetNonces(
// so we perform these operations in parallel.

for j, u := range uuids {
j := j
u := u

outerEg.Go(func() error {
meta, err := cosigner.generateNoncesIfNecessary(u)
if err != nil {
Expand All @@ -387,8 +384,6 @@ func (cosigner *LocalCosigner) GetNonces(
continue
}

i := i

eg.Go(func() error {
secretPart, err := cosigner.getNonce(meta, peerID)

Expand Down Expand Up @@ -522,17 +517,13 @@ func (cosigner *LocalCosigner) SetNoncesAndSign(
// so we perform these operations in parallel.

for _, secretPart := range req.Nonces.Nonces {
secretPart := secretPart

eg.Go(func() error {
return cosigner.setNonce(req.Nonces.UUID, secretPart)
})
}

if req.VoteExtensionNonces != nil {
for _, secretPart := range req.VoteExtensionNonces.Nonces {
secretPart := secretPart

eg.Go(func() error {
return cosigner.setNonce(req.VoteExtensionNonces.UUID, secretPart)
})
Expand Down
4 changes: 4 additions & 0 deletions signer/local_cosigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestLocalCosignerSignRSA3of5(t *testing.T) {
}

func testLocalCosignerSignRSA(t *testing.T, threshold, total uint8) {
t.Helper()
t.Parallel()

security := make([]CosignerSecurity, total)
Expand Down Expand Up @@ -73,6 +74,7 @@ func TestLocalCosignerSignECIES3of5(t *testing.T) {
}

func testLocalCosignerSignECIES(t *testing.T, threshold, total uint8) {
t.Helper()
t.Parallel()

security := make([]CosignerSecurity, total)
Expand Down Expand Up @@ -101,6 +103,8 @@ func testLocalCosignerSignECIES(t *testing.T, threshold, total uint8) {
}

func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []CosignerSecurity) {
t.Helper()

privateKey := cometcryptoed25519.GenPrivKey()

privKeyBytes := [64]byte{}
Expand Down
10 changes: 6 additions & 4 deletions signer/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ func RequireNotRunning(log cometlog.Logger, pidFilePath string) error {
return nil
}

errno, ok := err.(syscall.Errno)
var errno syscall.Errno
ok := errors.As(err, &errno)
if !ok {
return fmt.Errorf("unexpected error type from signaling horcrux PID: %d", pid)
}
switch errno {
case syscall.ESRCH:

switch {
case errors.Is(errno, syscall.ESRCH):
return fmt.Errorf("search error while signaling horcrux PID: %d", pid)
case syscall.EPERM:
case errors.Is(errno, syscall.EPERM):
return fmt.Errorf("permission denied accessing horcrux PID: %d", pid)
}
return fmt.Errorf("unexpected error while signaling horcrux PID: %d", pid)
Expand Down
4 changes: 2 additions & 2 deletions signer/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

fork "github.com/kraken-hpc/go-fork"
"github.com/kraken-hpc/go-fork"
"github.com/stretchr/testify/require"

cometlog "github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestIsRunningNonExistentPid(t *testing.T) {
require.NoError(t, err, "error writing pid file")

err = signer.RequireNotRunning(cometlog.NewNopLogger(), pidFilePath)
require.Nil(t, err)
require.NoError(t, err)

_, err = os.Stat(pidFilePath)
require.ErrorIs(t, err, os.ErrNotExist)
Expand Down
8 changes: 8 additions & 0 deletions signer/sign_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func CanonicalVoteToStep(vote *cometproto.CanonicalVote) int8 {
return stepPrevote
case cometproto.PrecommitType:
return stepPrecommit
case cometproto.ProposalType:
panic("Invalid vote type")
case cometproto.UnknownType:
panic("Unknown vote type")
default:
panic("Unknown vote type")
}
Expand All @@ -57,6 +61,10 @@ func VoteToStep(vote *cometproto.Vote) int8 {
return stepPrevote
case cometproto.PrecommitType:
return stepPrecommit
case cometproto.ProposalType:
panic("Invalid vote type")
case cometproto.UnknownType:
panic("Unknown vote type")
default:
panic("Unknown vote type")
}
Expand Down
3 changes: 2 additions & 1 deletion signer/single_signer_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/tmhash"
cometjson "github.com/cometbft/cometbft/libs/json"
cometrand "github.com/cometbft/cometbft/libs/rand"
cometprivval "github.com/cometbft/cometbft/privval"
cometproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
)

func TestSingleSignerValidator(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions signer/threshold_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ func (pv *ThresholdValidator) Sign(
var eg errgroup.Group
var mu sync.Mutex
for _, c := range cosignersForThisBlock {
c := c
eg.Go(func() error {
nonces, err := c.GetNonces(ctx, []uuid.UUID{u})
if err != nil {
Expand Down Expand Up @@ -792,7 +791,6 @@ func (pv *ThresholdValidator) Sign(

var eg errgroup.Group
for _, cosigner := range cosignersForThisBlock {
cosigner := cosigner
eg.Go(func() error {
for cosigner != nil {
signCtx, cancel := context.WithTimeout(ctx, pv.grpcTimeout)
Expand Down
Loading

0 comments on commit 3628b42

Please sign in to comment.