Skip to content

Commit

Permalink
add QueryTeeInfo (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Nov 15, 2023
1 parent 9525496 commit 3c3a2c9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
55 changes: 47 additions & 8 deletions chain/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@ import (
"github.com/pkg/errors"
)

func (c *chainClient) QueryTeeInfo(puk []byte) (pattern.TeeWorkerInfo, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var data pattern.TeeWorkerInfo

if !c.GetChainState() {
return data, pattern.ERR_RPC_CONNECTION
}

acc, err := types.NewAccountID(puk)
if err != nil {
return data, errors.Wrap(err, "[NewAccountID]")
}

owner, err := codec.Encode(*acc)
if err != nil {
return data, errors.Wrap(err, "[EncodeToBytes]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEWORKERMAP, owner)
if err != nil {
return data, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
return data, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return data, pattern.ERR_RPC_EMPTY_VALUE
}

return data, nil
}

func (c *chainClient) QueryTeePodr2Puk() ([]byte, error) {
defer func() {
if err := recover(); err != nil {
Expand Down Expand Up @@ -84,7 +123,7 @@ func (c *chainClient) QueryTeeInfoList() ([]pattern.TeeWorkerInfo, error) {
return list, nil
}

func (c *chainClient) QueryTeePeerID(puk []byte) ([]byte, error) {
func (c *chainClient) QueryTeeEndPoint(puk []byte) (string, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
Expand All @@ -94,33 +133,33 @@ func (c *chainClient) QueryTeePeerID(puk []byte) ([]byte, error) {
var data pattern.TeeWorkerInfo

if !c.GetChainState() {
return nil, pattern.ERR_RPC_CONNECTION
return "", pattern.ERR_RPC_CONNECTION
}

acc, err := types.NewAccountID(puk)
if err != nil {
return nil, errors.Wrap(err, "[NewAccountID]")
return "", errors.Wrap(err, "[NewAccountID]")
}

owner, err := codec.Encode(*acc)
if err != nil {
return nil, errors.Wrap(err, "[EncodeToBytes]")
return "", errors.Wrap(err, "[EncodeToBytes]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEWORKERMAP, owner)
if err != nil {
return nil, errors.Wrap(err, "[CreateStorageKey]")
return "", errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
return nil, errors.Wrap(err, "[GetStorageLatest]")
return "", errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return nil, pattern.ERR_RPC_EMPTY_VALUE
return "", pattern.ERR_RPC_EMPTY_VALUE
}

return []byte(string(data.PeerId[:])), nil
return string(data.EndPoint[:]), nil
}

func (c *chainClient) QueryTeeWorkerList() ([]pattern.TeeWorkerSt, error) {
Expand Down
6 changes: 4 additions & 2 deletions core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ type SDK interface {

// TeeWorker-State

// QueryTeeInfo queries the information of the Tee worker.
QueryTeeInfo(puk []byte) (pattern.TeeWorkerInfo, error)
// QueryTeePodr2Puk queries the public key of the TEE.
QueryTeePodr2Puk() ([]byte, error)
// QueryTeePeerID queries the peerid of the Tee worker.
QueryTeePeerID(puk []byte) ([]byte, error)
// QueryTeeEndPoint queries the end-point of the Tee worker.
QueryTeeEndPoint(puk []byte) (string, error)
// QueryTeeInfoList queries the information of all tee workers.
QueryTeeInfoList() ([]pattern.TeeWorkerInfo, error)
QueryTeeWorkerList() ([]pattern.TeeWorkerSt, error)
Expand Down

0 comments on commit 3c3a2c9

Please sign in to comment.