0
0
// docs/acceptance/0019-bidderdids-discovery.md (plan 0040 built 1): a rental's bidderDids filters discovery, never replaces it. The
// rental names [the target's second bidder, the requester account's own DID] (that DID publishes no offering: discovery never
// finds it). The requester's discovery (requester_bidder_discovery) must hold the bidder and never the other DID, the bidder
// must win (the receipt is in its repo), then the release. SKIP with fewer than 2 bidders.
use abi::{Event, Handle, Run, entries};
use serde_json::{Value, json};
use steps::{MIN, S, host};
fn run_0019(run: Run<Value>) -> Result<Handle, String> {
let args = &run.args;
let mut s = S::new(run.state, args["now"].as_str().unwrap_or_default());
if let Some(h) = steps::market_event(&mut s, &run.event) {
return Ok(h);
}
let name = args["name"].as_str().ok_or("name")?.to_string();
let bidders: Vec<String> = args["bidders"]
.as_array()
.into_iter()
.flatten()
.filter_map(|b| b["did"].as_str().map(String::from))
.collect();
let Some(bidder) = bidders.get(1).cloned() else {
return Ok(s.unmet("0019 needs 2 bidders in the target"));
};
let absent = args["did"].as_str().ok_or("did")?.to_string();
let mut h = Handle::new();
match run.event {
Event::Start => {
s.set("held");
let a = json!(["--hold", "--cpus", "2", "--mem", "4G", "--expires-in", "1h"]);
h = h
.call(steps::host_sub(
"req",
"request.start",
json!({ "name": name, "args": a, "bidderDids": [bidder, absent] }),
))
.timer("deadline", 30 * MIN);
}
Event::Timer { id } if id == "deadline" => {
return Ok(s.fail(format!("not done in 30 min (phase {})", s.phase())));
}
Event::Message { id, data, .. } if id == "req" => {
if s.st["receipt"].is_null() && data["receipt"].is_string() {
s.st["receipt"] = data["receipt"].clone();
}
match data["msg"].as_str().unwrap_or_default() {
"requester_bidder_discovery" => {
let found: Vec<String> = data["bidders"]
.as_array()
.into_iter()
.flatten()
.filter_map(|d| d.as_str().map(String::from))
.collect();
s.reading("discovery", json!({ "bidders": found, "sources": data["sources"], "candidates": data["candidates"] }));
if found.contains(&absent) {
return Ok(s.fail(format!("discovery took {absent}, which publishes no offering: bidderDids replaced discovery")));
}
if !found.contains(&bidder) {
return Ok(s.fail(format!("discovery did not keep {bidder}, a discovered bidder named in bidderDids: {data}")));
}
s.flag("discovered");
}
"contract" if data["phase"] == "Failed" => {
return Ok(s.fail(format!("the contract failed: {}", data["message"])));
}
"contract" if data["phase"] == "Held" => {
let receipt = s.st["receipt"].as_str().unwrap_or_default().to_string();
let winner = receipt
.trim_start_matches("at://")
.split('/')
.next()
.unwrap_or_default();
if winner != bidder {
return Ok(s.fail(format!(
"the winner is {winner}, not the named bidder {bidder}"
)));
}
if !s.has("discovered") {
return Ok(s.fail("Held before any discovery that kept the named bidder"));
}
s.reading("held_s", s.t());
s.set("release");
s.flag("releasing");
h = h.call(host("release", "request.release", json!({ "name": name })));
}
"exited" if !s.has("releasing") => {
return Ok(s.fail(format!("run request exited before Held: {data}")));
}
_ => {}
}
}
Event::Result { id, result, .. } => {
let o = match steps::out(&s, &id, result.output()) {
Ok(o) => o,
Err(f) => return Ok(f),
};
if id == "release" {
s.reading("release_s", o["seconds"].clone());
return Ok(s.pass());
}
}
Event::Closed { id, error } if id == "req" && !s.has("releasing") => {
return Ok(s.fail(format!("req closed: {}", error.unwrap_or_default())));
}
_ => {}
}
Ok(s.keep(h))
}
entries! { run => run_0019 }