Skip to content

Commit

Permalink
fix: empty proofs are returned for masternode vote state transition
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Jun 27, 2024
1 parent 9116c8c commit 522e3a1
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 112 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 10 additions & 17 deletions packages/dapi-grpc/clients/platform/v0/nodejs/platform_pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29722,7 +29722,6 @@ $root.org = (function() {
case 0:
case 1:
case 2:
case 3:
break;
}
if (message.allowIncludeLockedAndAbstainingVoteTally != null && message.hasOwnProperty("allowIncludeLockedAndAbstainingVoteTally"))
Expand Down Expand Up @@ -29774,22 +29773,18 @@ $root.org = (function() {
message.indexValues[i] = object.indexValues[i];
}
switch (object.resultType) {
case "IDENTITY_IDS_ONLY":
case "DOCUMENTS":
case 0:
message.resultType = 0;
break;
case "DOCUMENTS":
case "VOTE_TALLY":
case 1:
message.resultType = 1;
break;
case "VOTE_TALLY":
case "DOCUMENTS_AND_VOTE_TALLY":
case 2:
message.resultType = 2;
break;
case "DOCUMENTS_AND_VOTE_TALLY":
case 3:
message.resultType = 3;
break;
}
if (object.allowIncludeLockedAndAbstainingVoteTally != null)
message.allowIncludeLockedAndAbstainingVoteTally = Boolean(object.allowIncludeLockedAndAbstainingVoteTally);
Expand Down Expand Up @@ -29830,7 +29825,7 @@ $root.org = (function() {
}
object.documentTypeName = "";
object.indexName = "";
object.resultType = options.enums === String ? "IDENTITY_IDS_ONLY" : 0;
object.resultType = options.enums === String ? "DOCUMENTS" : 0;
object.allowIncludeLockedAndAbstainingVoteTally = false;
object.startAtIdentifierInfo = null;
object.count = 0;
Expand Down Expand Up @@ -30094,17 +30089,15 @@ $root.org = (function() {
* ResultType enum.
* @name org.dash.platform.dapi.v0.GetContestedResourceVoteStateRequest.GetContestedResourceVoteStateRequestV0.ResultType
* @enum {number}
* @property {number} IDENTITY_IDS_ONLY=0 IDENTITY_IDS_ONLY value
* @property {number} DOCUMENTS=1 DOCUMENTS value
* @property {number} VOTE_TALLY=2 VOTE_TALLY value
* @property {number} DOCUMENTS_AND_VOTE_TALLY=3 DOCUMENTS_AND_VOTE_TALLY value
* @property {number} DOCUMENTS=0 DOCUMENTS value
* @property {number} VOTE_TALLY=1 VOTE_TALLY value
* @property {number} DOCUMENTS_AND_VOTE_TALLY=2 DOCUMENTS_AND_VOTE_TALLY value
*/
GetContestedResourceVoteStateRequestV0.ResultType = (function() {
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "IDENTITY_IDS_ONLY"] = 0;
values[valuesById[1] = "DOCUMENTS"] = 1;
values[valuesById[2] = "VOTE_TALLY"] = 2;
values[valuesById[3] = "DOCUMENTS_AND_VOTE_TALLY"] = 3;
values[valuesById[0] = "DOCUMENTS"] = 0;
values[valuesById[1] = "VOTE_TALLY"] = 1;
values[valuesById[2] = "DOCUMENTS_AND_VOTE_TALLY"] = 2;
return values;
})();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29030,10 +29030,9 @@ proto.org.dash.platform.dapi.v0.GetContestedResourceVoteStateRequest.GetConteste
* @enum {number}
*/
proto.org.dash.platform.dapi.v0.GetContestedResourceVoteStateRequest.GetContestedResourceVoteStateRequestV0.ResultType = {
IDENTITY_IDS_ONLY: 0,
DOCUMENTS: 1,
VOTE_TALLY: 2,
DOCUMENTS_AND_VOTE_TALLY: 3
DOCUMENTS: 0,
VOTE_TALLY: 1,
DOCUMENTS_AND_VOTE_TALLY: 2
};


Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 69 additions & 74 deletions packages/dapi-grpc/clients/platform/v0/python/platform_pb2.py

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions packages/dapi-grpc/clients/platform/v0/web/platform_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3817,10 +3817,9 @@ export namespace GetContestedResourceVoteStateRequest {
}

export interface ResultTypeMap {
IDENTITY_IDS_ONLY: 0;
DOCUMENTS: 1;
VOTE_TALLY: 2;
DOCUMENTS_AND_VOTE_TALLY: 3;
DOCUMENTS: 0;
VOTE_TALLY: 1;
DOCUMENTS_AND_VOTE_TALLY: 2;
}

export const ResultType: ResultTypeMap;
Expand Down
7 changes: 3 additions & 4 deletions packages/dapi-grpc/clients/platform/v0/web/platform_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -29030,10 +29030,9 @@ proto.org.dash.platform.dapi.v0.GetContestedResourceVoteStateRequest.GetConteste
* @enum {number}
*/
proto.org.dash.platform.dapi.v0.GetContestedResourceVoteStateRequest.GetContestedResourceVoteStateRequestV0.ResultType = {
IDENTITY_IDS_ONLY: 0,
DOCUMENTS: 1,
VOTE_TALLY: 2,
DOCUMENTS_AND_VOTE_TALLY: 3
DOCUMENTS: 0,
VOTE_TALLY: 1,
DOCUMENTS_AND_VOTE_TALLY: 2
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ function fetchProofForStateTransitionFactory(driveClient) {
});

requestV0.setContractsList(contractsList);
} if (stateTransition.isVotingStateTransition()) {
const { VoteStatusRequest } = GetProofsRequestV0;
const { ContestedResourceVoteStatusRequest } = VoteStatusRequest;

const contestedResourceVoteStatusRequest = new ContestedResourceVoteStatusRequest();

const contestedVotePoll = stateTransition.getContestedDocumentResourceVotePoll();

if (!contestedVotePoll) {
throw new Error('Masternode vote state transition should have a contested vote poll');
}

contestedResourceVoteStatusRequest.setContractId(contestedVotePoll.contractId.toBuffer());
contestedResourceVoteStatusRequest.setDocumentTypeName(contestedVotePoll.documentTypeName);
contestedResourceVoteStatusRequest.setIndexName(contestedVotePoll.indexName);
contestedResourceVoteStatusRequest.setIndexValuesList(contestedVotePoll.indexValues);
contestedResourceVoteStatusRequest.setVoterIdentifier(
stateTransition.getProTxHash().toBuffer(),
);

const voteStatus = new VoteStatusRequest();
voteStatus.setContestedResourceVoteStatusRequest(contestedResourceVoteStatusRequest);

requestV0.setVotesList([voteStatus]);
}

const request = new GetProofsRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { expect } = require('chai');
const crypto = require('crypto');

const {
v0: {
Expand All @@ -18,6 +19,7 @@ describe('fetchProofForStateTransition', () => {
let identitiesProofResponse;
let dataContractsProofResponse;
let documentsProofResponse;
let masternodeVoteResponse;
let stateTransitionFixture;

beforeEach(async function beforeEach() {
Expand All @@ -28,6 +30,8 @@ describe('fetchProofForStateTransition', () => {
documentsProofResponse.setV0(new GetProofsResponseV0().setProof(new Proof([Buffer.from('documents contracts proof')])));
identitiesProofResponse = new GetProofsResponse();
identitiesProofResponse.setV0(new GetProofsResponseV0().setProof(new Proof([Buffer.from('identities contracts proof')])));
masternodeVoteResponse = new GetProofsResponse();
masternodeVoteResponse.setV0(new GetProofsResponseV0().setProof(new Proof([Buffer.from('masternode vote proof')])));

driveClientMock = {
getProofs: this.sinon.stub().callsFake(async (requestProto) => {
Expand All @@ -37,13 +41,16 @@ describe('fetchProofForStateTransition', () => {
return documentsProofResponse;
} if (requestProto.getV0().getContractsList().length > 0) {
return dataContractsProofResponse;
} if (requestProto.getV0().getVotesList().length > 0) {
return masternodeVoteResponse;
}

return null;
}),
};

stateTransitionFixture = {
isVotingStateTransition: this.sinon.stub(),
isIdentityStateTransition: this.sinon.stub(),
isDocumentStateTransition: this.sinon.stub(),
isDataContractStateTransition: this.sinon.stub(),
Expand Down Expand Up @@ -86,4 +93,25 @@ describe('fetchProofForStateTransition', () => {
expect(result.serializeBinary()).to.deep
.equal(documentsProofResponse.serializeBinary());
});

it('should fetch masternode vote proofs', async function it() {
const proTxHash = await generateRandomIdentifierAsync();
const contractId = await generateRandomIdentifierAsync();
const documentTypeName = 'documentType';
const indexName = 'indexName';
const indexValues = [crypto.randomBytes(32), crypto.randomBytes(32)];

stateTransitionFixture.getProTxHash = this.sinon.stub().returns(proTxHash);
stateTransitionFixture.isVotingStateTransition.returns(true);
stateTransitionFixture.getContestedDocumentResourceVotePoll = this.sinon.stub().returns({
contractId,
documentTypeName,
indexName,
indexValues,
});

const result = await fetchProofForStateTransition(stateTransitionFixture);
expect(result.serializeBinary()).to.deep
.equal(masternodeVoteResponse.serializeBinary());
});
});
1 change: 1 addition & 0 deletions packages/wasm-dpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ anyhow = { version = "1.0.75" }
# required, cargo-machete false positive
wasm-bindgen-futures = "0.4.33"
async-trait = "0.1.59"
bincode = "2.0.0-rc.3"
[profile.release]
lto = true
opt-level = 's'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod to_object;

use js_sys::{Array, Object, Reflect, Uint8Array};
use crate::bls_adapter::{BlsAdapter, JsBlsAdapter};
use crate::buffer::Buffer;
use crate::errors::from_dpp_err;
Expand All @@ -17,6 +18,9 @@ use dpp::state_transition::{StateTransition, StateTransitionIdentitySigned, Stat
use dpp::version::PlatformVersion;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};
use dpp::voting::vote_polls::VotePoll;
use dpp::voting::votes::resource_vote::accessors::v0::ResourceVoteGettersV0;
use dpp::voting::votes::Vote;

#[derive(Clone)]
#[wasm_bindgen(js_name=MasternodeVoteTransition)]
Expand Down Expand Up @@ -51,7 +55,7 @@ impl MasternodeVoteTransitionWasm {
self.0.state_transition_type() as u8
}

#[wasm_bindgen(getter, js_name=proTxHash)]
#[wasm_bindgen(getter, js_name=getProTxHash)]
pub fn pro_tx_hash(&self) -> IdentifierWrapper {
self.get_pro_tx_hash()
}
Expand Down Expand Up @@ -215,6 +219,43 @@ impl MasternodeVoteTransitionWasm {
self.0.is_voting_state_transition()
}

#[wasm_bindgen(js_name=getContestedDocumentResourceVotePoll)]
pub fn contested_document_resource_vote_poll(&self) -> Option<Object> {
match self.0.vote() {
Vote::ResourceVote(vote) => match vote.vote_poll() {
VotePoll::ContestedDocumentResourceVotePoll(contested_document_resource_vote_poll) => {
let js_object = Object::new();

let contract_id = IdentifierWrapper::from(contested_document_resource_vote_poll.contract_id.clone());

Reflect::set(&js_object, &"contractId".into(), &contract_id.into()).unwrap();
Reflect::set(&js_object, &"documentTypeName".into(), &contested_document_resource_vote_poll.document_type_name.clone().into()).unwrap();
Reflect::set(&js_object, &"indexName".into(), &contested_document_resource_vote_poll.index_name.clone().into()).unwrap();

let config = bincode::config::standard()
.with_big_endian()
.with_no_limit();

let serialized_index_values = contested_document_resource_vote_poll
.index_values
.iter()
.map(|value| {
JsValue::from(Buffer::from_bytes_owned(
bincode::encode_to_vec(value, config)
.expect("expected to encode value in path")
))
});

let js_array = Array::from_iter(serialized_index_values);

Reflect::set(&js_object, &"indexValues".into(), &js_array.into()).unwrap();

Some(js_object)
}
}
}
}

#[wasm_bindgen(js_name=signByPrivateKey)]
pub fn sign_by_private_key(
&mut self,
Expand Down

0 comments on commit 522e3a1

Please sign in to comment.