Skip to content

Commit

Permalink
chore: update exposed db object Evidence in x/finality module (#208)
Browse files Browse the repository at this point in the history
Part closes: babylonlabs-io/pm#72

Currently some of the objects in the babylon apis are exposed and need
to be handled correctly seen in
babylonlabs-io/pm#72

This PR handles the exposed objects in `x/finality`

Specifically the `Evidence` object.
  • Loading branch information
samricotta authored Oct 18, 2024
1 parent f418d1f commit c836041
Show file tree
Hide file tree
Showing 5 changed files with 666 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ to total bonded sat
* [#201](https://github.com/babylonlabs-io/babylon/pull/201) Adjusted handling of `ValidatorWithBlsKey` in checkpoint keeper queries to improve API security.
* [#202](https://github.com/babylonlabs-io/babylon/pull/202) Adjusted handling of `FinalityProviderWithMeta` in btcstaking keeper queries to improve API security.
* [#203](https://github.com/babylonlabs-io/babylon/pull/203) Adjusted handling of `RewardGauge` in incentive keeper queries to improve API security.
* [#208](https://github.com/babylonlabs-io/babylon/pull/208) Adjusted handling of `Evidence` in finality keeper queries to improve API security.

### State Machine Breaking

Expand Down
27 changes: 25 additions & 2 deletions proto/babylon/finality/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,33 @@ message QueryEvidenceRequest {
string fp_btc_pk_hex = 1;
}

// Evidence is the evidence that a finality provider has signed finality
// signatures with correct public randomness on two conflicting Babylon headers
message EvidenceResponse {
// fp_btc_pk_hex is the BTC PK of the finality provider that casts this vote
string fp_btc_pk_hex = 1;
// block_height is the height of the conflicting blocks
uint64 block_height = 2;
// pub_rand is the public randomness the finality provider has committed to
bytes pub_rand = 3 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.SchnorrPubRand" ];
// canonical_app_hash is the AppHash of the canonical block
bytes canonical_app_hash = 4;
// fork_app_hash is the AppHash of the fork block
bytes fork_app_hash = 5;
// canonical_finality_sig is the finality signature to the canonical block
// where finality signature is an EOTS signature, i.e.,
// the `s` in a Schnorr signature `(r, s)`
// `r` is the public randomness that is already committed by the finality provider
bytes canonical_finality_sig = 6 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.SchnorrEOTSSig" ];
// fork_finality_sig is the finality signature to the fork block
// where finality signature is an EOTS signature
bytes fork_finality_sig = 7 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.SchnorrEOTSSig" ];
}

// QueryEvidenceResponse is the response type for the
// Query/Evidence RPC method.
message QueryEvidenceResponse {
Evidence evidence = 1;
EvidenceResponse evidence = 1;
}

// QueryListEvidencesRequest is the request type for the
Expand All @@ -217,7 +240,7 @@ message QueryListEvidencesRequest {
// Query/ListEvidences RPC method.
message QueryListEvidencesResponse {
// blocks is the list of evidences
repeated Evidence evidences = 1;
repeated EvidenceResponse evidences = 1;

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
Expand Down
25 changes: 23 additions & 2 deletions x/finality/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (k Keeper) Evidence(ctx context.Context, req *types.QueryEvidenceRequest) (
}

resp := &types.QueryEvidenceResponse{
Evidence: evidence,
Evidence: convertToEvidenceResponse(evidence),
}
return resp, nil
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (k Keeper) ListEvidences(ctx context.Context, req *types.QueryListEvidences
}

resp := &types.QueryListEvidencesResponse{
Evidences: evidences,
Evidences: convertToEvidenceListResponse(evidences),
Pagination: pageRes,
}
return resp, nil
Expand Down Expand Up @@ -290,3 +290,24 @@ func convertToSigningInfosResponse(signInfos []types.FinalityProviderSigningInfo
}
return response
}

func convertToEvidenceResponse(evidence *types.Evidence) *types.EvidenceResponse {
return &types.EvidenceResponse{
FpBtcPkHex: evidence.FpBtcPk.MarshalHex(),
BlockHeight: evidence.BlockHeight,
PubRand: evidence.PubRand,
CanonicalAppHash: evidence.CanonicalAppHash,
ForkAppHash: evidence.ForkAppHash,
CanonicalFinalitySig: evidence.CanonicalFinalitySig,
ForkFinalitySig: evidence.ForkFinalitySig,
}
}

func convertToEvidenceListResponse(evidences []*types.Evidence) []*types.EvidenceResponse {
response := make([]*types.EvidenceResponse, len(evidences))
for i, evidence := range evidences {
resp := convertToEvidenceResponse(evidence)
response[i] = resp
}
return response
}
27 changes: 22 additions & 5 deletions x/finality/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ func FuzzQueryEvidence(f *testing.F) {
require.Nil(t, evidenceResp)
} else {
require.NoError(t, err)
require.Equal(t, randomFirstSlashableEvidence, evidenceResp.Evidence)
require.True(t, evidenceResp.Evidence.IsSlashable())
require.Equal(t, randomFirstSlashableEvidence, convertToEvidence(evidenceResp.Evidence))
require.True(t, convertToEvidence(evidenceResp.Evidence).IsSlashable())
}
})
}
Expand Down Expand Up @@ -361,9 +361,10 @@ func FuzzListEvidences(f *testing.F) {
require.NoError(t, err)
require.LessOrEqual(t, len(resp.Evidences), int(limit)) // check if pagination takes effect
require.EqualValues(t, resp.Pagination.Total, numEvidences) // ensure evidences before startHeight are not included
for _, actualEvidence := range resp.Evidences {
require.Equal(t, evidences[actualEvidence.FpBtcPk.MarshalHex()].CanonicalAppHash, actualEvidence.CanonicalAppHash)
require.Equal(t, evidences[actualEvidence.FpBtcPk.MarshalHex()].ForkAppHash, actualEvidence.ForkAppHash)
for _, actualEvidenceResponse := range resp.Evidences {
actualEvidence := convertToEvidence(actualEvidenceResponse)
expectedEvidence := evidences[actualEvidenceResponse.FpBtcPkHex]
require.Equal(t, expectedEvidence, actualEvidence)
}
})
}
Expand Down Expand Up @@ -436,3 +437,19 @@ func FuzzSigningInfo(f *testing.F) {
}
})
}

func convertToEvidence(er *types.EvidenceResponse) *types.Evidence {
fpBtcPk, err := bbn.NewBIP340PubKeyFromHex(er.FpBtcPkHex)
if err != nil {
return nil
}
return &types.Evidence{
FpBtcPk: fpBtcPk,
BlockHeight: er.BlockHeight,
PubRand: er.PubRand,
CanonicalAppHash: er.CanonicalAppHash,
ForkAppHash: er.ForkAppHash,
CanonicalFinalitySig: er.CanonicalFinalitySig,
ForkFinalitySig: er.ForkFinalitySig,
}
}
Loading

0 comments on commit c836041

Please sign in to comment.