Skip to content

Commit

Permalink
fix: identifier deserialization doesn't work for bincode (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov authored and markin-io committed Apr 26, 2023
1 parent 941a1b5 commit 151eae7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
6 changes: 3 additions & 3 deletions packages/rs-dpp/src/errors/consensus/consensus_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl ConsensusError {
.with_variable_int_encoding()
.with_big_endian();

bincode::serde::encode_to_vec(self, config).map_err(|_| {
ProtocolError::EncodingError(String::from("unable to serialize identity public key"))
bincode::serde::encode_to_vec(self, config).map_err(|e| {
ProtocolError::EncodingError(format!("unable to serialize consensus error: {e}"))
})
}

Expand All @@ -68,7 +68,7 @@ impl ConsensusError {
.with_big_endian();

bincode::serde::decode_borrowed_from_slice(bytes, config).map_err(|e| {
ProtocolError::EncodingError(format!("unable to deserialize consensus error {}", e))
ProtocolError::EncodingError(format!("unable to deserialize consensus error: {e}"))
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl From<&ProtocolVersionParsingError> for ProtocolVersionParsingErrorWasm {

#[wasm_bindgen(js_class=ProtocolVersionParsingError)]
impl ProtocolVersionParsingErrorWasm {
#[wasm_bindgen(constructor)]
pub fn new(parsing_error: String) -> Self {
Self {
inner: ProtocolVersionParsingError::new(parsing_error),
}
}

#[wasm_bindgen(js_name = getParsingError)]
pub fn get_parsing_error(&self) -> String {
self.inner.parsing_error().to_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ impl From<&BalanceIsNotEnoughError> for BalanceIsNotEnoughErrorWasm {

#[wasm_bindgen(js_class=BalanceIsNotEnoughError)]
impl BalanceIsNotEnoughErrorWasm {
#[wasm_bindgen(constructor)]
pub fn new(balance: Credits, fee: Credits) -> Self {
Self {
inner: BalanceIsNotEnoughError::new(balance, fee),
}
}

#[wasm_bindgen(js_name=getBalance)]
pub fn get_balance(&self) -> Credits {
self.inner.balance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dpp::consensus::ConsensusError;
use wasm_bindgen::prelude::*;

use crate::buffer::Buffer;
use crate::identifier::IdentifierWrapper;

#[wasm_bindgen(js_name=IdentityNotFoundError)]
pub struct IdentityNotFoundErrorWasm {
Expand All @@ -18,9 +19,16 @@ impl From<&IdentityNotFoundError> for IdentityNotFoundErrorWasm {

#[wasm_bindgen(js_class=IdentityNotFoundError)]
impl IdentityNotFoundErrorWasm {
#[wasm_bindgen(constructor)]
pub fn new(identity_id: IdentifierWrapper) -> Self {
Self {
inner: IdentityNotFoundError::new(identity_id.into()),
}
}

#[wasm_bindgen(js_name=getIdentityId)]
pub fn get_identity_id(&self) -> Buffer {
Buffer::from_bytes(self.inner.identity_id().as_bytes())
pub fn get_identity_id(&self) -> IdentifierWrapper {
self.inner.identity_id().into()
}

#[wasm_bindgen(js_name=getCode)]
Expand All @@ -37,7 +45,7 @@ impl IdentityNotFoundErrorWasm {
pub fn serialize(&self) -> Result<Buffer, JsError> {
let bytes = ConsensusError::from(self.inner.clone())
.serialize()
.map_err(|e| JsError::from(e))?;
.map_err(JsError::from)?;

Ok(Buffer::from_bytes(bytes.as_slice()))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::buffer::Buffer;
use crate::identifier::IdentifierWrapper;
use dpp::consensus::codes::ErrorWithCode;
use dpp::consensus::state::data_contract::data_contract_already_present_error::DataContractAlreadyPresentError;
use dpp::consensus::ConsensusError;
use dpp::identifier::Identifier;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name=DataContractAlreadyPresentError)]
Expand All @@ -17,9 +19,16 @@ impl From<&DataContractAlreadyPresentError> for DataContractAlreadyPresentErrorW

#[wasm_bindgen(js_class=DataContractAlreadyPresentError)]
impl DataContractAlreadyPresentErrorWasm {
#[wasm_bindgen(constructor)]
pub fn new(data_contract_id: IdentifierWrapper) -> Self {
Self {
inner: DataContractAlreadyPresentError::new(data_contract_id.into()),
}
}

#[wasm_bindgen(js_name=getDataContractId)]
pub fn data_contract_id(&self) -> Buffer {
Buffer::from_bytes(self.inner.data_contract_id().as_bytes())
pub fn data_contract_id(&self) -> IdentifierWrapper {
self.inner.data_contract_id().to_owned().into()
}

#[wasm_bindgen(js_name=getCode)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ const { default: loadWasmDpp } = require('../../../..');

let {
deserializeConsensusError,
decodeProtocolEntity,
ProtocolVersionParsingError,
} = require('../../../..');

describe('deserializeConsensusError', () => {
before(async () => {
({
deserializeConsensusError,
decodeProtocolEntity,
ProtocolVersionParsingError,
} = await loadWasmDpp());
});

it('should deserialize consensus error', async () => {
try {
await decodeProtocolEntity(Buffer.alloc(0));
} catch (consensusError) {
expect(consensusError).to.be.instanceOf(ProtocolVersionParsingError);
expect(consensusError.getCode()).to.equals(1000);
expect(consensusError.message).to.equals('Can\'t read protocol version from serialized object: protocol version could not be decoded as a varint');
const consensusError = new ProtocolVersionParsingError('test');
const message = 'Can\'t read protocol version from serialized object: test';

const bytes = consensusError.serialize();
expect(consensusError).to.be.instanceOf(ProtocolVersionParsingError);
expect(consensusError.getCode()).to.equals(1000);
expect(consensusError.message).to.equals(message);

const recoveredError = deserializeConsensusError(bytes);
expect(recoveredError).to.be.instanceOf(ProtocolVersionParsingError);
expect(recoveredError.getCode()).to.equals(1000);
expect(recoveredError.message).to.equals('Can\'t read protocol version from serialized object: protocol version could not be decoded as a varint');
}
const serializedConsensusError = consensusError.serialize();

const recoveredError = deserializeConsensusError(serializedConsensusError);
expect(recoveredError).to.be.instanceOf(ProtocolVersionParsingError);
expect(recoveredError.getCode()).to.equals(1000);
expect(recoveredError.message).to.equals(message);
});
});

0 comments on commit 151eae7

Please sign in to comment.