-
Notifications
You must be signed in to change notification settings - Fork 0
Message-Based Testing #42
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
Open
yHSJ
wants to merge
11
commits into
main
Choose a base branch
from
jshy/test-vectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
44121a4
feat: snapshot bootstrapper module
yHSJ 7786e8d
chore: only subscribe for snapshot messages if configured
yHSJ 9cf253d
chore: move cbor implementations to ledger_state from type
yHSJ d9c4bc8
chore: add additional validation for ratio decoding
yHSJ 814b8ad
feat: add dump functionality to spo_state
yHSJ 0456cc6
wip: scaffolding of test logic
yHSJ 9643837
chore: add timeout and ability to early exit
yHSJ 299a7c9
feat: initial CDDL definitions
yHSJ 1d8f1c6
wip: progress on CDDL definitions
yHSJ 0e0a742
feat: initial utxo definition and code generation via cddl-codegen
yHSJ 2ccff6c
chore: cleanup golden_test readme and config
yHSJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
use crate::{ | ||
KeyHash, MultiHostName, PoolRegistration, Ratio, Relay, SingleHostAddr, SingleHostName, | ||
}; | ||
use anyhow::{bail, Context, Result}; | ||
use minicbor::data::Tag; | ||
use std::{collections::BTreeMap, fs, path::Path}; | ||
|
||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)] | ||
pub struct LedgerState { | ||
pub spo_state: SPOState, | ||
} | ||
|
||
pub struct UTxOState {} | ||
|
||
pub struct StakeDistributionState {} | ||
|
||
pub struct AccountState {} | ||
|
||
pub struct ParametersState {} | ||
|
||
#[derive( | ||
Debug, | ||
Clone, | ||
serde::Serialize, | ||
serde::Deserialize, | ||
minicbor::Decode, | ||
minicbor::Encode, | ||
Default, | ||
Eq, | ||
PartialEq, | ||
)] | ||
pub struct SPOState { | ||
#[n(0)] | ||
pub pools: BTreeMap<KeyHash, PoolRegistration>, | ||
#[n(1)] | ||
pub retiring: BTreeMap<KeyHash, u64>, | ||
} | ||
|
||
pub struct DRepState {} | ||
|
||
pub struct ProposalState {} | ||
|
||
pub struct VotingState {} | ||
|
||
impl LedgerState { | ||
pub fn from_directory(directory_path: impl AsRef<Path>) -> Result<Self> { | ||
let directory_path = directory_path.as_ref(); | ||
if !directory_path.exists() { | ||
bail!("directory does not exist: {}", directory_path.display()); | ||
} | ||
|
||
if !directory_path.is_dir() { | ||
bail!("path is not a directory: {}", directory_path.display()); | ||
} | ||
|
||
let mut ledger_state = Self::default(); | ||
ledger_state | ||
.load_from_directory(directory_path) | ||
.with_context(|| { | ||
format!( | ||
"Failed to load ledger state from directory: {}", | ||
directory_path.display() | ||
) | ||
})?; | ||
|
||
Ok(ledger_state) | ||
} | ||
|
||
fn load_from_directory(&mut self, directory_path: impl AsRef<Path>) -> Result<()> { | ||
let directory_path = directory_path.as_ref(); | ||
let entries = fs::read_dir(directory_path) | ||
.with_context(|| format!("failed to read directory: {}", directory_path.display()))?; | ||
|
||
for entry in entries { | ||
let entry = entry.with_context(|| "failed to read directory entry")?; | ||
let path = entry.path(); | ||
|
||
if path.is_file() && path.extension().map_or(false, |ext| ext == "cbor") { | ||
self.load_cbor_file(&path) | ||
.with_context(|| format!("failed to load CBOR file: {}", path.display()))?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn load_cbor_file(&mut self, file_path: impl AsRef<Path>) -> Result<()> { | ||
let file_path = file_path.as_ref(); | ||
let filename = file_path | ||
.file_stem() | ||
.and_then(|s| s.to_str()) | ||
.with_context(|| format!("invalid filename: {}", file_path.display()))?; | ||
|
||
let bytes = fs::read(file_path) | ||
.with_context(|| format!("failed to read file: {}", file_path.display()))?; | ||
|
||
match filename { | ||
"pools" => { | ||
self.spo_state = minicbor::decode(&bytes).with_context(|| { | ||
format!("failed to decode SPO state from: {}", file_path.display()) | ||
})?; | ||
} | ||
_ => { | ||
// ignore unknown cbor files | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'b, C> minicbor::decode::Decode<'b, C> for Ratio { | ||
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> { | ||
let tag = d.tag()?; | ||
if tag.as_u64() != 30 { | ||
return Err(minicbor::decode::Error::message("tag must be 30")); | ||
} | ||
let maybe_array_length = d.array()?; | ||
if let Some(length) = maybe_array_length { | ||
if length != 2 { | ||
return Err(minicbor::decode::Error::message( | ||
"array must be of length 2", | ||
)); | ||
} | ||
} | ||
|
||
Ok(Ratio { | ||
numerator: d.decode_with(ctx)?, | ||
denominator: d.decode_with(ctx)?, | ||
}) | ||
} | ||
} | ||
|
||
impl<C> minicbor::encode::Encode<C> for Ratio { | ||
fn encode<W: minicbor::encode::Write>( | ||
&self, | ||
e: &mut minicbor::Encoder<W>, | ||
ctx: &mut C, | ||
) -> Result<(), minicbor::encode::Error<W::Error>> { | ||
e.tag(Tag::new(30))?; | ||
e.array(2)?; | ||
e.encode_with(self.numerator, ctx)?; | ||
e.encode_with(self.denominator, ctx)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'b, C> minicbor::decode::Decode<'b, C> for Relay { | ||
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> { | ||
d.array()?; | ||
let variant = d.u16()?; | ||
|
||
match variant { | ||
0 => Ok(Relay::SingleHostAddr(SingleHostAddr { | ||
port: d.decode_with(ctx)?, | ||
ipv4: d.decode_with(ctx)?, | ||
ipv6: d.decode_with(ctx)?, | ||
})), | ||
1 => Ok(Relay::SingleHostName(SingleHostName { | ||
port: d.decode_with(ctx)?, | ||
dns_name: d.decode_with(ctx)?, | ||
})), | ||
2 => Ok(Relay::MultiHostName(MultiHostName { | ||
dns_name: d.decode_with(ctx)?, | ||
})), | ||
_ => Err(minicbor::decode::Error::message( | ||
"invalid variant id for Relay", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl<C> minicbor::encode::Encode<C> for Relay { | ||
fn encode<W: minicbor::encode::Write>( | ||
&self, | ||
e: &mut minicbor::Encoder<W>, | ||
ctx: &mut C, | ||
) -> Result<(), minicbor::encode::Error<W::Error>> { | ||
match self { | ||
Relay::SingleHostAddr(SingleHostAddr { port, ipv4, ipv6 }) => { | ||
e.array(4)?; | ||
e.encode_with(0, ctx)?; | ||
e.encode_with(port, ctx)?; | ||
e.encode_with(ipv4, ctx)?; | ||
e.encode_with(ipv6, ctx)?; | ||
|
||
Ok(()) | ||
} | ||
Relay::SingleHostName(SingleHostName { port, dns_name }) => { | ||
e.array(3)?; | ||
e.encode_with(1, ctx)?; | ||
e.encode_with(port, ctx)?; | ||
e.encode_with(dns_name, ctx)?; | ||
|
||
Ok(()) | ||
} | ||
Relay::MultiHostName(MultiHostName { dns_name }) => { | ||
e.array(2)?; | ||
e.encode_with(2, ctx)?; | ||
e.encode_with(dns_name, ctx)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.