Skip to content

Commit

Permalink
Merge pull request #8 from xpladev/my/refactor-x
Browse files Browse the repository at this point in the history
refactor: get address without the private key when make tx and add http mutex
  • Loading branch information
Moonyongjung committed Nov 22, 2023
2 parents e305f9d + 35a0870 commit 4184033
Show file tree
Hide file tree
Showing 91 changed files with 374 additions and 391 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Test
run: |
go mod tidy
go test ./... -race -covermode=atomic -coverprofile=coverage.out
go test ./... -covermode=atomic -coverprofile=coverage.out
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Xpla.go is a Golang SDK for writing applications that interact with the Xpla blo

|xpla.go|XPLA|Note|
|:---:|:---:|:---:|
|v0.1.1-v0.1.2|[v1.2.3](https://github.com/xpladev/xpla/tree/v1.2.3)||
|v0.1.1-v0.1.3|[v1.2.3](https://github.com/xpladev/xpla/tree/v1.2.3)||
|v0.1.0|[v1.2.2](https://github.com/xpladev/xpla/tree/v1.2.2)||


Expand Down
4 changes: 3 additions & 1 deletion client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ type Options struct {
Pagination types.Pagination
// Set output document name when created transaction with json file
// "Generate only" is same that OutputDocument is not empty string
OutputDocument string
OutputDocument string
// Set from address manually
FromAddress sdk.AccAddress
}
```

Expand Down
3 changes: 3 additions & 0 deletions client/broadcast_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func broadcastTx(xplac *xplaClient, txBytes []byte, mode txtypes.BroadcastMode)
return nil, util.LogErr(errors.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
}
xplac.GetHttpMutex().Unlock()

var broadcastTxResponse txtypes.BroadcastTxResponse
err = xplac.GetEncoding().Marshaler.UnmarshalJSON(out, &broadcastTxResponse)
Expand Down
22 changes: 22 additions & 0 deletions client/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ func (s *ClientTestSuite) TestBroadcastEVM() {
s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestBroadcastSolidityContract() {
s.xplac.WithPrivateKey(s.accounts[0].PrivKey).
WithURL(s.apis[0]).
WithEvmRpc("http://" + s.network.Validators[0].AppConfig.JSONRPC.Address)

testABIJsonFilePath := "../util/testutil/test_files/abi.json"
testBytecodeJsonFilePath := "../util/testutil/test_files/bytecode.json"

deploySolContractMsg := types.DeploySolContractMsg{
ABIJsonFilePath: testABIJsonFilePath,
BytecodeJsonFilePath: testBytecodeJsonFilePath,
Args: nil,
}
txbytes, err := s.xplac.DeploySolidityContract(deploySolContractMsg).CreateAndSignTx()
s.Require().NoError(err)

_, err = s.xplac.Broadcast(txbytes)
s.Require().NoError(err)

s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestMultiSignature() {
s.xplac.WithURL(s.apis[0])
rootDir := s.network.Validators[0].Dir
Expand Down
6 changes: 6 additions & 0 deletions client/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ func (xplac *xplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.Acco

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
}
xplac.GetHttpMutex().Unlock()

var response authtypes.QueryAccountResponse
err = xplac.GetEncoding().Marshaler.UnmarshalJSON(out, &response)
Expand Down Expand Up @@ -89,10 +92,13 @@ func (xplac *xplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.Simulate
return nil, util.LogErr(errors.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
}
xplac.GetHttpMutex().Unlock()

var response sdktx.SimulateResponse
err = xplac.GetEncoding().Marshaler.UnmarshalJSON(out, &response)
Expand Down
9 changes: 9 additions & 0 deletions client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ func (xplac *xplaClient) CreateUnsignedTx() ([]byte, error) {
if xplac.GetErr() != nil {
return nil, xplac.GetErr()
}

if xplac.GetGasAdjustment() == "" {
xplac.WithGasAdjustment(types.DefaultGasAdjustment)
}

if xplac.GetGasPrice() == "" {
xplac.WithGasPrice(types.DefaultGasPrice)
}

builder, err := setTxBuilderMsg(xplac)
if err != nil {
return nil, err
Expand Down
22 changes: 22 additions & 0 deletions client/xplaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"sync"

"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/core/auth"
Expand Down Expand Up @@ -44,6 +45,7 @@ type xplaClient struct {
encodingConfig paramsapp.EncodingConfig
grpc grpc1.ClientConn
context context.Context
httpMutex *sync.Mutex

opts provider.Options

Expand All @@ -60,6 +62,7 @@ func NewXplaClient(
chainId string,
) provider.XplaClient {
var xplac xplaClient
xplac.httpMutex = new(sync.Mutex)
return xplac.
WithChainId(chainId).
WithEncoding(util.MakeEncodingConfig()).
Expand Down Expand Up @@ -89,6 +92,7 @@ func (xplac *xplaClient) WithOptions(
WithEvmRpc(options.EvmRpcURL).
WithPagination(options.Pagination).
WithOutputDocument(options.OutputDocument).
WithFromAddress(options.FromAddress).
UpdateXplacInCoreModule()
}

Expand Down Expand Up @@ -162,6 +166,15 @@ func (xplac *xplaClient) WithContext(ctx context.Context) provider.XplaClient {
// Set private key
func (xplac *xplaClient) WithPrivateKey(privateKey key.PrivateKey) provider.XplaClient {
xplac.opts.PrivateKey = privateKey
if privateKey != nil {
addr, err := util.GetAddrByPrivKey(privateKey)
if err != nil {
xplac.err = err
return xplac.UpdateXplacInCoreModule()
}
// Automatically setting FromAddress when xpla client has the private key
xplac.opts.FromAddress = addr
}
return xplac.UpdateXplacInCoreModule()
}

Expand Down Expand Up @@ -265,6 +278,7 @@ func (xplac *xplaClient) WithPagination(pagination types.Pagination) provider.Xp
pageReq, err := core.ReadPageRequest(pagination)
if err != nil {
xplac.err = err
return xplac.UpdateXplacInCoreModule()
}
core.PageRequest = pageReq
} else {
Expand All @@ -280,6 +294,12 @@ func (xplac *xplaClient) WithOutputDocument(outputDocument string) provider.Xpla
return xplac.UpdateXplacInCoreModule()
}

// Set from address manually
func (xplac *xplaClient) WithFromAddress(fromAddress sdk.AccAddress) provider.XplaClient {
xplac.opts.FromAddress = fromAddress
return xplac.UpdateXplacInCoreModule()
}

// Set module name
func (xplac *xplaClient) WithModule(module string) provider.XplaClient {
xplac.module = module
Expand Down Expand Up @@ -326,6 +346,8 @@ func (xplac *xplaClient) GetFeeGranter() sdk.AccAddress { return xplac.o
func (xplac *xplaClient) GetTimeoutHeight() string { return xplac.opts.TimeoutHeight }
func (xplac *xplaClient) GetPagination() *query.PageRequest { return core.PageRequest }
func (xplac *xplaClient) GetOutputDocument() string { return xplac.opts.OutputDocument }
func (xplac *xplaClient) GetFromAddress() sdk.AccAddress { return xplac.opts.FromAddress }
func (xplac *xplaClient) GetHttpMutex() *sync.Mutex { return xplac.httpMutex }
func (xplac *xplaClient) GetModule() string { return xplac.module }
func (xplac *xplaClient) GetMsgType() string { return xplac.msgType }
func (xplac *xplaClient) GetMsg() interface{} { return xplac.msg }
Expand Down
2 changes: 1 addition & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var once sync.Once
var cc *coreController

// Controller is able to contol modules in the core package.
// Controller is able to control modules in the core package.
// Route Tx & Query logic by message type.
// If need to add new modules of XPLA, insert NewCoreModule in the core controller.
type coreController struct {
Expand Down
3 changes: 3 additions & 0 deletions core/auth/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,13 @@ func queryByLcdAuth(i core.QueryClient) (string, error) {
return "", util.LogErr(errors.ErrInvalidMsgType, i.Ixplac.GetMsgType())
}

i.Ixplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("GET", i.Ixplac.GetLcdURL()+url, nil, i.Ixplac.GetContext())
if err != nil {
i.Ixplac.GetHttpMutex().Unlock()
return "", err
}
i.Ixplac.GetHttpMutex().Unlock()

return string(out), nil
}
4 changes: 2 additions & 2 deletions core/authz/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewAuthzExternal(xplac provider.XplaClient) (e AuthzExternal) {

// Grant authorization to an address.
func (e AuthzExternal) AuthzGrant(authzGrantMsg types.AuthzGrantMsg) provider.XplaClient {
msg, err := MakeAuthzGrantMsg(authzGrantMsg, e.Xplac.GetPrivateKey())
msg, err := MakeAuthzGrantMsg(authzGrantMsg, e.Xplac.GetFromAddress())
if err != nil {
return provider.ResetModuleAndMsgXplac(e.Xplac).WithErr(err)
}
Expand All @@ -32,7 +32,7 @@ func (e AuthzExternal) AuthzGrant(authzGrantMsg types.AuthzGrantMsg) provider.Xp

// Revoke authorization.
func (e AuthzExternal) AuthzRevoke(authzRevokeMsg types.AuthzRevokeMsg) provider.XplaClient {
msg, err := MakeAuthzRevokeMsg(authzRevokeMsg, e.Xplac.GetPrivateKey())
msg, err := MakeAuthzRevokeMsg(authzRevokeMsg, e.Xplac.GetFromAddress())
if err != nil {
return provider.ResetModuleAndMsgXplac(e.Xplac).WithErr(err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/authz/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *IntegrationTestSuite) TestAuthzTx() {
}
s.xplac.AuthzGrant(authzGrantMsg)

makeAuthzGrantMsg, err := mauthz.MakeAuthzGrantMsg(authzGrantMsg, s.xplac.GetPrivateKey())
makeAuthzGrantMsg, err := mauthz.MakeAuthzGrantMsg(authzGrantMsg, s.xplac.GetFromAddress())
s.Require().NoError(err)

s.Require().Equal(makeAuthzGrantMsg, s.xplac.GetMsg())
Expand All @@ -39,7 +39,7 @@ func (s *IntegrationTestSuite) TestAuthzTx() {
}
s.xplac.AuthzRevoke(authzRevokeMsg)

makeAuthzRevokeMsg, err := mauthz.MakeAuthzRevokeMsg(authzRevokeMsg, s.xplac.GetPrivateKey())
makeAuthzRevokeMsg, err := mauthz.MakeAuthzRevokeMsg(authzRevokeMsg, s.xplac.GetFromAddress())
s.Require().NoError(err)

s.Require().Equal(makeAuthzRevokeMsg, s.xplac.GetMsg())
Expand Down
4 changes: 2 additions & 2 deletions core/authz/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *IntegrationTestSuite) TestCoreModule() {
SpendLimit: "1000",
}

makeAuthzGrantMsg, err := authz.MakeAuthzGrantMsg(authzGrantMsg, s.xplac.GetPrivateKey())
makeAuthzGrantMsg, err := authz.MakeAuthzGrantMsg(authzGrantMsg, s.xplac.GetFromAddress())
s.Require().NoError(err)

testMsg = makeAuthzGrantMsg
Expand All @@ -47,7 +47,7 @@ func (s *IntegrationTestSuite) TestCoreModule() {
MsgType: "/cosmos.bank.v1beta1.MsgSend",
}

makeAuthzRevokeMsg, err := authz.MakeAuthzRevokeMsg(authzRevokeMsg, s.xplac.GetPrivateKey())
makeAuthzRevokeMsg, err := authz.MakeAuthzRevokeMsg(authzRevokeMsg, s.xplac.GetFromAddress())
s.Require().NoError(err)

testMsg = makeAuthzRevokeMsg
Expand Down
10 changes: 5 additions & 5 deletions core/authz/msg.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package authz

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla/app/params"
)

// (Tx) make msg - authz grant
func MakeAuthzGrantMsg(authzGrantMsg types.AuthzGrantMsg, privKey key.PrivateKey) (authz.MsgGrant, error) {
return parseAuthzGrantArgs(authzGrantMsg, privKey)
func MakeAuthzGrantMsg(authzGrantMsg types.AuthzGrantMsg, granter sdk.AccAddress) (authz.MsgGrant, error) {
return parseAuthzGrantArgs(authzGrantMsg, granter)
}

// (Tx) make msg - revoke
func MakeAuthzRevokeMsg(authzRevokeMsg types.AuthzRevokeMsg, privKey key.PrivateKey) (authz.MsgRevoke, error) {
return parseAuthzRevokeArgs(authzRevokeMsg, privKey)
func MakeAuthzRevokeMsg(authzRevokeMsg types.AuthzRevokeMsg, granter sdk.AccAddress) (authz.MsgRevoke, error) {
return parseAuthzRevokeArgs(authzRevokeMsg, granter)
}

// (Tx) make msg - authz execute
Expand Down
13 changes: 2 additions & 11 deletions core/authz/msg_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"
"github.com/xpladev/xpla/app/params"
)

// Parsing - authz grant
func parseAuthzGrantArgs(authzGrantMsg types.AuthzGrantMsg, privKey key.PrivateKey) (authz.MsgGrant, error) {
granter, err := util.GetAddrByPrivKey(privKey)
if err != nil {
return authz.MsgGrant{}, util.LogErr(errors.ErrParse, err)
}
func parseAuthzGrantArgs(authzGrantMsg types.AuthzGrantMsg, granter sdk.AccAddress) (authz.MsgGrant, error) {
if authzGrantMsg.Granter != granter.String() {
return authz.MsgGrant{}, util.LogErr(errors.ErrAccountNotMatch, "Account address generated by private key is not equal input granter of msg")
}
Expand Down Expand Up @@ -108,11 +103,7 @@ func parseAuthzGrantArgs(authzGrantMsg types.AuthzGrantMsg, privKey key.PrivateK
}

// Parsing - revoke
func parseAuthzRevokeArgs(authzRevokeMsg types.AuthzRevokeMsg, privKey key.PrivateKey) (authz.MsgRevoke, error) {
granter, err := util.GetAddrByPrivKey(privKey)
if err != nil {
return authz.MsgRevoke{}, util.LogErr(errors.ErrParse, err)
}
func parseAuthzRevokeArgs(authzRevokeMsg types.AuthzRevokeMsg, granter sdk.AccAddress) (authz.MsgRevoke, error) {
if authzRevokeMsg.Granter != granter.String() {
return authz.MsgRevoke{}, util.LogErr(errors.ErrAccountNotMatch, "Account address generated by private key is not equal input granter of msg")
}
Expand Down
3 changes: 3 additions & 0 deletions core/authz/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ func queryByLcdAuthz(i core.QueryClient) (string, error) {
return "", util.LogErr(errors.ErrInvalidMsgType, i.Ixplac.GetMsgType())
}

i.Ixplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("GET", i.Ixplac.GetLcdURL()+url, nil, i.Ixplac.GetContext())
if err != nil {
i.Ixplac.GetHttpMutex().Unlock()
return "", err
}
i.Ixplac.GetHttpMutex().Unlock()

return string(out), nil
}
2 changes: 1 addition & 1 deletion core/bank/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewBankExternal(xplac provider.XplaClient) (e BankExternal) {

// Send funds from one account to another.
func (e BankExternal) BankSend(bankSendMsg types.BankSendMsg) provider.XplaClient {
msg, err := MakeBankSendMsg(bankSendMsg, e.Xplac.GetPrivateKey())
msg, err := MakeBankSendMsg(bankSendMsg)
if err != nil {
return provider.ResetModuleAndMsgXplac(e.Xplac).WithErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/bank/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (s *IntegrationTestSuite) TestBankTx() {
}
s.xplac.BankSend(bankSendMsg)

makeBankSendMsg, err := mbank.MakeBankSendMsg(bankSendMsg, s.xplac.GetPrivateKey())
makeBankSendMsg, err := mbank.MakeBankSendMsg(bankSendMsg)
s.Require().NoError(err)

s.Require().Equal(makeBankSendMsg, s.xplac.GetMsg())
Expand Down
2 changes: 1 addition & 1 deletion core/bank/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *IntegrationTestSuite) TestCoreModule() {
Amount: "1000",
}

makeBankSendMsg, err := bank.MakeBankSendMsg(bankSendMsg, s.xplac.GetPrivateKey())
makeBankSendMsg, err := bank.MakeBankSendMsg(bankSendMsg)
s.Require().NoError(err)

testMsg = makeBankSendMsg
Expand Down
5 changes: 2 additions & 3 deletions core/bank/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bank

import (
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"
Expand All @@ -11,8 +10,8 @@ import (
)

// (Tx) make msg - bank send
func MakeBankSendMsg(bankSendMsg types.BankSendMsg, privKey key.PrivateKey) (banktypes.MsgSend, error) {
return parseBankSendArgs(bankSendMsg, privKey)
func MakeBankSendMsg(bankSendMsg types.BankSendMsg) (banktypes.MsgSend, error) {
return parseBankSendArgs(bankSendMsg)
}

// (Query) make msg - all balances
Expand Down

0 comments on commit 4184033

Please sign in to comment.