Skip to content

Commit

Permalink
refactor(ethexe): refine storage accesses; redesign maybehash (#4310)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx authored Nov 7, 2024
1 parent d43076a commit 2006b3d
Show file tree
Hide file tree
Showing 22 changed files with 1,416 additions and 1,252 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl<T, E: Default, const N: usize> TryFrom<Vec<T>> for LimitedVec<T, E, N> {
}

impl<T: Clone + Default, E: Default, const N: usize> LimitedVec<T, E, N> {
/// Constructs a new, empty `LimitedVec<T>`.
pub const fn new() -> Self {
Self(Vec::new(), PhantomData)
}

/// Tries to create new limited vector of length `len`
/// with default initialized elements.
pub fn try_new_default(len: usize) -> Result<Self, E> {
Expand Down
13 changes: 7 additions & 6 deletions ethexe/cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use ethexe_db::{BlockMetaStorage, Database, MemDb, ScheduledTask};
use ethexe_ethereum::{router::RouterQuery, Ethereum};
use ethexe_observer::{Event, MockBlobReader, Observer, Query};
use ethexe_processor::Processor;
use ethexe_runtime_common::state::Storage;
use ethexe_runtime_common::state::{Storage, ValueWithExpiry};
use ethexe_sequencer::Sequencer;
use ethexe_signer::Signer;
use ethexe_validator::Validator;
Expand Down Expand Up @@ -250,10 +250,11 @@ async fn mailbox() {
let expected_mailbox = BTreeMap::from_iter([(
env.sender_id,
BTreeMap::from_iter([
(mid_expected_message, (0, expiry)),
(ping_expected_message, (0, expiry)),
(mid_expected_message, ValueWithExpiry { value: 0, expiry }),
(ping_expected_message, ValueWithExpiry { value: 0, expiry }),
]),
)]);

let mirror = env.ethereum.mirror(pid.try_into().unwrap());
let state_hash = mirror.query().state_hash().await.unwrap();

Expand All @@ -263,7 +264,7 @@ async fn mailbox() {
.mailbox_hash
.with_hash_or_default(|hash| node.db.read_mailbox(hash).unwrap());

assert_eq!(mailbox, expected_mailbox);
assert_eq!(mailbox.into_inner(), expected_mailbox);

mirror
.send_reply(ping_expected_message, "PONG", 0)
Expand All @@ -288,10 +289,10 @@ async fn mailbox() {

let expected_mailbox = BTreeMap::from_iter([(
env.sender_id,
BTreeMap::from_iter([(mid_expected_message, (0, expiry))]),
BTreeMap::from_iter([(mid_expected_message, ValueWithExpiry { value: 0, expiry })]),
)]);

assert_eq!(mailbox, expected_mailbox);
assert_eq!(mailbox.into_inner(), expected_mailbox);

mirror.claim_value(mid_expected_message).await.unwrap();

Expand Down
8 changes: 4 additions & 4 deletions ethexe/common/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use alloc::vec::Vec;
use gear_core::message::{Message, ReplyDetails};
use gear_core::message::{ReplyDetails, StoredMessage};
use gprimitives::{ActorId, CodeId, MessageId, H256};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -74,9 +74,9 @@ pub struct OutgoingMessage {
pub reply_details: Option<ReplyDetails>,
}

impl From<Message> for OutgoingMessage {
fn from(value: Message) -> Self {
let (id, _source, destination, payload, _gas_limit, value, details) = value.into_parts();
impl From<StoredMessage> for OutgoingMessage {
fn from(value: StoredMessage) -> Self {
let (id, _source, destination, payload, value, details) = value.into_parts();
Self {
id,
destination,
Expand Down
85 changes: 44 additions & 41 deletions ethexe/db/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use ethexe_common::{
BlockRequestEvent,
};
use ethexe_runtime_common::state::{
Allocations, DispatchStash, Mailbox, MemoryPages, MessageQueue, ProgramState, Storage, Waitlist,
Allocations, DispatchStash, HashOf, Mailbox, MemoryPages, MessageQueue, ProgramState, Storage,
Waitlist,
};
use gear_core::{
code::InstrumentedCode,
Expand Down Expand Up @@ -482,83 +483,85 @@ impl Storage for Database {
self.cas.write(&state.encode())
}

fn read_queue(&self, hash: H256) -> Option<MessageQueue> {
let data = self.cas.read(&hash)?;
Some(
MessageQueue::decode(&mut &data[..])
.expect("Failed to decode data into `MessageQueue`"),
)
fn read_queue(&self, hash: HashOf<MessageQueue>) -> Option<MessageQueue> {
self.cas.read(&hash.hash()).map(|data| {
MessageQueue::decode(&mut &data[..]).expect("Failed to decode data into `MessageQueue`")
})
}

fn write_queue(&self, queue: MessageQueue) -> H256 {
self.cas.write(&queue.encode())
fn write_queue(&self, queue: MessageQueue) -> HashOf<MessageQueue> {
unsafe { HashOf::new(self.cas.write(&queue.encode())) }
}

fn read_waitlist(&self, hash: H256) -> Option<Waitlist> {
self.cas.read(&hash).map(|data| {
fn read_waitlist(&self, hash: HashOf<Waitlist>) -> Option<Waitlist> {
self.cas.read(&hash.hash()).map(|data| {
Waitlist::decode(&mut data.as_slice()).expect("Failed to decode data into `Waitlist`")
})
}

fn write_waitlist(&self, waitlist: Waitlist) -> H256 {
self.cas.write(&waitlist.encode())
fn write_waitlist(&self, waitlist: Waitlist) -> HashOf<Waitlist> {
unsafe { HashOf::new(self.cas.write(&waitlist.encode())) }
}

fn read_stash(&self, hash: H256) -> Option<DispatchStash> {
self.cas.read(&hash).map(|data| {
fn read_stash(&self, hash: HashOf<DispatchStash>) -> Option<DispatchStash> {
self.cas.read(&hash.hash()).map(|data| {
DispatchStash::decode(&mut data.as_slice())
.expect("Failed to decode data into `DispatchStash`")
})
}

fn write_stash(&self, stash: DispatchStash) -> H256 {
self.cas.write(&stash.encode())
fn write_stash(&self, stash: DispatchStash) -> HashOf<DispatchStash> {
unsafe { HashOf::new(self.cas.write(&stash.encode())) }
}

fn read_mailbox(&self, hash: H256) -> Option<Mailbox> {
self.cas.read(&hash).map(|data| {
fn read_mailbox(&self, hash: HashOf<Mailbox>) -> Option<Mailbox> {
self.cas.read(&hash.hash()).map(|data| {
Mailbox::decode(&mut data.as_slice()).expect("Failed to decode data into `Mailbox`")
})
}

fn write_mailbox(&self, mailbox: Mailbox) -> H256 {
self.cas.write(&mailbox.encode())
fn write_mailbox(&self, mailbox: Mailbox) -> HashOf<Mailbox> {
unsafe { HashOf::new(self.cas.write(&mailbox.encode())) }
}

fn read_pages(&self, hash: H256) -> Option<MemoryPages> {
let data = self.cas.read(&hash)?;
Some(MemoryPages::decode(&mut &data[..]).expect("Failed to decode data into `MemoryPages`"))
fn read_pages(&self, hash: HashOf<MemoryPages>) -> Option<MemoryPages> {
self.cas.read(&hash.hash()).map(|data| {
MemoryPages::decode(&mut &data[..]).expect("Failed to decode data into `MemoryPages`")
})
}

fn write_pages(&self, pages: MemoryPages) -> H256 {
self.cas.write(&pages.encode())
fn write_pages(&self, pages: MemoryPages) -> HashOf<MemoryPages> {
unsafe { HashOf::new(self.cas.write(&pages.encode())) }
}

fn read_allocations(&self, hash: H256) -> Option<Allocations> {
let data = self.cas.read(&hash)?;
Some(Allocations::decode(&mut &data[..]).expect("Failed to decode data into `Allocations`"))
fn read_allocations(&self, hash: HashOf<Allocations>) -> Option<Allocations> {
self.cas.read(&hash.hash()).map(|data| {
Allocations::decode(&mut &data[..]).expect("Failed to decode data into `Allocations`")
})
}

fn write_allocations(&self, allocations: Allocations) -> H256 {
self.cas.write(&allocations.encode())
fn write_allocations(&self, allocations: Allocations) -> HashOf<Allocations> {
unsafe { HashOf::new(self.cas.write(&allocations.encode())) }
}

fn read_payload(&self, hash: H256) -> Option<Payload> {
let data = self.cas.read(&hash)?;
Some(Payload::try_from(data).expect("Failed to decode data into `Payload`"))
fn read_payload(&self, hash: HashOf<Payload>) -> Option<Payload> {
self.cas
.read(&hash.hash())
.map(|data| Payload::try_from(data).expect("Failed to decode data into `Payload`"))
}

fn write_payload(&self, payload: Payload) -> H256 {
self.cas.write(payload.inner())
fn write_payload(&self, payload: Payload) -> HashOf<Payload> {
unsafe { HashOf::new(self.cas.write(payload.inner())) }
}

fn read_page_data(&self, hash: H256) -> Option<PageBuf> {
let data = self.cas.read(&hash)?;
Some(PageBuf::decode(&mut data.as_slice()).expect("Failed to decode data into `PageBuf`"))
fn read_page_data(&self, hash: HashOf<PageBuf>) -> Option<PageBuf> {
self.cas.read(&hash.hash()).map(|data| {
PageBuf::decode(&mut data.as_slice()).expect("Failed to decode data into `PageBuf`")
})
}

fn write_page_data(&self, data: PageBuf) -> H256 {
self.cas.write(&data)
fn write_page_data(&self, data: PageBuf) -> HashOf<PageBuf> {
unsafe { HashOf::new(self.cas.write(&data)) }
}
}

Expand Down
Loading

0 comments on commit 2006b3d

Please sign in to comment.