Skip to content

Commit

Permalink
feat(ioctl): support znode project management commands(based #3987) (#…
Browse files Browse the repository at this point in the history
…3989)

* feat(ioctl): add znode commands configs

* feat(ioctl): add znode commands configs

* feat(ioctl): support znode message committing and querying commands(based #3986)

* chore: code fmt

* draft

* feat(ioctl): support ws project mgr commands

* feat(ioctl): support w3bstream message committing and querying commands(based #3986)

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

* feat(ioctl): ws node project management

---------

Co-authored-by: CoderZhi <[email protected]>
  • Loading branch information
saitofun and CoderZhi authored Nov 28, 2023
1 parent 10c227e commit 8a94adb
Show file tree
Hide file tree
Showing 15 changed files with 1,182 additions and 53 deletions.
96 changes: 59 additions & 37 deletions ioctl/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import (
"strings"

"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/ioctl/cmd/account"
Expand Down Expand Up @@ -212,26 +211,11 @@ func fixGasLimit(caller string, execution *action.Execution) (*action.Execution,

// SendRaw sends raw action to blockchain
func SendRaw(selp *iotextypes.Action) error {
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
_, err := SendRawAndRespond(selp)
if err != nil {
return output.NewError(output.NetworkError, "failed to connect to endpoint", err)
}
defer conn.Close()
cli := iotexapi.NewAPIServiceClient(conn)
ctx := context.Background()

jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
return err
}

request := &iotexapi.SendActionRequest{Action: selp}
if _, err = cli.SendAction(ctx, request); err != nil {
if sta, ok := status.FromError(err); ok {
return output.NewError(output.APIError, sta.Message(), nil)
}
return output.NewError(output.NetworkError, "failed to invoke SendAction api", err)
}
shash := hash.Hash256b(byteutil.Must(proto.Marshal(selp)))
txhash := hex.EncodeToString(shash[:])
message := sendMessage{Info: "Action has been sent to blockchain.", TxHash: txhash, URL: "https://"}
Expand All @@ -250,16 +234,48 @@ func SendRaw(selp *iotextypes.Action) error {
return nil
}

// SendRawAndRespond sends raw action to blockchain with response and error return
func SendRawAndRespond(selp *iotextypes.Action) (*iotexapi.SendActionResponse, error) {
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
if err != nil {
return nil, output.NewError(output.NetworkError, "failed to connect to endpoint", err)
}
defer conn.Close()
cli := iotexapi.NewAPIServiceClient(conn)
ctx := context.Background()

jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}

request := &iotexapi.SendActionRequest{Action: selp}
response, err := cli.SendAction(ctx, request)
if err != nil {
if sta, ok := status.FromError(err); ok {
return nil, output.NewError(output.APIError, sta.Message(), nil)
}
return nil, output.NewError(output.NetworkError, "failed to invoke SendAction api", err)
}
return response, nil
}

// SendAction sends signed action to blockchain
func SendAction(elp action.Envelope, signer string) error {
_, err := SendActionAndResponse(elp, signer)
return err
}

// SendActionAndResponse sends signed action to blockchain with response and error return
func SendActionAndResponse(elp action.Envelope, signer string) (*iotexapi.SendActionResponse, error) {
prvKey, err := account.PrivateKeyFromSigner(signer, _passwordFlag.Value().(string))
if err != nil {
return err
return nil, err
}

chainMeta, err := bc.GetChainMeta()
if err != nil {
return output.NewError(0, "failed to get chain meta", err)
return nil, output.NewError(0, "failed to get chain meta", err)
}
elp.SetChainID(chainMeta.GetChainID())

Expand All @@ -268,24 +284,24 @@ func SendAction(elp action.Envelope, signer string) error {
signer = addr.String()
nonce, err := nonce(signer)
if err != nil {
return output.NewError(0, "failed to get nonce ", err)
return nil, output.NewError(0, "failed to get nonce ", err)
}
elp.SetNonce(nonce)
}

sealed, err := action.Sign(elp, prvKey)
prvKey.Zero()
if err != nil {
return output.NewError(output.CryptoError, "failed to sign action", err)
return nil, output.NewError(output.CryptoError, "failed to sign action", err)
}
if err := isBalanceEnough(signer, sealed); err != nil {
return output.NewError(0, "failed to pass balance check", err) // TODO: undefined error
return nil, output.NewError(0, "failed to pass balance check", err) // TODO: undefined error
}

selp := sealed.Proto()
actionInfo, err := printActionProto(selp)
if err != nil {
return output.NewError(0, "failed to print action proto message", err)
return nil, output.NewError(0, "failed to print action proto message", err)
}

if _yesFlag.Value() == false {
Expand All @@ -295,47 +311,53 @@ func SendAction(elp action.Envelope, signer string) error {
fmt.Println(message.String())

if _, err := fmt.Scanf("%s", &confirm); err != nil {
return output.NewError(output.InputError, "failed to input yes", err)
return nil, output.NewError(output.InputError, "failed to input yes", err)
}
if !strings.EqualFold(confirm, "yes") {
output.PrintResult("quit")
return nil
return nil, nil
}
}

return SendRaw(selp)
return SendRawAndRespond(selp)
}

// Execute sends signed execution transaction to blockchain
func Execute(contract string, amount *big.Int, bytecode []byte) error {
_, err := ExecuteAndResponse(contract, amount, bytecode)
return err
}

// ExecuteAndResponse sends signed execution transaction to blockchain and with response and error return
func ExecuteAndResponse(contract string, amount *big.Int, bytecode []byte) (*iotexapi.SendActionResponse, error) {
if len(contract) == 0 && len(bytecode) == 0 {
return output.NewError(output.InputError, "failed to deploy contract with empty bytecode", nil)
return nil, output.NewError(output.InputError, "failed to deploy contract with empty bytecode", nil)
}
gasPriceRau, err := gasPriceInRau()
if err != nil {
return output.NewError(0, "failed to get gas price", err)
return nil, output.NewError(0, "failed to get gas price", err)
}
signer, err := Signer()
if err != nil {
return output.NewError(output.AddressError, "failed to get signer address", err)
return nil, output.NewError(output.AddressError, "failed to get signer address", err)
}
nonce, err := nonce(signer)
if err != nil {
return output.NewError(0, "failed to get nonce", err)
return nil, output.NewError(0, "failed to get nonce", err)
}
gasLimit := _gasLimitFlag.Value().(uint64)
tx, err := action.NewExecution(contract, nonce, amount, gasLimit, gasPriceRau, bytecode)
if err != nil || tx == nil {
return output.NewError(output.InstantiationError, "failed to make a Execution instance", err)
return nil, output.NewError(output.InstantiationError, "failed to make a Execution instance", err)
}
if gasLimit == 0 {
tx, err = fixGasLimit(signer, tx)
if err != nil || tx == nil {
return output.NewError(0, "failed to fix Execution gaslimit", err)
return nil, output.NewError(0, "failed to fix Execution gaslimit", err)
}
gasLimit = tx.GasLimit()
}
return SendAction(
return SendActionAndResponse(
(&action.EnvelopeBuilder{}).
SetNonce(nonce).
SetGasPrice(gasPriceRau).
Expand Down
7 changes: 3 additions & 4 deletions ioctl/cmd/action/actionhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import (

protoV1 "github.com/golang/protobuf/proto"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/action/protocol/staking"
"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
)

// Multi-language support
Expand Down
3 changes: 1 addition & 2 deletions ioctl/cmd/action/actionsendraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package action
import (
"encoding/hex"

"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
)
Expand Down
3 changes: 1 addition & 2 deletions ioctl/cmd/action/xrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
"math/big"
"strconv"

"github.com/spf13/cobra"

"github.com/iotexproject/iotex-address/address"
"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/ioctl/cmd/alias"
"github.com/iotexproject/iotex-core/ioctl/config"
Expand Down
3 changes: 2 additions & 1 deletion ioctl/cmd/action/xrc20const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-core/pkg/log"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/pkg/log"
)

const _abiConst = `[
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestParseOutput(t *testing.T) {
}

for _, test := range tests {
v, err := parseOutput(testAbi, test.method, test.outputs)
v, err := ParseOutput(testAbi, test.method, test.outputs)
r.NoError(err)
r.Equal(test.expectResult, fmt.Sprint(v))
}
Expand Down
3 changes: 1 addition & 2 deletions ioctl/cmd/contract/contracttestbytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ package contract
import (
"math/big"

"github.com/spf13/cobra"

"github.com/iotexproject/iotex-address/address"
"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/ioctl/cmd/action"
"github.com/iotexproject/iotex-core/ioctl/config"
Expand Down
5 changes: 2 additions & 3 deletions ioctl/cmd/contract/contracttestfunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ package contract
import (
"math/big"

"github.com/spf13/cobra"

"github.com/iotexproject/iotex-address/address"
"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/ioctl/cmd/action"
"github.com/iotexproject/iotex-core/ioctl/config"
Expand Down Expand Up @@ -80,7 +79,7 @@ func contractTestFunction(args []string) error {
return err
}

result, err := parseOutput(abi, methodName, rowResult)
result, err := ParseOutput(abi, methodName, rowResult)
if err != nil {
result = rowResult
}
Expand Down
3 changes: 2 additions & 1 deletion ioctl/cmd/contract/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func parseInput(rowInput string) (map[string]interface{}, error) {
return input, nil
}

func parseOutput(targetAbi *abi.ABI, targetMethod string, result string) (string, error) {
// ParseOutput parse contract output data
func ParseOutput(targetAbi *abi.ABI, targetMethod string, result string) (string, error) {
resultBytes, err := hex.DecodeString(result)
if err != nil {
return "", output.NewError(output.ConvertError, "failed to decode result", err)
Expand Down
1 change: 1 addition & 0 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (

func init() {
WsCmd.AddCommand(wsMessage)
WsCmd.AddCommand(wsProject)

WsCmd.PersistentFlags().StringVar(
&config.ReadConfig.Endpoint, "endpoint",
Expand Down
Loading

0 comments on commit 8a94adb

Please sign in to comment.