Skip to content

Commit

Permalink
chore: in/out nonce string
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Mar 12, 2024
1 parent 0c0306a commit b8cf15d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 64 deletions.
90 changes: 71 additions & 19 deletions homestar-invocation/src/task/instruction/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ use uuid::Uuid;
type Nonce96 = GenericArray<u8, U12>;
type Nonce128 = GenericArray<u8, U16>;

/// Incoming type for nonce conversion.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum IncomingTyp {
/// Nonce incoming as a string.
String,
/// Nonce incoming as bytes.
Bytes,
}

/// Enumeration over allowed `nonce` types.
#[derive(Clone, Debug, PartialEq, EnumAsInner, Serialize, Deserialize)]
pub enum Nonce {
/// 96-bit, 12-byte nonce, e.g. [xid].
Nonce96(Nonce96),
Nonce96(Nonce96, IncomingTyp),
/// 128-bit, 16-byte nonce.
Nonce128(Nonce128),
Nonce128(Nonce128, IncomingTyp),
/// No Nonce attributed.
Empty,
}
Expand All @@ -38,22 +47,37 @@ impl Nonce {
/// Default generator, outputting a [xid] nonce, which is a 96-bit, 12-byte
/// nonce.
pub fn generate() -> Self {
Nonce::Nonce96(*GenericArray::from_slice(xid::new().as_bytes()))
Nonce::Nonce96(
*GenericArray::from_slice(xid::new().as_bytes()),
IncomingTyp::Bytes,
)
}

/// Generate a default, 128-bit, 16-byte nonce via [Uuid::new_v4()].
pub fn generate_128() -> Self {
Nonce::Nonce128(*GenericArray::from_slice(Uuid::new_v4().as_bytes()))
Nonce::Nonce128(
*GenericArray::from_slice(Uuid::new_v4().as_bytes()),
IncomingTyp::Bytes,
)
}

/// Convert the nonce to a byte vector.
pub fn to_vec(&self) -> Vec<u8> {
match self {
Nonce::Nonce96(nonce, _) => nonce.to_vec(),
Nonce::Nonce128(nonce, _) => nonce.to_vec(),
Nonce::Empty => vec![],
}
}
}

