Skip to content

Commit

Permalink
#64 Claim service: increment tx sequence manually
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov authored and hleb-albau committed Oct 24, 2018
1 parent 7b3b25e commit 539ae54
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
4 changes: 4 additions & 0 deletions cosmos/poc/claim/client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func ClaimHandlerFn(ctx context.ClaimContext) func(http.ResponseWriter, *http.Re
coins, _ := sdk.ParseCoins(amount + token)
msg := client.CreateMsg(ctx.ClaimFrom, claimTo, coins)

ctx.Mtx.Lock()
defer ctx.Mtx.Unlock()

txBldr, err := ctx.TxBuilder()
if err != nil {
util.HandleError(err, w)
Expand All @@ -80,6 +83,7 @@ func ClaimHandlerFn(ctx context.ClaimContext) func(http.ResponseWriter, *http.Re
util.HandleError(err, w)
return
}
*ctx.Sequence++
w.Write(resultJson)
} else {
util.HandleError(errors.New("Account already has tokens"), w)
Expand Down
56 changes: 32 additions & 24 deletions cosmos/poc/claim/context/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import (
"github.com/pkg/errors"
"github.com/spf13/viper"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"sync"
)

type ClaimContext struct {
Name string
Passphrase string
ChainId string
ClaimFrom types.AccAddress
Codec *codec.Codec
CliContext *cli.CLIContext
ipClaims map[string]int
Name string
Passphrase string
ChainId string
ClaimFrom types.AccAddress
Codec *codec.Codec
CliContext cli.CLIContext
ipClaims map[string]int
Mtx *sync.Mutex
Sequence *int64
AccountNumber int64
}

func NewClaimContext() (ClaimContext, error) {
Expand All @@ -37,14 +41,26 @@ func NewClaimContext() (ClaimContext, error) {
return ClaimContext{}, err
}

accountNumber, err := cliCtx.GetAccountNumber(address)
if err != nil {
return ClaimContext{}, err
}
seq, err := cliCtx.GetAccountSequence(address)
if err != nil {
return ClaimContext{}, err
}

return ClaimContext{
Name: name,
ClaimFrom: address,
Passphrase: viper.GetString(common.FlagPassphrase),
ChainId: chainId,
Codec: cdc,
CliContext: &cliCtx,
ipClaims: make(map[string]int),
Name: name,
ClaimFrom: address,
Passphrase: viper.GetString(common.FlagPassphrase),
ChainId: chainId,
Codec: cdc,
CliContext: cliCtx,
ipClaims: make(map[string]int),
Mtx: new(sync.Mutex),
Sequence: &seq,
AccountNumber: accountNumber,
}, nil
}

Expand All @@ -58,20 +74,12 @@ func (ctx ClaimContext) IncrementIp(ip string) error {
}

func (ctx ClaimContext) TxBuilder() (authtxb.TxBuilder, error) {
accountNumber, err := ctx.CliContext.GetAccountNumber(ctx.ClaimFrom)
if err != nil {
return authtxb.TxBuilder{}, err
}
seq, err := ctx.CliContext.GetAccountSequence(ctx.ClaimFrom)
if err != nil {
return authtxb.TxBuilder{}, err
}

return authtxb.TxBuilder{
ChainID: ctx.ChainId,
Gas: 10000000,
AccountNumber: accountNumber,
Sequence: seq,
AccountNumber: ctx.AccountNumber,
Sequence: *ctx.Sequence,
Fee: "",
Memo: "",
Codec: ctx.Codec,
Expand Down

0 comments on commit 539ae54

Please sign in to comment.