Skip to content

Commit bca54f3

Browse files
Manyfestationclaude
andcommitted
fix: align with upstream PR #44 — restore serialize_i64 encoding
Bad merge resolution had reverted PR #44 changes: - Restore serialize_i64 instead of to_le_bytes+OpBin2Num for int fields - Restore Ok(9) field chunk size (was incorrectly Ok(10)) - Remove spurious OpBin2Num injection in validate_output_state - Update test expectations to match corrected encoding Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e2a13fd commit bca54f3

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

silverscript-lang/src/compiler.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
22

33
use kaspa_txscript::opcodes::codes::*;
44
use kaspa_txscript::script_builder::ScriptBuilder;
5+
use kaspa_txscript::serialize_i64;
56
use serde::{Deserialize, Serialize};
67

78
use crate::ast::{
@@ -246,8 +247,9 @@ fn compile_contract_fields<'i>(
246247
let ExprKind::Int(value) = &resolved.kind else {
247248
return Err(CompilerError::Unsupported(format!("contract field '{}' expects compile-time int value", field.name)));
248249
};
249-
builder.add_data(&value.to_le_bytes())?;
250-
builder.add_op(OpBin2Num)?;
250+
let serialized = serialize_i64(*value, Some(8usize))
251+
.map_err(|err| CompilerError::Unsupported(format!("failed to serialize int literal {}: {err}", value)))?;
252+
builder.add_data(&serialized)?;
251253
} else {
252254
compile_expr(
253255
&resolved,
@@ -792,7 +794,8 @@ fn encode_fixed_size_value<'i>(value: &Expr<'i>, type_name: &str) -> Result<Vec<
792794
ExprKind::Int(number) | ExprKind::DateLiteral(number) => *number,
793795
_ => return Err(CompilerError::Unsupported("array literal element type mismatch".to_string())),
794796
};
795-
Ok(number.to_le_bytes().to_vec())
797+
serialize_i64(number, Some(8usize))
798+
.map_err(|err| CompilerError::Unsupported(format!("failed to serialize int literal {}: {err}", number)))
796799
}
797800
"bool" => {
798801
let ExprKind::Bool(flag) = &value.kind else {
@@ -1475,7 +1478,9 @@ fn encoded_field_chunk_size<'i>(
14751478
contract_constants: &HashMap<String, Expr<'i>>,
14761479
) -> Result<usize, CompilerError> {
14771480
if field.type_ref.array_dims.is_empty() && field.type_ref.base == TypeBase::Int {
1478-
return Ok(10);
1481+
// Int fields are encoded as PUSHDATA8-prefixed script numbers:
1482+
// 1-byte push opcode (0x08) + 8-byte payload from serialize_i64(..., Some(8)).
1483+
return Ok(9);
14791484
}
14801485

14811486
if field.type_ref.base != TypeBase::Byte {
@@ -1662,10 +1667,6 @@ fn compile_validate_output_state_statement(
16621667
builder.add_op(OpSwap)?;
16631668
builder.add_op(OpCat)?;
16641669
stack_depth -= 1;
1665-
builder.add_data(&[OpBin2Num])?;
1666-
stack_depth += 1;
1667-
builder.add_op(OpCat)?;
1668-
stack_depth -= 1;
16691670
continue;
16701671
}
16711672

silverscript-lang/tests/compiler_tests.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,8 +1562,6 @@ fn compiles_contract_fields_as_script_prolog() {
15621562
let expected = ScriptBuilder::new()
15631563
.add_data(&5i64.to_le_bytes())
15641564
.unwrap()
1565-
.add_op(OpBin2Num)
1566-
.unwrap()
15671565
.add_data(&[0x12, 0x34])
15681566
.unwrap()
15691567
.add_i64(1)
@@ -1622,11 +1620,9 @@ fn compiles_validate_output_state_to_expected_script() {
16221620
let compiled = compile_contract(source, &[5.into(), vec![1u8, 2u8].into()], CompileOptions::default()).expect("compile succeeds");
16231621

16241622
let expected = ScriptBuilder::new()
1625-
// <x> as fixed-size int field encoding: <PUSHDATA8><8-byte little-endian><OpBin2Num>
1623+
// <x> as fixed-size int field encoding: <PUSHDATA8><8-byte little-endian>
16261624
.add_data(&5i64.to_le_bytes())
16271625
.unwrap()
1628-
.add_op(OpBin2Num)
1629-
.unwrap()
16301626
// <y>
16311627
.add_data(&[1u8, 2u8])
16321628
.unwrap()
@@ -1645,7 +1641,7 @@ fn compiles_validate_output_state_to_expected_script() {
16451641
.add_op(OpAdd)
16461642
.unwrap()
16471643

1648-
// ---- Convert x+1 to fixed-size int field chunk: <0x08><8-byte payload><OpBin2Num> ----
1644+
// ---- Convert x+1 to fixed-size int field chunk: <0x08><8-byte payload> ----
16491645
// convert numeric value to 8-byte payload
16501646
.add_i64(8)
16511647
.unwrap()
@@ -1658,12 +1654,6 @@ fn compiles_validate_output_state_to_expected_script() {
16581654
.unwrap()
16591655
.add_op(OpCat)
16601656
.unwrap()
1661-
// append OpBin2Num opcode byte
1662-
.add_data(&[OpBin2Num])
1663-
.unwrap()
1664-
.add_op(OpCat)
1665-
.unwrap()
1666-
16671657
// ---- Build new_state.y pushdata chunk ----
16681658
// raw y bytes
16691659
.add_data(&[0x34, 0x12])
@@ -1697,8 +1687,8 @@ fn compiles_validate_output_state_to_expected_script() {
16971687
// sigscript_len - script_size => bytes before current redeem
16981688
.add_op(OpSub)
16991689
.unwrap()
1700-
// add fixed current-state field prefix length: len(<x><y>) = 13
1701-
.add_i64(13)
1690+
// add fixed current-state field prefix length: len(<x><y>) = 12
1691+
.add_i64(12)
17021692
.unwrap()
17031693
// start offset of REST_OF_SCRIPT inside sigscript
17041694
.add_op(OpAdd)
@@ -1831,9 +1821,6 @@ fn compiles_read_input_state_to_expected_script() {
18311821
// push x payload (8-byte LE)
18321822
.add_data(&5i64.to_le_bytes())
18331823
.unwrap()
1834-
// decode x to numeric form
1835-
.add_op(OpBin2Num)
1836-
.unwrap()
18371824
// push y payload bytes
18381825
.add_data(&[1u8, 2u8])
18391826
.unwrap()
@@ -1917,10 +1904,10 @@ fn compiles_read_input_state_to_expected_script() {
19171904
// base = sig_len - script_size
19181905
.add_op(OpSub)
19191906
.unwrap()
1920-
// skip x encoded chunk (10 bytes) + y pushdata prefix (1 byte)
1921-
.add_i64(11)
1907+
// skip x encoded chunk (9 bytes) + y pushdata prefix (1 byte)
1908+
.add_i64(10)
19221909
.unwrap()
1923-
// start_y = base + 11
1910+
// start_y = base + 10
19241911
.add_op(OpAdd)
19251912
.unwrap()
19261913

@@ -1937,9 +1924,9 @@ fn compiles_read_input_state_to_expected_script() {
19371924
.add_op(OpSub)
19381925
.unwrap()
19391926
// skip x chunk + y prefix
1940-
.add_i64(11)
1927+
.add_i64(10)
19411928
.unwrap()
1942-
// start_y = base + 11
1929+
// start_y = base + 10
19431930
.add_op(OpAdd)
19441931
.unwrap()
19451932
// y payload length

0 commit comments

Comments
 (0)