impl fmt::Display for Nonce {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Nonce::Nonce96(nonce) => {
Nonce::Nonce96(nonce, _) => {
write!(f, "{}", Base32HexLower.encode(nonce.as_slice()))
}
Nonce::Nonce128(nonce) => {
Nonce::Nonce128(nonce, _) => {
write!(f, "{}", Base32HexLower.encode(nonce.as_slice()))
}
Nonce::Empty => write!(f, ""),
Expand All @@ -64,8 +88,20 @@ impl fmt::Display for Nonce {
impl From<Nonce> for Ipld {
fn from(nonce: Nonce) -> Self {
match nonce {
Nonce::Nonce96(nonce) => Ipld::Bytes(nonce.to_vec()),
Nonce::Nonce128(nonce) => Ipld::Bytes(nonce.to_vec()),
Nonce::Nonce96(nonce, typ) => {
if let IncomingTyp::Bytes = typ {
Ipld::Bytes(nonce.to_vec())
} else {
Ipld::String(Base32HexLower.encode(nonce.as_slice()))
}
}
Nonce::Nonce128(nonce, typ) => {
if let IncomingTyp::Bytes = typ {
Ipld::Bytes(nonce.to_vec())
} else {
Ipld::String(Base32HexLower.encode(nonce.as_slice()))
}
}
Nonce::Empty => Ipld::String("".to_string()),
}
}
Expand All @@ -80,14 +116,26 @@ impl TryFrom<Ipld> for Nonce {
Ipld::String(s) => {
let bytes = Base32HexLower.decode(s)?;
match bytes.len() {
12 => Ok(Nonce::Nonce96(*GenericArray::from_slice(&bytes))),
16 => Ok(Nonce::Nonce128(*GenericArray::from_slice(&bytes))),
12 => Ok(Nonce::Nonce96(
*GenericArray::from_slice(&bytes),
IncomingTyp::String,
)),
16 => Ok(Nonce::Nonce128(
*GenericArray::from_slice(&bytes),
IncomingTyp::String,
)),
other => Err(Error::unexpected_ipld(other.to_owned().into())),
}
}
Ipld::Bytes(v) => match v.len() {
12 => Ok(Nonce::Nonce96(*GenericArray::from_slice(&v))),
16 => Ok(Nonce::Nonce128(*GenericArray::from_slice(&v))),
12 => Ok(Nonce::Nonce96(
*GenericArray::from_slice(&v),
IncomingTyp::Bytes,
)),
16 => Ok(Nonce::Nonce128(
*GenericArray::from_slice(&v),
IncomingTyp::Bytes,
)),
other_ipld => Err(Error::unexpected_ipld(other_ipld.to_owned().into())),
},
_ => Ok(Nonce::Empty),
Expand Down Expand Up @@ -163,7 +211,7 @@ mod test {
let gen = Nonce::generate();
let ipld = Ipld::from(gen.clone());

let inner = if let Nonce::Nonce96(nonce) = gen {
let inner = if let Nonce::Nonce96(nonce, _) = gen {
Ipld::Bytes(nonce.to_vec())
} else {
panic!("No conversion!")
Expand All @@ -178,7 +226,7 @@ mod test {
let gen = Nonce::generate_128();
let ipld = Ipld::from(gen.clone());

let inner = if let Nonce::Nonce128(nonce) = gen {
let inner = if let Nonce::Nonce128(nonce, _) = gen {
Ipld::Bytes(nonce.to_vec())
} else {
panic!("No conversion!")
Expand Down Expand Up @@ -214,7 +262,10 @@ mod test {

assert_eq!(bytes, b);
assert_eq!(ipld, Ipld::Bytes(b.to_vec()));
assert_eq!(nonce, Nonce::Nonce128(*GenericArray::from_slice(b)));
assert_eq!(
nonce,
Nonce::Nonce128(*GenericArray::from_slice(b), IncomingTyp::Bytes)
);
assert_eq!(nonce, Nonce::try_from(ipld.clone()).unwrap());

let nonce: Nonce = ipld.clone().try_into().unwrap();
Expand All @@ -228,7 +279,7 @@ mod test {
let string = nonce.to_string();
let from_string = Nonce::try_from(Ipld::String(string.clone())).unwrap();

assert_eq!(nonce, from_string);
assert_eq!(nonce.to_vec(), from_string.to_vec());
assert_eq!(string, nonce.to_string());
}

Expand All @@ -246,9 +297,10 @@ mod test {
let nnc = map.get("nnc").unwrap();
let nnc: Nonce = Nonce::try_from(nnc.clone()).unwrap();
assert_eq!(nnc.to_string(), in_nnc);
let nonce = Nonce::Nonce96(*GenericArray::from_slice(
Base32HexLower.decode(in_nnc).unwrap().as_slice(),
));
let nonce = Nonce::Nonce96(
*GenericArray::from_slice(Base32HexLower.decode(in_nnc).unwrap().as_slice()),
IncomingTyp::String,
);
assert_eq!(nnc, nonce);
}
}
4 changes: 2 additions & 2 deletions homestar-invocation/src/test_utils/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn wasm_instruction_with_nonce<'a, T>() -> (Instruction<'a, T>, NonceBytes)
]))),
nonce.clone(),
),
nonce.as_nonce96().unwrap().to_vec(),
nonce.to_vec(),
)
}

Expand All @@ -139,7 +139,7 @@ pub fn instruction_with_nonce<'a, T>() -> (Instruction<'a, T>, NonceBytes) {
Input::Ipld(Ipld::List(vec![Ipld::Bool(true)])),
nonce.clone(),
),
nonce.as_nonce96().unwrap().to_vec(),
nonce.to_vec(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion homestar-runtime/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn test_workflow_run_integration_nonced() -> Result<()> {
let rpc_port = proc_info.rpc_port;
let metrics_port = proc_info.metrics_port;
let ws_port = proc_info.ws_port;
let workflow_cid = "bafyrmicbtl7g4zrbarazdbjnk2gxxbbzin3iaf6y2zs6va5auqiyhu5m2e";
let workflow_cid = "bafyrmibpelk6e7cmfqfbjulatdyaiyoobb7mme26iskxsl6w4viyac37q4";
let toml = format!(
r#"
[node]
Expand Down
84 changes: 42 additions & 42 deletions homestar-runtime/tests/fixtures/test-workflow-add-one-nonced.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
{
"tasks": [
{
"cause": null,
"meta": {
"fuel": 18446744073709552000,
"memory": 4294967296,
"time": 100000
},
"prf": [],
"run": {
"input": {
"args": [1],
"func": "add_one"
},
"nnc": "1sod60ml6g26mfhsrsa0",
"op": "wasm/run",
"rsc": "ipfs://bafybeia32q3oy6u47x624rmsmgrrlpn7ulruissmz5z2ap6alv7goe7h3q"
}
"tasks": [
{
"cause": null,
"meta": {
"fuel": 18446744073709552000,
"memory": 4294967296,
"time": 100000
},
"prf": [],
"run": {
"input": {
"args": [1],
"func": "add_one"
},
{
"cause": null,
"meta": {
"fuel": 18446744073709552000,
"memory": 4294967296,
"time": 100000
},
"prf": [],
"run": {
"input": {
"args": [
{
"await/ok": {
"/": "bafyrmie3zzu3mhtyjghgqvskoc7maiiablp2htexigs2xr3lvgt6csslly"
}
}
],
"func": "add_one"
},
"nnc": "4ja3jhlhs3b9rk3app40",
"op": "wasm/run",
"rsc": "ipfs://bafybeia32q3oy6u47x624rmsmgrrlpn7ulruissmz5z2ap6alv7goe7h3q"
"nnc": "1sod60ml6g26mfhsrsa0",
"op": "wasm/run",
"rsc": "ipfs://bafybeia32q3oy6u47x624rmsmgrrlpn7ulruissmz5z2ap6alv7goe7h3q"
}
},
{
"cause": null,
"meta": {
"fuel": 18446744073709552000,
"memory": 4294967296,
"time": 100000
},
"prf": [],
"run": {
"input": {
"args": [
{
"await/ok": {
"/": "bafyrmig5x46c6nzk74adlo6ffwo4bi7fr42jkv54zqa5kzazfirb47ninm"
}
}
}
]
],
"func": "add_one"
},
"nnc": "4ja3jhlhs3b9rk3app40",
"op": "wasm/run",
"rsc": "ipfs://bafybeia32q3oy6u47x624rmsmgrrlpn7ulruissmz5z2ap6alv7goe7h3q"
}
}
]
}

0 comments on commit b8cf15d

Please sign in to comment.