Skip to content
Merged
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
36 changes: 36 additions & 0 deletions crates/tx3-cardano/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub type TxBody =
pub struct ChainPoint {
pub slot: u64,
pub hash: Vec<u8>,
pub timestamp: u128,
}

pub struct Compiler {
Expand Down Expand Up @@ -109,6 +110,28 @@ impl tx3_lang::backend::Compiler for Compiler {
}]))
}
ir::CompilerOp::ComputeTipSlot => Ok(ir::Expression::Number(self.cursor.slot as i128)),
ir::CompilerOp::ComputeSlotToTime(x) => {
let slot = coercion::expr_into_number(&x)?;
if slot < 0 {
return Err(tx3_lang::backend::Error::CoerceError(
format!("{}", slot),
"positive slot number".to_string(),
));
}

Ok(ir::Expression::Number(slot_to_time(slot, &self.cursor)))
}
ir::CompilerOp::ComputeTimeToSlot(x) => {
let time = coercion::expr_into_number(&x)?;
if time < 0 {
return Err(tx3_lang::backend::Error::CoerceError(
format!("{}", time),
"positive timestamp".to_string(),
));
}

Ok(ir::Expression::Number(time_to_slot(time, &self.cursor)))
}
}
}
}
Expand All @@ -118,3 +141,16 @@ fn eval_size_fees(tx: &[u8], pparams: &PParams, extra_fees: Option<u64>) -> u64
+ pparams.min_fee_constant
+ extra_fees.unwrap_or(DEFAULT_EXTRA_FEES)
}

fn slot_to_time(slot: i128, cursor: &ChainPoint) -> i128 {
let current_time = cursor.timestamp as i128;
let time_diff = slot - cursor.slot as i128;
current_time + (time_diff * 1000)
}

fn time_to_slot(time: i128, cursor: &ChainPoint) -> i128 {
let current_slot = cursor.slot as i128;
let current_time = cursor.timestamp as i128;
let time_diff = time - current_time;
current_slot + (time_diff / 1000)
}
1 change: 1 addition & 0 deletions crates/tx3-cardano/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn test_compiler(config: Option<Config>) -> Compiler {
ChainPoint {
slot: 101674141,
hash: vec![],
timestamp: 1757611408,
},
)
}
Expand Down
38 changes: 38 additions & 0 deletions crates/tx3-lang/src/analyzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ impl Analyzable for DataExpr {
DataExpr::StaticAssetConstructor(x) => x.analyze(parent),
DataExpr::AnyAssetConstructor(x) => x.analyze(parent),
DataExpr::MinUtxo(x) => x.analyze(parent),
DataExpr::SlotToTime(x) => x.analyze(parent),
DataExpr::TimeToSlot(x) => x.analyze(parent),
DataExpr::ConcatOp(x) => x.analyze(parent),
_ => AnalyzeReport::default(),
}
Expand All @@ -583,6 +585,8 @@ impl Analyzable for DataExpr {
DataExpr::StaticAssetConstructor(x) => x.is_resolved(),
DataExpr::AnyAssetConstructor(x) => x.is_resolved(),
DataExpr::MinUtxo(x) => x.is_resolved(),
DataExpr::SlotToTime(x) => x.is_resolved(),
DataExpr::TimeToSlot(x) => x.is_resolved(),
DataExpr::ConcatOp(x) => x.is_resolved(),
_ => true,
}
Expand Down Expand Up @@ -1233,4 +1237,38 @@ mod tests {
let result = analyze(&mut ast);
assert!(!result.errors.is_empty());
}

#[test]
fn test_time_and_slot_conversion() {
let mut ast = crate::parsing::parse_string(
r#"
party Sender;

type TimestampDatum {
slot_time: Int,
time: Int,
}

tx create_timestamp_tx() {
input source {
from: Sender,
min_amount: Ada(2000000),
}

output timestamp_output {
to: Sender,
amount: source - fees,
datum: TimestampDatum {
slot_time: time_to_slot(1666716638000),
time: slot_to_time(60638),
},
}
}
"#,
)
.unwrap();

let result = analyze(&mut ast);
assert!(result.errors.is_empty());
}
}
4 changes: 4 additions & 0 deletions crates/tx3-lang/src/applying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,8 @@ impl Composite for ir::CompilerOp {
ir::CompilerOp::BuildScriptAddress(x) => vec![x],
ir::CompilerOp::ComputeMinUtxo(x) => vec![x],
ir::CompilerOp::ComputeTipSlot => vec![],
ir::CompilerOp::ComputeSlotToTime(x) => vec![x],
ir::CompilerOp::ComputeTimeToSlot(x) => vec![x],
}
}

