|
| 1 | +// Copyright 2021-2023 Protocol Labs |
| 2 | +// SPDX-License-Identifier: Apache-2.0, MIT |
| 3 | + |
| 4 | +use fil_actors_runtime::actor_dispatch; |
| 5 | +use fil_actors_runtime::actor_error; |
| 6 | +use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR; |
| 7 | +use fil_actors_runtime::runtime::{ActorCode, Runtime}; |
| 8 | +use fil_actors_runtime::ActorDowncast; |
| 9 | +use fil_actors_runtime::ActorError; |
| 10 | +use fvm_shared::error::ExitCode; |
| 11 | + |
| 12 | +use crate::{ConstructorParams, Method, State, OBJECTSTORE_ACTOR_NAME}; |
| 13 | + |
| 14 | +fil_actors_runtime::wasm_trampoline!(Actor); |
| 15 | + |
| 16 | +pub struct Actor; |
| 17 | + |
| 18 | +impl Actor { |
| 19 | + fn constructor(rt: &impl Runtime, params: ConstructorParams) -> Result<(), ActorError> { |
| 20 | + // Note(sander): We're setting this up to be a subnet-wide actor for a single repo. |
| 21 | + // Note(sander): In the future, this could be deployed dynamically for multi repo subnets. |
| 22 | + rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?; |
| 23 | + |
| 24 | + let state = State::new(rt.store()).map_err(|e| { |
| 25 | + e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to create empty KAMT") |
| 26 | + })?; |
| 27 | + |
| 28 | + rt.create(&state)?; |
| 29 | + |
| 30 | + Ok(()) |
| 31 | + } |
| 32 | + |
| 33 | + // Note(sander): Probably obvious, but example actor method that mutates state |
| 34 | + // fn push_block_hash(rt: &impl Runtime, params: PushBlockParams) -> Result<(), ActorError> { |
| 35 | + // rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?; |
| 36 | + // |
| 37 | + // rt.transaction(|st: &mut State, rt| { |
| 38 | + // // load the blockhashes AMT |
| 39 | + // let mut blockhashes = Array::load(&st.blockhashes, rt.store()).map_err(|e| { |
| 40 | + // e.downcast_default( |
| 41 | + // ExitCode::USR_ILLEGAL_STATE, |
| 42 | + // "failed to load blockhashes states", |
| 43 | + // ) |
| 44 | + // })?; |
| 45 | + // |
| 46 | + // // push the block to the AMT |
| 47 | + // blockhashes.set(params.epoch as u64, params.block).unwrap(); |
| 48 | + // |
| 49 | + // // remove the oldest block if the AMT is full (note that this assume the |
| 50 | + // // for_each_while iterates in order, which it seems to do) |
| 51 | + // if blockhashes.count() > st.lookback_len { |
| 52 | + // let mut first_idx = 0; |
| 53 | + // blockhashes |
| 54 | + // .for_each_while(|i, _: &BlockHash| { |
| 55 | + // first_idx = i; |
| 56 | + // Ok(false) |
| 57 | + // }) |
| 58 | + // .unwrap(); |
| 59 | + // blockhashes.delete(first_idx).unwrap(); |
| 60 | + // } |
| 61 | + // |
| 62 | + // // save the new blockhashes AMT cid root |
| 63 | + // st.blockhashes = blockhashes.flush().map_err(|e| { |
| 64 | + // e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to save blockhashes") |
| 65 | + // })?; |
| 66 | + // |
| 67 | + // Ok(()) |
| 68 | + // })?; |
| 69 | + // |
| 70 | + // Ok(()) |
| 71 | + // } |
| 72 | +} |
| 73 | + |
| 74 | +impl ActorCode for Actor { |
| 75 | + type Methods = Method; |
| 76 | + |
| 77 | + fn name() -> &'static str { |
| 78 | + OBJECTSTORE_ACTOR_NAME |
| 79 | + } |
| 80 | + |
| 81 | + actor_dispatch! { |
| 82 | + Constructor => constructor, |
| 83 | + // PushBlockHash => push_block_hash, |
| 84 | + // LookbackLen => lookback_len, |
| 85 | + // GetBlockHash => get_block_hash, |
| 86 | + } |
| 87 | +} |
0 commit comments