Skip to content

Commit

Permalink
Reorg files and update imports, and fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Jan 16, 2024
1 parent 9d5bf25 commit 493d6af
Show file tree
Hide file tree
Showing 32 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func createCosignerEd25519ShardsCmd() *cobra.Command {

csKeys, err := tss.CreatePersistentEd25519ThresholdSignShardsFromFile(keyFile, threshold, shards)
if err != nil {
return err
return fmt.Errorf("error creating CreatePersistentEd25519ThresholdSignShardsFromFile (%s): %d, %d, %w", keyFile, threshold, shards, err)
}

out, _ := cmd.Flags().GetString(flagOutputDir)
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/single_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"io"

"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/node"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
cometlog "github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/libs/service"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/node"
cconfig "github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/node"
)

func startCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/strangelove-ventures/horcrux/src/types"

"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/node"

cometjson "github.com/cometbft/cometbft/libs/json"
cometlog "github.com/cometbft/cometbft/libs/log"
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

cometlog "github.com/cometbft/cometbft/libs/log"
cometservice "github.com/cometbft/cometbft/libs/service"
"github.com/strangelove-ventures/horcrux/node"
cconfig "github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/cosigner"
"github.com/strangelove-ventures/horcrux/src/cosigner/nodesecurity"
"github.com/strangelove-ventures/horcrux/src/node"
)

const maxWaitForSameBlockAttempts = 3
Expand Down
6 changes: 4 additions & 2 deletions src/connector/sentry_signer_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"context"
"errors"
"net"
"time"

