-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/runtime/registry: Refactor runtime host handler
- Loading branch information
Showing
2 changed files
with
140 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common/cbor" | ||
"github.com/oasisprotocol/oasis-core/go/runtime/host/protocol" | ||
runtimeKeymanager "github.com/oasisprotocol/oasis-core/go/runtime/keymanager/api" | ||
storage "github.com/oasisprotocol/oasis-core/go/storage/api" | ||
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/syncer" | ||
) | ||
|
||
func handlerHostRPCCallRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostRPCCallRequest) (*protocol.HostRPCCallResponse, error) { | ||
switch rq.Endpoint { | ||
case runtimeKeymanager.EnclaveRPCEndpoint: | ||
// Call into the remote key manager. | ||
kmCli, err := h.env.GetKeyManagerClient(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := kmCli.CallEnclave(ctx, rq.Request, rq.PeerFeedback) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &protocol.HostRPCCallResponse{ | ||
Response: cbor.FixSliceForSerde(res), | ||
}, nil | ||
default: | ||
return nil, errEndpointNotSupported | ||
} | ||
} | ||
|
||
func handlerHostStorageSyncRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostStorageSyncRequest) (*protocol.HostStorageSyncResponse, error) { | ||
var rs syncer.ReadSyncer | ||
switch rq.Endpoint { | ||
case protocol.HostStorageEndpointRuntime: | ||
// Runtime storage. | ||
rs = h.runtime.Storage() | ||
case protocol.HostStorageEndpointConsensus: | ||
// Consensus state storage. | ||
rs = h.consensus.State() | ||
default: | ||
return nil, errEndpointNotSupported | ||
} | ||
|
||
var rsp *storage.ProofResponse | ||
var err error | ||
switch { | ||
case rq.SyncGet != nil: | ||
rsp, err = rs.SyncGet(ctx, rq.SyncGet) | ||
case rq.SyncGetPrefixes != nil: | ||
rsp, err = rs.SyncGetPrefixes(ctx, rq.SyncGetPrefixes) | ||
case rq.SyncIterate != nil: | ||
rsp, err = rs.SyncIterate(ctx, rq.SyncIterate) | ||
default: | ||
return nil, errMethodNotSupported | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protocol.HostStorageSyncResponse{ProofResponse: rsp}, nil | ||
} | ||
|
||
func handlerHostLocalStorageGetRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostLocalStorageGetRequest) (*protocol.HostLocalStorageGetResponse, error) { | ||
value, err := h.runtime.LocalStorage().Get(rq.Key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &protocol.HostLocalStorageGetResponse{Value: value}, nil | ||
} | ||
|
||
func handlerHostLocalStorageSetRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostLocalStorageSetRequest) (*protocol.Empty, error) { | ||
if err := h.runtime.LocalStorage().Set(rq.Key, rq.Value); err != nil { | ||
return nil, err | ||
} | ||
return &protocol.Empty{}, nil | ||
} | ||
|
||
func handlerHostFetchConsensusBlockRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostFetchConsensusBlockRequest) (*protocol.HostFetchConsensusBlockResponse, error) { | ||
lb, err := h.consensus.GetLightBlock(ctx, int64(rq.Height)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &protocol.HostFetchConsensusBlockResponse{ | ||
Block: *lb, | ||
}, nil | ||
} | ||
|
||
func handlerHostFetchGenesisHeightRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostFetchGenesisHeightRequest) (*protocol.HostFetchGenesisHeightResponse, error) { | ||
doc, err := h.consensus.GetGenesisDocument(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &protocol.HostFetchGenesisHeightResponse{ | ||
Height: uint64(doc.Height), | ||
}, nil | ||
} | ||
|
||
func handlerHostFetchTxBatchRequest(ctx context.Context, h *runtimeHostHandler, rq *protocol.HostFetchTxBatchRequest) (*protocol.HostFetchTxBatchResponse, error) { | ||
txPool, err := h.env.GetTxPool(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
batch := txPool.GetSchedulingExtra(rq.Offset, rq.Limit) | ||
raw := make([][]byte, 0, len(batch)) | ||
for _, tx := range batch { | ||
raw = append(raw, tx.Raw()) | ||
} | ||
|
||
return &protocol.HostFetchTxBatchResponse{ | ||
Batch: raw, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters