Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better logs #139

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/initiator/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var StartDKG = &cobra.Command{
// Load operators TODO: add more sources.
operatorIDs, err := cli_utils.StringSliceToUintArray(flags.OperatorIDs)
if err != nil {
logger.Fatal("😥 Failed to load participants: ", zap.Error(err))
logger.Fatal("😥 Failed to load operator IDs: ", zap.Error(err))
}
opMap, err := cli_utils.LoadOperators(logger, flags.OperatorsInfo, flags.OperatorsInfoPath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cli/initiator/reshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ var StartReshare = &cobra.Command{
}
oldOperatorIDs, err := cli_utils.StringSliceToUintArray(flags.OperatorIDs)
if err != nil {
logger.Fatal("😥 Failed to load participants: ", zap.Error(err))
logger.Fatal("😥 Failed to load old operator IDs: ", zap.Error(err))
}
newOperatorIDs, err := cli_utils.StringSliceToUintArray(flags.NewOperatorIDs)
if err != nil {
logger.Fatal("😥 Failed to load new participants: ", zap.Error(err))
logger.Fatal("😥 Failed to load new operator IDs: ", zap.Error(err))
}
// create a new ID for resharing
id := spec.NewID()
Expand Down
2 changes: 1 addition & 1 deletion cli/initiator/resigning.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ var StartResigning = &cobra.Command{
}
operatorIDs, err := cli_utils.StringSliceToUintArray(flags.OperatorIDs)
if err != nil {
logger.Fatal("😥 Failed to load participants: ", zap.Error(err))
logger.Fatal("😥 Failed to load operator IDs: ", zap.Error(err))
}
ethNetwork := e2m_core.NetworkFromString(flags.Network)
if ethNetwork == "" {
Expand Down
2 changes: 1 addition & 1 deletion cli/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func StringSliceToUintArray(flagdata []string) ([]uint64, error) {
for i := 0; i < len(flagdata); i++ {
opid, err := strconv.ParseUint(flagdata[i], 10, strconv.IntSize)
if err != nil {
return nil, fmt.Errorf("😥 cant load operator err: %v , data: %v, ", err, flagdata[i])
return nil, fmt.Errorf("err: %w , data: %v, ", err, flagdata[i])
}
partsarr = append(partsarr, opid)
}
Expand Down
3 changes: 1 addition & 2 deletions cli/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ var Verify = &cobra.Command{
flags.WithdrawAddress,
)
if err != nil {
log.Printf("Failed to validate ceremony directory: %v", err)
return err
return fmt.Errorf("failed to validate ceremony directory: %w",err)
}

log.Printf("Ceremony is valid.")
Expand Down
4 changes: 2 additions & 2 deletions pkgs/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (b *Board) PushDeals(bundle *dkg.DealBundle) {

byts, err := wire2.EncodeDealBundle(bundle)
if err != nil {
b.logger.Error(err.Error())
b.logger.Error("error encoding deal bundle", zap.Error(err))
return
}
msg := &wire2.KyberMessage{
Expand All @@ -49,7 +49,7 @@ func (b *Board) PushDeals(bundle *dkg.DealBundle) {
}

if err := b.broadcastF(msg); err != nil {
b.logger.Error(err.Error())
b.logger.Error("error broadcasting deal bundle", zap.Error(err))
return
}
}
Expand Down
24 changes: 12 additions & 12 deletions pkgs/crypto/deposit_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func BuildDepositDataCLI(network core.Network, depositData *phase0.DepositData,
copy(depositMsg.PublicKey[:], depositData.PublicKey[:])
depositMsgRoot, err := depositMsg.HashTreeRoot()
if err != nil {
return nil, fmt.Errorf("failed to compute deposit message root: %v", err)
return nil, fmt.Errorf("failed to compute deposit message root: %w", err)
}

depositDataRoot, err := depositData.HashTreeRoot()
if err != nil {
return nil, fmt.Errorf("failed to compute deposit data root: %v", err)
return nil, fmt.Errorf("failed to compute deposit data root: %w", err)
}

// Final checks of prepared deposit data
Expand Down Expand Up @@ -63,11 +63,11 @@ func validateDepositDataCLI(d *wire.DepositDataCLI, expectedWithdrawalCredential
// Re-encode and re-decode the deposit data json to ensure encoding is valid.
b, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("failed to marshal deposit data json: %v", err)
return fmt.Errorf("failed to marshal deposit data json: %w", err)
}
var depositData wire.DepositDataCLI
if err := json.Unmarshal(b, &depositData); err != nil {
return fmt.Errorf("failed to unmarshal deposit data json: %v", err)
return fmt.Errorf("failed to unmarshal deposit data json: %w", err)
}
if !reflect.DeepEqual(d, &depositData) {
return fmt.Errorf("failed to validate deposit data json")
Expand All @@ -76,11 +76,11 @@ func validateDepositDataCLI(d *wire.DepositDataCLI, expectedWithdrawalCredential

// 1. Validate format
if err := validateFieldFormatting(d); err != nil {
return fmt.Errorf("failed to validate deposit data json: %v", err)
return fmt.Errorf("failed to validate deposit data json: %w", err)
}
// 2. Verify deposit roots and signature
if err := verifyDepositRoots(d); err != nil {
return fmt.Errorf("failed to verify deposit roots: %v", err)
return fmt.Errorf("failed to verify deposit roots: %w", err)
}
// 3. Verify withdrawal address
if d.WithdrawalCredentials != hex.EncodeToString(expectedWithdrawalCredentials) {
Expand Down Expand Up @@ -143,26 +143,26 @@ func validateFieldFormatting(d *wire.DepositDataCLI) error {
func verifyDepositRoots(d *wire.DepositDataCLI) error {
pubKey, err := hex.DecodeString(d.PubKey)
if err != nil {
return fmt.Errorf("failed to decode public key: %v", err)
return fmt.Errorf("failed to decode public key: %w", err)
}
withdrCreds, err := hex.DecodeString(d.WithdrawalCredentials)
if err != nil {
return fmt.Errorf("failed to decode withdrawal credentials: %v", err)
return fmt.Errorf("failed to decode withdrawal credentials: %w", err)
}
sig, err := hex.DecodeString(d.Signature)
if err != nil {
return fmt.Errorf("failed to decode signature: %v", err)
return fmt.Errorf("failed to decode signature: %w", err)
}
fork, err := hex.DecodeString(d.ForkVersion)
if err != nil {
return fmt.Errorf("failed to decode fork version: %v", err)
return fmt.Errorf("failed to decode fork version: %w", err)
}
if len(fork) != 4 {
return fmt.Errorf("fork version has wrong length")
}
network, err := spec_crypto.GetNetworkByFork([4]byte(fork))
if err != nil {
return fmt.Errorf("failed to get network by fork: %v", err)
return fmt.Errorf("failed to get network by fork: %w", err)
}
depositData := &phase0.DepositData{
PublicKey: phase0.BLSPubKey(pubKey),
Expand All @@ -172,7 +172,7 @@ func verifyDepositRoots(d *wire.DepositDataCLI) error {
}
err = spec_crypto.VerifyDepositData(network, depositData)
if err != nil {
return fmt.Errorf("failed to verify deposit data: %v", err)
return fmt.Errorf("failed to verify deposit data: %w", err)
}
return nil
}
8 changes: 4 additions & 4 deletions pkgs/dkg/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (o *LocalOwner) Init(reqID [24]byte, init *spec.Init) (*wire.Transport, err
func (o *LocalOwner) processDKG(from uint64, msg *wire.Transport) error {
kyberMsg := &wire.KyberMessage{}
if err := kyberMsg.UnmarshalSSZ(msg.Data); err != nil {
return err
return fmt.Errorf("failed to ssz unmarshal message: probably an upgrade to latest version needed: %w", err)
}
o.Logger.Debug("operator: received kyber msg", zap.String("type", kyberMsg.Type.String()), zap.Uint64("from", from))
switch kyberMsg.Type {
Expand Down Expand Up @@ -371,7 +371,7 @@ func (o *LocalOwner) Process(st *wire.SignedTransport, incOperators []*spec.Oper
case wire.ExchangeMessageType:
exchMsg := &wire.Exchange{}
if err := exchMsg.UnmarshalSSZ(st.Message.Data); err != nil {
return err
return fmt.Errorf("failed to ssz unmarshal message: probably an upgrade to latest version needed: %w", err)
}
if _, ok := o.exchanges[from]; ok {
return fmt.Errorf("error at init exchange message processing: %w", ErrAlreadyExists)
Expand All @@ -388,7 +388,7 @@ func (o *LocalOwner) Process(st *wire.SignedTransport, incOperators []*spec.Oper
case wire.ReshareExchangeMessageType:
exchMsg := &wire.Exchange{}
if err := exchMsg.UnmarshalSSZ(st.Message.Data); err != nil {
return err
return fmt.Errorf("failed to ssz unmarshal message: probably an upgrade to latest version needed: %w", err)
}
if _, ok := o.exchanges[from]; ok {
return fmt.Errorf("error at reshare exchange message processing: %w, from %d", ErrAlreadyExists, from)
Expand Down Expand Up @@ -438,7 +438,7 @@ func (o *LocalOwner) Process(st *wire.SignedTransport, incOperators []*spec.Oper
case wire.ReshareKyberMessageType:
kyberMsg := &wire.ReshareKyberMessage{}
if err := kyberMsg.UnmarshalSSZ(st.Message.Data); err != nil {
return err
return fmt.Errorf("failed to ssz unmarshal message: probably an upgrade to latest version needed: %w", err)
}
b, err := wire.DecodeDealBundle(kyberMsg.Data, o.Suite.G1().(kyber_dkg.Suite))
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions pkgs/initiator/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"sort"
"strings"
"time"

"github.com/attestantio/go-eth2-client/spec/phase0"
Expand Down Expand Up @@ -148,7 +149,7 @@ func ValidatedOperatorData(ids []uint64, operators wire.OperatorsCLI) ([]*spec.O

pkBytes, err := spec_crypto.EncodeRSAPublicKey(op.PubKey)
if err != nil {
return nil, fmt.Errorf("can't encode public key err: %v", err)
return nil, fmt.Errorf("can't encode public key err: %w", err)
}
ops[i] = &spec.Operator{
ID: op.ID,
Expand Down Expand Up @@ -569,7 +570,7 @@ func (c *Initiator) processDKGResultResponse(dkgResults []*spec.Result,
}
depositDataJson, err := crypto.BuildDepositDataCLI(network, depositData, wire.DepositCliVersion)
if err != nil {
return nil, nil, fmt.Errorf("failed to create deposit data json: %v", err)
return nil, nil, fmt.Errorf("failed to create deposit data json: %w", err)
}
c.Logger.Info("✅ deposit data was successfully reconstructed")
keyshares, err := c.generateSSVKeysharesPayload(ops, dkgResults, masterSigOwnerNonce, ownerAddress, nonce)
Expand Down Expand Up @@ -774,11 +775,15 @@ func (c *Initiator) processPongMessage(res wire.PongResult) error {
}
signedPongMsg := &wire.SignedTransport{}
if err := signedPongMsg.UnmarshalSSZ(res.Result); err != nil {
errmsg, parseErr := wire.ParseAsError(res.Result)
if parseErr == nil {
return fmt.Errorf("operator returned err: %v", errmsg)
if strings.Contains(err.Error(), "incorrect offset") {
return fmt.Errorf("%w, operator probably of old version, please upgrade", err)
}
return err
// in case we received error message, try unmarshall
errString, err := wire.ParseAsError(res.Result)
if err == nil {
return fmt.Errorf("cant parse error message: %w", err)
}
return fmt.Errorf("operator returned error: %s", errString)
}
// Validate that incoming message is an pong message
if signedPongMsg.Message.Type != wire.PongMessageType {
Expand Down Expand Up @@ -896,6 +901,9 @@ func checkThreshold(responses map[uint64][]byte, errs map[uint64]error, oldOpera
var finalErr error
for _, op := range newOperators {
if err, ok := errs[op.ID]; ok {
if strings.Contains(err.Error(), "invalid ssz encoding") {
err = fmt.Errorf("%w, operator probably of old version 1.*.*, please upgrade", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not define 1, just old version is enough

}
finalErr = errors.Join(finalErr, fmt.Errorf("error: %w", err))
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/initiator/initiator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func TestValidateDKGParams(t *testing.T) {
t.Errorf("expected error message %q but got %q", tt.errMsg, err.Error())
}
case err != nil:
t.Errorf("unexpected error: %v", err)
t.Errorf("unexpected error: %s", err)
default:
// verify list is ok
need := len(tt.ids)
Expand Down
18 changes: 9 additions & 9 deletions pkgs/initiator/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func verifyMessageSignatures(id [24]byte, messages map[uint64][]byte, verify Ver
for _, msg := range messages {
tsp := &wire.SignedTransport{}
if err := tsp.UnmarshalSSZ(msg); err != nil {
errmsg, parseErr := wire.ParseAsError(msg)
if parseErr == nil {
errs = errors.Join(errs, fmt.Errorf("%v", errmsg))
continue
errString, err := wire.ParseAsError(msg)
if err != nil {
return fmt.Errorf("cant parse error message: %w", err)
}
return err
errs = errors.Join(errs, fmt.Errorf("%s", errString))
continue
}
signedBytes, err := tsp.Message.MarshalSSZ()
if err != nil {
Expand Down Expand Up @@ -70,11 +70,11 @@ func makeMultipleSignedTransports(privateKey *rsa.PrivateKey, id [24]byte, messa
for i, msg := range messages {
tsp := &wire.SignedTransport{}
if err := tsp.UnmarshalSSZ(msg); err != nil {
errmsg, parseErr := wire.ParseAsError(msg)
if parseErr == nil {
return nil, fmt.Errorf("operator %d returned: %v", i, errmsg)
errString, err := wire.ParseAsError(msg)
if err != nil {
return nil, fmt.Errorf("cant parse error message: %w", err)
}
return nil, err
return nil, fmt.Errorf("operator %d returned: %s", i, errString)
}
// Verify that incoming messages have valid DKG ceremony ID
if !bytes.Equal(id[:], tsp.Message.Identifier[:]) {
Expand Down
18 changes: 12 additions & 6 deletions pkgs/initiator/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (c *Initiator) SendAndCollect(op wire.OperatorCLI, method string, data []by
if err != nil {
return nil, err
}
c.Logger.Debug("operator responded", zap.Uint64("operator", op.ID), zap.String("method", method))
c.Logger.Debug("operator responded", zap.Uint64("operator", op.ID), zap.String("IP", op.Addr), zap.String("method", method), zap.Int("status", res.StatusCode))
if res.StatusCode < 200 || res.StatusCode >= 300 {
errmsg, parseErr := wire.ParseAsError(resdata)
if parseErr == nil {
return nil, fmt.Errorf("%v", errmsg)
errString, err := wire.ParseAsError(resdata)
if err != nil {
return nil, fmt.Errorf("cant parse error message: %w", err)
}
return nil, fmt.Errorf("operator %d failed with: %w", op.ID, errors.New(string(resdata)))
return nil, fmt.Errorf("operator %d failed with: probably of old version, please upgrade %w", op.ID, errors.New(errString))
}
return resdata, nil
}
Expand All @@ -53,7 +53,13 @@ func (c *Initiator) GetAndCollect(op wire.OperatorCLI, method string) ([]byte, e
if err != nil {
return nil, err
}
c.Logger.Debug("operator responded", zap.String("IP", op.Addr), zap.String("method", method), zap.Int("status", res.StatusCode))
if res.StatusCode < 200 || res.StatusCode >= 300 {
errmsg, parseErr := wire.ParseAsError(resdata)
if parseErr == nil {
return nil, fmt.Errorf("%v", errmsg)
}
return nil, fmt.Errorf("operator failed with: %w, probably of old version 1.*.*, please upgrade", errors.New(string(resdata)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}
return resdata, nil
}

Expand Down
Loading
Loading