Skip to content

Commit

Permalink
rpc: add query and set RPC endpoints for federation sync config
Browse files Browse the repository at this point in the history
Add RPC endpoints for setting and querying for federation sync config:
* SetFederationSyncConfig
* QueryFederationSyncConfig
  • Loading branch information
ffranr committed Oct 10, 2023
1 parent 01ec302 commit 2ef0034
Show file tree
Hide file tree
Showing 8 changed files with 1,352 additions and 202 deletions.
157 changes: 157 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,26 @@ func UnmarshalUniProofType(rpcType unirpc.ProofType) (universe.ProofType,
}
}

// unmarshalAssetSyncConfig parses the RPC asset sync config into the native
// counterpart.
func unmarshalAssetSyncConfig(
config *unirpc.AssetFederationSyncConfig) (*universe.FedUniSyncConfig,
error) {

// Parse the universe ID from the RPC form.
uniID, err := unmarshalUniID(config.Id)
if err != nil {
return nil, fmt.Errorf("unable to parse universe id: %w",
err)
}

return &universe.FedUniSyncConfig{
UniverseID: uniID,
AllowSyncInsert: config.AllowSyncInsert,
AllowSyncExport: config.AllowSyncExport,
}, nil
}

// unmarshalUniID parses the RPC universe ID into the native counterpart.
func unmarshalUniID(rpcID *unirpc.ID) (universe.Identifier, error) {
// Unmarshal the proof type.
Expand Down Expand Up @@ -3458,6 +3478,113 @@ func (r *rpcServer) DeleteFederationServer(ctx context.Context,
return &unirpc.DeleteFederationServerResponse{}, nil
}

// SetFederationSyncConfig sets the configuration of the universe federation
// sync.
func (r *rpcServer) SetFederationSyncConfig(ctx context.Context,
req *unirpc.SetFederationSyncConfigRequest) (
*unirpc.SetFederationSyncConfigResponse, error) {

// Unmarshal global sync configs.
globalSyncConfig := make(
[]*universe.FedGlobalSyncConfig, len(req.GlobalSyncConfigs),
)
for i := range req.GlobalSyncConfigs {
config := req.GlobalSyncConfigs[i]

proofType, err := UnmarshalUniProofType(config.ProofType)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal "+
"proof type: %w", err)
}

globalSyncConfig[i] = &universe.FedGlobalSyncConfig{
ProofType: proofType,
AllowSyncInsert: config.AllowSyncInsert,
AllowSyncExport: config.AllowSyncExport,
}
}

// Unmarshal asset (asset/asset group) specific sync configs.
assetSyncConfigs := make(
[]*universe.FedUniSyncConfig, len(req.AssetSyncConfigs),
)
for i := range req.AssetSyncConfigs {
assetSyncConfig := req.AssetSyncConfigs[i]
config, err := unmarshalAssetSyncConfig(assetSyncConfig)
if err != nil {
return nil, fmt.Errorf("unable to parse asset sync "+
"config: %w", err)
}

assetSyncConfigs[i] = config
}

// Update asset (asset/asset group) specific sync configs.
err := r.cfg.FederationDB.UpsertFederationSyncConfig(
ctx, globalSyncConfig, assetSyncConfigs,
)
if err != nil {
return nil, fmt.Errorf("unable to set federation sync "+
"config: %w", err)
}

return &unirpc.SetFederationSyncConfigResponse{}, nil
}

// QueryFederationSyncConfig queries the universe federation sync configuration
// settings.
func (r *rpcServer) QueryFederationSyncConfig(ctx context.Context,
_ *unirpc.QueryFederationSyncConfigRequest,
) (*unirpc.QueryFederationSyncConfigResponse, error) {

// Obtain the general and universe specific federation sync configs.
queryFedSyncConfigs := r.cfg.FederationDB.QueryFederationSyncConfigs
globalConfigs, uniSyncConfigs, err := queryFedSyncConfigs(ctx)
if err != nil {
return nil, fmt.Errorf("unable to query federation sync "+
"config(s): %w", err)
}

// Marshal the general sync config into the RPC form.
globalConfigRPC := make(
[]*unirpc.GlobalFederationSyncConfig, len(globalConfigs),
)
for i := range globalConfigs {
globalConfig := globalConfigs[i]

proofTypeRpc, err := MarshalUniProofType(globalConfig.ProofType)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal "+
"proof type: %w", err)
}

globalConfigRPC[i] = &unirpc.GlobalFederationSyncConfig{
ProofType: proofTypeRpc,
AllowSyncInsert: globalConfig.AllowSyncInsert,
AllowSyncExport: globalConfig.AllowSyncExport,
}
}

// Marshal universe specific sync configs into the RPC form.
uniConfigRPCs := make(
[]*unirpc.AssetFederationSyncConfig, len(uniSyncConfigs),
)
for i := range uniSyncConfigs {
uniSyncConfig := uniSyncConfigs[i]
uniConfigRPC, err := MarshalAssetFedSyncCfg(*uniSyncConfig)
if err != nil {
return nil, fmt.Errorf("unable to marshal universe "+
"specific federation sync config: %w", err)
}
uniConfigRPCs[i] = uniConfigRPC
}

return &unirpc.QueryFederationSyncConfigResponse{
GlobalSyncConfigs: globalConfigRPC,
AssetSyncConfigs: uniConfigRPCs,
}, nil
}

// ProveAssetOwnership creates an ownership proof embedded in an asset
// transition proof. That ownership proof is a signed virtual transaction
// spending the asset with a valid witness to prove the prover owns the keys
Expand Down Expand Up @@ -3768,3 +3895,33 @@ func unmarshalMetaType(rpcMeta taprpc.AssetMetaType) (proof.MetaType, error) {
return 0, fmt.Errorf("unknown meta type: %v", rpcMeta)
}
}

// MarshalAssetFedSyncCfg returns an RPC ready asset specific federation sync
// config.
func MarshalAssetFedSyncCfg(
config universe.FedUniSyncConfig) (*unirpc.AssetFederationSyncConfig,
error) {

// Marshal universe ID into the RPC form.
uniID := config.UniverseID
assetIDBytes := uniID.AssetID[:]

var groupKeyBytes []byte
if uniID.GroupKey != nil {
groupKeyBytes = uniID.GroupKey.SerializeCompressed()
}
uniIdRPC := unirpc.MarshalUniverseID(assetIDBytes, groupKeyBytes)

// Marshal proof type.
proofTypeRpc, err := MarshalUniProofType(uniID.ProofType)
if err != nil {
return nil, fmt.Errorf("unable to marshal proof type: %w", err)
}
uniIdRPC.ProofType = proofTypeRpc

return &unirpc.AssetFederationSyncConfig{
Id: uniIdRPC,
AllowSyncInsert: config.AllowSyncInsert,
AllowSyncExport: config.AllowSyncExport,
}, nil
}
Loading

0 comments on commit 2ef0034

Please sign in to comment.