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 Sep 28, 2023
1 parent e9990fe commit 3cb9b8e
Show file tree
Hide file tree
Showing 6 changed files with 1,027 additions and 196 deletions.
127 changes: 127 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3204,6 +3204,105 @@ 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) {

// Update general sync config if set.
if req.GeneralSyncConfig != nil {
err := r.cfg.FederationDB.SetFederationGeneralSyncConfig(
ctx, req.GeneralSyncConfig.OnlySyncIssuanceProofs,
)
if err != nil {
return nil, fmt.Errorf("unable to set general sync "+
"config: %w", err)
}
}

// Update asset (asset/asset group) specific sync configs.
for i := range req.AssetSyncConfig {
assetSyncConfig := req.AssetSyncConfig[i]

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

var uniIDBuf bytes.Buffer
if err := uniID.Encode(&uniIDBuf); err != nil {
return nil, fmt.Errorf("unable to encode universe id: %w",
err)
}
uniIDBytes := uniIDBuf.Bytes()

config := tapdb.UpsertFedUniSyncConfigParams{
UniverseID: uniIDBytes,
Exclude: assetSyncConfig.Exclude,
OnlySyncIssuanceProofs: assetSyncConfig.OnlySyncIssuanceProofs,
}
err = r.cfg.FederationDB.UpsertFederationUniSyncConfig(
ctx, config,
)
if err != nil {
return nil, fmt.Errorf("unable to set universe specific "+
"federation sync config: %w", err)
}
}

return &unirpc.SetFederationSyncConfigResponse{}, nil
}

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

// Obtain the general sync config and marshal into the RPC form.
generalOnlySyncIssuanceProofs, err :=
r.cfg.FederationDB.QueryFederationGeneralSyncConfig(ctx)
if err != nil {
return nil, fmt.Errorf("unable to query general federation "+
"sync config: %w", err)
}

generalConfigRPC := unirpc.GeneralFederationSyncConfig{
OnlySyncIssuanceProofs: generalOnlySyncIssuanceProofs,
}

// Obtain the universe specific federation sync configs then marshal
// into the RPC form.
uniSyncConfigs, err :=
r.cfg.FederationDB.QueryFederationUniSyncConfigs(ctx)
if err != nil {
return nil, fmt.Errorf("unable to query universe specific "+
"federation sync config(s): %w", err)
}

uniSyncConfigRPCs := make(
[]*unirpc.AssetFederationSyncConfig, len(uniSyncConfigs),
)
for i := range uniSyncConfigs {
uniSyncConfig := uniSyncConfigs[i]
uniSyncConfigRPC, err :=
MarshalAssetFederationSyncConfig(uniSyncConfig)
if err != nil {
return nil, fmt.Errorf("unable to marshal universe "+
"specific federation sync config: %w", err)
}
uniSyncConfigRPCs[i] = uniSyncConfigRPC
}

return &unirpc.QueryFederationSyncConfigResponse{
GeneralSyncConfig: &generalConfigRPC,
AssetSyncConfigs: uniSyncConfigRPCs,
}, 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 @@ -3499,3 +3598,31 @@ func (r *rpcServer) RemoveUTXOLease(ctx context.Context,

return &wrpc.RemoveUTXOLeaseResponse{}, nil
}

// MarshalAssetFederationSyncConfig returns an RPC ready asset specific
// federation sync config.
func MarshalAssetFederationSyncConfig(
config tapdb.FedUniSyncConfigs) (*unirpc.AssetFederationSyncConfig,
error) {

// Decode universe ID from the database.
uniID := &universe.Identifier{}
if err := uniID.Decode(bytes.NewReader(config.UniverseID)); err != nil {
return nil, fmt.Errorf("unable to decode universe ID: %w", err)
}

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

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

return &unirpc.AssetFederationSyncConfig{
Id: uniIdRPC,
Exclude: config.Exclude,
OnlySyncIssuanceProofs: config.OnlySyncIssuanceProofs,
}, nil
}
Loading

0 comments on commit 3cb9b8e

Please sign in to comment.