Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ioctl): project state and operator subcommands #4024

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/iotexproject/iotex-antenna-go/v2 v2.5.1
github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d
github.com/iotexproject/iotex-proto v0.5.15-0.20231122045957-12615a5b58b2
github.com/ipfs/go-ipfs-api v0.2.0
github.com/libp2p/go-libp2p-core v0.8.5
github.com/mackerelio/go-osstat v0.2.4
github.com/miguelmota/go-ethereum-hdwallet v0.1.1
Expand Down Expand Up @@ -62,6 +63,12 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/ipfs/go-ipfs-files v0.0.8 // indirect
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
)

require (
cloud.google.com/go/compute v1.23.0 // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 h1:SKI1/fuSdodxmNNyVBR8d7X/HuLnRpvvFO0AgyQk764=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -234,6 +235,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 h1:HVTnpeuvF6Owjd5mniCL8DEXo7uYXdQEmOP4FJbV5tg=
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3/go.mod h1:p1d6YEZWvFzEh4KLyvBcVSnrfNDDvK2zfK/4x2v/4pE=
github.com/crate-crypto/go-ipa v0.0.0-20220523130400-f11357ae11c7/go.mod h1:gFnFS95y8HstDP6P9pPwzrxOOC5TRDkwbM+ao15ChAI=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -685,7 +688,11 @@ github.com/ipfs/go-ds-leveldb v0.0.1/go.mod h1:feO8V3kubwsEF22n0YRQCffeb79OOYIyk
github.com/ipfs/go-ds-leveldb v0.1.0/go.mod h1:hqAW8y4bwX5LWcCtku2rFNX3vjDZCy5LZCg+cSZvYb8=
github.com/ipfs/go-ds-leveldb v0.4.1/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s=
github.com/ipfs/go-ds-leveldb v0.4.2/go.mod h1:jpbku/YqBSsBc1qgME8BkWS4AxzF2cEu1Ii2r79Hh9s=
github.com/ipfs/go-ipfs-api v0.2.0 h1:BXRctUU8YOUOQT/jW1s56d9wLa85ntOqK6bptvCKb8c=
github.com/ipfs/go-ipfs-api v0.2.0/go.mod h1:zCTyTl+BuyvUqoSmVb8vjezCJLVTW7G/HBZbCXpTgeM=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-files v0.0.8 h1:8o0oFJkJ8UkO/ABl8T6ac6tKF3+NIpj67aAB6ZpusRg=
github.com/ipfs/go-ipfs-files v0.0.8/go.mod h1:wiN/jSG8FKyk7N0WyctKSvq3ljIa2NNTiZB55kpTdOs=
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
Expand Down Expand Up @@ -1581,6 +1588,8 @@ github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvX
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4=
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds=
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI=
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c h1:GGsyl0dZ2jJgVT+VvWBf/cNijrHRhkrTjkmp5wg7li0=
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c/go.mod h1:xxcJeBb7SIUl/Wzkz1eVKJE/CB34YNrqX2TQI6jY9zs=
github.com/whyrusleeping/timecache v0.0.0-20160911033111-cfcb2f1abfee h1:lYbXeSvJi5zk5GLKVuid9TVjS9a0OmLIDKTfoZBL6Ow=
github.com/whyrusleeping/timecache v0.0.0-20160911033111-cfcb2f1abfee/go.mod h1:m2aV4LZI4Aez7dP5PMyVKEHhUyEJ/RjmPEDOpDvudHg=
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
Expand Down Expand Up @@ -1899,6 +1908,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190316082340-a2f829d7f35f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
40 changes: 39 additions & 1 deletion ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,54 @@ var (
config.English: "W3bstream node operations",
config.Chinese: "W3bstream节点操作",
}
)

// multi-language flags descriptions
var (
_flagChainEndpointUsages = map[config.Language]string{
config.English: "set endpoint for once",
config.Chinese: "一次设置端点",
}

_flagWsEndpointUsages = map[config.Language]string{
config.English: "set w3bsteram endpoint for once",
config.Chinese: "一次设置w3bstream端点",
}
_flagProjectIDUsages = map[config.Language]string{
config.English: "project id",
config.Chinese: "项目ID",
}
_flagProjectVersionUsages = map[config.Language]string{
config.English: "project version",
config.Chinese: "项目版本",
}
_flagSendDataUsages = map[config.Language]string{
config.English: "send data",
config.Chinese: "要发送的数据",
}
_flagVMTypeUsages = map[config.Language]string{
config.English: "vm type, support risc0, halo2",
config.Chinese: "虚拟机类型,目前支持risc0和halo2",
}
_flagCodeFileUsages = map[config.Language]string{
config.English: "code file",
config.Chinese: "代码文件",
}
_flagConfFileUsages = map[config.Language]string{
config.English: "conf file",
config.Chinese: "配置文件",
}
_flagExpandParamUsages = map[config.Language]string{
config.English: "expand param, if you use risc0 vm, need it.",
config.Chinese: "扩展参数,risc0虚拟机需要此参数",
}
_flagMessageIDUsages = map[config.Language]string{
config.English: "message id",
config.Chinese: "消息ID",
}
_flagProjectOperatorUsages = map[config.Language]string{
config.English: "project operator",
config.Chinese: "项目操作者账户地址",
}
)

