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

Define separate error types for activation service errors #986

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 14 additions & 6 deletions clients/tfchain-client-go/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type AccountInfo struct {
Data Balance `json:"data"`
}

type ActivationServiceError struct {
Err error
}

func (e ActivationServiceError) Error() string {
return fmt.Sprintf("Activation service error: %s", e.Err.Error())
}
sameh-farouk marked this conversation as resolved.
Show resolved Hide resolved

// PublicKey gets public key from account id
func (a AccountID) PublicKey() []byte {
return a[:]
Expand Down Expand Up @@ -135,7 +143,7 @@ func (s *Substrate) activateAccount(identity Identity, activationURL string) err

response, err := http.Post(activationURL, "application/json", &buf)
if err != nil {
return errors.Wrap(err, "failed to call activation service")
return ActivationServiceError{Err: errors.Wrap(err, "failed to call activation service")}
}

defer response.Body.Close()
Expand All @@ -145,22 +153,22 @@ func (s *Substrate) activateAccount(identity Identity, activationURL string) err
return nil
}

return fmt.Errorf("failed to activate account: %s", response.Status)
return ActivationServiceError{Err: fmt.Errorf("failed to activate account: %s", response.Status)}
}

// EnsureAccount makes sure account is available on blockchain
// if not, it uses activation service to create one.
// EnsureAccount is safe to call on already activated accounts and it's mostly
// a NO-OP operation unless the account funds are very low, it will then make
// sure to reactivate the account (fund it) if the free tokes are <= ReactivateThreefold uTFT
func (s *Substrate) EnsureAccount(identity Identity, activationURL, termsAndConditionsLink, terminsAndConditionsHash string) (info AccountInfo, err error) {
// sure to reactivate the account (fund it) if the free tokens are <= ReactivateThreefold uTFT
func (s *Substrate) EnsureAccount(identity Identity, activationURL, termsAndConditionsLink, termsAndConditionsHash string) (info AccountInfo, err error) {
log.Debug().Str("account", identity.Address()).Msg("ensuring account")
cl, meta, err := s.GetClient()
if err != nil {
return info, err
}
info, err = s.getAccount(cl, meta, identity)
// if account does not exist OR account has tokes less that reactivateAt
// if account does not exist OR account has tokens less than reactivateAt
// then we activate the account.
if errors.Is(err, ErrAccountNotFound) || info.Data.Free.Cmp(reactivateAt) <= 0 {
// account activation
Expand Down Expand Up @@ -203,7 +211,7 @@ func (s *Substrate) EnsureAccount(identity Identity, activationURL, termsAndCond
return info, nil
}

return info, s.AcceptTermsAndConditions(identity, termsAndConditionsLink, terminsAndConditionsHash)
return info, s.AcceptTermsAndConditions(identity, termsAndConditionsLink, termsAndConditionsHash)
}

// Identity is a user identity
Expand Down
15 changes: 15 additions & 0 deletions clients/tfchain-client-go/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ func TestGetBalance(t *testing.T) {
_, err = cl.GetBalance(account)
require.NoError(err)
}

func TestEnsureAccount_WithDownActivationService(t *testing.T) {
sameh-farouk marked this conversation as resolved.
Show resolved Hide resolved
cl := startLocalConnection(t)
defer cl.Close()

require := require.New(t)
id, err := NewIdentityFromEd25519Phrase("paper endless radio rude wage hood cabin praise girl income chief craft")
require.NoError(err)
_, err = cl.EnsureAccount(id, "https://test.wrong-activation-service-url.tf", "test", "test")
require.Error(err)
require.IsType(ActivationServiceError{}, err)
_, err = cl.EnsureAccount(id, "https://httpstat.us/503", "test", "test")
require.Error(err)
require.IsType(ActivationServiceError{}, err)
}
Loading