Skip to content

Commit

Permalink
feat(core): adds constitution UTXO features (#4121)
Browse files Browse the repository at this point in the history
Description
---
- adds `ContractConstitution` and related structs 
- implements consensus de/encoding for `ConsensusConstitution`
- implements `ConsensusEncoding` ,`Deref` and `DerefMut` for `MaxSizeVec`
- adds check for extraneous bytes to `check_consensus_encoding_correctness` 

Motivation and Context
---
Add initial structs for contract constitution UTXO. Adding this to OutputFeatures is left for a subsequent PR.

How Has This Been Tested?
---
Unit tests for consensus encoding
  • Loading branch information
sdbondi authored May 25, 2022
1 parent 5ac6133 commit da5696a
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 5 deletions.
4 changes: 3 additions & 1 deletion base_layer/core/src/consensus/consensus_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ pub mod test {
let mut buf = Vec::new();
subject.consensus_encode(&mut buf)?;
assert_eq!(buf.len(), subject.consensus_encode_exact_size());
let decoded = T::consensus_decode(&mut buf.as_slice())?;
let mut reader = buf.as_slice();
let decoded = T::consensus_decode(&mut reader)?;
assert_eq!(decoded, subject);
assert!(reader.is_empty());
Ok(())
}
}
37 changes: 33 additions & 4 deletions base_layer/core/src/consensus/consensus_encoding/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{convert::TryFrom, io, io::Read};
use std::{
convert::TryFrom,
io,
ops::{Deref, DerefMut},
};

use integer_encoding::VarIntReader;
use serde::{Deserialize, Serialize};

use crate::consensus::ConsensusDecoding;
use crate::consensus::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized};

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct MaxSizeVec<T, const MAX: usize> {
inner: Vec<T>,
}
Expand All @@ -37,6 +42,20 @@ impl<T, const MAX: usize> MaxSizeVec<T, MAX> {
}
}

impl<T, const MAX: usize> Deref for MaxSizeVec<T, MAX> {
type Target = Vec<T>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T, const MAX: usize> DerefMut for MaxSizeVec<T, MAX> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

impl<T, const MAX: usize> From<MaxSizeVec<T, MAX>> for Vec<T> {
fn from(value: MaxSizeVec<T, MAX>) -> Self {
value.into_vec()
Expand All @@ -55,8 +74,18 @@ impl<T, const MAX: usize> TryFrom<Vec<T>> for MaxSizeVec<T, MAX> {
}
}

impl<T: ConsensusEncoding, const MAX: usize> ConsensusEncoding for MaxSizeVec<T, MAX> {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> {
// We do not have to check the number of elements is correct, because it is not possible to construct MaxSizeVec
// with more than MAX elements.
self.inner.consensus_encode(writer)
}
}

impl<T: ConsensusEncoding, const MAX: usize> ConsensusEncodingSized for MaxSizeVec<T, MAX> {}

impl<T: ConsensusDecoding, const MAX: usize> ConsensusDecoding for MaxSizeVec<T, MAX> {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> {
let len = reader.read_varint()?;
if len > MAX {
return Err(io::Error::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use output_features::OutputFeatures;
pub use output_features_version::OutputFeaturesVersion;
pub use output_flags::OutputFlags;
pub use rewind_result::RewindResult;
pub use side_chain::ContractConstitution;
pub use side_chain_checkpoint_features::SideChainCheckpointFeatures;
use tari_common_types::types::Commitment;
use tari_script::TariScript;
Expand Down Expand Up @@ -62,6 +63,7 @@ mod output_features;
mod output_features_version;
mod output_flags;
mod rewind_result;
mod side_chain;
mod side_chain_checkpoint_features;
mod template_parameter;
mod transaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ pub struct OutputFeatures {
#[serde(default)]
pub recovery_byte: u8,
pub metadata: Vec<u8>,
// TODO: Add these fields
// pub contract_id: Option<FixedHash>,
// pub constitution: Option<ContractConstitution>,

// Deprecated fields
pub unique_id: Option<Vec<u8>>,
pub parent_public_key: Option<PublicKey>,
pub asset: Option<AssetOutputFeatures>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::io;

use serde::{Deserialize, Serialize};
use tari_common_types::types::PublicKey;

use crate::consensus::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized, MaxSizeVec};

#[derive(Debug, Clone, Hash, PartialEq, Deserialize, Serialize, Eq, Default)]
pub struct CommitteeMembers {
members: MaxSizeVec<PublicKey, { CommitteeMembers::MAX_MEMBERS }>,
}

impl CommitteeMembers {
pub const MAX_MEMBERS: usize = 512;

pub fn new(members: MaxSizeVec<PublicKey, { Self::MAX_MEMBERS }>) -> Self {
Self { members }
}
}

impl ConsensusEncoding for CommitteeMembers {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<(), io::Error> {
self.members.consensus_encode(writer)?;
Ok(())
}
}

impl ConsensusEncodingSized for CommitteeMembers {}

impl ConsensusDecoding for CommitteeMembers {
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> {
Ok(Self {
members: ConsensusDecoding::consensus_decode(reader)?,
})
}
}

#[cfg(test)]
mod tests {
use std::convert::TryInto;

use super::*;
use crate::consensus::{check_consensus_encoding_correctness, ToConsensusBytes};

#[test]
fn it_encodes_and_decodes_correctly() {
let subject = CommitteeMembers::new(
vec![PublicKey::default(); CommitteeMembers::MAX_MEMBERS]
.try_into()
.unwrap(),
);
check_consensus_encoding_correctness(subject).unwrap();

let subject = CommitteeMembers::default();
check_consensus_encoding_correctness(subject).unwrap();
}

#[test]
fn it_fails_for_more_than_max_members() {
let v = vec![PublicKey::default(); CommitteeMembers::MAX_MEMBERS + 1];
let encoded = v.to_consensus_bytes();
CommitteeMembers::consensus_decode(&mut encoded.as_slice()).unwrap_err();
}
}
Loading

0 comments on commit da5696a

Please sign in to comment.