Skip to content

Commit

Permalink
feat: logger of client
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonyongjung committed Jan 4, 2024
1 parent d6e27ea commit bf91179
Show file tree
Hide file tree
Showing 145 changed files with 2,353 additions and 1,999 deletions.
4 changes: 3 additions & 1 deletion client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ type Options struct {
// "Generate only" is same that OutputDocument is not empty string
OutputDocument string
// Set from address manually
FromAddress sdk.AccAddress
FromAddress sdk.AccAddress
// Set log verbose (0: default, 1: details, 2: implication)
Verbose int
}
```

Expand Down
5 changes: 2 additions & 3 deletions client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
mevm "github.com/xpladev/xpla.go/core/evm"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

txtypes "github.com/cosmos/cosmos-sdk/types/tx"
Expand Down Expand Up @@ -56,11 +55,11 @@ func (xplac *xplaClient) BroadcastAsync(txBytes []byte) (*types.TxRes, error) {
// Broadcast the transaction which is evm transaction by using ethclient of go-ethereum.
func (xplac *xplaClient) broadcastEvm(txBytes []byte) (*types.TxRes, error) {
if xplac.GetEvmRpc() == "" {
return nil, util.LogErr(errors.ErrNotSatisfiedOptions, "evm JSON-RPC URL must exist")
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrNotSatisfiedOptions, "evm JSON-RPC URL must exist"))

Check warning on line 58 in client/broadcast.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast.go#L58

Added line #L58 was not covered by tests
}
evmClient, err := util.NewEvmClient(xplac.GetEvmRpc(), xplac.GetContext())
if err != nil {
return nil, err
return nil, xplac.GetLogger().Err(err)

Check warning on line 62 in client/broadcast.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast.go#L62

Added line #L62 was not covered by tests
}
broadcastMode := xplac.GetBroadcastMode()
return broadcastTxEvm(xplac, txBytes, broadcastMode, evmClient)
Expand Down
39 changes: 19 additions & 20 deletions client/broadcast_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

mevm "github.com/xpladev/xpla.go/core/evm"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"

txtypes "github.com/cosmos/cosmos-sdk/types/tx"
Expand All @@ -27,34 +26,34 @@ func broadcastTx(xplac *xplaClient, txBytes []byte, mode txtypes.BroadcastMode)
if xplac.GetGrpcUrl() == "" {
reqBytes, err := json.Marshal(broadcastReq)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToMarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToMarshal, err))

Check warning on line 29 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L29

Added line #L29 was not covered by tests
}

xplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("POST", xplac.GetLcdURL()+broadcastUrl, reqBytes, xplac.GetContext())
if err != nil {
xplac.GetHttpMutex().Unlock()
return nil, err
return nil, xplac.GetLogger().Err(err)

Check warning on line 36 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L36

Added line #L36 was not covered by tests
}
xplac.GetHttpMutex().Unlock()

var broadcastTxResponse txtypes.BroadcastTxResponse
err = xplac.GetEncoding().Codec.UnmarshalJSON(out, &broadcastTxResponse)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToUnmarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToUnmarshal, err))

Check warning on line 43 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L43

Added line #L43 was not covered by tests
}

txResponse := broadcastTxResponse.TxResponse
if txResponse.Code != 0 {
return &xplaTxRes, util.LogErr(errors.ErrTxFailed, "with code", txResponse.Code, ":", txResponse.RawLog)
return &xplaTxRes, xplac.GetLogger().Err(types.ErrWrap(types.ErrTxFailed, "with code", txResponse.Code, ":", txResponse.RawLog))
}

xplaTxRes.Response = txResponse
} else {
txClient := txtypes.NewServiceClient(xplac.GetGrpcClient())
txResponse, err := txClient.BroadcastTx(xplac.GetContext(), &broadcastReq)
if err != nil {
return nil, util.LogErr(errors.ErrGrpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrGrpcRequest, err))

Check warning on line 56 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L56

Added line #L56 was not covered by tests
}
xplaTxRes.Response = txResponse.TxResponse
}
Expand All @@ -71,32 +70,32 @@ func broadcastTxEvm(xplac *xplaClient, txBytes []byte, broadcastMode string, evm
var signedTx evmtypes.Transaction
err := signedTx.UnmarshalJSON(txBytes)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToUnmarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToUnmarshal, err))

Check warning on line 73 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L73

Added line #L73 was not covered by tests
}

err = evmClient.Client.SendTransaction(evmClient.Ctx, &signedTx)
if err != nil {
return nil, util.LogErr(errors.ErrEvmRpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrEvmRpcRequest, err))

Check warning on line 78 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L78

Added line #L78 was not covered by tests
}

return checkEvmBroadcastMode(broadcastMode, evmClient, &signedTx)
return checkEvmBroadcastMode(xplac, broadcastMode, evmClient, &signedTx)

case xplac.GetMsgType() == mevm.EvmDeploySolContractMsgType:
var deployTx mevm.DeploySolTx

err := json.Unmarshal(txBytes, &deployTx)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToUnmarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToUnmarshal, err))

Check warning on line 88 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L88

Added line #L88 was not covered by tests
}

ethPrivKey, err := toECDSA(xplac.GetPrivateKey())
if err != nil {
return nil, util.LogErr(errors.ErrCannotConvert, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, err))

Check warning on line 93 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L93

Added line #L93 was not covered by tests
}

contractAuth, err := bind.NewKeyedTransactorWithChainID(ethPrivKey, deployTx.ChainId)
if err != nil {
return nil, util.LogErr(errors.ErrInsufficientParams, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrInsufficientParams, err))

Check warning on line 98 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L98

Added line #L98 was not covered by tests
}
contractAuth.Nonce = deployTx.Nonce
contractAuth.Value = deployTx.Value
Expand All @@ -106,10 +105,10 @@ func broadcastTxEvm(xplac *xplaClient, txBytes []byte, broadcastMode string, evm
metadata := util.GetBindMetaData(deployTx.ABI, deployTx.Bytecode)
parsedAbi, err := metadata.GetAbi()
if err != nil {
return nil, util.LogErr(errors.ErrEvmRpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, err))

Check warning on line 108 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L108

Added line #L108 was not covered by tests
}
if parsedAbi == nil {
return nil, util.LogErr(errors.ErrEvmRpcRequest, "GetABI returned nil")
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, "GetABI returned nil"))

Check warning on line 111 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L111

Added line #L111 was not covered by tests
}
parsedBytecode := common.FromHex(metadata.Bin)

Expand All @@ -121,24 +120,24 @@ func broadcastTxEvm(xplac *xplaClient, txBytes []byte, broadcastMode string, evm
mevm.Args = nil
}
if err != nil {
return nil, util.LogErr(errors.ErrEvmRpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrEvmRpcRequest, err))

Check warning on line 123 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L123

Added line #L123 was not covered by tests
}

return checkEvmBroadcastMode(broadcastMode, evmClient, transaction)
return checkEvmBroadcastMode(xplac, broadcastMode, evmClient, transaction)

default:
return nil, util.LogErr(errors.ErrInvalidMsgType, "invalid EVM msg type:", xplac.GetMsgType())
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrInvalidMsgType, "invalid EVM msg type:", xplac.GetMsgType()))

Check warning on line 129 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L129

Added line #L129 was not covered by tests
}
}

// Handle evm broadcast mode.
// Similarly, determine broadcast mode included in the options of xpla client.
func checkEvmBroadcastMode(broadcastMode string, evmClient *util.EvmClient, tx *evmtypes.Transaction) (*types.TxRes, error) {
func checkEvmBroadcastMode(xplac *xplaClient, broadcastMode string, evmClient *util.EvmClient, tx *evmtypes.Transaction) (*types.TxRes, error) {
// Wait tx receipt (Broadcast Block)
if broadcastMode == "block" {
receipt, err := waitTxReceipt(evmClient, tx)
if err != nil {
return nil, err
return nil, xplac.GetLogger().Err(err)

Check warning on line 140 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L140

Added line #L140 was not covered by tests
}
xplaTxRes.EvmReceipt = receipt
return &xplaTxRes, nil
Expand All @@ -157,7 +156,7 @@ func waitTxReceipt(evmClient *util.EvmClient, signedTx *evmtypes.Transaction) (*
if err != nil {
count = count - 1
if count < 0 {
return nil, util.LogErr(errors.ErrEvmRpcRequest, "cannot receive the transaction receipt in count time is", count)
return nil, types.ErrWrap(types.ErrEvmRpcRequest, "cannot receive the transaction receipt in count time is", count)

Check warning on line 159 in client/broadcast_handle.go

View check run for this annotation

Codecov / codecov/patch

client/broadcast_handle.go#L159

Added line #L159 was not covered by tests
}
time.Sleep(time.Second * 1)
} else {
Expand Down
14 changes: 8 additions & 6 deletions client/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,22 @@ func (s *ClientTestSuite) TestMultiSignature() {
kb, err := keyring.New(types.XplaToolDefaultName, keyring.BackendTest, rootDir, nil, hd.EthSecp256k1Option())
s.Require().NoError(err)

armor1, err := key.EncryptArmorPrivKey(key1.PrivKey, key.DefaultEncryptPassphrase)
s.Require().NoError(err)

err = kb.ImportPrivKey(
key1Name,
key.EncryptArmorPrivKey(key1.PrivKey, key.DefaultEncryptPassphrase),
armor1,
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)

armor2, err := key.EncryptArmorPrivKey(key2.PrivKey, key.DefaultEncryptPassphrase)
s.Require().NoError(err)

err = kb.ImportPrivKey(
key2Name,
key.EncryptArmorPrivKey(key2.PrivKey, key.DefaultEncryptPassphrase),
armor2,
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)
Expand Down Expand Up @@ -309,10 +315,6 @@ func (s *ClientTestSuite) TestMultiSignature() {
// generate error insufficient funds
// multisig tx is normal
s.Require().Error(err)
s.Require().Equal(
`code 8 : tx failed - [with code 5 : 10000000000axpla is smaller than 1077997200000000000axpla: insufficient funds: insufficient funds]`,
err.Error(),
)

s.xplac = provider.ResetXplac(s.xplac)
}
29 changes: 14 additions & 15 deletions client/info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util"

cmclient "github.com/cosmos/cosmos-sdk/client"
Expand All @@ -22,19 +22,18 @@ const (
func (xplac *xplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.AccountI, err error) {

if xplac.GetGrpcUrl() == "" {

xplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("GET", xplac.GetLcdURL()+userInfoUrl+address.String(), nil, xplac.GetContext())
if err != nil {
xplac.GetHttpMutex().Unlock()
return nil, err
return nil, xplac.GetLogger().Err(err)

Check warning on line 29 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L29

Added line #L29 was not covered by tests
}
xplac.GetHttpMutex().Unlock()

var response authtypes.QueryAccountResponse
err = xplac.GetEncoding().Codec.UnmarshalJSON(out, &response)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToUnmarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToUnmarshal, err))

Check warning on line 36 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L36

Added line #L36 was not covered by tests
}
return response.Account.GetCachedValue().(authtypes.AccountI), nil

Expand All @@ -45,13 +44,13 @@ func (xplac *xplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.Acco
}
response, err := queryClient.Account(xplac.GetContext(), &queryAccountRequest)
if err != nil {
return nil, util.LogErr(errors.ErrGrpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrGrpcRequest, err))

Check warning on line 47 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L47

Added line #L47 was not covered by tests
}

var newAccount authtypes.AccountI
err = xplac.GetEncoding().InterfaceRegistry.UnpackAny(response.Account, &newAccount)
if err != nil {
return nil, util.LogErr(errors.ErrParse, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, err))

Check warning on line 53 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L53

Added line #L53 was not covered by tests
}

return newAccount, nil
Expand All @@ -63,21 +62,21 @@ func (xplac *xplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.Acco
func (xplac *xplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.SimulateResponse, error) {
seq, err := util.FromStringToUint64(xplac.GetSequence())
if err != nil {
return nil, err
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrConvert, err))

Check warning on line 65 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L65

Added line #L65 was not covered by tests
}

pubKey := xplac.GetPublicKey()
if xplac.GetPublicKey() == nil {
if xplac.GetFromAddress() != nil {
accountInfo, err := xplac.LoadAccount(xplac.GetFromAddress())
if err != nil {
return nil, util.LogErr(errors.ErrParse, err)
return nil, err

Check warning on line 73 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L73

Added line #L73 was not covered by tests
}

pubKey = accountInfo.GetPubKey()

} else {
return nil, util.LogErr(errors.ErrInvalidRequest, "cannot be simulated without the public key.")
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrInvalidRequest, "cannot be simulated without the public key."))

Check warning on line 79 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L79

Added line #L79 was not covered by tests
}
}

Expand All @@ -90,35 +89,35 @@ func (xplac *xplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.Simulate
}

if err := txbuilder.SetSignatures(sig); err != nil {
return nil, util.LogErr(errors.ErrParse, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, err))

Check warning on line 92 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L92

Added line #L92 was not covered by tests
}

sdkTx := txbuilder.GetTx()
txBytes, err := xplac.GetEncoding().TxConfig.TxEncoder()(sdkTx)
if err != nil {
return nil, util.LogErr(errors.ErrParse, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, err))

Check warning on line 98 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L98

Added line #L98 was not covered by tests
}

if xplac.GetGrpcUrl() == "" {
reqBytes, err := xplac.GetEncoding().Codec.MarshalJSON(&sdktx.SimulateRequest{
TxBytes: txBytes,
})
if err != nil {
return nil, util.LogErr(errors.ErrFailedToMarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToMarshal, err))

Check warning on line 106 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L106

Added line #L106 was not covered by tests
}

xplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("POST", xplac.GetLcdURL()+simulateUrl, reqBytes, xplac.GetContext())
if err != nil {
xplac.GetHttpMutex().Unlock()
return nil, err
return nil, xplac.GetLogger().Err(err)

Check warning on line 113 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L113

Added line #L113 was not covered by tests
}
xplac.GetHttpMutex().Unlock()

var response sdktx.SimulateResponse
err = xplac.GetEncoding().Codec.UnmarshalJSON(out, &response)
if err != nil {
return nil, util.LogErr(errors.ErrFailedToUnmarshal, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrFailedToUnmarshal, err))

Check warning on line 120 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L120

Added line #L120 was not covered by tests
}

return &response, nil
Expand All @@ -130,7 +129,7 @@ func (xplac *xplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.Simulate

response, err := serviceClient.Simulate(xplac.GetContext(), &simulateRequest)
if err != nil {
return nil, util.LogErr(errors.ErrGrpcRequest, err)
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrGrpcRequest, err))

Check warning on line 132 in client/info.go

View check run for this annotation

Codecov / codecov/patch

client/info.go#L132

Added line #L132 was not covered by tests
}

return response, nil
Expand Down
Loading

0 comments on commit bf91179

Please sign in to comment.