From 539ae549660140decdfbdf7286f92b922a5973a7 Mon Sep 17 00:00:00 2001 From: Artur Albov Date: Wed, 24 Oct 2018 18:11:21 +0300 Subject: [PATCH] #64 Claim service: increment tx sequence manually --- cosmos/poc/claim/client/handler.go | 4 +++ cosmos/poc/claim/context/ctx.go | 56 +++++++++++++++++------------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/cosmos/poc/claim/client/handler.go b/cosmos/poc/claim/client/handler.go index 64f1a0c6..1e75e52b 100644 --- a/cosmos/poc/claim/client/handler.go +++ b/cosmos/poc/claim/client/handler.go @@ -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) @@ -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) diff --git a/cosmos/poc/claim/context/ctx.go b/cosmos/poc/claim/context/ctx.go index 5b803831..377f72a1 100644 --- a/cosmos/poc/claim/context/ctx.go +++ b/cosmos/poc/claim/context/ctx.go @@ -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) { @@ -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 } @@ -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,