filecoin-project / filecoin-project/builtin-actors

Strongly typed Cid fields

Open
#489 7 comments 1 reaction 0 assignees View on GitHub
enhancement P3
Dominant language
Rust
Stars
96
Forks
94
Avg merge
6d 17h
Merged PRs (30d)
3

Description

To improve type safety of using `Cid` fields in `State` like [here](https://github.com/filecoin-project/builtin-actors/blob/v8.0.0/actors/market/src/state.rs#L29), we just merged a PR in the fork Alfonso's fork for Hierarchical Subnet Coordinator Actors: https://github.com/adlrocha/builtin-actors/pull/8

The PR added a couple of new types: `TCid` is a typed CID that can take `TLink`, `THamt` or `TAmt` as a generic type descriptor. With that the `State` definition looks like this:
```rust
pub struct State {
pub network_name: SubnetID,
pub total_subnets: u64,
#[serde(with = "bigint_ser")]
pub min_stake: TokenAmount,
pub subnets: TCid>,
pub check_period: ChainEpoch,
pub checkpoints: TCid>,
pub check_msg_registry: TCid>,
pub nonce: u64,
pub bottomup_nonce: u64,
pub bottomup_msg_meta: TCid>,
pub applied_bottomup_nonce: u64,
pub applied_topdown_nonce: u64,
pub atomic_exec_registry: TCid>,
}
```

Creating an instance of it no longer requires initializing fields separately, it simplifies to assignments such as:

```rust
subnets: TCid::new_hamt(store)?,
```

These fields can be manipulated with a few convenience methods that make sure the content is loaded, flushed, and the underlying CID updated:

```rust
let deleted = self.subnets.modify(store, |subnets| {
subnets
.delete(&id.to_bytes())
.map_err(|e| e.downcast_wrap(format!("failed to delete subnet for id {}", id)))
.map(|x| x.is_some())
})?;
```

We reckon that all actor writers would benefit from using this module, if we could find a good place for it. Perhaps in `fil_actors_runtime` or `fvm_shared`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.