# uftp
**UFTP 3.1, the USEF Flex Trading Protocol, in Rust.** Distribution system operators (DSOs)
and aggregators use UFTP to trade grid flexibility: a DSO asks for less consumption at a
congestion point, an aggregator offers it, the DSO orders it and settles it. Since version 3,
UFTP is maintained by LF Energy as [Shapeshifter](https://lfenergy.org/projects/shapeshifter/).
**[Documentation](https://hupe1980.github.io/uftp/)** ·
[API reference](https://docs.rs/uftp) ·
[Getting started](https://hupe1980.github.io/uftp/docs/getting-started/)
> Not the UDP multicast file-transfer tool of the same name.
```sh
cargo add uftp # add `--features http` for the http 1.x mapping
```
**Status:** 0.1, early. Before 1.0 a minor version may change the API; the
[changelog](CHANGELOG.md) says how.
## Highlights
- **All 26 messages of UFTP 3.1.0**, with their XML, CS1 (Ed25519) signatures, and the rules
of §3.5 and the use cases, under the specification's own rejection reasons.
- **A receiver that answers correctly.** A bad document gets an HTTP status. A valid message
that breaks a rule gets 200 and a signed Rejected response, sent only to the verified
sender.
- **One receive call.** `Endpoint::receive` verifies before it parses, then checks the rules,
the market, the clock, the outbox and replays, in that order.
- **Checked against the reference.** In CI, LF Energy's Java library verifies and re-signs
every type uftp writes, and its validators judge 51 rule cases next to uftp. The
differences are its own bugs, each listed in the
[documentation](https://hupe1980.github.io/uftp/docs/interoperability/).
- **Sans I/O.** No clock, randomness, files or network: you bring them. With the `http`
feature it maps onto the `http` 1.x types (axum, hyper, reqwest, ureq) without a runtime.
- **DST-correct ISPs**, refusals that are safe to log, fuzzed decoding and mutation-tested
rules.
## Example
```rust
use uftp::*;
// The DSO asks for 500 kW less consumption tomorrow, 17:00–19:00 (ISPs 69–76).
let meta = Metadata::new(dso_domain, agr_domain, now, new_id());
let header = FlexHeader::new(meta, "Europe/Amsterdam".parse()?, day, congestion_point);
let isps = vec![FlexRequestIsp::requested(IspSpan::new(69, 8)?, -500_000, 0)];
let request = FlexRequest::new(header, expires, isps);
let document = seal(&request.into(), Role::Dso, &dso_key)?.to_xml(); // what it POSTs
// The aggregator's endpoint.
let mut endpoint = Endpoint::new(OpenOptions::for_recipient(agr_domain), SignedDuration::from_mins(15))
.with_market(Market { isp_duration: IspDuration::PT15M, time_zone: "Europe/Amsterdam".parse()? });
match endpoint.receive(document.as_bytes(), now, directory.lookup()) {
Ok(opened) => { /* offer, or answer */ }
// e.http_status() answers the POST; a broken rule also gets a signed Rejected response.
Err(e) => if let Some(answer) = e.response(new_id(), now) { /* seal and send */ },
}
```
Runnable examples:
- `cargo run --example flex_request`: one request, signed, opened and answered.
- `cargo run --example loopback`: a whole trade (request, offer, order, settlement) with a
replayed order.
## Development
The [justfile](justfile) mirrors CI:
```sh
just ci # fmt, clippy (both feature sets), tests with xmllint, examples, docs, deny, MSRV
just fuzz # both fuzz targets, 60 s each (nightly, cargo-fuzz)
just mutants # mutation testing
just interop # the Shapeshifter differential (Docker)
just site # the documentation site, with live reload (zola)
```
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option. The XSDs under `xsd/` are the Shapeshifter
project's, Apache-2.0 (see `xsd/NOTICE`), and are used by the tests only.