Skip to content

Commit

Permalink
Introduce maybe_with_gas constructor for packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Sep 22, 2023
1 parent 46f2e3f commit e9c0958
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 49 deletions.
96 changes: 47 additions & 49 deletions core-backend/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,12 @@ where
} = ctx.read_as(read_hash_val)?;
let payload = Self::read_message_payload(ctx, read_payload)?;

let packet = if let Some(gas_limit) = gas_limit {
HandlePacket::new_with_gas(destination.into(), payload, gas_limit, value)
} else {
HandlePacket::new(destination.into(), payload, value)
};

ctx.ext_mut().send(packet, delay).map_err(Into::into)
ctx.ext_mut()
.send(
HandlePacket::maybe_with_gas(destination.into(), payload, gas_limit, value),
delay,
)
.map_err(Into::into)
}

pub fn send(pid_value_ptr: u32, payload_ptr: u32, len: u32, delay: u32) -> impl SysCall<Ext> {
Expand Down Expand Up @@ -396,14 +395,17 @@ where
value,
} = ctx.read_as(read_pid_value)?;

let packet = if let Some(gas_limit) = gas_limit {
HandlePacket::new_with_gas(destination.into(), Default::default(), gas_limit, value)
} else {
HandlePacket::new(destination.into(), Default::default(), value)
};

ctx.ext_mut()
.send_commit(handle, packet, delay)
.send_commit(
handle,
HandlePacket::maybe_with_gas(
destination.into(),
Default::default(),
gas_limit,
value,
),
delay,
)
.map_err(Into::into)
}

Expand Down Expand Up @@ -688,13 +690,9 @@ where
let value = Self::register_and_read_value(ctx, value_ptr)?;
let payload = Self::read_message_payload(ctx, read_payload)?;

let packet = if let Some(gas_limit) = gas_limit {
ReplyPacket::new_with_gas(payload, gas_limit, value)
} else {
ReplyPacket::new(payload, value)
};

ctx.ext_mut().reply(packet).map_err(Into::into)
ctx.ext_mut()
.reply(ReplyPacket::maybe_with_gas(payload, gas_limit, value))
.map_err(Into::into)
}

pub fn reply(payload_ptr: u32, len: u32, value_ptr: u32) -> impl SysCall<Ext> {
Expand Down Expand Up @@ -729,13 +727,13 @@ where
) -> Result<MessageId, RunFallibleError> {
let value = Self::register_and_read_value(ctx, value_ptr)?;

let packet = if let Some(gas_limit) = gas_limit {
ReplyPacket::new_with_gas(Default::default(), gas_limit, value)
} else {
ReplyPacket::new(Default::default(), value)
};

ctx.ext_mut().reply_commit(packet).map_err(Into::into)
ctx.ext_mut()
.reply_commit(ReplyPacket::maybe_with_gas(
Default::default(),
gas_limit,
value,
))
.map_err(Into::into)
}

pub fn reply_commit(value_ptr: u32) -> impl SysCall<Ext> {
Expand Down Expand Up @@ -839,13 +837,13 @@ where
// Charge for `len` is inside `reply_push_input`
ctx.ext_mut().reply_push_input(offset, len)?;

let packet = if let Some(gas_limit) = gas_limit {
ReplyPacket::new_with_gas(Default::default(), gas_limit, value)
} else {
ReplyPacket::new(Default::default(), value)
};

ctx.ext_mut().reply_commit(packet).map_err(Into::into)
ctx.ext_mut()
.reply_commit(ReplyPacket::maybe_with_gas(
Default::default(),
gas_limit,
value,
))
.map_err(Into::into)
}

pub fn reply_input(offset: u32, len: u32, value_ptr: u32) -> impl SysCall<Ext> {
Expand Down Expand Up @@ -903,14 +901,17 @@ where
// Charge for `len` inside `send_push_input`
ctx.ext_mut().send_push_input(handle, offset, len)?;

let packet = if let Some(gas_limit) = gas_limit {
HandlePacket::new_with_gas(destination.into(), Default::default(), gas_limit, value)
} else {
HandlePacket::new(destination.into(), Default::default(), value)
};

ctx.ext_mut()
.send_commit(handle, packet, delay)
.send_commit(
handle,
HandlePacket::maybe_with_gas(
destination.into(),
Default::default(),
gas_limit,
value,
),
delay,
)
.map_err(Into::into)
}

Expand Down Expand Up @@ -1196,14 +1197,11 @@ where
let salt = Self::read_message_payload(ctx, read_salt)?;
let payload = Self::read_message_payload(ctx, read_payload)?;

let packet = if let Some(gas_limit) = gas_limit {
InitPacket::new_with_gas(code_id.into(), salt, payload, gas_limit, value)
} else {
InitPacket::new(code_id.into(), salt, payload, value)
};

ctx.ext_mut()
.create_program(packet, delay)
.create_program(
InitPacket::maybe_with_gas(code_id.into(), salt, payload, gas_limit, value),
delay,
)
.map_err(Into::into)
}

Expand Down
13 changes: 13 additions & 0 deletions core/src/message/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ impl HandlePacket {
}
}

/// Create new packet with optional gas.
pub fn maybe_with_gas(
destination: ProgramId,
payload: Payload,
gas_limit: Option<GasLimit>,
value: Value,
) -> Self {
match gas_limit {
None => Self::new(destination, payload, value),
Some(gas_limit) => Self::new_with_gas(destination, payload, gas_limit, value),
}
}

/// Prepend payload.
pub(super) fn try_prepend(&mut self, data: Payload) -> Result<(), PayloadSizeError> {
self.payload.try_prepend(data)
Expand Down
14 changes: 14 additions & 0 deletions core/src/message/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ impl InitPacket {
}
}

/// Create new InitPacket with optional gas.
pub fn maybe_with_gas(
code_id: CodeId,
salt: Salt,
payload: Payload,
gas_limit: Option<GasLimit>,
value: Value,
) -> Self {
match gas_limit {
None => Self::new(code_id, salt, payload, value),
Some(gas_limit) => Self::new_with_gas(code_id, salt, payload, gas_limit, value),
}
}

/// Packet destination (newly created program id).
pub fn destination(&self) -> ProgramId {
self.program_id
Expand Down
8 changes: 8 additions & 0 deletions core/src/message/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ impl ReplyPacket {
}
}

/// Create new manual ReplyPacket with optional gas.
pub fn maybe_with_gas(payload: Payload, gas_limit: Option<GasLimit>, value: Value) -> Self {
match gas_limit {
None => Self::new(payload, value),
Some(gas_limit) => Self::new_with_gas(payload, gas_limit, value),
}
}

// TODO: consider using here `impl CoreError` and/or provide `AsStatusCode`
// trait or append such functionality to `CoreError` (issue #1083).
/// Create new system generated ReplyPacket.
Expand Down

0 comments on commit e9c0958

Please sign in to comment.