Skip to content

Commit

Permalink
fix: Update nonce schema with IPLD bytes (#593)
Browse files Browse the repository at this point in the history
# Description

This PR makes the following changes:

- [x] Update the nonce schema one of IPLD bytes or empty string
- [x] Add workflow validation test that uses a nonce

The schema will be updated by the schemas action, but as a preview it
looks like this:

```json
"nnc": {
  "description": "A 12-byte or 16-byte nonce encoded as IPLD bytes. Use empty string for no nonce.",
  "oneOf": [
    {
      "$ref": "#/definitions/ipld_bytes"
    },
    {
      "type": "string",
      "const": ""
    }
  ]
},
```
```json
"ipld_bytes": {
  "title": "IPLD bytes",
  "description": "Base64 encoded binary",
  "type": "object",
  "properties": {
    "/": {
      "type": "object",
      "properties": {
        "bytes": {
          "type": "string"
        }
      }
    }
  }
},
```

## Type of change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] This change requires a documentation update (schemas will be
updated by the action)

## Test plan (required)

A new `validate_and_parse_workflow_with_nonce` has been added that
validates a workflow with a task that uses a nonce.
  • Loading branch information
bgins committed Feb 29, 2024
1 parent 8fb03c3 commit 13f8503
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
23 changes: 18 additions & 5 deletions homestar-invocation/src/task/instruction/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! [Instruction]: super::Instruction

use crate::{Error, Unit};
use crate::{ipld::schema, Error, Unit};
use const_format::formatcp;
use enum_as_inner::EnumAsInner;
use generic_array::{
Expand All @@ -16,6 +16,7 @@ use schemars::{
JsonSchema,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{borrow::Cow, fmt, module_path};
use uuid::Uuid;

Expand Down Expand Up @@ -103,17 +104,29 @@ impl JsonSchema for Nonce {
Cow::Borrowed(formatcp!("{}::Nonce", module_path!()))
}

fn json_schema(_gen: &mut SchemaGenerator) -> Schema {
let schema = SchemaObject {
instance_type: Some(SingleOrVec::Single(InstanceType::String.into())),
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema = SchemaObject {
instance_type: None,
metadata: Some(Box::new(Metadata {
description: Some(
"A 12-byte or 16-byte nonce. Use empty string for no nonce.".to_string(),
"A 12-byte or 16-byte nonce encoded as IPLD bytes. Use empty string for no nonce.".to_string(),
),
..Default::default()
})),
..Default::default()
};

let empty_string = SchemaObject {
instance_type: Some(SingleOrVec::Single(InstanceType::String.into())),
const_value: Some(json!("")),
..Default::default()
};

schema.subschemas().one_of = Some(vec![
gen.subschema_for::<schema::IpldBytesStub>(),
Schema::Object(empty_string),
]);

schema.into()
}
}
Expand Down
22 changes: 22 additions & 0 deletions homestar-runtime/src/runner/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ mod test {
workflow_file.validate_and_parse().await.unwrap();
assert_eq!(workflow, newly_validated_workflow);
}

#[tokio::test]
async fn validate_and_parse_workflow_with_nonce() {
let path = PathBuf::from("./fixtures/test_nonce.json");
let config = Resources::default();
let (instruction, _) = test_utils::wasm_instruction_with_nonce::<Arg>();

let task = Task::new(
RunInstruction::Expanded(instruction.clone()),
config.clone().into(),
UcanPrf::default(),
);

let workflow = Workflow::new(vec![task]);

workflow.to_file(path.display().to_string()).unwrap();
let workflow_file = ReadWorkflow { file: path.clone() };

let (validated_workflow, _settings) = workflow_file.validate_and_parse().await.unwrap();

assert_eq!(workflow, validated_workflow);
}
}

0 comments on commit 13f8503

Please sign in to comment.