diff --git a/pkg/client/account.go b/pkg/client/account.go index 9a844470..9688aa00 100644 --- a/pkg/client/account.go +++ b/pkg/client/account.go @@ -24,7 +24,7 @@ func (g *GrpcClient) GetAccount(addr string) (*core.Account, error) { return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() acc, err := g.Client.GetAccount(ctx, account) @@ -47,7 +47,7 @@ func (g *GrpcClient) GetAccountNet(addr string) (*api.AccountNetMessage, error) return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetAccountNet(ctx, account) @@ -64,7 +64,7 @@ func (g *GrpcClient) CreateAccount(from, addr string) (*api.TransactionExtention if contract.AccountAddress, err = common.DecodeCheck(addr); err != nil { return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.CreateAccount2(ctx, contract) @@ -89,7 +89,7 @@ func (g *GrpcClient) UpdateAccount(from, accountName string) (*api.TransactionEx return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.UpdateAccount2(ctx, contract) @@ -210,7 +210,7 @@ func (g *GrpcClient) WithdrawBalance(from string) (*api.TransactionExtention, er return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.WithdrawBalance2(ctx, contract) @@ -351,7 +351,7 @@ func (g *GrpcClient) UpdateAccountPermission(from string, owner, witness map[str contract.Witness = witnessPermission } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.AccountPermissionUpdate(ctx, contract) diff --git a/pkg/client/assets.go b/pkg/client/assets.go index efb9419b..0b9b3858 100644 --- a/pkg/client/assets.go +++ b/pkg/client/assets.go @@ -23,7 +23,7 @@ func (g *GrpcClient) GetAssetIssueByAccount(address string) (*api.AssetIssueList return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetAssetIssueByAccount(ctx, account) @@ -31,7 +31,7 @@ func (g *GrpcClient) GetAssetIssueByAccount(address string) (*api.AssetIssueList // GetAssetIssueByName list asset issued by name func (g *GrpcClient) GetAssetIssueByName(name string) (*core.AssetIssueContract, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetAssetIssueByName(ctx, GetMessageBytes([]byte(name))) @@ -41,7 +41,7 @@ func (g *GrpcClient) GetAssetIssueByName(name string) (*core.AssetIssueContract, func (g *GrpcClient) GetAssetIssueByID(tokenID string) (*core.AssetIssueContract, error) { bn := new(big.Int).SetBytes([]byte(tokenID)) - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetAssetIssueById(ctx, GetMessageBytes(bn.Bytes())) @@ -49,7 +49,7 @@ func (g *GrpcClient) GetAssetIssueByID(tokenID string) (*core.AssetIssueContract // GetAssetIssueList list all TRC10 func (g *GrpcClient) GetAssetIssueList(page int64, limit ...int) (*api.AssetIssueList, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() if page == -1 { @@ -136,7 +136,7 @@ func (g *GrpcClient) AssetIssue(from, name, description, abbr, urlStr string, FrozenSupply, assetIssueContractFrozenSupply) } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.CreateAssetIssue2(ctx, contract) @@ -167,7 +167,7 @@ func (g *GrpcClient) UpdateAssetIssue(from, description, urlStr string, contract.NewLimit = newLimit contract.NewPublicLimit = newPublicLimit - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.UpdateAsset2(ctx, contract) @@ -198,7 +198,7 @@ func (g *GrpcClient) TransferAsset(from, toAddress, contract.AssetName = []byte(assetName) contract.Amount = amount - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.TransferAsset2(ctx, contract) @@ -229,7 +229,7 @@ func (g *GrpcClient) ParticipateAssetIssue(from, issuerAddress, contract.AssetName = []byte(tokenID) contract.Amount = amount - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ParticipateAssetIssue2(ctx, contract) @@ -253,7 +253,7 @@ func (g *GrpcClient) UnfreezeAsset(from string) (*api.TransactionExtention, erro if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil { return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.UnfreezeAsset2(ctx, contract) diff --git a/pkg/client/bank.go b/pkg/client/bank.go index 5722f978..0cd6d86e 100644 --- a/pkg/client/bank.go +++ b/pkg/client/bank.go @@ -31,7 +31,7 @@ func (g *GrpcClient) FreezeBalance(from, delegateTo string, } contract.Resource = resource - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.FreezeBalance2(ctx, contract) @@ -64,7 +64,7 @@ func (g *GrpcClient) UnfreezeBalance(from, delegateTo string, resource core.Reso } contract.Resource = resource - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.UnfreezeBalance2(ctx, contract) diff --git a/pkg/client/block.go b/pkg/client/block.go index 66b34054..759176bc 100644 --- a/pkg/client/block.go +++ b/pkg/client/block.go @@ -11,7 +11,7 @@ import ( // GetNowBlock return TIP block func (g *GrpcClient) GetNowBlock() (*api.BlockExtention, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() result, err := g.Client.GetNowBlock2(ctx, new(api.EmptyMessage)) @@ -28,7 +28,7 @@ func (g *GrpcClient) GetBlockByNum(num int64) (*api.BlockExtention, error) { numMessage := new(api.NumberMessage) numMessage.Num = num - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() result, err := g.Client.GetBlockByNum2(ctx, numMessage) @@ -45,7 +45,7 @@ func (g *GrpcClient) GetBlockInfoByNum(num int64) (*api.TransactionInfoList, err numMessage := new(api.NumberMessage) numMessage.Num = num - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() result, err := g.Client.GetTransactionInfoByBlockNum(ctx, numMessage) @@ -67,7 +67,7 @@ func (g *GrpcClient) GetBlockByID(id string) (*core.Block, error) { return nil, fmt.Errorf("get block by id: %v", err) } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetBlockById(ctx, blockID) @@ -79,7 +79,7 @@ func (g *GrpcClient) GetBlockByLimitNext(start, end int64) (*api.BlockListExtent blockLimit.StartNum = start blockLimit.EndNum = end - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetBlockByLimitNext2(ctx, blockLimit) @@ -90,7 +90,7 @@ func (g *GrpcClient) GetBlockByLatestNum(num int64) (*api.BlockListExtention, er numMessage := new(api.NumberMessage) numMessage.Num = num - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetBlockByLatestNum2(ctx, numMessage) diff --git a/pkg/client/client.go b/pkg/client/client.go index 6b607e25..4eb84a6f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -8,22 +8,37 @@ import ( "google.golang.org/grpc" ) -const grpcTimeout = 5 * time.Second - // GrpcClient controller structure type GrpcClient struct { - Address string - Conn *grpc.ClientConn - Client api.WalletClient + Address string + Conn *grpc.ClientConn + Client api.WalletClient + grpcTimeout time.Duration } // NewGrpcClient create grpc controller func NewGrpcClient(address string) *GrpcClient { - client := new(GrpcClient) - client.Address = address + client := &GrpcClient{ + Address: address, + grpcTimeout: 5 * time.Second, + } + return client +} + +// NewGrpcClientWithTimeout create grpc controller +func NewGrpcClientWithTimeout(address string, timeout time.Duration) *GrpcClient { + client := &GrpcClient{ + Address: address, + grpcTimeout: timeout, + } return client } +// SetTimeout for Client connections +func (g *GrpcClient) SetTimeout(timeout time.Duration) { + g.grpcTimeout = timeout +} + // Start initiate grpc connection func (g *GrpcClient) Start() error { var err error diff --git a/pkg/client/contracts.go b/pkg/client/contracts.go index f5edd661..4f9091d2 100644 --- a/pkg/client/contracts.go +++ b/pkg/client/contracts.go @@ -50,7 +50,7 @@ func (g *GrpcClient) TriggerConstantContract(from, contractAddress, method, json // triggerConstantContract and return tx result func (g *GrpcClient) triggerConstantContract(ct *core.TriggerSmartContract) (*api.TransactionExtention, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.TriggerConstantContract(ctx, ct) @@ -100,7 +100,7 @@ func (g *GrpcClient) TriggerContract(from, contractAddress, method, jsonString s // triggerContract and return tx result func (g *GrpcClient) triggerContract(ct *core.TriggerSmartContract, feeLimit int64) (*api.TransactionExtention, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.TriggerContract(ctx, ct) @@ -156,7 +156,7 @@ func (g *GrpcClient) DeployContract(from, contractName string, }, } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.DeployContract(ctx, ct) @@ -193,7 +193,7 @@ func (g *GrpcClient) GetContractABI(contractAddress string) (*core.SmartContract return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() sm, err := g.Client.GetContract(ctx, GetMessageBytes(contractDesc)) diff --git a/pkg/client/exchange.go b/pkg/client/exchange.go index 9dd4fdde..9cb686bd 100644 --- a/pkg/client/exchange.go +++ b/pkg/client/exchange.go @@ -13,7 +13,7 @@ import ( // ExchangeList of bancor TRC10, use page -1 to list all func (g *GrpcClient) ExchangeList(page int64, limit ...int) (*api.ExchangeList, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() if page == -1 { @@ -29,7 +29,7 @@ func (g *GrpcClient) ExchangeList(page int64, limit ...int) (*api.ExchangeList, // ExchangeByID returns exchangeDetails func (g *GrpcClient) ExchangeByID(id int64) (*core.Exchange, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() bID := make([]byte, 8) binary.BigEndian.PutUint64(bID, uint64(id)) @@ -64,7 +64,7 @@ func (g *GrpcClient) ExchangeCreate( return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ExchangeCreate(ctx, contract) @@ -98,7 +98,7 @@ func (g *GrpcClient) ExchangeInject( return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ExchangeInject(ctx, contract) @@ -132,7 +132,7 @@ func (g *GrpcClient) ExchangeWithdraw( return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ExchangeWithdraw(ctx, contract) @@ -168,7 +168,7 @@ func (g *GrpcClient) ExchangeTrade( return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ExchangeTransaction(ctx, contract) diff --git a/pkg/client/network.go b/pkg/client/network.go index 2a912dfd..5a18e3bb 100644 --- a/pkg/client/network.go +++ b/pkg/client/network.go @@ -14,7 +14,7 @@ import ( // ListNodes provides list of network nodes func (g *GrpcClient) ListNodes() (*api.NodeList, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() nodeList, err := g.Client.ListNodes(ctx, new(api.EmptyMessage)) @@ -26,7 +26,7 @@ func (g *GrpcClient) ListNodes() (*api.NodeList, error) { // GetNextMaintenanceTime get next epoch timestamp func (g *GrpcClient) GetNextMaintenanceTime() (*api.NumberMessage, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetNextMaintenanceTime(ctx, @@ -35,7 +35,7 @@ func (g *GrpcClient) GetNextMaintenanceTime() (*api.NumberMessage, error) { // TotalTransaction return total transciton in network func (g *GrpcClient) TotalTransaction() (*api.NumberMessage, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.TotalTransaction(ctx, @@ -52,7 +52,7 @@ func (g *GrpcClient) GetTransactionByID(id string) (*core.Transaction, error) { return nil, fmt.Errorf("get transaction by id error: %v", err) } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.GetTransactionById(ctx, transactionID) @@ -75,7 +75,7 @@ func (g *GrpcClient) GetTransactionInfoByID(id string) (*core.TransactionInfo, e return nil, fmt.Errorf("get transaction by id error: %v", err) } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() txi, err := g.Client.GetTransactionInfoById(ctx, transactionID) @@ -90,7 +90,7 @@ func (g *GrpcClient) GetTransactionInfoByID(id string) (*core.TransactionInfo, e // Broadcast broadcast TX func (g *GrpcClient) Broadcast(tx *core.Transaction) (*api.Return, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() result, err := g.Client.BroadcastTransaction(ctx, tx) if err != nil { @@ -107,7 +107,7 @@ func (g *GrpcClient) Broadcast(tx *core.Transaction) (*api.Return, error) { // GetNodeInfo current connection func (g *GrpcClient) GetNodeInfo() (*core.NodeInfo, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetNodeInfo(ctx, new(api.EmptyMessage)) diff --git a/pkg/client/proposal.go b/pkg/client/proposal.go index 5053db7a..1a50b09d 100644 --- a/pkg/client/proposal.go +++ b/pkg/client/proposal.go @@ -12,7 +12,7 @@ import ( // ProposalsList return all network proposals func (g *GrpcClient) ProposalsList() (*api.ProposalList, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.ListProposals(ctx, new(api.EmptyMessage)) @@ -29,7 +29,7 @@ func (g *GrpcClient) ProposalCreate(from string, parameters map[int64]int64) (*a return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ProposalCreate(ctx, contract) @@ -57,7 +57,7 @@ func (g *GrpcClient) ProposalApprove(from string, id int64, confirm bool) (*api. return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.ProposalApprove(ctx, contract) diff --git a/pkg/client/resources.go b/pkg/client/resources.go index 2b05e761..c3a9dee0 100644 --- a/pkg/client/resources.go +++ b/pkg/client/resources.go @@ -18,7 +18,7 @@ func (g *GrpcClient) GetAccountResource(addr string) (*api.AccountResourceMessag return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.GetAccountResource(ctx, account) @@ -30,7 +30,7 @@ func (g *GrpcClient) GetDelegatedResources(address string) ([]*api.DelegatedReso if err != nil { return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() ai, err := g.Client.GetDelegatedResourceAccountIndex(ctx, GetMessageBytes(addrBytes)) diff --git a/pkg/client/transfer.go b/pkg/client/transfer.go index b2a610ff..e9fb1209 100644 --- a/pkg/client/transfer.go +++ b/pkg/client/transfer.go @@ -23,7 +23,7 @@ func (g *GrpcClient) Transfer(from, toAddress string, amount int64) (*api.Transa } contract.Amount = amount - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.CreateTransaction2(ctx, contract) diff --git a/pkg/client/witnesses.go b/pkg/client/witnesses.go index 2ede3328..f272ae34 100644 --- a/pkg/client/witnesses.go +++ b/pkg/client/witnesses.go @@ -12,7 +12,7 @@ import ( // ListWitnesses return all witnesses func (g *GrpcClient) ListWitnesses() (*api.WitnessList, error) { - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() return g.Client.ListWitnesses(ctx, new(api.EmptyMessage)) @@ -29,7 +29,7 @@ func (g *GrpcClient) CreateWitness(from, urlStr string) (*api.TransactionExtenti return nil, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.CreateWitness2(ctx, contract) @@ -55,7 +55,7 @@ func (g *GrpcClient) UpdateWitness(from, urlStr string) (*api.TransactionExtenti } contract.UpdateUrl = []byte(urlStr) - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.UpdateWitness2(ctx, contract) @@ -94,7 +94,7 @@ func (g *GrpcClient) VoteWitnessAccount(from string, } } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() tx, err := g.Client.VoteWitnessAccount2(ctx, contract) @@ -117,7 +117,7 @@ func (g *GrpcClient) GetWitnessBrokerage(witness string) (float64, error) { return 0, err } - ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) defer cancel() result, err := g.Client.GetBrokerageInfo(ctx, GetMessageBytes(addr))