|
1 | 1 | // Copyright 2019-2022 ChainSafe Systems
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0, MIT
|
3 | 3 |
|
| 4 | +use cid::multihash::{Code, MultihashGeneric}; |
4 | 5 | use cid::Cid;
|
5 | 6 | use std::collections::{BTreeMap, BTreeSet};
|
6 | 7 |
|
@@ -347,9 +348,7 @@ impl Actor {
|
347 | 348 |
|
348 | 349 | deal.proposal.provider = provider;
|
349 | 350 | deal.proposal.client = client;
|
350 |
| - let pcid = deal.proposal.cid().map_err( |
351 |
| - |e| actor_error!(illegal_argument; "failed to take cid of proposal {}: {}", di, e), |
352 |
| - )?; |
| 351 | + let pcid = cid(rt, &deal.proposal)?; |
353 | 352 |
|
354 | 353 | // check proposalCids for duplication within message batch
|
355 | 354 | // check state PendingProposals for duplication across messages
|
@@ -593,9 +592,7 @@ impl Actor {
|
593 | 592 | })?
|
594 | 593 | .ok_or_else(|| actor_error!(not_found, "no such deal_id: {}", deal_id))?;
|
595 | 594 |
|
596 |
| - let propc = proposal |
597 |
| - .cid() |
598 |
| - .map_err(|e| ActorError::from(e).wrap("failed to calculate proposal Cid"))?; |
| 595 | + let propc = cid(rt, proposal)?; |
599 | 596 |
|
600 | 597 | let has =
|
601 | 598 | msm.pending_deals.as_ref().unwrap().has(&propc.to_bytes()).map_err(|e| {
|
@@ -820,10 +817,7 @@ impl Actor {
|
820 | 817 | })?
|
821 | 818 | .clone();
|
822 | 819 |
|
823 |
| - let dcid = deal.cid().map_err(|e| { |
824 |
| - ActorError::from(e) |
825 |
| - .wrap(format!("failed to calculate cid for proposal {}", deal_id)) |
826 |
| - })?; |
| 820 | + let dcid = cid(rt, &deal)?; |
827 | 821 |
|
828 | 822 | let state = msm
|
829 | 823 | .deal_states
|
@@ -1292,6 +1286,21 @@ where
|
1292 | 1286 | Ok(())
|
1293 | 1287 | }
|
1294 | 1288 |
|
| 1289 | +pub const DAG_CBOR: u64 = 0x71; // TODO is there a better place to get this? |
| 1290 | + |
| 1291 | +fn cid<BS, RT>(rt: &RT, proposal: &DealProposal) -> Result<Cid, ActorError> |
| 1292 | +where |
| 1293 | + BS: Blockstore, |
| 1294 | + RT: Runtime<BS>, |
| 1295 | +{ |
| 1296 | + const DIGEST_SIZE: u32 = 32; |
| 1297 | + let data = &proposal.marshal_cbor()?; |
| 1298 | + let hash = MultihashGeneric::wrap(Code::Blake2b256.into(), &rt.hash_blake2b(data)) |
| 1299 | + .map_err(|e| actor_error!(illegal_argument; "failed to take cid of proposal {}", e))?; |
| 1300 | + debug_assert_eq!(u32::from(hash.size()), DIGEST_SIZE, "expected 32byte digest"); |
| 1301 | + Ok(Cid::new_v1(DAG_CBOR, hash)) |
| 1302 | +} |
| 1303 | + |
1295 | 1304 | /// Resolves a provider or client address to the canonical form against which a balance should be held, and
|
1296 | 1305 | /// the designated recipient address of withdrawals (which is the same, for simple account parties).
|
1297 | 1306 | fn escrow_address<BS, RT>(
|
|
0 commit comments