Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ethexe): optimize inheritor field passed to eth #4375

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ethexe/common/src/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct ProtocolData {
pub struct StateTransition {
pub actor_id: ActorId,
pub new_state_hash: H256,
pub inheritor: ActorId,
pub inheritor: Option<ActorId>,
pub value_to_receive: u128,
pub value_claims: Vec<ValueClaim>,
pub messages: Vec<Message>,
Expand Down
5 changes: 2 additions & 3 deletions ethexe/contracts/src/Mirror.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ contract Mirror is IMirror {
bytes32 valueClaimsHash = _claimValues(_transition.valueClaims);

/// @dev Set inheritor if specified.
if (_transition.inheritor != address(0)) {
_setInheritor(_transition.inheritor);
if (_transition.inheritor.length != 0) {
_setInheritor(Gear.decodePackedAddress(_transition.inheritor));
}

/// @dev Update the state hash if changed.
Expand Down Expand Up @@ -232,7 +232,6 @@ contract Mirror is IMirror {
return keccak256(valueClaimsBytes);
}

// TODO (breathx): optimize inheritor to bytes WITHIN THE PR.
function _setInheritor(address _inheritor) private whileActive {
/// @dev Set inheritor.
inheritor = _inheritor;
Expand Down
17 changes: 15 additions & 2 deletions ethexe/contracts/src/libraries/Gear.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ library Gear {
struct StateTransition {
address actorId;
bytes32 newStateHash;
address inheritor;
/// @dev Must be empty if no inheritor is set, otherwise 20 bytes of an inheritor address.
bytes inheritor;
uint128 valueToReceive;
ValueClaim[] valueClaims;
Message[] messages;
Expand Down Expand Up @@ -130,6 +131,18 @@ library Gear {
return keccak256(abi.encodePacked(codeCommitment.id, codeCommitment.valid));
}

function decodePackedAddress(bytes memory data) internal pure returns (address) {
require(data.length == 20, "Address must be 20 bytes long if exist");

address addr;

assembly ("memory-safe") {
addr := mload(add(data, 20))
}

return addr;
}

function defaultComputationSettings() internal pure returns (ComputationSettings memory) {
return ComputationSettings(COMPUTATION_THRESHOLD, WVARA_PER_SECOND);
}
Expand All @@ -154,7 +167,7 @@ library Gear {
function stateTransitionHash(
address actor,
bytes32 newStateHash,
address inheritor,
bytes memory inheritor,
uint128 valueToReceive,
bytes32 valueClaimsHash,
bytes32 messagesHashesHash
Expand Down
4 changes: 3 additions & 1 deletion ethexe/contracts/test/Router.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ contract RouterTest is Test {
);

Gear.StateTransition[] memory transitions = new Gear.StateTransition[](1);

transitions[0] = Gear.StateTransition(
actorId, // actor id
bytes32(uint256(42)), // new state hash
address(0), // inheritor
abi.encodePacked(address(137)), // inheritor
uint128(0), // value to receive
new Gear.ValueClaim[](0), // value claims
messages // messages
Expand All @@ -142,6 +143,7 @@ contract RouterTest is Test {

assertEq(actor.stateHash(), bytes32(uint256(42)));
assertEq(actor.nonce(), uint256(1));
assertEq(actor.inheritor(), address(137));
}

/* helper functions */
Expand Down
2 changes: 1 addition & 1 deletion ethexe/ethereum/Mirror.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ethexe/ethereum/MirrorProxy.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ethexe/ethereum/Router.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ethexe/ethereum/TransparentUpgradeableProxy.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ethexe/ethereum/WrappedVara.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ethexe/ethereum/src/abi/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl From<StateTransition> for Gear::StateTransition {
Self {
actorId: actor_id_to_address_lossy(value.actor_id),
newStateHash: h256_to_bytes32(value.new_state_hash),
inheritor: actor_id_to_address_lossy(value.inheritor),
inheritor: maybe_actor_id_to_addr_bytes(value.inheritor),
valueToReceive: value.value_to_receive,
valueClaims: value.value_claims.into_iter().map(Into::into).collect(),
messages: value.messages.into_iter().map(Into::into).collect(),
Expand Down
8 changes: 7 additions & 1 deletion ethexe/ethereum/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sol!(
);

pub(crate) mod utils {
use alloy::primitives::{FixedBytes, Uint};
use alloy::primitives::{Bytes, FixedBytes, Uint};
use gprimitives::{ActorId, CodeId, MessageId, H256};

pub type Bytes32 = FixedBytes<32>;
Expand Down Expand Up @@ -90,6 +90,12 @@ pub(crate) mod utils {
code_id.into_bytes().into()
}

pub fn maybe_actor_id_to_addr_bytes(maybe_actor_id: Option<ActorId>) -> Bytes {
maybe_actor_id
.map(|actor_id| actor_id_to_address_lossy(actor_id).0.into())
.unwrap_or_default()
}

pub fn message_id_to_bytes32(message_id: MessageId) -> Bytes32 {
message_id.into_bytes().into()
}
Expand Down
2 changes: 1 addition & 1 deletion ethexe/runtime/common/src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<S: Storage> JournalHandler for Handler<'_, S> {
state.program = Program::Exited(value_destination);

transitions.modify_transition(id_exited, |transition| {
transition.inheritor = value_destination
transition.inheritor = Some(value_destination);
});

mem::replace(&mut state.balance, 0)
Expand Down
6 changes: 3 additions & 3 deletions ethexe/runtime/common/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl InBlockTransitions {
res.push(StateTransition {
actor_id,
new_state_hash,
inheritor: ActorId::zero(),
inheritor: modification.inheritor,
value_to_receive: modification.value_to_receive,
value_claims: modification.claims,
messages: modification.messages,
Expand All @@ -187,7 +187,7 @@ impl InBlockTransitions {
#[derive(Debug, Default)]
pub struct NonFinalTransition {
initial_state: H256,
pub inheritor: ActorId,
pub inheritor: Option<ActorId>,
pub value_to_receive: u128,
pub claims: Vec<ValueClaim>,
pub messages: Vec<Message>,
Expand All @@ -200,6 +200,6 @@ impl NonFinalTransition {
// check if state hash changed at final (always op)
&& current_state == self.initial_state
// check if with unchanged state needs commitment (op)
&& (self.inheritor.is_zero() && self.value_to_receive == 0 && self.claims.is_empty() && self.messages.is_empty())
&& (self.inheritor.is_none() && self.value_to_receive == 0 && self.claims.is_empty() && self.messages.is_empty())
}
}
7 changes: 5 additions & 2 deletions ethexe/signer/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ impl ToDigest for StateTransition {

hasher.update(actor_id.to_address_lossy().as_bytes());
hasher.update(new_state_hash.as_bytes());
hasher.update(inheritor.to_address_lossy().as_bytes());
if let Some(inheritor) = inheritor {
hasher.update(inheritor.to_address_lossy().as_bytes());
}
hasher.update(value_to_receive.to_be_bytes().as_slice());

let mut value_hasher = sha3::Keccak256::new();
Expand Down Expand Up @@ -208,7 +210,8 @@ mod tests {
let state_transition = StateTransition {
actor_id: ActorId::from(0),
new_state_hash: H256::from([1; 32]),
inheritor: ActorId::from(0),
// TODO (breathx): change from u64 impl WITHIN THE PR.
inheritor: Some(ActorId::from(0)),
value_to_receive: 0,
value_claims: vec![],
messages: vec![Message {
Expand Down
2 changes: 1 addition & 1 deletion ethexe/validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ mod tests {
let transition = StateTransition {
actor_id: H256::random().0.into(),
new_state_hash: H256::random(),
inheritor: H256::random().0.into(),
inheritor: Some(H256::random().0.into()),
value_to_receive: 123,
value_claims: vec![],
messages: vec![],
Expand Down
Loading