Skip to content

Commit

Permalink
refactor unexported functions and consts
Browse files Browse the repository at this point in the history
Signed-off-by: Pranay Valson <[email protected]>
  • Loading branch information
noslav committed Sep 4, 2024
1 parent 17daf82 commit 56f9b4f
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions internal/ewm/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import (
var encCfg ewmparams.EncodingConfig

const (
proofTxTimeout uint64 = 480
retryCountLimit int = 3 // 3 retry for covenet proofchain submission
proofTxTimeout uint64 = 480
retryCountLimit int = 3 // 3 retry for covenet proofchain
bech32PrefixAccAddr string = "cxtmos" // Set the bech32 prefix for your chain
)

func init() {
Expand Down Expand Up @@ -139,14 +140,9 @@ func GetGRPCConnection(config *config.AgentConfig) (*grpc.ClientConn, error) {

// ProcessKey initializes the interactor's public key and address from the private key in the config.
func (interactor *CovenetInteractor) ProcessKey() error {
// Set the bech32 prefix for your chain
const (
Bech32PrefixAccAddr = "cxtmos"
)

// Configure the address
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccAddr+"pub")
config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccAddr+"pub")

// Decode the hex string to bytes
privKeyBytes, err := hex.DecodeString(interactor.config.CovenetConfig.PrivateKey)
Expand All @@ -164,7 +160,7 @@ func (interactor *CovenetInteractor) ProcessKey() error {
addrBytes := sdk.AccAddress(pubKey.Address())

// Encode with the correct prefix
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixAccAddr, addrBytes)
bech32Addr, err := bech32.ConvertAndEncode(bech32PrefixAccAddr, addrBytes)
if err != nil {
return fmt.Errorf("error encoding bech32 address: %w", err)
}

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L154-L166

Added lines #L154 - L166 were not covered by tests
Expand Down Expand Up @@ -245,23 +241,23 @@ func (interactor *CovenetInteractor) SendCovenetBlockReplicaProofTx(ctx context.
sha256Result := sha256.Sum256(jsonResult)

// Call Create Proof With Retry Count at 0 and Empty Err
err = interactor.CreateProofTxWithRetry(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result, 0, emptyErr)
err = interactor.createProofTxWithRetry(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result, 0, emptyErr)
if err != nil {
log.Error("covenet tx failed: ", err.Error())
}

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L228-L247

Added lines #L228 - L247 were not covered by tests
}

// 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 {
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: %w", interactor.address.String(), err)
}

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L251-L256

Added lines #L251 - L256 were not covered by tests

// Get the nonce of the account
sequence := interactor.GetLatestSequence()

// Create a new TxBuilder.
// Create a new TxBuilder
txBuilder := encCfg.TxConfig.NewTxBuilder()

// Create Msg from covenet types
Expand All @@ -280,6 +276,7 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep
// Assuming we have a single private key, account number, and sequence
log.Info("account details: ", interactor.address.String(), " number: ", interactor.accountNumber, " nonce: ", sequence)

// First round: gather all the signer infos using the "set empty signature".
sigV2 := signing.SignatureV2{
PubKey: interactor.pubKey,
Data: &signing.SingleSignatureData{
Expand All @@ -294,12 +291,13 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep
return fmt.Errorf("failed to set signatures: %w", err)
}

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L271-L292

Added lines #L271 - L292 were not covered by tests

// Second round: actual signing
// Second round: actual setting of the signature
signerData := xauthsigning.SignerData{
ChainID: "covenet",
AccountNumber: interactor.accountNumber,
Sequence: sequence,
}

sigV2, err = clientTx.SignWithPrivKey(
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData,
txBuilder, privK, encCfg.TxConfig, sequence)
Expand Down Expand Up @@ -345,7 +343,7 @@ func (interactor *CovenetInteractor) CreateProofTx(ctx context.Context, blockRep
}

// CreateProofTxWithRetry attempts to create and broadcast a proof transaction with retries on failure.
func (interactor *CovenetInteractor) CreateProofTxWithRetry(ctx context.Context, blockReplica *bsptypes.BlockReplica, txHash chan string, chainHeight uint64, replicaURL string, sha256Result [sha256.Size]byte, retryCount int, lastError error) error {
func (interactor *CovenetInteractor) createProofTxWithRetry(ctx context.Context, blockReplica *bsptypes.BlockReplica, txHash chan string, chainHeight uint64, replicaURL string, sha256Result [sha256.Size]byte, retryCount int, lastError error) error {
if retryCount >= retryCountLimit {
errStr := lastError.Error()
switch {
Expand All @@ -367,7 +365,7 @@ func (interactor *CovenetInteractor) CreateProofTxWithRetry(ctx context.Context,
return fmt.Errorf("exceeded retry limit of attempts: %d, with response: %w", retryCountLimit, lastError)

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L365

Added line #L365 was not covered by tests
}

err := interactor.CreateProofTx(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result)
err := interactor.createProofTx(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result)
if err != nil {
lastError = err
log.Errorf("retry count %d, error: %v", retryCount, err)
Expand All @@ -381,5 +379,5 @@ func (interactor *CovenetInteractor) CreateProofTxWithRetry(ctx context.Context,
time.Sleep(backoffDuration)

// Recursive call with now incremented retry count
return interactor.CreateProofTxWithRetry(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result, retryCount+1, lastError)
return interactor.createProofTxWithRetry(ctx, blockReplica, txHash, chainHeight, replicaURL, sha256Result, retryCount+1, lastError)

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

View check run for this annotation

Codecov / codecov/patch

internal/ewm/tx.go#L377-L382

Added lines #L377 - L382 were not covered by tests
}

0 comments on commit 56f9b4f

Please sign in to comment.