Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an gRPC endpoint for request verification #45

Merged
merged 17 commits into from
May 6, 2021

Conversation

ntchjb
Copy link
Contributor

@ntchjb ntchjb commented Mar 31, 2021

Fixed: BAND-220

When new oracle request is arrived on bandchain, yoda received the event and trying to request data from data provider. When there is a data provider that requires payment in order to access the data, the data provider should setup a server to check the request sent by yoda before providing data. The server may use this gRPC endpoint to verify the request sent by yoda to make sure that data provider will provide data to correct reporter account.

Implementation details

The gRPC endpoint is at /oracle/verify_request. It requires the following information for verification before report the data

  • Chain ID
  • A validator address
  • A request ID
  • An data source's external ID
  • Public key of a reporter
  • Report's signature for the content of RequestVerification proto message.

It will verify the following logic

  • Request should not be empty
  • Provided chain ID should match validator's chain ID
  • Provided signature should be valid, which means this query request should be signed by the given reporter
  • Provided reporter should be authorized by the given validator
  • Provided request ID should exist on chain
  • Provided validator should be assigned to response to the request
  • Provided external ID should be required by the request determined by oracle script
  • Provided validator should not have reported data for the request
  • The request should not be expired

There is also CLI command provided for ease of use and testing.

bandd query oracle verify-request [chain-id] [validator-addr] [request-id] [data-source-external-id] [reporter-addr] [reporter-signature-hex] [flags]

In order to query via grpc-gateway, it can be done by sending GET request to /oracle/v1/verify_request with following parameters

  • chain_id: current chain ID
  • validator: validator account
  • request_id: request ID
  • external_id: raw request's external ID
  • reporter: reporter's public key in bech32 format
  • signature: signature of all above data in base64 format. This can be done by following example code
    // privKey is secp256k1 private key
    privKey, _, err := cosmoscrypto.UnarmorDecryptPrivKey(reporterPrivKey, reporterPrivKeyPasswd)
    requestVerification := oracletypes.NewRequestVerification(chainID, testvaladdr, types.RequestID(requestID), types.ExternalID(externalID))
    // GetSignBytes marshals the struct into JSON and sort all properties in ASC alphanumeric order
    signature, err := privKey.Sign(requestVerification.GetSignBytes())

The following example command sends HTTP GET request

curl -G -X GET \
--data-urlencode "chain_id=bandchain" \
--data-urlencode "validator=bandvaloper1p40yh3zkmhcv0ecqp3mcazy83sa57rgjde6wec" \
--data-urlencode "request_id=0" \
--data-urlencode "external_id=1" \
--data-urlencode "reporter=bandpub1addwnpepqw4dfcp7k93c3xaa6jphvrez9s6f29aaem5eqlaag8ag3xcdgndfs7cda04" \
--data-urlencode "signature=H317sxHzJllFK9X1u1KtTc8jzLzVmVcimdU0d90PzAYSfcrUgTcxkk5WAmdYaF/Ml2p3cAwIMwKN7Wt+LOn7Fw==" \
localhost:1317/oracle/v1/verify_request

Please note that in unit test, there is a method that will be deprecated and unsupported soon, begins at v0.43.0, which is Bech32ifyPubKey (also affeects MustBech32ifyPubKey). Start at v0.43.0, we need to change the way to input reporter's public key by encoding the public key using protobuf's Any type field. See cosmos/cosmos-sdk#7357 for more information

Please ensure the following requirements are met before submitting a pull request:

  • The pull request is targeted against the correct target branch
  • The pull request is linked to an issue with appropriate discussion and an accepted design OR is linked to a spec that describes the work.
  • The pull request includes a description of the implementation/work done in detail.
  • The pull request includes any and all appropriate unit/integration tests
  • You have added a relevant changelog entry to CHANGELOG_UNRELEASED.md
  • You have re-reviewed the files affected by the pull request (e.g. using the Files changed tab in the Github PR explorer)

x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
}
}
if dataSourceID == nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("no data source required by the request %d found which relates to the external data source with ID %d.", req.RequestId, req.ExternalId))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should return 502 (Internal server error). If a request existed it should have a data source for every raw requests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is another possibility that a client sends some random external ID that does not exists in the request, which causes data source ID to be nil.

x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
@ntchjb ntchjb requested a review from taobun April 1, 2021 08:04
proto/oracle/v1/query.proto Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
@ntchjb ntchjb requested a review from taobun April 7, 2021 03:11
@ntchjb ntchjb self-assigned this Apr 8, 2021
x/oracle/client/cli/query.go Outdated Show resolved Hide resolved
x/oracle/keeper/grpc_query.go Outdated Show resolved Hide resolved
@ntchjb ntchjb requested a review from taobun May 5, 2021 15:37
@taobun taobun merged commit 71c8944 into master May 6, 2021
@taobun taobun deleted the nathachai/request-verification-endpoint branch May 27, 2021 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants