Skip to content

Commit 128bb13

Browse files
committed
rpc: refactor QueryProof and InsertProof
1 parent fca9d11 commit 128bb13

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

rpcserver.go

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5110,17 +5110,27 @@ func (r *rpcServer) marshalUniverseProofLeaf(ctx context.Context,
51105110
func (r *rpcServer) QueryProof(ctx context.Context,
51115111
req *unirpc.UniverseKey) (*unirpc.AssetProofResponse, error) {
51125112

5113-
universeID, err := UnmarshalUniID(req.Id)
5113+
universeID, leafKey, err := unmarshalUniverseKey(req)
51145114
if err != nil {
51155115
return nil, err
51165116
}
5117-
leafKey, err := unmarshalLeafKey(req.LeafKey)
5117+
5118+
firstProof, err := r.queryProof(ctx, universeID, leafKey)
51185119
if err != nil {
51195120
return nil, err
51205121
}
51215122

5122-
rpcsLog.Tracef("[QueryProof]: fetching proof at (universeID=%v, "+
5123-
"leafKey=%x)", universeID.StringForLog(), leafKey.UniverseKey())
5123+
return r.marshalUniverseProofLeaf(ctx, req, firstProof)
5124+
}
5125+
5126+
// queryProof attempts to query for an issuance or transfer proof for a given
5127+
// asset based on its UniverseKey. A UniverseKey is composed of the Universe ID
5128+
// (asset_id/group_key) and also a leaf key (outpoint || script_key).
5129+
func (r *rpcServer) queryProof(ctx context.Context, uniID universe.Identifier,
5130+
leafKey universe.LeafKey) (*universe.Proof, error) {
5131+
5132+
rpcsLog.Tracef("[QueryProof]: fetching proof at (uniID=%v, "+
5133+
"leafKey=%x)", uniID.StringForLog(), leafKey.UniverseKey())
51245134

51255135
// Retrieve proof export config for the given universe.
51265136
syncConfigs, err := r.cfg.UniverseFederation.QuerySyncConfigs(ctx)
@@ -5130,29 +5140,29 @@ func (r *rpcServer) QueryProof(ctx context.Context,
51305140

51315141
var candidateIDs []universe.Identifier
51325142

5133-
if universeID.ProofType == universe.ProofTypeUnspecified {
5143+
if uniID.ProofType == universe.ProofTypeUnspecified {
51345144
// If the proof type is unspecified, then we'll attempt to
51355145
// retrieve both the issuance and transfer proofs. We gather the
51365146
// corresponding universe IDs into a candidate set.
5137-
universeID.ProofType = universe.ProofTypeIssuance
5138-
if syncConfigs.IsSyncExportEnabled(universeID) {
5139-
candidateIDs = append(candidateIDs, universeID)
5147+
uniID.ProofType = universe.ProofTypeIssuance
5148+
if syncConfigs.IsSyncExportEnabled(uniID) {
5149+
candidateIDs = append(candidateIDs, uniID)
51405150
}
51415151

5142-
universeID.ProofType = universe.ProofTypeTransfer
5143-
if syncConfigs.IsSyncExportEnabled(universeID) {
5144-
candidateIDs = append(candidateIDs, universeID)
5152+
uniID.ProofType = universe.ProofTypeTransfer
5153+
if syncConfigs.IsSyncExportEnabled(uniID) {
5154+
candidateIDs = append(candidateIDs, uniID)
51455155
}
51465156
} else {
51475157
// Otherwise, we'll only attempt to retrieve the proof for the
51485158
// specified proof type. But first we'll check that proof export
51495159
// is enabled for the given universe.
5150-
if !syncConfigs.IsSyncExportEnabled(universeID) {
5160+
if !syncConfigs.IsSyncExportEnabled(uniID) {
51515161
return nil, fmt.Errorf("proof export is disabled for " +
51525162
"the given universe")
51535163
}
51545164

5155-
candidateIDs = append(candidateIDs, universeID)
5165+
candidateIDs = append(candidateIDs, uniID)
51565166
}
51575167

51585168
// If no candidate IDs were applicable then our config must have
@@ -5183,9 +5193,8 @@ func (r *rpcServer) QueryProof(ctx context.Context,
51835193
}
51845194

51855195
rpcsLog.Debugf("[QueryProof]: error querying for "+
5186-
"proof at (universeID=%v, leafKey=%x)",
5187-
universeID.StringForLog(),
5188-
leafKey.UniverseKey())
5196+
"proof at (uniID=%v, leafKey=%x)",
5197+
uniID.StringForLog(), leafKey.UniverseKey())
51895198
return nil, err
51905199
}
51915200

@@ -5203,11 +5212,37 @@ func (r *rpcServer) QueryProof(ctx context.Context,
52035212
// not be fully specified
52045213
firstProof := proofs[0]
52055214

5206-
rpcsLog.Tracef("[QueryProof]: found proof at (universeID=%v, "+
5207-
"leafKey=%x)", universeID.StringForLog(),
5208-
leafKey.UniverseKey())
5215+
rpcsLog.Tracef("[QueryProof]: found proof at (uniID=%v, "+
5216+
"leafKey=%x)", uniID.StringForLog(), leafKey.UniverseKey())
52095217

5210-
return r.marshalUniverseProofLeaf(ctx, req, firstProof)
5218+
return firstProof, nil
5219+
}
5220+
5221+
// unmarshalUniverseKey unmarshals a universe key from the RPC form.
5222+
func unmarshalUniverseKey(key *unirpc.UniverseKey) (universe.Identifier,
5223+
universe.LeafKey, error) {
5224+
5225+
var (
5226+
uniID = universe.Identifier{}
5227+
uniKey = universe.LeafKey{}
5228+
err error
5229+
)
5230+
5231+
if key == nil {
5232+
return uniID, uniKey, fmt.Errorf("universe key cannot be nil")
5233+
}
5234+
5235+
uniID, err = UnmarshalUniID(key.Id)
5236+
if err != nil {
5237+
return uniID, uniKey, err
5238+
}
5239+
5240+
uniKey, err = unmarshalLeafKey(key.LeafKey)
5241+
if err != nil {
5242+
return uniID, uniKey, err
5243+
}
5244+
5245+
return uniID, uniKey, nil
52115246
}
52125247

52135248
// unmarshalAssetLeaf unmarshals an asset leaf from the RPC form.
@@ -5242,15 +5277,7 @@ func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.Leaf, error) {
52425277
func (r *rpcServer) InsertProof(ctx context.Context,
52435278
req *unirpc.AssetProof) (*unirpc.AssetProofResponse, error) {
52445279

5245-
if req.Key == nil {
5246-
return nil, fmt.Errorf("key cannot be nil")
5247-
}
5248-
5249-
universeID, err := UnmarshalUniID(req.Key.Id)
5250-
if err != nil {
5251-
return nil, err
5252-
}
5253-
leafKey, err := unmarshalLeafKey(req.Key.LeafKey)
5280+
universeID, leafKey, err := unmarshalUniverseKey(req.Key)
52545281
if err != nil {
52555282
return nil, err
52565283
}
@@ -5296,7 +5323,7 @@ func (r *rpcServer) InsertProof(ctx context.Context,
52965323
}
52975324

52985325
rpcsLog.Debugf("[InsertProof]: inserting proof at "+
5299-
"(universeID=%v, leafKey=%x)", universeID,
5326+
"(universeID=%v, leafKey=%x)", universeID.StringForLog(),
53005327
leafKey.UniverseKey())
53015328

53025329
newUniverseState, err := r.cfg.UniverseArchive.UpsertProofLeaf(

0 commit comments

Comments
 (0)