Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/message_pool/msgpool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn republish_pending_messages<T>(
cur_tipset: &SyncRwLock<Tipset>,
republished: &SyncRwLock<HashSet<Cid>>,
local_addrs: &SyncRwLock<Vec<Address>>,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
Comment thread
hanabi1224 marked this conversation as resolved.
Outdated
chain_config: &ChainConfig,
) -> Result<(), Error>
where
Expand Down Expand Up @@ -223,7 +223,7 @@ pub async fn head_change<T>(
republished: &SyncRwLock<HashSet<Cid>>,
pending: &SyncRwLock<HashMap<Address, MsgSet>>,
cur_tipset: &SyncRwLock<Tipset>,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
state_nonce_cache: &SizeTrackingLruCache<StateNonceCacheKey, u64>,
revert: Vec<Tipset>,
apply: Vec<Tipset>,
Expand Down Expand Up @@ -326,7 +326,7 @@ where

pub(in crate::message_pool) struct MpoolCtx<'a, T> {
pub api: &'a T,
pub key_cache: &'a SizeTrackingLruCache<Address, Address>,
pub key_cache: &'a SizeTrackingLruCache<u64, Address>,
pub pending: &'a SyncRwLock<HashMap<Address, MsgSet>>,
pub ts: &'a Tipset,
}
Expand Down
33 changes: 20 additions & 13 deletions src/message_pool/msgpool/msg_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ pub struct MessagePool<T> {
pub bls_sig_cache: Arc<SizeTrackingLruCache<CidWrapper, Signature>>,
/// A cache for BLS signature keyed by Cid
pub sig_val_cache: Arc<SizeTrackingLruCache<CidWrapper, ()>>,
/// Cache for ID address to key address resolution.
pub key_cache: Arc<SizeTrackingLruCache<Address, Address>>,
/// Cache for ID address ID to key address resolution.
pub key_cache: Arc<SizeTrackingLruCache<u64, Address>>,
/// Cache for state nonce lookups keyed by (`TipsetKey`, `Address`).
pub state_nonce_cache: Arc<SizeTrackingLruCache<StateNonceCacheKey, u64>>,
/// A set of republished messages identified by their Cid
Expand All @@ -303,25 +303,27 @@ pub struct MessagePool<T> {
/// Non-ID addresses are returned unchanged.
pub(in crate::message_pool) fn resolve_to_key<T: Provider>(
api: &T,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
addr: &Address,
cur_ts: &Tipset,
) -> Result<Address, Error> {
if addr.protocol() != Protocol::ID {
Comment thread
hanabi1224 marked this conversation as resolved.
return Ok(*addr);
}
if let Some(resolved) = key_cache.get_cloned(addr) {
let id = addr.id().ok();
Comment thread
hanabi1224 marked this conversation as resolved.
if let Some(id) = &id
&& let Some(resolved) = key_cache.get_cloned(id)
{
return Ok(resolved);
}
let resolved = api.resolve_to_key(addr, cur_ts)?;
key_cache.push(*addr, resolved);
let resolved = api.resolve_to_deterministic_address_at_finality(addr, cur_ts)?;
if let Some(id) = id {
key_cache.push(id, resolved);
}
Ok(resolved)
}

/// Get the state nonce for an address, accounting for messages already included in `cur_ts`.
pub(in crate::message_pool) fn get_state_sequence<T: Provider>(
api: &T,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
state_nonce_cache: &SizeTrackingLruCache<StateNonceCacheKey, u64>,
addr: &Address,
cur_ts: &Tipset,
Expand Down Expand Up @@ -854,7 +856,7 @@ pub(in crate::message_pool) fn add_helper<T>(
api: &T,
bls_sig_cache: &SizeTrackingLruCache<CidWrapper, Signature>,
pending: &SyncRwLock<HashMap<Address, MsgSet>>,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
cur_ts: &Tipset,
msg: SignedMessage,
sequence: u64,
Expand Down Expand Up @@ -1572,11 +1574,16 @@ mod tests {
.unwrap();

// f0300 exists in lookback state (root_a) → resolves successfully.
let result = Provider::resolve_to_key(&cs, &Address::new_id(300), &head).unwrap();
let result = Provider::resolve_to_deterministic_address_at_finality(
&cs,
&Address::new_id(300),
&head,
)
.unwrap();
assert_eq!(result, bls_a);

// f0400 exists only in head state (root_b), not in lookback → fails.
Provider::resolve_to_key(&cs, &Address::new_id(400), &head)
Provider::resolve_to_deterministic_address_at_finality(&cs, &Address::new_id(400), &head)
.expect_err("actor only in head state must not resolve via finality lookback");
}
}
18 changes: 13 additions & 5 deletions src/message_pool/msgpool/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ pub trait Provider {
fn load_tipset(&self, tsk: &TipsetKey) -> Result<Tipset, Error>;
/// Computes the base fee
fn chain_compute_base_fee(&self, ts: &Tipset) -> Result<TokenAmount, Error>;
/// Resolve an address to its key form using the tipset's parent state.
fn resolve_to_key(&self, addr: &Address, ts: &Tipset) -> Result<Address, Error>;
/// Similar to [`crate::state_manager::StateManager::resolve_to_deterministic_address`] but fails if the ID address being resolved isn't reorg-stable yet.
/// It should not be used for consensus-critical subsystems.
fn resolve_to_deterministic_address_at_finality(
&self,
addr: &Address,
ts: &Tipset,
) -> Result<Address, Error>;
/// Return all messages included in the given tipset.
fn messages_for_tipset(&self, ts: &Tipset) -> Result<Arc<Vec<ChainMessage>>, Error>;
// Get max number of messages per actor in the pool
Expand Down Expand Up @@ -103,9 +108,11 @@ impl<DB: Blockstore> Provider for ChainStore<DB> {
.map_err(|err| err.into())
}

/// Resolves an address to its deterministic key form using the state at
/// finality look-back, This ensures the resolved address is reorg-stable.
fn resolve_to_key(&self, addr: &Address, ts: &Tipset) -> Result<Address, Error> {
fn resolve_to_deterministic_address_at_finality(
&self,
addr: &Address,
ts: &Tipset,
) -> Result<Address, Error> {
match addr.protocol() {
BLS | Secp256k1 | Delegated => Ok(*addr),
Actor => Err(Error::Other(
Expand All @@ -121,6 +128,7 @@ impl<DB: Blockstore> Provider for ChainStore<DB> {
)
.map_err(|e| Error::Other(e.to_string()))?
} else {
// Matches the logic at <https://github.com/filecoin-project/lotus/blob/v1.35.1/chain/stmgr/stmgr.go#L361>
ts.clone()
};

Expand Down
2 changes: 1 addition & 1 deletion src/message_pool/msgpool/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ pub(in crate::message_pool) fn run_head_change<T>(
api: &T,
bls_sig_cache: &SizeTrackingLruCache<CidWrapper, Signature>,
pending: &RwLock<HashMap<Address, MsgSet>>,
key_cache: &SizeTrackingLruCache<Address, Address>,
key_cache: &SizeTrackingLruCache<u64, Address>,
from: Tipset,
to: Tipset,
rmsgs: &mut HashMap<Address, HashMap<u64, SignedMessage>>,
Expand Down
6 changes: 5 additions & 1 deletion src/message_pool/msgpool/test_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ impl Provider for TestApi {
Ok(TokenAmount::from_atto(100))
}

fn resolve_to_key(&self, addr: &Address, _ts: &Tipset) -> Result<Address, Error> {
fn resolve_to_deterministic_address_at_finality(
&self,
addr: &Address,
_ts: &Tipset,
) -> Result<Address, Error> {
Ok(self.inner.lock().resolve_addr(addr))
}

Expand Down
2 changes: 2 additions & 0 deletions src/state_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,8 @@ where
state.verified_client_data_cap(self.blockstore(), id)
}

/// Similar to [`StateTree::resolve_to_deterministic_addr`] but does not allow [`crate::shim::address::Protocol::Actor`] type of addresses.
/// Uses the [`Tipset`] `ts` to generate the VM state.
pub async fn resolve_to_deterministic_address(
self: &Arc<Self>,
address: Address,
Expand Down
Loading