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
2 changes: 2 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub enum HostFunctions {
StringNewFromLinearMemory,
StrKeyToAddr,
GetLedgerTimestamp,
GetLedgerSequence,
GetCurrentContractAddress,
BytesNewFromLinearMemory,
BytesLen,
Expand Down Expand Up @@ -191,6 +192,7 @@ impl HostFunctions {
HostFunctions::VecPushBack => "v.6",
HostFunctions::StringNewFromLinearMemory => "b.i",
HostFunctions::StrKeyToAddr => "a.1",
HostFunctions::GetLedgerSequence => "x.3",
HostFunctions::GetLedgerTimestamp => "x.4",
HostFunctions::GetCurrentContractAddress => "x.7",
HostFunctions::BytesNewFromLinearMemory => "b.3",
Expand Down
26 changes: 26 additions & 0 deletions src/codegen/targets/soroban/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,32 @@ impl TargetCodegen for SorobanTarget {
);
Some(address_var)
}
ast::Builtin::BlockNumber => {
let block_var_no = vartab.temp_name("block_number", &Type::Uint(64));
let block_var = Expression::Variable {
loc: *loc,
ty: Type::Uint(64),
var_no: block_var_no,
};
cfg.add(
vartab,
Instr::Call {
res: vec![block_var_no],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::GetLedgerSequence.name().to_string(),
},
args: vec![],
},
);
Some(soroban_decode_arg(
block_var,
cfg,
vartab,
ns,
Some(Type::Uint(64)),
))
}
ast::Builtin::RequireAuth => {
let var_temp = vartab.temp(
&pt::Identifier {
Expand Down
2 changes: 2 additions & 0 deletions src/emit/soroban/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl HostFunctions {
.fn_type(&[ty.into(), ty.into()], false),
HostFunctions::StrKeyToAddr => bin.context.i64_type().fn_type(&[ty.into()], false),
HostFunctions::GetLedgerTimestamp => bin.context.i64_type().fn_type(&[], false),
HostFunctions::GetLedgerSequence => bin.context.i64_type().fn_type(&[], false),
HostFunctions::GetCurrentContractAddress => bin.context.i64_type().fn_type(&[], false),
HostFunctions::ObjToI128Lo64 => bin.context.i64_type().fn_type(&[ty.into()], false),
HostFunctions::ObjToI128Hi64 => bin.context.i64_type().fn_type(&[ty.into()], false),
Expand Down Expand Up @@ -495,6 +496,7 @@ impl SorobanTarget {
HostFunctions::StringNewFromLinearMemory,
HostFunctions::StrKeyToAddr,
HostFunctions::GetLedgerTimestamp,
HostFunctions::GetLedgerSequence,
HostFunctions::GetCurrentContractAddress,
HostFunctions::BytesNewFromLinearMemory,
HostFunctions::BytesCopyToLinearMemory,
Expand Down
38 changes: 38 additions & 0 deletions tests/soroban_testcases/block_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::build_solidity;
use soroban_sdk::testutils::Ledger;
use soroban_sdk::{FromVal, Val};

#[test]
fn get_ledger_sequence() {
let runtime = build_solidity(
r#"
contract LedgerSequence {
function get_ledger_sequence() public view returns (uint64) {
return block.number;
}
}
"#,
|_| {},
);
let addr = runtime.contracts.last().unwrap();
let samples: Vec<u64> = vec![
1,
99,
2,
33,
13,
9,
15,
0,
10001,
1_000_000_000,
2 << 16,
2 << 24,
];
for number in samples {
runtime.env.ledger().set_sequence_number(number as u32);
let block_number_val: Val = runtime.invoke_contract(addr, "get_ledger_sequence", vec![]);
let block_number_u64: u64 = FromVal::from_val(&runtime.env, &block_number_val);
assert_eq!(number, block_number_u64);
}
}
1 change: 1 addition & 0 deletions tests/soroban_testcases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod alloc;
mod array_args;
mod atomic_swap;
mod auth;
mod block_number;
mod bytes_fail;
mod bytes_pass;
mod constructor;
Expand Down
Loading