func init() {
Expand Down
17 changes: 0 additions & 17 deletions ioctl/cmd/ws/wscodeconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@ var (
config.English: "convert zkp code to hex string compressed with zlib",
config.Chinese: "将zkp代码通过zlib进行压缩之后转成hex字符串",
}

_flagVMTypeUsages = map[config.Language]string{
config.English: "vm type, support risc0, halo2",
config.Chinese: "虚拟机类型,目前支持risc0和halo2",
}
_flagCodeFileUsages = map[config.Language]string{
config.English: "code file",
config.Chinese: "代码文件",
}
_flagConfFileUsages = map[config.Language]string{
config.English: "conf file",
config.Chinese: "配置文件",
}
_flagExpandParamUsages = map[config.Language]string{
config.English: "expand param, if you use risc0 vm, need it.",
config.Chinese: "扩展参数,risc0虚拟机需要此参数",
}
)

func init() {
Expand Down
5 changes: 0 additions & 5 deletions ioctl/cmd/ws/wsmessagequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ var (
config.English: "query message status from w3bstream",
config.Chinese: "向w3bstream查询消息状态",
}

_flagMessageIDUsages = map[config.Language]string{
config.English: "message id",
config.Chinese: "消息ID",
}
)

func init() {
Expand Down
13 changes: 0 additions & 13 deletions ioctl/cmd/ws/wsmessagesend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,6 @@ var (
config.English: "send message to w3bstream for zk proofing",
config.Chinese: "向w3bstream发送消息请求zk证明",
}

_flagProjectIDUsages = map[config.Language]string{
config.English: "project id",
config.Chinese: "项目ID",
}
_flagProjectVersionUsages = map[config.Language]string{
config.English: "project version",
config.Chinese: "项目版本",
}
_flagSendDataUsages = map[config.Language]string{
config.English: "send data",
config.Chinese: "要发送的数据",
}
)

func init() {
Expand Down
81 changes: 77 additions & 4 deletions ioctl/cmd/ws/wsproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"context"
_ "embed" // import ws project ABI
"encoding/hex"
"fmt"
"os"
"reflect"
"time"

"github.com/cenkalti/backoff"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
shell "github.com/ipfs/go-ipfs-api"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -49,13 +53,27 @@ var (

//go:embed wsproject.json
wsProjectRegisterContractJSONABI []byte
wsProjectIPFSEndpoint string
wsProjectIPFSGatewayEndpoint string
)

// Errors
var (
errProjectConfigHashUnmatched = errors.New("project config hash unmatched")
errProjectConfigReadFailed = errors.New("failed to read project config file")
errUploadProjectConfigFailed = errors.New("failed to upload project config file")
)

// Constants
const (
createWsProjectFuncName = "createProject"
createWsProjectEventName = "ProjectUpserted"
updateWsProjectFuncName = "updateProject"
queryWsProjectFuncName = "projects"
createWsProjectEventName = "ProjectUpserted"
createWsProjectFuncName = "createProject"
startWsProjectFuncName = "unpauseProject"
stopWsProjectFuncName = "pauseProject"
updateWsProjectFuncName = "updateProject"
queryWsProjectFuncName = "projects"
addProjectOperatorFuncName = "addOperator"
delProjectOperatorFuncName = "removeOperator"
)

func init() {
Expand All @@ -68,6 +86,10 @@ func init() {
wsProject.AddCommand(wsProjectCreate)
wsProject.AddCommand(wsProjectUpdate)
wsProject.AddCommand(wsProjectQuery)
wsProject.AddCommand(wsProjectStart)
wsProject.AddCommand(wsProjectStop)
wsProject.AddCommand(wsProjectAddOperator)
wsProject.AddCommand(wsProjectDelOperator)

wsProject.PersistentFlags().StringVarP(
&wsProjectRegisterContractAddress,
Expand All @@ -77,6 +99,11 @@ func init() {
config.TranslateInLang(_flagProjectRegisterContractAddressUsages, config.UILanguage),
)
_ = wsProject.MarkFlagRequired("contract-address")

if wsProjectIPFSEndpoint == "" {
wsProjectIPFSEndpoint = "ipfs.mainnet.iotex.io"
wsProjectIPFSGatewayEndpoint = "https://ipfs.io"
}
}

func convertStringToAbiBytes32(hash string) (interface{}, error) {
Expand Down Expand Up @@ -178,3 +205,49 @@ func getEventInputsByName(logs []*iotextypes.Log, eventName string) (map[string]

return inputs, nil
}

// upload content to endpoint, returns fetch url, content hash and error
func upload(endpoint string, filename, hashstr string) (url string, hash256b *hash.Hash256, err error) {
// read file content
var content []byte
content, err = os.ReadFile(filename)
if err != nil {
err = errors.Wrap(err, errProjectConfigReadFailed.Error())
return
}

// calculate and validate hash
hash256b = new(hash.Hash256)
*hash256b = hash.Hash256b(content)
if hashstr != "" {
var hashInput hash.Hash256
hashInput, err = hash.HexStringToHash256(hashstr)
if err != nil {
return
}
if hashInput != *hash256b {
err = errProjectConfigHashUnmatched
return
}
}

// upload content to ipfs endpoint
var (
sh = shell.NewShell(endpoint)
cid string
)
cid, err = sh.Add(bytes.NewReader(content))
if err != nil {
return "", nil, errors.Wrap(err, errUploadProjectConfigFailed.Error())
}

err = sh.Pin(cid)
if err != nil {
return "", nil, errors.Wrap(err, errUploadProjectConfigFailed.Error())
}

// generate fetch url
url = fmt.Sprintf("%s/ipfs/%s", wsProjectIPFSGatewayEndpoint, cid)

return
}
Loading