Expand All @@ -1162,6 +1164,8 @@ impl Composite for ir::CompilerOp {
ir::CompilerOp::BuildScriptAddress(x) => Ok(ir::CompilerOp::BuildScriptAddress(f(x)?)),
ir::CompilerOp::ComputeMinUtxo(x) => Ok(ir::CompilerOp::ComputeMinUtxo(f(x)?)),
ir::CompilerOp::ComputeTipSlot => Ok(ir::CompilerOp::ComputeTipSlot),
ir::CompilerOp::ComputeSlotToTime(x) => Ok(ir::CompilerOp::ComputeSlotToTime(f(x)?)),
ir::CompilerOp::ComputeTimeToSlot(x) => Ok(ir::CompilerOp::ComputeTimeToSlot(f(x)?)),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/tx3-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ pub enum DataExpr {
Identifier(Identifier),
MinUtxo(Identifier),
ComputeTipSlot,
SlotToTime(Box<DataExpr>),
TimeToSlot(Box<DataExpr>),
AddOp(AddOp),
SubOp(SubOp),
ConcatOp(ConcatOp),
Expand Down Expand Up @@ -740,6 +742,8 @@ impl DataExpr {
DataExpr::UtxoRef(_) => Some(Type::UtxoRef),
DataExpr::MinUtxo(_) => Some(Type::AnyAsset),
DataExpr::ComputeTipSlot => Some(Type::Int),
DataExpr::SlotToTime(_) => Some(Type::Int),
DataExpr::TimeToSlot(_) => Some(Type::Int),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/tx3-lang/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum CompilerOp {
BuildScriptAddress(Expression),
ComputeMinUtxo(Expression),
ComputeTipSlot,
ComputeSlotToTime(Expression),
ComputeTimeToSlot(Expression),
}

#[derive(Encode, Decode, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -467,6 +469,8 @@ impl Node for CompilerOp {
CompilerOp::BuildScriptAddress(x) => CompilerOp::BuildScriptAddress(x.apply(visitor)?),
CompilerOp::ComputeMinUtxo(x) => CompilerOp::ComputeMinUtxo(x.apply(visitor)?),
CompilerOp::ComputeTipSlot => CompilerOp::ComputeTipSlot,
CompilerOp::ComputeSlotToTime(x) => CompilerOp::ComputeSlotToTime(x.apply(visitor)?),
CompilerOp::ComputeTimeToSlot(x) => CompilerOp::ComputeTimeToSlot(x.apply(visitor)?),
};

Ok(visited)
Expand Down
6 changes: 6 additions & 0 deletions crates/tx3-lang/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ impl IntoLower for ast::DataExpr {
ast::DataExpr::ComputeTipSlot => {
ir::Expression::EvalCompiler(Box::new(ir::CompilerOp::ComputeTipSlot))
}
ast::DataExpr::SlotToTime(x) => ir::Expression::EvalCompiler(Box::new(
ir::CompilerOp::ComputeSlotToTime(x.into_lower(ctx)?),
)),
ast::DataExpr::TimeToSlot(x) => ir::Expression::EvalCompiler(Box::new(
ir::CompilerOp::ComputeTimeToSlot(x.into_lower(ctx)?),
)),
};

Ok(out)
Expand Down
14 changes: 14 additions & 0 deletions crates/tx3-lang/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,16 @@ impl DataExpr {
Ok(DataExpr::ComputeTipSlot)
}

fn slot_to_time_parse(pair: Pair<Rule>) -> Result<Self, Error> {
let inner = pair.into_inner().next().unwrap();
Ok(DataExpr::SlotToTime(Box::new(DataExpr::parse(inner)?)))
}

fn time_to_slot_parse(pair: Pair<Rule>) -> Result<Self, Error> {
let inner = pair.into_inner().next().unwrap();
Ok(DataExpr::TimeToSlot(Box::new(DataExpr::parse(inner)?)))
}

fn concat_constructor_parse(pair: Pair<Rule>) -> Result<Self, Error> {
Ok(DataExpr::ConcatOp(ConcatOp::parse(pair)?))
}
Expand Down Expand Up @@ -1185,6 +1195,8 @@ impl AstNode for DataExpr {
Rule::concat_constructor => DataExpr::concat_constructor_parse(x),
Rule::min_utxo => DataExpr::min_utxo_parse(x),
Rule::tip_slot => DataExpr::tip_slot_parse(x),
Rule::slot_to_time => DataExpr::slot_to_time_parse(x),
Rule::time_to_slot => DataExpr::time_to_slot_parse(x),
Rule::data_expr => DataExpr::parse(x),
x => unreachable!("unexpected rule as data primary: {:?}", x),
})
Expand Down Expand Up @@ -1225,6 +1237,8 @@ impl AstNode for DataExpr {
DataExpr::PropertyOp(x) => &x.span,
DataExpr::UtxoRef(x) => x.span(),
DataExpr::MinUtxo(x) => x.span(),
DataExpr::SlotToTime(x) => x.span(),
DataExpr::TimeToSlot(x) => x.span(),
DataExpr::ComputeTipSlot => &Span::DUMMY, // TODO
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/tx3-lang/src/tx3.pest
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ min_utxo = { "min_utxo" ~ "(" ~ identifier ~ ")" }

tip_slot = { "tip_slot" ~ "(" ~ ")" }

slot_to_time = { "slot_to_time" ~ "(" ~ data_expr ~ ")" }

time_to_slot = { "time_to_slot" ~ "(" ~ data_expr ~ ")" }

data_expr = { data_prefix* ~ data_primary ~ data_postfix* ~ (data_infix ~ data_prefix* ~ data_primary ~ data_postfix* )* }

data_infix = _{ data_add | data_sub }
Expand All @@ -150,6 +154,8 @@ data_expr = { data_prefix* ~ data_primary ~ data_postfix* ~ (data_infix ~ data_p
string |
min_utxo |
tip_slot |
slot_to_time |
time_to_slot |
struct_constructor |
list_constructor |
concat_constructor |
Expand Down
24 changes: 14 additions & 10 deletions examples/asteria.ast
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,13 @@
}
},
"property": {
"value": "pos_x",
"span": {
"dummy": false,
"start": 1281,
"end": 1286
"Identifier": {
"value": "pos_x",
"span": {
"dummy": false,
"start": 1281,
"end": 1286
}
}
},
"span": {
Expand Down Expand Up @@ -511,11 +513,13 @@
}
},
"property": {
"value": "pos_y",
"span": {
"dummy": false,
"start": 1324,
"end": 1329
"Identifier": {
"value": "pos_y",
"span": {
"dummy": false,
"start": 1324,
"end": 1329
}
}
},
"span": {
Expand Down
20 changes: 15 additions & 5 deletions examples/asteria.move_ship.tir
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@
}
}
},
0
{
"Number": 0
}
]
}
},
Expand Down Expand Up @@ -552,7 +554,9 @@
}
}
},
1
{
"Number": 1
}
]
}
},
Expand Down Expand Up @@ -698,7 +702,9 @@
}
}
},
2
{
"Number": 2
}
]
}
},
Expand Down Expand Up @@ -833,7 +839,9 @@
}
}
},
3
{
"Number": 3
}
]
}
},
Expand Down Expand Up @@ -968,7 +976,9 @@
}
}
},
4
{
"Number": 4
}
]
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cardano_witness.mint_from_plutus.tir
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@
{
"name": "plutus_witness",
"data": {
"version": {
"Number": 3
},
"script": {
"Bytes": [
81,
Expand All @@ -222,9 +225,6 @@
174,
105
]
},
"version": {
"Number": 3
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions examples/input_datum.ast
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@
}
},
"property": {
"value": "counter",
"span": {
"dummy": false,
"start": 323,
"end": 330
"Identifier": {
"value": "counter",
"span": {
"dummy": false,
"start": 323,
"end": 330
}
}
},
"span": {
Expand Down Expand Up @@ -213,11 +215,13 @@
}
},
"property": {
"value": "other_field",
"span": {
"dummy": false,
"start": 368,
"end": 379
"Identifier": {
"value": "other_field",
"span": {
"dummy": false,
"start": 368,
"end": 379
}
}
},
"span": {
Expand Down
Loading