Skip to content

Commit

Permalink
linter recom fixes for scripts and ewm packages
Browse files Browse the repository at this point in the history
Signed-off-by: Pranay Valson <[email protected]>
  • Loading branch information
noslav committed Aug 30, 2024
1 parent 9ce8919 commit a26c5c4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 57 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ChainConfig struct {
WebsocketURLs string
}

// CovenetConfig contains the gRPC url and private key for EWM node
type CovenetConfig struct {
PrivateKey string
GRPCURL string
Expand Down
2 changes: 1 addition & 1 deletion internal/config/env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RedisEnvConfig struct {
Password string `envconfig:"REDIS_PWD" default:""`
}

// EWM node config
// CovenetEnvConfig (ewm) node config
type CovenetEnvConfig struct {
PrivateKey string `envconfig:"COVENET_PRIVATE_KEY"`
GRPCURL string `envconfig:"COVENET_GRPC_URL"`
Expand Down
64 changes: 36 additions & 28 deletions internal/ewm/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type CovenetInteractor struct {
func (interactor *CovenetInteractor) getPrivateKey() (cryptotypes.PrivKey, error) {
privKeyBytes, err := hex.DecodeString(interactor.config.CovenetConfig.PrivateKey)
if err != nil {
return nil, fmt.Errorf("error decoding private key: %v", err)
return nil, fmt.Errorf("error decoding private key: %w", err)
}

Check warning on line 63 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L59-L63

Added lines #L59 - L63 were not covered by tests

return &secp256k1.PrivKey{Key: privKeyBytes}, nil

Check warning on line 65 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L65

Added line #L65 was not covered by tests
}

Expand All @@ -84,13 +85,13 @@ func NewCovenetInteractor(config *config.AgentConfig) (*CovenetInteractor, error
// Process the key and set up the interactor account
err = interactor.ProcessKey()
if err != nil {
return nil, fmt.Errorf("failed to process key: %v", err)
return nil, fmt.Errorf("failed to process key: %w", err)
}

Check warning on line 89 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L75-L89

Added lines #L75 - L89 were not covered by tests

// Cache initial account info
sequenceNum, accountNum, err := interactor.GetAccountInfo()
if err != nil {
return nil, fmt.Errorf("failed to get initial account info: %v", err)
return nil, fmt.Errorf("failed to get initial account info: %w", err)
}
interactor.sequenceNumber = sequenceNum
interactor.accountNumber = accountNum
Expand All @@ -106,26 +107,29 @@ func (interactor *CovenetInteractor) GetSystemInfo() (*ewmtypes.SystemInfo, erro

res, err := covenetClient.SystemInfo(context.Background(), params)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get system info: %w", err)
}
log.Info("System Info: ", res)
log.Info("system info: ", res)

return &res.SystemInfo, nil

Check warning on line 114 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L103-L114

Added lines #L103 - L114 were not covered by tests
}

// GetGRPCConnection establishes a gRPC connection to the Covenet node.
func GetGRPCConnection(config *config.AgentConfig) (*grpc.ClientConn, error) {
// Create a connection to the gRPC server.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
_, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

grpcConn, err := grpc.DialContext(
ctx,
config.CovenetConfig.GRPCURL,
options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}

grpcConn, err := grpc.NewClient(
config.CovenetConfig.GRPCURL,
options...,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to init grpc client: %w", err)
}

Check warning on line 133 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L118-L133

Added lines #L118 - L133 were not covered by tests

log.Info("GRPC connection status: ", grpcConn.GetState())
Expand All @@ -147,7 +151,7 @@ func (interactor *CovenetInteractor) ProcessKey() error {
// Decode the hex string to bytes
privKeyBytes, err := hex.DecodeString(interactor.config.CovenetConfig.PrivateKey)
if err != nil {
return fmt.Errorf("error decoding private key: %v", err)
return fmt.Errorf("error decoding private key: %w", err)
}

Check warning on line 155 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L141-L155

Added lines #L141 - L155 were not covered by tests

// Create a new PrivKey object
Expand All @@ -162,13 +166,13 @@ func (interactor *CovenetInteractor) ProcessKey() error {
// Encode with the correct prefix
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixAccAddr, addrBytes)
if err != nil {
return fmt.Errorf("error encoding bech32 address: %v", err)
return fmt.Errorf("error encoding bech32 address: %w", err)
}

Check warning on line 170 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L158-L170

Added lines #L158 - L170 were not covered by tests

// If you specifically need a Covenet address type
covenetAddr, err := ewmtypes.CovenetAccAddressFromBech32(bech32Addr)
if err != nil {
return fmt.Errorf("error converting to Covenet address: %v", err)
return fmt.Errorf("error converting to Covenet address: %w", err)
}

Check warning on line 176 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L173-L176

Added lines #L173 - L176 were not covered by tests

// Set interactor key address values
Expand All @@ -182,6 +186,7 @@ func (interactor *CovenetInteractor) ProcessKey() error {
func (interactor *CovenetInteractor) GetLatestSequence() uint64 {
interactor.sequenceMutex.Lock()
defer interactor.sequenceMutex.Unlock()

return interactor.sequenceNumber

Check warning on line 190 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L186-L190

Added lines #L186 - L190 were not covered by tests
}

Expand All @@ -204,7 +209,7 @@ func (interactor *CovenetInteractor) GetAccountInfo() (uint64, uint64, error) {
)
if err != nil {
// If the account is not found return 0 values with error
return 0, 0, fmt.Errorf("failed to query account %s: %v", interactor.address.String(), err)
return 0, 0, fmt.Errorf("failed to query account %s: %w", interactor.address.String(), err)
}

Check warning on line 213 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L201-L213

Added lines #L201 - L213 were not covered by tests

// Create a new AccountI interface
Expand All @@ -213,7 +218,7 @@ func (interactor *CovenetInteractor) GetAccountInfo() (uint64, uint64, error) {
// Unmarshal the account data
err = encCfg.InterfaceRegistry.UnpackAny(res.Account, &account)
if err != nil {
return 0, 0, fmt.Errorf("failed to unpack account %s: %v", interactor.address.String(), err)
return 0, 0, fmt.Errorf("failed to unpack account %s: %w", interactor.address.String(), err)
}

Check warning on line 222 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L216-L222

Added lines #L216 - L222 were not covered by tests

// Get the sequence number
Expand Down Expand Up @@ -248,11 +253,10 @@ func (interactor *CovenetInteractor) SendCovenetBlockReplicaProofTx(ctx context.

// CreateProofTx creates and broadcasts a proof transaction on the Covenet blockchain.
func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockReplica *bsptypes.BlockReplica, txHash chan string, chainHeight uint64, replicaURL string, sha256Result [sha256.Size]byte) error {

// Get account from private key
privK, err := interactor.getPrivateKey()
if err != nil {
return fmt.Errorf("failed to get private key for address %s: %v", interactor.address.String(), err)
return fmt.Errorf("failed to get private key for address %s: %w", interactor.address.String(), err)
}

Check warning on line 260 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L255-L260

Added lines #L255 - L260 were not covered by tests

sequence := interactor.GetLatestSequence()
Expand All @@ -265,7 +269,7 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep

err = txBuilder.SetMsgs(proofMsg)
if err != nil {
return err
return fmt.Errorf("failed to set messages: %w", err)
}

Check warning on line 273 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L262-L273

Added lines #L262 - L273 were not covered by tests

txBuilder.SetGasLimit(200000)
Expand All @@ -287,7 +291,7 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep

err = txBuilder.SetSignatures(sigV2)
if err != nil {
return err
return fmt.Errorf("failed to set signatures: %w", err)
}

Check warning on line 295 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L275-L295

Added lines #L275 - L295 were not covered by tests

// Second round: actual signing
Expand All @@ -300,37 +304,40 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData,
txBuilder, privK, encCfg.TxConfig, sequence)
if err != nil {
return err
return fmt.Errorf("failed to sign with private key: %w", err)
}

Check warning on line 308 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L298-L308

Added lines #L298 - L308 were not covered by tests

err = txBuilder.SetSignatures(sigV2)
if err != nil {
return err
return fmt.Errorf("failed to set signatures: %w", err)
}

Check warning on line 313 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L310-L313

Added lines #L310 - L313 were not covered by tests

// Generated Protobuf-encoded bytes.
txBytes, err := encCfg.TxConfig.TxEncoder()(txBuilder.GetTx())

if err != nil {
return fmt.Errorf("failed to encode transaction: %w", err)
}

Check warning on line 319 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L316-L319

Added lines #L316 - L319 were not covered by tests
// Broadcast the tx via gRPC.
// We create a new client for the Protobuf Tx service.
txClient := tx.NewServiceClient(interactor.grpcClient)
// We then call the BroadcastTx method on this client.
grpcRes, err := txClient.BroadcastTx(
context.Background(),
ctx,
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes, // Proto-binary of the signed transaction, see previous step.
},
)
if err != nil {
return err
return fmt.Errorf("failed to broadcast transaction: %w", err)
}

Check warning on line 333 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L322-L333

Added lines #L322 - L333 were not covered by tests

log.Info("response code\n", grpcRes.TxResponse.String()) // Should be `0` if the tx is successful

if grpcRes.TxResponse.Code == 0 {
interactor.IncrementSequence()
txHash <- grpcRes.TxResponse.TxHash

return nil
}

Check warning on line 342 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L335-L342

Added lines #L335 - L342 were not covered by tests

Expand All @@ -346,7 +353,7 @@ func (interactor *CovenetInteractor) CreateProofTxWithRetry(ctx context.Context,
log.Warn("skipping: covenet creator is already a session member")
txHash <- "presubmitted hash"
case strings.Contains(errStr, "proof session submitted out of acceptable live bounds"):
log.Warn("skipping: covenet Proof session out of acceptable live bounds")
log.Warn("skipping: covenet proof session out of acceptable live bounds")
txHash <- "out-of-bounds block"

Check warning on line 357 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L348-L357

Added lines #L348 - L357 were not covered by tests
// Add additional cases that need skipping based on response from covenet
// case strings.Contains(errStr, "the client connection is closing"):
Expand All @@ -356,20 +363,21 @@ func (interactor *CovenetInteractor) CreateProofTxWithRetry(ctx context.Context,
log.Error("too many errors in creating proof on covenet: ", errStr)
txHash <- ""

Check warning on line 364 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L362-L364

Added lines #L362 - L364 were not covered by tests
}
return fmt.Errorf("exceeded retry limit of %d attempts, with response %s", retryCountLimit, lastError)

return fmt.Errorf("exceeded retry limit of attempts: %d, with response: %w", retryCountLimit, lastError)

Check warning on line 367 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L367

Added line #L367 was not covered by tests
}

err := interactor.CreateProofTx(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result)
if err != nil {
lastError = err
log.Error(err)
log.Errorf("retry count %d, error: %v", retryCount, err)
} else {
return nil
}

Check warning on line 376 in internal/ewm/tx.go

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L370-L376

Added lines #L370 - L376 were not covered by tests

// Exponential backoff
backoffDuration := time.Duration(1<<uint(retryCount)) * time.Second
log.Info("Retrying Create Proof tx in: ", backoffDuration)
log.Info("retrying create proof tx in: ", backoffDuration)
time.Sleep(backoffDuration)

// Recursive call with now incremented retry count
Expand Down
Loading

0 comments on commit a26c5c4

Please sign in to comment.