Skip to content

Commit

Permalink
Refactor cosigner handling and add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 5, 2023
1 parent 8298bc8 commit d4ba04d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
15 changes: 6 additions & 9 deletions cmd/horcrux/cmd/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewThresholdValidator(

thresholdCfg := config.Config.ThresholdModeConfig
// NOTE: Shouldnt this be a list of concrete type instead of interface type?
remoteCosigners := make([]pcosigner.IRemoteCosigner, 0, len(thresholdCfg.Cosigners)-1)
remoteCosigners := make([]pcosigner.IRemoteCosigner, 0, len(thresholdCfg.Cosigners)-1) // list of remote cosigners
// peers := make([]pcosigner.ICosigner, 0, len(thresholdCfg.Cosigners)-1)

var p2pListen string
Expand All @@ -43,15 +43,12 @@ func NewThresholdValidator(

for _, c := range thresholdCfg.Cosigners {
if c.ShardID != security.GetID() {

// remoteCosigners = append(
// remoteCosigners,
// temp,
// )
remoteCosigners = append(remoteCosigners, pcosigner.NewRemoteCosigner(security.GetID(), c.P2PAddr))
logger.Info("Added remote cosigner", "id", c.ShardID, "address", c.P2PAddr)
} else {
// p2pListen = c.P2PAddr
p2pListen = c.P2PAddr
cosign = pcosigner.NewCosign(c.ShardID, c.P2PAddr)
logger.Info("Created a new cosigner", "id", c.ShardID, "address", c.P2PAddr)

}
}
Expand Down Expand Up @@ -92,8 +89,8 @@ func NewThresholdValidator(
thresholdCfg.Threshold,
grpcTimeout,
maxWaitForSameBlockAttempts,
localCosigner,
remoteCosigners, // remote Cosigners are the peers we are calling remotely
localCosigner, // our "server"
remoteCosigners, // remote Cosigners are the peers we are requesting remotely (client to server)
raftStore, // raftStore implements the ILeader interface
)

Expand Down
6 changes: 4 additions & 2 deletions pkg/pcosigner/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func NewThresholdSignerSoft(config *RuntimeConfig, id int, chainID string) (*cip
pubKey,
threshold,
total)

if err != nil {
return nil, fmt.Errorf("cipher.NewThresholdSignerSoft error: (%s)", err)
}
return &s, nil
}

Expand All @@ -55,7 +57,7 @@ func (cosign *Cosigner) GetAddress() string {

// GetID returns the ID of the remote cosigner
// Implements the cosigner interface
func (cosign *RemoteCosigner) GetID() int {
func (cosign *Cosigner) GetID() int {
return cosign.id
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/pcosigner/remote_cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package pcosigner

import (
"context"
"fmt"
"net/url"
"time"

Expand Down Expand Up @@ -65,31 +66,33 @@ func (cosigner *RemoteCosigner) VerifySignature(_ string, _, _ []byte) bool {
return false
}
*/
func (cosigner *RemoteCosigner) getGRPCClient() (proto.ICosignerGRPCClient, *grpc.ClientConn, error) {
// getGRPCClient returns a gRPC client and a connection.
func (rc *RemoteCosigner) getGRPCClient() (proto.ICosignerGRPCClient, *grpc.ClientConn, error) {
var grpcAddress string
url, err := url.Parse(cosigner.address)
url, err := url.Parse(rc.address)
if err != nil {
grpcAddress = cosigner.address
grpcAddress = rc.address
} else {
grpcAddress = url.Host
}
conn, err := grpc.Dial(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Printf("Error grpc.Dial: %v, with grpcAddress = %s\n", err, grpcAddress)
return nil, nil, err
}
return proto.NewICosignerGRPCClient(conn), conn, nil
}

// GetNonces implements the Cosigner interface
// It uses the gRPC client to request the nonces from the other
// Its the client side (Stub) of the gRPC
// It uses the gRPC client to request the nonces from the specific peer cosigner
// This is the client side (Stub) of the gRPC
// TODO: Change name to DealNonces or RequestNonces
func (cosigner *RemoteCosigner) GetNonces(
func (rc *RemoteCosigner) GetNonces(
chainID string,
req types.HRSTKey,
) (*CosignNoncesResponse, error) {

client, conn, err := cosigner.getGRPCClient()
client, conn, err := rc.getGRPCClient()
if err != nil {
return nil, err
}
Expand All @@ -106,7 +109,7 @@ func (cosigner *RemoteCosigner) GetNonces(
if err != nil {
return nil, err
}
// Returns one nonce from each cosigner
// Returns nonce from a cosigner
return &CosignNoncesResponse{
Nonces: CosignerNoncesFromProto(res.GetNonces()),
}, nil
Expand Down
2 changes: 1 addition & 1 deletion test/horcrux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
// Test2Of3SignerOneSentry will spin up a chain with one single-node validator and one horcrux validator
// the horcrux validator will have three pcosigners nodes with a threshold of two, and one sentry node
func Test2Of3SignerOneSentry(t *testing.T) {
testChainSingleNodeAndHorcruxThreshold(t, 2, 3, 2, 1, 1)
testChainSingleNodeAndHorcruxThreshold(t, 4, 3, 2, 1, 1)
}

// Test2Of3SignerTwoSentries will spin up a chain with one single-node validator and one horcrux validator
Expand Down

0 comments on commit d4ba04d

Please sign in to comment.