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

feat: covenants implementation #3656

Merged
6 changes: 3 additions & 3 deletions Cargo.lock

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

166 changes: 151 additions & 15 deletions base_layer/core/src/consensus/consensus_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::io;

/// Abstracts the ability of a type to canonically encode itself for the purposes of consensus
pub trait ConsensusEncoding {
/// Encode to the given writer returning the number of bytes writter.
/// If writing to this Writer is infallible, this implementation must always succeed.
/// Encode to the given writer returning the number of bytes written.
/// If writing to this Writer is infallible, this implementation MUST always succeed.
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error>;
}

Expand All @@ -41,33 +41,81 @@ pub trait ConsensusDecoding: Sized {
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, io::Error>;
}

pub struct ConsensusEncodingWrapper<'a, T> {
inner: &'a T,
pub trait ToConsensusBytes {
fn to_consensus_bytes(&self) -> Vec<u8>;
}

impl<'a, T> ConsensusEncodingWrapper<'a, T> {
pub fn wrap(inner: &'a T) -> Self {
Self { inner }
impl<T: ConsensusEncoding + ConsensusEncodingSized + ?Sized> ToConsensusBytes for T {
fn to_consensus_bytes(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.consensus_encode_exact_size());
// Vec's write impl is infallible, as per the ConsensusEncoding contract, consensus_encode is infallible
self.consensus_encode(&mut buf).expect("unreachable panic");
buf
}
}

// TODO: move traits and implement consensus encoding for TariScript
// for now, this wrapper will do that job
mod tariscript_impl {
impl ConsensusEncoding for Vec<u8> {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
writer.write(self)
}
}

impl ConsensusEncodingSized for Vec<u8> {
fn consensus_encode_exact_size(&self) -> usize {
self.len()
}
}

macro_rules! consensus_encoding_varint_impl {
($ty:ty) => {
impl ConsensusEncoding for $ty {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
use integer_encoding::VarIntWriter;
let bytes_written = writer.write_varint(*self)?;
Ok(bytes_written)
}
}

impl ConsensusDecoding for $ty {
fn consensus_decode<R: io::Read>(reader: &mut R) -> Result<Self, io::Error> {
use integer_encoding::VarIntReader;
let value = reader.read_varint()?;
Ok(value)
}
}

impl ConsensusEncodingSized for $ty {
fn consensus_encode_exact_size(&self) -> usize {
use integer_encoding::VarInt;
self.required_space()
}
}
};
}

consensus_encoding_varint_impl!(u8);
consensus_encoding_varint_impl!(u64);

// Keep separate the dependencies of the impls that may in future be implemented in tari crypto
mod impls {
use std::io::Read;

use tari_common_types::types::{Commitment, PrivateKey, PublicKey, Signature};
use tari_crypto::script::TariScript;
use tari_utilities::ByteArray;

use super::*;
use crate::common::byte_counter::ByteCounter;

impl<'a> ConsensusEncoding for ConsensusEncodingWrapper<'a, TariScript> {
//---------------------------------- TariScript --------------------------------------------//

impl ConsensusEncoding for TariScript {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let bytes = self.inner.as_bytes();
writer.write_all(&bytes)?;
Ok(bytes.len())
self.as_bytes().consensus_encode(writer)
}
}

impl<'a> ConsensusEncodingSized for ConsensusEncodingWrapper<'a, TariScript> {
impl ConsensusEncodingSized for TariScript {
fn consensus_encode_exact_size(&self) -> usize {
let mut counter = ByteCounter::new();
// TODO: consensus_encode_exact_size must be cheap to run
Expand All @@ -76,4 +124,92 @@ mod tariscript_impl {
counter.get()
}
}

//---------------------------------- PublicKey --------------------------------------------//

impl ConsensusEncoding for PublicKey {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
writer.write(self.as_bytes())
}
}

impl ConsensusEncodingSized for PublicKey {
fn consensus_encode_exact_size(&self) -> usize {
let mut counter = ByteCounter::new();
// TODO: consensus_encode_exact_size must be cheap to run
// unreachable panic: ByteCounter is infallible
self.consensus_encode(&mut counter).expect("unreachable");
counter.get()
}
}

impl ConsensusDecoding for PublicKey {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let mut buf = [0u8; 32];
reader.read_exact(&mut buf)?;
let pk = PublicKey::from_bytes(&buf[..]).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
Ok(pk)
}
}

//---------------------------------- Commitment --------------------------------------------//

impl ConsensusEncoding for Commitment {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let buf = self.as_bytes();
let len = buf.len();
writer.write_all(buf)?;
Ok(len)
}
}

impl ConsensusEncodingSized for Commitment {
fn consensus_encode_exact_size(&self) -> usize {
32
}
}

impl ConsensusDecoding for Commitment {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let mut buf = [0u8; 32];
reader.read_exact(&mut buf)?;
let commitment =
Commitment::from_bytes(&buf[..]).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
Ok(commitment)
}
}

//---------------------------------- Signature --------------------------------------------//

impl ConsensusEncoding for Signature {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let pub_nonce = self.get_public_nonce().as_bytes();
let mut written = pub_nonce.len();
writer.write_all(pub_nonce)?;
let sig = self.get_signature().as_bytes();
written += sig.len();
writer.write_all(sig)?;
Ok(written)
}
}

impl ConsensusEncodingSized for Signature {
fn consensus_encode_exact_size(&self) -> usize {
96
}
}

impl ConsensusDecoding for Signature {
fn consensus_decode<R: Read>(reader: &mut R) -> Result<Self, io::Error> {
let mut buf = [0u8; 32];
reader.read_exact(&mut buf)?;
let pub_nonce =
PublicKey::from_bytes(&buf[..]).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
let mut buf = [0u8; 64];
reader.read_exact(&mut buf)?;
let sig =
PrivateKey::from_bytes(&buf[..]).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
Ok(Signature::new(pub_nonce, sig))
}
}
}
2 changes: 1 addition & 1 deletion base_layer/core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod consensus_manager;
pub use consensus_manager::{ConsensusManager, ConsensusManagerBuilder, ConsensusManagerError};

mod consensus_encoding;
pub use consensus_encoding::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized, ConsensusEncodingWrapper};
pub use consensus_encoding::{ConsensusDecoding, ConsensusEncoding, ConsensusEncodingSized, ToConsensusBytes};

mod network;
pub use network::NetworkConsensus;
Expand Down
Loading