Skip to content

Commit 99ee121

Browse files
authored
refactor: move proof retrieval from DAPI to Drive ABCI (#2535)
1 parent d2ca233 commit 99ee121

File tree

56 files changed

+71075
-14279
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+71075
-14279
lines changed

.pnp.cjs

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dapi-grpc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
src/core/*/
22
src/platform/*/
3+
src/drive/*/

packages/dapi-grpc/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ license = "MIT"
1515

1616
[features]
1717
default = ["core", "platform", "client"]
18+
# Internal Drive endpoints. Used by DAPI
19+
drive = ["platform"]
1820
core = []
1921
platform = []
2022
# Re-export Dash Platform protobuf types as `dapi_grpc::platform::proto`
@@ -25,7 +27,7 @@ tenderdash-proto = []
2527
client = ["platform"]
2628

2729
# Build tonic server code. Includes all client features and adds server-specific dependencies.
28-
server = ["platform", "tenderdash-proto/server", "client"]
30+
server = ["platform", "tenderdash-proto/server", "client", "drive"]
2931

3032
serde = ["dep:serde", "dep:serde_bytes", "tenderdash-proto/serde"]
3133
mocks = ["serde", "dep:serde_json"]

packages/dapi-grpc/build.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ fn generate_code(typ: ImplType) {
4747
.generate()
4848
.expect("generate platform proto");
4949

50+
let drive = MappingConfig::new(
51+
PathBuf::from("protos/drive/v0/drive.proto"),
52+
PathBuf::from("src/drive"),
53+
&typ,
54+
);
55+
56+
configure_drive(drive)
57+
.generate()
58+
.expect("generate platform proto");
59+
5060
println!("cargo:rerun-if-changed=./protos");
5161
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_SERDE");
5262
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ARCH");
@@ -63,7 +73,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
6373
// Derive features for versioned messages
6474
//
6575
// "GetConsensusParamsRequest" is excluded as this message does not support proofs
66-
const VERSIONED_REQUESTS: [&str; 40] = [
76+
const VERSIONED_REQUESTS: [&str; 39] = [
6777
"GetDataContractHistoryRequest",
6878
"GetDataContractRequest",
6979
"GetDataContractsRequest",
@@ -78,7 +88,6 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
7888
"GetIdentityByPublicKeyHashRequest",
7989
"GetIdentityKeysRequest",
8090
"GetIdentityRequest",
81-
"GetProofsRequest",
8291
"WaitForStateTransitionResultRequest",
8392
"GetProtocolVersionUpgradeStateRequest",
8493
"GetProtocolVersionUpgradeVoteStatusRequest",
@@ -111,7 +120,7 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
111120
// - "GetStatusResponse"
112121
//
113122
// "GetEvonodesProposedEpochBlocksResponse" is used for 2 Requests
114-
const VERSIONED_RESPONSES: [&str; 39] = [
123+
const VERSIONED_RESPONSES: [&str; 38] = [
115124
"GetDataContractHistoryResponse",
116125
"GetDataContractResponse",
117126
"GetDataContractsResponse",
@@ -126,7 +135,6 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
126135
"GetIdentityByPublicKeyHashResponse",
127136
"GetIdentityKeysResponse",
128137
"GetIdentityResponse",
129-
"GetProofsResponse",
130138
"WaitForStateTransitionResultResponse",
131139
"GetEpochsInfoResponse",
132140
"GetProtocolVersionUpgradeStateResponse",
@@ -210,6 +218,19 @@ fn configure_platform(mut platform: MappingConfig) -> MappingConfig {
210218
platform
211219
}
212220

221+
fn configure_drive(drive: MappingConfig) -> MappingConfig {
222+
drive
223+
.message_attribute(".", r#"#[derive( ::dapi_grpc_macros::Mockable)]"#)
224+
.type_attribute(
225+
".",
226+
r#"#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]"#,
227+
)
228+
.type_attribute(
229+
".",
230+
r#"#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]"#,
231+
)
232+
}
233+
213234
/// Check for duplicate messages in the list.
214235
fn check_unique(messages: &[&'static str]) -> Result<(), String> {
215236
let mut hashset: HashSet<&'static str> = HashSet::new();
@@ -322,6 +343,14 @@ impl MappingConfig {
322343
self
323344
}
324345

346+
#[allow(unused)]
347+
fn includes(mut self, includes: &[PathBuf]) -> Self {
348+
for include in includes {
349+
self.proto_includes.push(abs_path(include));
350+
}
351+
self
352+
}
353+
325354
#[allow(unused)]
326355
fn field_attribute(mut self, path: &str, attribute: &str) -> Self {
327356
self.builder = self.builder.field_attribute(path, attribute);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const grpc = require('@grpc/grpc-js');
2+
const { promisify } = require('util');
3+
4+
const {
5+
convertObjectToMetadata,
6+
utils: {
7+
isObject,
8+
},
9+
client: {
10+
interceptors: {
11+
jsonToProtobufInterceptorFactory,
12+
},
13+
converters: {
14+
jsonToProtobufFactory,
15+
protobufToJsonFactory,
16+
},
17+
},
18+
} = require('@dashevo/grpc-common');
19+
20+
const { URL } = require('url');
21+
const {
22+
org: {
23+
dash: {
24+
platform: {
25+
drive: {
26+
v0: {
27+
GetProofsRequest: PBJSGetProofsRequest,
28+
GetProofsResponse: PBJSGetProofsResponse,
29+
},
30+
},
31+
},
32+
},
33+
},
34+
} = require('./drive_pbjs');
35+
36+
const {
37+
GetProofsResponse: ProtocGetProofsResponse,
38+
} = require('./drive_protoc');
39+
40+
const getDriveDefinition = require('../../../../lib/getDriveDefinition');
41+
42+
const DriveNodeJSClient = getDriveDefinition(0);
43+
44+
class DrivePromiseClient {
45+
/**
46+
* @param {string} hostname
47+
* @param {?Object} credentials
48+
* @param {?Object} options
49+
*/
50+
constructor(hostname, credentials, options = {}) {
51+
if (credentials !== undefined) {
52+
throw new Error('"credentials" option is not supported yet');
53+
}
54+
55+
const url = new URL(hostname);
56+
const { protocol, host: strippedHostname } = url;
57+
58+
// See this issue https://github.com/nodejs/node/issues/3176
59+
// eslint-disable-next-line no-param-reassign
60+
credentials = protocol.replace(':', '') === 'https' ? grpc.credentials.createSsl() : grpc.credentials.createInsecure();
61+
62+
this.client = new DriveNodeJSClient(strippedHostname, credentials, options);
63+
64+
this.client.getProofs = promisify(
65+
this.client.getProofs.bind(this.client),
66+
);
67+
68+
this.protocolVersion = undefined;
69+
}
70+
71+
/**
72+
*
73+
* @param {!GetProofsRequest} request
74+
* @param {?Object<string, string>} metadata
75+
* @param {CallOptions} [options={}]
76+
* @returns {Promise<!GetProofsResponse>}
77+
*/
78+
getProofs(request, metadata = {}, options = {}) {
79+
if (!isObject(metadata)) {
80+
throw new Error('metadata must be an object');
81+
}
82+
83+
return this.client.getProofs(
84+
request,
85+
convertObjectToMetadata(metadata),
86+
{
87+
interceptors: [
88+
jsonToProtobufInterceptorFactory(
89+
jsonToProtobufFactory(
90+
ProtocGetProofsResponse,
91+
PBJSGetProofsResponse,
92+
),
93+
protobufToJsonFactory(
94+
PBJSGetProofsRequest,
95+
),
96+
),
97+
],
98+
...options,
99+
},
100+
);
101+
}
102+
103+
/**
104+
* @param {string} protocolVersion
105+
*/
106+
setProtocolVersion(protocolVersion) {
107+
this.protocolVersion = protocolVersion;
108+
}
109+
}
110+
111+
module.exports = DrivePromiseClient;

0 commit comments

Comments
 (0)