Skip to content

Commit

Permalink
Merge branch 'casper-network:feat-2.0' into feat-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zacshowa authored Apr 23, 2024
2 parents 39d1150 + a528ab8 commit b4a5674
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 136 deletions.
3 changes: 2 additions & 1 deletion execution_engine/src/runtime_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,8 @@ where
| StoredValue::ContractPackage(_)
| StoredValue::ContractWasm(_)
| StoredValue::MessageTopic(_)
| StoredValue::Message(_) => Ok(()),
| StoredValue::Message(_)
| StoredValue::Reservation(_) => Ok(()),
}
}

Expand Down
51 changes: 36 additions & 15 deletions resources/test/sse_data_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1699,9 +1699,7 @@
"Reserved": {
"type": "object",
"required": [
"paid_amount",
"receipt",
"strike_price"
"receipt"
],
"properties": {
"receipt": {
Expand All @@ -1711,18 +1709,6 @@
"$ref": "#/definitions/Digest"
}
]
},
"paid_amount": {
"description": "Price paid in the past to reserve space in a future block.",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"strike_price": {
"description": "The gas price at the time of reservation.",
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
"additionalProperties": false
Expand Down Expand Up @@ -3920,6 +3906,19 @@
}
},
"additionalProperties": false
},
{
"description": "A reservation record.",
"type": "object",
"required": [
"Reservation"
],
"properties": {
"Reservation": {
"$ref": "#/definitions/ReservationKind"
}
},
"additionalProperties": false
}
]
},
Expand Down Expand Up @@ -4783,6 +4782,28 @@
}
}
},
"ReservationKind": {
"description": "Container for bytes recording location, type and data for a gas reservation",
"type": "object",
"required": [
"receipt",
"reservation_data",
"reservation_kind"
],
"properties": {
"receipt": {
"$ref": "#/definitions/Digest"
},
"reservation_kind": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
},
"reservation_data": {
"$ref": "#/definitions/Bytes"
}
}
},
"TransformError": {
"description": "Error type for applying and combining transforms.\n\nA `TypeMismatch` occurs when a transform cannot be applied because the types are not compatible (e.g. trying to add a number to a string).",
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions storage/src/tracking_copy/byte_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ByteSize for StoredValue {
}
StoredValue::Message(message_summary) => message_summary.serialized_length(),
StoredValue::NamedKey(named_key) => named_key.serialized_length(),
StoredValue::Reservation(reservation_kind) => reservation_kind.serialized_length(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions storage/src/tracking_copy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,10 @@ where
StoredValue::Message(_) => {
return Ok(query.into_not_found_result("Message value found."));
}
// TODO: We may be interested in this value, check the logic
StoredValue::Reservation(_) => {
return Ok(query.into_not_found_result("Reservation value found."))
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions types/src/execution/transform_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ impl TransformKindV2 {
let found = "Message".to_string();
Err(StoredValueTypeMismatch::new(expected, found).into())
}
StoredValue::Reservation(_) => {
let expected = "Contract or Account".to_string();
let found = "Reservation".to_string();
Err(StoredValueTypeMismatch::new(expected, found).into())
}
},
TransformKindV2::Failure(error) => Err(error),
}
Expand Down
1 change: 1 addition & 0 deletions types/src/gens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ pub fn stored_value_arb() -> impl Strategy<Value = StoredValue> {
StoredValue::MessageTopic(_) => stored_value,
StoredValue::Message(_) => stored_value,
StoredValue::NamedKey(_) => stored_value,
StoredValue::Reservation(_) => stored_value,
})
}

Expand Down
14 changes: 13 additions & 1 deletion types/src/stored_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::{
contract_wasm::ContractWasm,
contracts::{Contract, ContractPackage},
package::Package,
system::auction::{Bid, BidKind, EraInfo, UnbondingPurse, WithdrawPurse},
system::{
auction::{Bid, BidKind, EraInfo, UnbondingPurse, WithdrawPurse},
reservations::ReservationKind,
},
AddressableEntity, ByteCode, CLValue, DeployInfo, TransferV1,
};
pub use global_state_identifier::GlobalStateIdentifier;
Expand All @@ -50,6 +53,7 @@ enum Tag {
MessageTopic = 15,
Message = 16,
NamedKey = 17,
Reservation = 18,
}

/// A value stored in Global State.
Expand Down Expand Up @@ -98,6 +102,8 @@ pub enum StoredValue {
Message(MessageChecksum),
/// A NamedKey record.
NamedKey(NamedKeyValue),
/// A reservation record.
Reservation(ReservationKind),
}

impl StoredValue {
Expand Down Expand Up @@ -360,6 +366,7 @@ impl StoredValue {
StoredValue::MessageTopic(_) => "MessageTopic".to_string(),
StoredValue::Message(_) => "Message".to_string(),
StoredValue::NamedKey(_) => "NamedKey".to_string(),
StoredValue::Reservation(_) => "Reservation".to_string(),
}
}

Expand All @@ -383,6 +390,7 @@ impl StoredValue {
StoredValue::MessageTopic(_) => Tag::MessageTopic,
StoredValue::Message(_) => Tag::Message,
StoredValue::NamedKey(_) => Tag::NamedKey,
StoredValue::Reservation(_) => Tag::Reservation,
}
}
}
Expand Down Expand Up @@ -664,6 +672,7 @@ impl ToBytes for StoredValue {
}
StoredValue::Message(message_digest) => message_digest.serialized_length(),
StoredValue::NamedKey(named_key_value) => named_key_value.serialized_length(),
StoredValue::Reservation(reservation_kind) => reservation_kind.serialized_length(),
}
}

