Skip to content

Commit

Permalink
Merge pull request #10 from xpladev/my/feat-x
Browse files Browse the repository at this point in the history
feat: logger of client and refactoring module message structure
  • Loading branch information
Moonyongjung committed Jan 5, 2024
2 parents d6e27ea + bf91179 commit 573ebde
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"))
}
evmClient, err := util.NewEvmClient(xplac.GetEvmRpc(), xplac.GetContext())
if err != nil {
return nil, err
return nil, xplac.GetLogger().Err(err)
}
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))
}

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)
}
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))
}

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))
}
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))
}

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))
}

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))
}

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))
}

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))
}
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))
}
if parsedAbi == nil {
return nil, util.LogErr(errors.ErrEvmRpcRequest, "GetABI returned nil")
return nil, xplac.GetLogger().Err(types.ErrWrap(types.ErrParse, "GetABI returned nil"))
}
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))
}

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()))
}
}

// 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)
}
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)
}
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)
}
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))
}
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))
}

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))
}

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))
}

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
}

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."))
}
}

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))
}

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))
}

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))
}

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)
}
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))
}

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))
}

return response, nil
Expand Down
Loading

0 comments on commit 573ebde

Please sign in to comment.