Skip to content

Commit 9dc62bc

Browse files
authored
Merge pull request #406 from filecoin-project/steb/one-engine-per-fvm
feat: fvm: create one multi-engine per FVM, not network version
2 parents c149dfa + 64cbc26 commit 9dc62bc

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

rust/src/fvm/engine.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::hash_map::Entry;
21
use std::collections::HashMap;
32
use std::ops::RangeInclusive;
43
use std::sync::{Arc, Mutex};
@@ -52,15 +51,32 @@ pub trait AbstractMultiEngine: Send + Sync {
5251
) -> anyhow::Result<InnerFvmMachine>;
5352
}
5453

54+
#[derive(Eq, PartialEq, Hash, Debug, Copy, Clone)]
55+
enum EngineVersion {
56+
V1,
57+
V2,
58+
}
59+
5560
// The generic engine container
5661
pub struct MultiEngineContainer {
5762
concurrency: u32,
58-
engines: Mutex<HashMap<u32, Arc<dyn AbstractMultiEngine + 'static>>>,
63+
engines: Mutex<HashMap<EngineVersion, Arc<dyn AbstractMultiEngine + 'static>>>,
5964
}
6065

6166
const LOTUS_FVM_CONCURRENCY_ENV_NAME: &str = "LOTUS_FVM_CONCURRENCY";
6267
const VALID_CONCURRENCY_RANGE: RangeInclusive<u32> = 1..=128;
6368

69+
impl TryFrom<u32> for EngineVersion {
70+
type Error = anyhow::Error;
71+
fn try_from(value: u32) -> Result<Self, Self::Error> {
72+
match value {
73+
16 | 17 => Ok(EngineVersion::V1),
74+
18 | 19 | 20 => Ok(EngineVersion::V2),
75+
_ => return Err(anyhow!("network version not supported")),
76+
}
77+
}
78+
}
79+
6480
impl MultiEngineContainer {
6581
/// Constructs a new multi-engine container with the default concurrency (4).
6682
pub fn new() -> MultiEngineContainer {
@@ -107,23 +123,21 @@ impl MultiEngineContainer {
107123
}
108124

109125
pub fn get(&self, nv: u32) -> anyhow::Result<Arc<dyn AbstractMultiEngine + 'static>> {
126+
let engine_version = nv.try_into()?;
110127
let mut locked = self
111128
.engines
112129
.lock()
113130
.map_err(|e| anyhow!("engine lock poisoned: {e}"))?;
114-
Ok(match locked.entry(nv) {
115-
Entry::Occupied(v) => v.get().clone(),
116-
Entry::Vacant(v) => v
117-
.insert(match nv {
118-
16 | 17 => {
119-
Arc::new(MultiEngine2::new()) as Arc<dyn AbstractMultiEngine + 'static>
120-
}
121-
18 | 19 | 20 => Arc::new(MultiEngine3::new(self.concurrency))
122-
as Arc<dyn AbstractMultiEngine + 'static>,
123-
_ => return Err(anyhow!("network version not supported")),
124-
})
125-
.clone(),
126-
})
131+
Ok(locked
132+
.entry(engine_version)
133+
.or_insert_with(|| match engine_version {
134+
EngineVersion::V1 => {
135+
Arc::new(MultiEngine2::new()) as Arc<dyn AbstractMultiEngine + 'static>
136+
}
137+
EngineVersion::V2 => Arc::new(MultiEngine3::new(self.concurrency))
138+
as Arc<dyn AbstractMultiEngine + 'static>,
139+
})
140+
.clone())
127141
}
128142
}
129143

0 commit comments

Comments
 (0)