Expand All @@ -690,6 +699,7 @@ impl ToBytes for StoredValue {
}
StoredValue::Message(message_digest) => message_digest.write_bytes(writer),
StoredValue::NamedKey(named_key_value) => named_key_value.write_bytes(writer),
StoredValue::Reservation(reservation_kind) => reservation_kind.write_bytes(writer),
}
}
}
Expand Down Expand Up @@ -782,6 +792,7 @@ mod serde_helpers {
MessageTopic(&'a MessageTopicSummary),
Message(&'a MessageChecksum),
NamedKey(&'a NamedKeyValue),
Reservation(&'a ReservationKind),
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -837,6 +848,7 @@ mod serde_helpers {
HumanReadableSerHelper::Message(message_digest)
}
StoredValue::NamedKey(payload) => HumanReadableSerHelper::NamedKey(payload),
StoredValue::Reservation(payload) => HumanReadableSerHelper::Reservation(payload),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions types/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod caller;
mod error;
pub mod handle_payment;
pub mod mint;
pub mod reservations;
pub mod standard_payment;
mod system_contract_type;

Expand Down
4 changes: 4 additions & 0 deletions types/src/system/reservations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Contains implementation of the gas reservation system
mod reservation_kind;

pub use reservation_kind::ReservationKind;
42 changes: 42 additions & 0 deletions types/src/system/reservations/reservation_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::{
bytesrepr,
bytesrepr::{Bytes, ToBytes, U8_SERIALIZED_LENGTH},
Digest,
};
use alloc::vec::Vec;
#[cfg(feature = "datasize")]
use datasize::DataSize;
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Container for bytes recording location, type and data for a gas reservation
#[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct ReservationKind {
receipt: Digest,
reservation_kind: u8,
reservation_data: Bytes,
}

impl ToBytes for ReservationKind {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let mut buffer = bytesrepr::allocate_buffer(self)?;
self.write_bytes(&mut buffer)?;
Ok(buffer)
}

fn serialized_length(&self) -> usize {
self.receipt.serialized_length()
+ U8_SERIALIZED_LENGTH
+ self.reservation_data.serialized_length()
}

fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
self.receipt.write_bytes(writer)?;
self.reservation_kind.write_bytes(writer)?;
self.reservation_data.write_bytes(writer)?;
Ok(())
}
}
51 changes: 6 additions & 45 deletions types/src/transaction/pricing_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ pub enum PricingMode {
Reserved {
/// Pre-paid receipt.
receipt: Digest,
/// Price paid in the past to reserve space in a future block.
paid_amount: u64,
/// The gas price at the time of reservation.
strike_price: u8,
},
}

Expand All @@ -77,11 +73,7 @@ impl PricingMode {
1 => PricingMode::Fixed {
gas_price_tolerance: rng.gen(),
},
2 => PricingMode::Reserved {
receipt: rng.gen(),
paid_amount: rng.gen(),
strike_price: rng.gen(),
},
2 => PricingMode::Reserved { receipt: rng.gen() },
_ => unreachable!(),
}
}
Expand All @@ -101,15 +93,7 @@ impl Display for PricingMode {
payment_amount, gas_price, standard_payment
)
}
PricingMode::Reserved {
receipt,
paid_amount,
strike_price,
} => write!(
formatter,
"reserved: {} paid_amount: {} strike_price: {}",
receipt, paid_amount, strike_price
),
PricingMode::Reserved { receipt } => write!(formatter, "reserved: {}", receipt),
PricingMode::Fixed {
gas_price_tolerance,
} => write!(formatter, "fixed pricing {}", gas_price_tolerance),
Expand All @@ -130,15 +114,9 @@ impl ToBytes for PricingMode {
gas_price.write_bytes(writer)?;
standard_payment.write_bytes(writer)
}
PricingMode::Reserved {
receipt,
paid_amount,
strike_price,
} => {
PricingMode::Reserved { receipt } => {
RESERVED_TAG.write_bytes(writer)?;
receipt.write_bytes(writer)?;
paid_amount.write_bytes(writer)?;
strike_price.write_bytes(writer)
receipt.write_bytes(writer)
}
PricingMode::Fixed {
gas_price_tolerance,
Expand Down Expand Up @@ -167,15 +145,7 @@ impl ToBytes for PricingMode {
+ gas_price.serialized_length()
+ standard_payment.serialized_length()
}
PricingMode::Reserved {
receipt,
paid_amount,
strike_price,
} => {
receipt.serialized_length()
+ paid_amount.serialized_length()
+ strike_price.serialized_length()
}
PricingMode::Reserved { receipt } => receipt.serialized_length(),
PricingMode::Fixed {
gas_price_tolerance,
} => gas_price_tolerance.serialized_length(),
Expand Down Expand Up @@ -212,16 +182,7 @@ impl FromBytes for PricingMode {
}
RESERVED_TAG => {
let (receipt, remainder) = Digest::from_bytes(remainder)?;
let (paid_amount, remainder) = u64::from_bytes(remainder)?;
let (strike_price, remainder) = u8::from_bytes(remainder)?;
Ok((
PricingMode::Reserved {
receipt,
paid_amount,
strike_price,
},
remainder,
))
Ok((PricingMode::Reserved { receipt }, remainder))
}
_ => Err(bytesrepr::Error::Formatting),
}
Expand Down
Loading

0 comments on commit b4a5674

Please sign in to comment.