Skip to content

Commit 5633356

Browse files
feat: Avoid blake2b hashing in wasm (#577)
Avoid blake2b hashing in wasm Co-authored-by: Jiaying Wang <[email protected]>
1 parent b206906 commit 5633356

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

actors/market/src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2019-2022 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4+
use cid::multihash::{Code, MultihashGeneric};
45
use cid::Cid;
56
use std::collections::{BTreeMap, BTreeSet};
67

@@ -347,9 +348,7 @@ impl Actor {
347348

348349
deal.proposal.provider = provider;
349350
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)?;
353352

354353
// check proposalCids for duplication within message batch
355354
// check state PendingProposals for duplication across messages
@@ -593,9 +592,7 @@ impl Actor {
593592
})?
594593
.ok_or_else(|| actor_error!(not_found, "no such deal_id: {}", deal_id))?;
595594

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)?;
599596

600597
let has =
601598
msm.pending_deals.as_ref().unwrap().has(&propc.to_bytes()).map_err(|e| {
@@ -820,10 +817,7 @@ impl Actor {
820817
})?
821818
.clone();
822819

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)?;
827821

828822
let state = msm
829823
.deal_states
@@ -1292,6 +1286,21 @@ where
12921286
Ok(())
12931287
}
12941288

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+
12951304
/// Resolves a provider or client address to the canonical form against which a balance should be held, and
12961305
/// the designated recipient address of withdrawals (which is the same, for simple account parties).
12971306
fn escrow_address<BS, RT>(

0 commit comments

Comments
 (0)