Skip to content

Commit

Permalink
Expose EnclaveID to RPC (#2149)
Browse files Browse the repository at this point in the history
* expose enclaveIDs to host HealthCheck JSON RPC
  • Loading branch information
badgersrus authored Nov 20, 2024
1 parent 0a5cf6e commit 531ee56
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 529 deletions.
3 changes: 2 additions & 1 deletion go/common/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
type StatusCode int

// Status represents the enclave's current state - whether the enclave is healthy and ready to process requests, as well
// as its latest known heads for the L1 and L2 chains
// as its latest known heads for the L1 and L2 chains and enclave ID derived from the public key
type Status struct {
StatusCode StatusCode
L1Head gethcommon.Hash
L2Head *big.Int
EnclaveID EnclaveID
}

type TxAndReceiptAndBlobs struct {
Expand Down
5 changes: 5 additions & 0 deletions go/common/host/host_healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package host

import (
"github.com/ten-protocol/go-ten/go/common"
)

// HealthStatus is an interface supported by all Services on the host
type HealthStatus interface {
OK() bool
Expand All @@ -10,6 +14,7 @@ type HealthStatus interface {
type HealthCheck struct {
OverallHealth bool
Errors []string
Enclaves []common.Status
}

// BasicErrHealthStatus is a simple health status implementation, if the ErrMsg is non-empty then OK() returns false
Expand Down
3 changes: 3 additions & 0 deletions go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ type EnclaveService interface {
// GetEnclaveClient returns an enclave client // todo (@matt) we probably don't want to expose this
GetEnclaveClient() common.Enclave

// GetEnclaveClients returns a list of all enclave clients
GetEnclaveClients() []common.Enclave

// SubmitAndBroadcastTx submits an encrypted transaction to the enclave, and broadcasts it to other hosts on the network (in particular, to the sequencer)
SubmitAndBroadcastTx(ctx context.Context, encryptedParams common.EncryptedParamsSendRawTx) (*responses.RawTx, error)

Expand Down
1,060 changes: 535 additions & 525 deletions go/common/rpc/generated/enclave.pb.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion go/common/rpc/generated/enclave.proto
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ message StatusResponse {
int32 statusCode = 1;
bytes l1Head = 2; // hash for the L1 head block in enclave's view of the canonical chain
bytes l2Head = 3; // seq number (big.Int) for the L2 head batch that the enclave has seen
SystemError systemError = 4;
bytes enclaveID = 4; // enclave ID derived from the public key
SystemError systemError = 5;
}

message AttestationRequest {}
Expand Down
2 changes: 1 addition & 1 deletion go/common/rpc/generated/enclave_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ func (e *enclaveImpl) Status(ctx context.Context) (common.Status, common.SystemE
} else {
l2HeadSeqNo = currSeqNo
}
return common.Status{StatusCode: common.Running, L1Head: l1HeadHash, L2Head: l2HeadSeqNo}, nil
enclaveID := e.enclaveKey.EnclaveID()
return common.Status{StatusCode: common.Running, L1Head: l1HeadHash, L2Head: l2HeadSeqNo, EnclaveID: enclaveID}, nil
}

// StopClient is only implemented by the RPC wrapper
Expand Down
1 change: 1 addition & 0 deletions go/enclave/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (s *RPCServer) Status(ctx context.Context, _ *generated.StatusRequest) (*ge
StatusCode: int32(status.StatusCode),
L1Head: status.L1Head.Bytes(),
L2Head: l2Head,
EnclaveID: status.EnclaveID.Bytes(),
SystemError: toRPCError(sysError),
}, nil
}
Expand Down
8 changes: 8 additions & 0 deletions go/host/enclave/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func (e *Service) GetEnclaveClient() common.Enclave {
return e.enclaveGuardians[0].GetEnclaveClient()
}

func (e *Service) GetEnclaveClients() []common.Enclave {
clients := make([]common.Enclave, len(e.enclaveGuardians))
for i, guardian := range e.enclaveGuardians {
clients[i] = guardian.enclaveClient
}
return clients
}

func (e *Service) SubmitAndBroadcastTx(ctx context.Context, encryptedParams common.EncryptedParamsSendRawTx) (*responses.RawTx, error) {
encryptedTx := common.EncryptedTx(encryptedParams)

Expand Down
17 changes: 17 additions & 0 deletions go/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,26 @@ func (h *host) HealthCheck(ctx context.Context) (*hostcommon.HealthCheck, error)
}
}

// fetch all enclaves and check status of each
enclaveStatus := make([]common.Status, 0)
for _, client := range h.services.Enclaves().GetEnclaveClients() {
status, err := client.Status(ctx)
if err != nil {
healthErrors = append(healthErrors, fmt.Sprintf("Enclave error: failed to get status - %v", err))
continue
}

enclaveStatus = append(enclaveStatus, status)

if status.StatusCode == common.Unavailable {
healthErrors = append(healthErrors, fmt.Sprintf("Enclave with ID [%s] is unavailable", status.EnclaveID))
}
}

return &hostcommon.HealthCheck{
OverallHealth: len(healthErrors) == 0,
Errors: healthErrors,
Enclaves: enclaveStatus,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions go/host/rpc/enclaverpc/enclave_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (c *Client) Status(ctx context.Context) (common.Status, common.SystemError)
StatusCode: common.StatusCode(response.StatusCode),
L1Head: gethcommon.BytesToHash(response.L1Head),
L2Head: big.NewInt(0).SetBytes(response.L2Head),
EnclaveID: common.EnclaveID(response.EnclaveID),
}, nil
}

Expand Down

0 comments on commit 531ee56

Please sign in to comment.