Skip to content

Commit

Permalink
add QueryTeeWorkEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode committed Jan 23, 2024
1 parent c6e5e85 commit 293f0af
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
39 changes: 38 additions & 1 deletion chain/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ func (c *chainClient) QueryTeeWorkerMap(puk []byte) (pattern.TeeWorkerInfo, erro

key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEWORKERMAP, owner)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWORKERMAP, err)
return data, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWORKERMAP, err)
return data, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
Expand Down Expand Up @@ -108,11 +110,13 @@ func (c *chainClient) QueryTeePodr2Puk() ([]byte, error) {

key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEPODR2PK)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEPODR2PK, err)
return nil, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEPODR2PK, err)
return nil, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
Expand All @@ -138,19 +142,21 @@ func (c *chainClient) QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) {
key := createPrefixedKey(pattern.TEEWORKER, pattern.TEEWORKERMAP)
keys, err := c.api.RPC.State.GetKeysLatest(key)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWORKERMAP, err)
return list, errors.Wrap(err, "[GetKeysLatest]")
}

set, err := c.api.RPC.State.QueryStorageAtLatest(keys)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWORKERMAP, err)
return list, errors.Wrap(err, "[QueryStorageAtLatest]")
}

for _, elem := range set {
for _, change := range elem.Changes {
var teeWorker pattern.TeeWorkerInfo
if err := codec.Decode(change.StorageData, &teeWorker); err != nil {
fmt.Println(err)
log.Println(err)
continue
}
list = append(list, teeWorker)
Expand Down Expand Up @@ -189,3 +195,34 @@ func (c *chainClient) QueryAllTeeInfo() ([]pattern.TeeInfo, error) {
}
return results, nil
}

func (c *chainClient) QueryTeeWorkEndpoint() (string, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var data types.Text

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

key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEEndpoints)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEEndpoints, err)
return "", errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEEndpoints, err)
return "", errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return "", pattern.ERR_RPC_EMPTY_VALUE
}

return string(data), nil
}
22 changes: 3 additions & 19 deletions core/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,6 @@ type Event_LeaseExpireIn24Hours struct {
}

// ------------------------TEE Worker--------------------
type Event_RegistrationTeeWorker struct {
Phase types.Phase
Acc types.AccountID
PeerId pattern.PeerId
Topics []types.Hash
}

type Event_UpdatePeerId struct {
Phase types.Phase
Acc types.AccountID
Topics []types.Hash
}

type Event_Exit struct {
Phase types.Phase
Acc types.AccountID
Expand Down Expand Up @@ -517,12 +504,9 @@ type EventRecords struct {
StorageHandler_LeaseExpireIn24Hours []Event_LeaseExpireIn24Hours

// TeeWorker
TeeWorker_RegistrationTeeWorker []Event_RegistrationTeeWorker
TeeWorker_UpdatePeerId []Event_UpdatePeerId
TeeWorker_Exit []Event_Exit
TeeWorker_MasterKeyLaunched []Event_MasterKeyLaunched
TeeWorker_KeyfairyAdded []Event_KeyfairyAdded

TeeWorker_Exit []Event_Exit
TeeWorker_MasterKeyLaunched []Event_MasterKeyLaunched
TeeWorker_KeyfairyAdded []Event_KeyfairyAdded
TeeWorker_WorkerUpdated []Event_WorkerUpdated
TeeWorker_MasterKeyRotated []Event_MasterKeyRotated
TeeWorker_MasterKeyRotationFailed []Event_MasterKeyRotationFailed
Expand Down
2 changes: 0 additions & 2 deletions core/event/eventsName.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ const (
StorageHandlerRenewalSpace = "StorageHandler.RenewalSpace"

// TeeWorker
TeeWorkerRegistrationTeeWorker = "TeeWorker.RegistrationTeeWorker"
TeeWorkerUpdatePeerId = "TeeWorker.UpdatePeerId"
TeeWorkerExit = "TeeWorker.Exit"
TeeWorkerMasterKeyLaunched = "TeeWorker.MasterKeyLaunched"
TeeWorkerKeyfairyAdded = "TeeWorker.KeyfairyAdded"
Expand Down
1 change: 1 addition & 0 deletions core/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const (
// TEEWORKER
TEEWORKERMAP = "TeeWorkerMap"
TEEPODR2PK = "TeePodr2Pk"
TEEEndpoints = "Endpoints"

// FILEBANK
FILE = "File"
Expand Down
2 changes: 2 additions & 0 deletions core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ type SDK interface {
QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error)
// QueryAllTeeInfo queries the information of all tee workers.
QueryAllTeeInfo() ([]pattern.TeeInfo, error)
// QueryTeeWorkEndpoint queries the endpoint of tee workers.
QueryTeeWorkEndpoint() (string, error)

// System

Expand Down

0 comments on commit 293f0af

Please sign in to comment.