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

Remove SiaEncodable trait #17

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mod serializer;

// place the deserializer and serializer modules in the encoding module.
pub use deserializer::{from_reader, Deserializer, Error as DeserializeError};
pub use serializer::{to_bytes, to_writer, Error as SerializeError};
pub use serializer::{serialize_array, to_bytes, to_writer, Error as SerializeError};
30 changes: 23 additions & 7 deletions src/encoding/serializer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{
ser::{self, SerializeSeq},
ser::{self, SerializeTuple},
Serialize,
};
use std::fmt::Display;
Expand All @@ -21,6 +21,22 @@ pub enum Error {
IO(#[from] io::Error),
}

// Helper function to serialize array of fixed size. Serde only implements
// Serialize for arrays up to 32 elements in size. The reason being that Rust
// didn't have const generics back then and now they can't easily upgrade
// without breaking code.
pub fn serialize_array<const N: usize, S, T>(t: &[T; N], serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
T: Serialize,
{
let mut ser_tuple = serializer.serialize_tuple(N)?;
for elem in t {
ser_tuple.serialize_element(elem)?;
}
ser_tuple.end()
}

// Implement ser::Error for Error
impl ser::Error for Error {
fn custom<T>(msg: T) -> Self
Expand Down Expand Up @@ -147,9 +163,9 @@ impl<W: io::Write> ser::Serializer for &mut Serializer<'_, W> {
}

// 'some' is serialized by writing a '1' byte followed by the value
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
T: Serialize + ?Sized,
{
self.writer.write_all(&[1])?;
value.serialize(self)
Expand Down Expand Up @@ -177,27 +193,27 @@ impl<W: io::Write> ser::Serializer for &mut Serializer<'_, W> {

// As is done here, serializers are encouraged to treat newtype structs as
// insignificant wrappers around the data they contain
fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self)
}

// newtype_variants are not supported
fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
T: Serialize + ?Sized,
{
Err(Error::UnsupportedType("newtype_variant"))
}
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use core::fmt;
use std::io::{Error, Write};

pub trait SiaEncodable {
fn encode<W: Write>(&self, w: &mut W) -> Result<(), Error>;
}

pub mod address;
pub mod consensus;
Expand Down
24 changes: 12 additions & 12 deletions src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::SystemTime;
use crate::consensus::ChainIndex;
use crate::encoding::{to_writer, SerializeError};
use crate::transactions::{CoveredFields, Transaction};
use crate::{Algorithm, Hash256, HexParseError, SiaEncodable};
use crate::{Algorithm, Hash256, HexParseError};
use blake2b_simd::Params;
use ed25519_dalek::{Signature as ED25519Signature, Signer, SigningKey, Verifier, VerifyingKey};
use serde::ser::SerializeStruct;
Expand Down Expand Up @@ -268,7 +268,7 @@ impl SigningState {
state.update(&(txn.siacoin_inputs.len() as u64).to_le_bytes());
for input in txn.siacoin_inputs.iter() {
state.update(self.replay_prefix());
input.encode(&mut state).unwrap();
to_writer(&mut state, input)?;
}

state.update(&(txn.siacoin_outputs.len() as u64).to_le_bytes());
Expand All @@ -278,23 +278,23 @@ impl SigningState {

state.update(&(txn.file_contracts.len() as u64).to_le_bytes());
for file_contract in txn.file_contracts.iter() {
file_contract.encode(&mut state).unwrap();
to_writer(&mut state, file_contract)?;
}

state.update(&(txn.file_contract_revisions.len() as u64).to_le_bytes());
for file_contract_revision in txn.file_contract_revisions.iter() {
file_contract_revision.encode(&mut state).unwrap();
to_writer(&mut state, file_contract_revision)?;
}

state.update(&(txn.storage_proofs.len() as u64).to_le_bytes());
for storage_proof in txn.storage_proofs.iter() {
storage_proof.encode(&mut state).unwrap();
to_writer(&mut state, storage_proof).unwrap();
}

state.update(&(txn.siafund_inputs.len() as u64).to_le_bytes());
for input in txn.siafund_inputs.iter() {
state.update(self.replay_prefix());
input.encode(&mut state).unwrap();
to_writer(&mut state, input).unwrap();
}

state.update(&(txn.siafund_outputs.len() as u64).to_le_bytes());
Expand Down Expand Up @@ -333,33 +333,33 @@ impl SigningState {

for i in covered_fields.siacoin_inputs.into_iter() {
state.update(self.replay_prefix());
txn.siacoin_inputs[i].encode(&mut state).unwrap();
to_writer(&mut state, &txn.siacoin_inputs[i])?;
}

for i in covered_fields.siacoin_outputs.into_iter() {
to_writer(&mut state, &txn.siacoin_outputs[i])?;
}

for i in covered_fields.file_contracts.into_iter() {
txn.file_contracts[i].encode(&mut state).unwrap();
to_writer(&mut state, &txn.file_contracts[i])?;
}

for i in covered_fields.file_contract_revisions.into_iter() {
txn.file_contract_revisions[i].encode(&mut state).unwrap();
to_writer(&mut state, &txn.file_contract_revisions[i])?;
}

for i in covered_fields.storage_proofs.into_iter() {
txn.storage_proofs[i].encode(&mut state).unwrap();
to_writer(&mut state, &txn.storage_proofs[i])?;
}

for i in covered_fields.siafund_inputs.into_iter() {
txn.siafund_inputs[i].encode(&mut state).unwrap();
to_writer(&mut state, &txn.siafund_inputs[i])?;
state.update(self.replay_prefix());
}

for i in covered_fields.siafund_outputs.into_iter() {
to_writer(&mut state, &txn.siafund_outputs[i])?;
state.update(self.replay_prefix());
to_writer(&mut state, &txn.siafund_outputs[i])?;
}

for i in covered_fields.miner_fees.into_iter() {
Expand Down
6 changes: 3 additions & 3 deletions src/spendpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ impl SpendPolicy {
}
}

/// Encode the policy to a writer. This is used by the SiaEncodable trait to
/// handle recursive threshold policies. The version byte is only written
/// for the top-level policy.
/// Encode the policy to a writer. This is used to handle recursive
/// threshold policies. The version byte is only written for the top-level
/// policy.
fn serialize_policy<S: serde::ser::SerializeTuple>(&self, s: &mut S) -> Result<(), S::Error> {
s.serialize_element(&self.type_prefix())?; // type prefix
match self {
Expand Down
Loading