Expand Down Expand Up @@ -109,8 +110,9 @@ func signAndTrack(
) ([]byte, time.Time, error) {
signature, timestamp, err := validator.Sign(ctx, chainID, block)
if err != nil {
switch typedErr := err.(type) {
case *metrics.BeyondBlockError:
var typedErr *metrics.BeyondBlockError
switch {
case errors.As(err, &typedErr):
logger.Debug(
"Rejecting sign request",
"type", types.SignType(block.Step),
Expand Down
5 changes: 3 additions & 2 deletions src/cosigner/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ func (mpc *MPC) Stop(ctx context.Context) error {
}

func (mpc *MPC) Start(ctx context.Context) error {
//mpc.logger.Info("Starting ThresholdValidator services")

// mpc.logger.Info("Starting serverHealth services")
go mpc.serverHealth.Start(ctx)

Check failure on line 91 in src/cosigner/cosigner.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `mpc.serverHealth.Start` is not checked (errcheck)

// mpc.logger.Info("Starting serverHealth services")
go mpc.nonceCache.Start(ctx)

Check failure on line 94 in src/cosigner/cosigner.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `mpc.nonceCache.Start` is not checked (errcheck)

// mpc.logger.Info("Starting noncePruner services")
go mpc.noncePruner.Start(ctx)

Check failure on line 97 in src/cosigner/cosigner.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `mpc.noncePruner.Start` is not checked (errcheck)

// Should start the servers/clients?
Expand Down
2 changes: 1 addition & 1 deletion src/cosigner/ithresholdsigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type IThresholdSigner interface {
}

type IThresholdDealer interface {
KeyGenerator(threshold, total uint8) (types.Nonces, error)
GenerateNonces(threshold, total uint8) (types.Nonces, error)
}

type IThreshold interface {
Expand Down
8 changes: 4 additions & 4 deletions src/cosigner/local_cosigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi

sigs := make([]types.PartialSignature, threshold)

for i, local_cosigner := range thresholdCosigners {
for i, localCosigner := range thresholdCosigners {
cosignerNonces := make([]cosigner.Nonce, 0, threshold-1)

for j, nonce := range nonces {
Expand All @@ -210,13 +210,13 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi
}

for _, n := range nonce {
if n.DestinationID == local_cosigner.GetIndex() {
if n.DestinationID == localCosigner.GetIndex() {
cosignerNonces = append(cosignerNonces, n)
}
}
}

sigRes, err := local_cosigner.SetNoncesAndSign(ctx, cosigner.CosignerSetNoncesAndSignRequest{
sigRes, err := localCosigner.SetNoncesAndSign(ctx, cosigner.CosignerSetNoncesAndSignRequest{
Nonces: &cosigner.CosignerUUIDNonces{
UUID: u,
Nonces: cosignerNonces,
Expand All @@ -228,7 +228,7 @@ func testLocalCosignerSign(t *testing.T, threshold, total uint8, security []cosi
require.NoError(t, err)

sigs[i] = types.PartialSignature{
Index: local_cosigner.GetIndex(),
Index: localCosigner.GetIndex(),
Signature: sigRes.Signature,
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/cosigner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/metrics"
"github.com/strangelove-ventures/horcrux/src/tss"
"github.com/strangelove-ventures/horcrux/src/tss/ted25519"

"github.com/strangelove-ventures/horcrux/src/types"

Expand Down Expand Up @@ -59,7 +60,7 @@ func NewLocalCosigner(
security: security,
address: address,
nonces: make(map[uuid.UUID]*types.NoncesWithExpiration),
//dealer: tss.NewThresholdEd25519DealerSoft(config),
dealer: &ted25519.NonceGenerator{},
}
}

Expand Down Expand Up @@ -275,7 +276,8 @@ func (cosigner *LocalCosigner) sign(req SignatureRequest) (SignatureResponse, er
}, &cosigner.pendingDiskWG)

if err != nil {
if _, isSameHRSError := err.(*types.SameHRSError); !isSameHRSError {
var sameHRSError *types.SameHRSError
if !errors.As(err, &sameHRSError) {
return res, err
}
}
Expand All @@ -300,8 +302,9 @@ func (cosigner *LocalCosigner) generateNonces() ([]types.Nonces, error) {
// although it might doesnt matter if we arent doing DKG
// Should call an interface: dealnonce or something
threshold := uint8(cosigner.config.Config.ThresholdModeConfig.Threshold)
nonces, err := cosigner.dealer.KeyGenerator(uint8(threshold), uint8(total))
nonces, err := cosigner.dealer.GenerateNonces(uint8(threshold), uint8(total))
if err != nil {
fmt.Println("Error is: ", err)
return nil, err
}

Expand Down Expand Up @@ -421,6 +424,7 @@ func (cosigner *LocalCosigner) generateNoncesIfNecessary(uuid uuid.UUID) (*types

newNonces, err := cosigner.generateNonces()
if err != nil {
fmt.Println(err)
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion src/multiresolver/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/multiresolver"
"github.com/strangelove-ventures/horcrux/src/node"
"github.com/strangelove-ventures/horcrux/src/proto"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion node/leader_mock_test.go → src/node/leader_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package node_test
import (
"sync"

"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/cosigner"
"github.com/strangelove-ventures/horcrux/src/node"
"github.com/strangelove-ventures/horcrux/src/types"
)

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion node/raft_store_test.go → src/node/raft_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"
"time"

"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/cosigner"
"github.com/strangelove-ventures/horcrux/src/cosigner/nodesecurity"
"github.com/strangelove-ventures/horcrux/src/node"
tss "github.com/strangelove-ventures/horcrux/src/tss"

cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
Expand Down
3 changes: 2 additions & 1 deletion node/services.go → src/node/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ 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)
}
Expand Down
3 changes: 2 additions & 1 deletion node/services_test.go → src/node/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

cometlog "github.com/cometbft/cometbft/libs/log"
cometservice "github.com/cometbft/cometbft/libs/service"
"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/node"

fork "github.com/kraken-hpc/go-fork"
"github.com/stretchr/testify/require"
Expand All @@ -33,6 +33,7 @@ func mockHorcruxChildProcess(pidFilePath string) {
)
}

// waitForFileToExist waits for a file to exist, returning an error if it does not exist after timeout.
func waitForFileToExist(file string, timeout time.Duration) error {
exp := time.After(timeout)
tick := time.Tick(20 * time.Millisecond)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
"time"

"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/node"
"github.com/strangelove-ventures/horcrux/src/types"

"os"
Expand Down
File renamed without changes.
18 changes: 13 additions & 5 deletions node/threshold_validator.go → src/node/threshold_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ func (pv *ThresholdValidator) saveLastSignedStateInitiated(
if sameBlockErr == nil {
return existingSignature, existingTimestamp, nil
}
if _, ok := sameBlockErr.(*StillWaitingForBlockError); !ok {
var stillWaitingForBlockError *StillWaitingForBlockError
if !errors.As(sameBlockErr, &stillWaitingForBlockError) {
return nil, existingTimestamp, fmt.Errorf(
"same block error in loop, but we are not still waiting for signature: %w",
sameBlockErr,
Expand Down Expand Up @@ -413,7 +414,8 @@ func (pv *ThresholdValidator) compareBlockSignatureAgainstSSC(
stamp, signBytes := block.Timestamp, block.SignBytes

if err := compareBlockSignatureAgainstHRS(pv, chainID, block, existingSignature.HRSKey()); err != nil {
if _, ok := err.(*metrics.SameBlockError); !ok {
var sameBlockError *metrics.SameBlockError
if !errors.As(err, &sameBlockError) {
return nil, stamp, err
}
}
Expand Down Expand Up @@ -589,8 +591,11 @@ func (pv *ThresholdValidator) proxyIfNecessary(
Block: &block,
})
if err != nil {
if _, ok := err.(*cometrpcjsontypes.RPCError); ok {
rpcErrUnwrapped := err.(*cometrpcjsontypes.RPCError).Data
var RPCError *cometrpcjsontypes.RPCError
if errors.As(err, &RPCError) {
var RPCError *cometrpcjsontypes.RPCError
errors.As(err, &RPCError)
rpcErrUnwrapped := RPCError.Data
// Need to return BeyondBlockError after proxy since the error type will be lost over RPC
if len(rpcErrUnwrapped) > 33 && rpcErrUnwrapped[:33] == "Progress already started on block" {
return true, nil, stamp, &metrics.BeyondBlockError{Msg: rpcErrUnwrapped}
Expand Down Expand Up @@ -660,7 +665,9 @@ func (pv *ThresholdValidator) Sign(ctx context.Context, chainID string, block ty
peerStartTime := time.Now()

cosignersOrderedByFastest := pv.cosignerHealth.GetFastest()
fmt.Println("cosignersOrderedByFastest", len(cosignersOrderedByFastest))
cosignersForThisBlock := make([]ICosigner, pv.threshold)
fmt.Println("cosignersForThisBlock", len(cosignersForThisBlock), pv.threshold)
cosignersForThisBlock[0] = pv.MyCosigner
copy(cosignersForThisBlock[1:], cosignersOrderedByFastest[:pv.threshold-1])

Expand Down Expand Up @@ -832,7 +839,8 @@ func (pv *ThresholdValidator) Sign(ctx context.Context, chainID string, block ty
err = css.lastSignState.Save(newLss.SignStateConsensus, &pv.pendingDiskWG)
css.lastSignStateMutex.Unlock()
if err != nil {
if _, isSameHRSError := err.(*types.SameHRSError); !isSameHRSError {
var sameHRSError *types.SameHRSError
if !errors.As(err, &sameHRSError) {

pv.notifyBlockSignError(chainID, block.GetHRS(), signBytes)
return nil, stamp, fmt.Errorf("error saving last sign state: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"path/filepath"
"time"

"github.com/strangelove-ventures/horcrux/node"
"github.com/strangelove-ventures/horcrux/src/config"
"github.com/strangelove-ventures/horcrux/src/cosigner"
"github.com/strangelove-ventures/horcrux/src/cosigner/nodesecurity"
"github.com/strangelove-ventures/horcrux/src/node"
"github.com/strangelove-ventures/horcrux/src/tss"

"github.com/strangelove-ventures/horcrux/src/types"
Expand Down Expand Up @@ -139,7 +139,10 @@ func loadKeyForLocalCosigner(
func testThresholdValidator(t *testing.T, threshold, total uint8, configuration *config.RuntimeConfig) {
cosigners, pubKey := getTestLocalCosigners(t, threshold, total)

fmt.Println("cosigners", threshold, total, len(cosigners))

thresholdCosigners := make([]node.ICosigner, 0, threshold-1)
fmt.Println("thresholdCosigners", threshold, total, len(thresholdCosigners))

for i, cosigner := range cosigners {
require.Equal(t, i+1, cosigner.GetIndex())
Expand Down
7 changes: 4 additions & 3 deletions src/tss/threshold_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ func LoadVaultKeyFromFile(file string) (Ed25519Key, error) {
return pvKey, err
}

fmt.Printf("keyJsonBytes is: %s", keyJSONBytes)

err = json.Unmarshal(keyJSONBytes, &pvKey)
if err != nil {
fmt.Printf("Could not unmarshal key from file %s", file)
Expand Down Expand Up @@ -111,8 +109,11 @@ func generatePersistentThresholdSignShards(privateKey []byte, publicKey PubKey,
keys := function(privateKey, threshold, shards)
// Transform ed25519Keys to VaultKey type
vaultKeys := make([]VaultKey, len(keys))

fmt.Printf("Number of keys to create: %d\n", len(keys))
for id, key := range keys {
vaultKeys[id] = VaultKey{
fmt.Printf("VaultKey: %d\n", id)
vaultKeys[id-1] = VaultKey{
PubKey: publicKey,
PrivateShard: key,
ID: int(id),
Expand Down

0 comments on commit 493d6af

Please sign in to comment.