Skip to content

Commit

Permalink
Merge pull request #301 from athenavm/remove-code-from-athena_message
Browse files Browse the repository at this point in the history
remove unused code field from `athena_message`
  • Loading branch information
poszu authored Jan 3, 2025
2 parents 22acee0 + 6feae32 commit b048267
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 122 deletions.
1 change: 0 additions & 1 deletion core/src/syscall/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ impl Syscall for SyscallHostCall {
*athena_ctx.address(),
input,
amount,
Vec::new(),
);
let host = ctx.rt.host.as_deref_mut().expect("Missing host interface");
let res = host.call(msg);
Expand Down
2 changes: 0 additions & 2 deletions examples/wallet/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ mod tests {
Address::default(),
Some(payload.into()),
Balance::default(),
vec![],
),
super::ELF,
);
Expand Down Expand Up @@ -269,7 +268,6 @@ mod tests {
Address::default(),
Some(payload.into()),
Balance::default(),
vec![],
),
super::ELF,
);
Expand Down
8 changes: 2 additions & 6 deletions ffi/athcon/bindings/rust/athcon-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ mod tests {
set_option: None,
};

let code = [0u8; 0];

let message = &::athcon_sys::athcon_message {
kind: ::athcon_sys::athcon_call_kind::ATHCON_CALL,
depth: 0,
Expand All @@ -122,8 +120,6 @@ mod tests {
input_data: std::ptr::null(),
input_size: 0,
value: 0,
code: std::ptr::null(),
code_size: 0,
};
let message: ExecutionMessage = message.try_into().unwrap();

Expand All @@ -145,7 +141,7 @@ mod tests {
container
.execute(
athcon_sys::athcon_revision::ATHCON_FRONTIER,
&code,
&[],
&message,
&host,
host_context,
Expand All @@ -161,7 +157,7 @@ mod tests {
container
.execute(
athcon_sys::athcon_revision::ATHCON_FRONTIER,
&code,
&[],
&message,
&host,
host_context,
Expand Down
72 changes: 0 additions & 72 deletions ffi/athcon/bindings/rust/athcon-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub struct ExecutionMessage {
sender: Address,
input: Option<Vec<u8>>,
value: u64,
code: Option<Vec<u8>>,
}

/// ATHCON transaction context structure.
Expand Down Expand Up @@ -123,7 +122,6 @@ impl ExecutionMessage {
sender: Address,
input: Option<&[u8]>,
value: u64,
code: Option<&[u8]>,
) -> Self {
ExecutionMessage {
kind,
Expand All @@ -133,7 +131,6 @@ impl ExecutionMessage {
sender,
input: input.map(|s| s.to_vec()),
value,
code: code.map(|s| s.to_vec()),
}
}

Expand Down Expand Up @@ -171,11 +168,6 @@ impl ExecutionMessage {
pub fn value(&self) -> u64 {
self.value
}

/// Read the optional init code.
pub fn code(&self) -> Option<&Vec<u8>> {
self.code.as_ref()
}
}

impl<'a> ExecutionContext<'a> {
Expand Down Expand Up @@ -246,13 +238,6 @@ impl<'a> ExecutionContext<'a> {
} else {
(null(), 0)
};
let code = message.code();
let code_size = if let Some(code) = code { code.len() } else { 0 };
let code_data = if let Some(code) = code {
code.as_ptr()
} else {
null()
};
// Cannot use a nice from trait here because that complicates memory management,
// athcon_message doesn't have a release() method we could abstract it with.
let message = ffi::athcon_message {
Expand All @@ -264,8 +249,6 @@ impl<'a> ExecutionContext<'a> {
input_data,
input_size,
value: message.value,
code: code_data,
code_size,
};
unsafe {
assert!(self.host.call.is_some());
Expand Down Expand Up @@ -380,16 +363,6 @@ impl TryFrom<&ffi::athcon_message> for ExecutionMessage {
Some(unsafe { slice::from_raw_parts(message.input_data, message.input_size).to_vec() })
},
value: message.value,
code: if message.code.is_null() {
if message.code_size != 0 {
return Err("msg.code is null but msg.code_size is not 0".to_string());
}
None
} else if message.code_size == 0 {
None
} else {
Some(unsafe { slice::from_raw_parts(message.code, message.code_size).to_vec() })
},
})
}
}
Expand Down Expand Up @@ -536,7 +509,6 @@ mod tests {
sender,
Some(&input),
value,
None,
);

assert_eq!(ret.kind(), MessageKind::ATHCON_CALL);
Expand All @@ -554,7 +526,6 @@ mod tests {
let recipient = Address { bytes: [32u8; 24] };
let sender = Address { bytes: [128u8; 24] };
let value = 0;
let code = vec![0x5f, 0x5f, 0xfd];

let ret = ExecutionMessage::new(
MessageKind::ATHCON_CALL,
Expand All @@ -564,7 +535,6 @@ mod tests {
sender,
None,
value,
Some(&code),
);

assert_eq!(ret.kind(), MessageKind::ATHCON_CALL);
Expand All @@ -573,8 +543,6 @@ mod tests {
assert_eq!(*ret.recipient(), recipient);
assert_eq!(*ret.sender(), sender);
assert_eq!(ret.value, value);
assert!(ret.code().is_some());
assert_eq!(*ret.code().unwrap(), code);
}

fn valid_athcon_message() -> ffi::athcon_message {
Expand All @@ -591,8 +559,6 @@ mod tests {
input_data: std::ptr::null(),
input_size: 0,
value,
code: std::ptr::null(),
code_size: 0,
}
}

Expand All @@ -608,7 +574,6 @@ mod tests {
assert_eq!(*ret.sender(), msg.sender);
assert!(ret.input().is_none());
assert_eq!(ret.value, msg.value);
assert!(ret.code().is_none());
}

#[test]
Expand All @@ -631,41 +596,6 @@ mod tests {
assert!(ret.input().is_some());
assert_eq!(*ret.input().unwrap(), input);
assert_eq!(ret.value, msg.value);
assert!(ret.code().is_none());
}

#[test]
fn message_from_ffi_with_code() {
let code = vec![0x5f, 0x5f, 0xfd];

let msg = &ffi::athcon_message {
code: code.as_ptr(),
code_size: code.len(),
..valid_athcon_message()
};

let ret: ExecutionMessage = msg.try_into().unwrap();

assert_eq!(ret.kind(), msg.kind);
assert_eq!(ret.depth(), msg.depth);
assert_eq!(ret.gas(), msg.gas);
assert_eq!(*ret.recipient(), msg.recipient);
assert_eq!(*ret.sender(), msg.sender);
assert!(ret.input().is_none());
assert_eq!(ret.value, msg.value);
assert!(ret.code().is_some());
assert_eq!(*ret.code().unwrap(), code);
}

#[test]
fn message_from_ffi_code_size_must_be_0_when_no_code() {
let msg = &ffi::athcon_message {
code: std::ptr::null(),
code_size: 10,
..valid_athcon_message()
};
let ret: Result<ExecutionMessage, _> = msg.try_into();
assert!(ret.is_err());
}

#[test]
Expand Down Expand Up @@ -762,7 +692,6 @@ mod tests {
test_addr,
None,
0,
None,
);

let b = exe_context.call(&message);
Expand Down Expand Up @@ -790,7 +719,6 @@ mod tests {
test_addr,
Some(&data),
0,
None,
);

let b = exe_context.call(&message);
Expand Down
10 changes: 0 additions & 10 deletions ffi/athcon/include/athcon/athcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ extern "C"
* This is transferred value for ::ATHCON_CALL or apparent value for ::ATHCON_DELEGATECALL.
*/
uint64_t value;

/**
* The code to be executed.
*/
const uint8_t *code;

/**
* The length of the code to be executed.
*/
size_t code_size;
};

/** The transaction and block data for execution. */
Expand Down
15 changes: 0 additions & 15 deletions ffi/vmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ impl From<ffi::athcon_message> for AthenaMessageWrapper {
None
};

// Convert code pointer and size to Vec<u8>
let code = if !item.code.is_null() && item.code_size > 0 {
unsafe { std::slice::from_raw_parts(item.code, item.code_size) }.to_vec()
} else {
Vec::new()
};

let kind: MessageKindWrapper = item.kind.into();
AthenaMessageWrapper(AthenaMessage {
kind: kind.0,
Expand All @@ -135,7 +128,6 @@ impl From<ffi::athcon_message> for AthenaMessageWrapper {
sender: Address::from(item.sender.bytes),
input_data,
value: item.value,
code,
})
}
}
Expand All @@ -145,11 +137,6 @@ impl From<AthenaMessageWrapper> for AthconExecutionMessage {
let kind = match item.0.kind {
MessageKind::Call => ffi::athcon_call_kind::ATHCON_CALL,
};
let code = if !item.0.code.is_empty() {
Some(item.0.code.as_slice())
} else {
None
};
AthconExecutionMessage::new(
kind,
item.0.depth as i32,
Expand All @@ -162,7 +149,6 @@ impl From<AthenaMessageWrapper> for AthconExecutionMessage {
},
item.0.input_data.as_deref(),
item.0.value,
code,
)
}
}
Expand All @@ -178,7 +164,6 @@ impl From<&AthconExecutionMessage> for AthenaMessageWrapper {
sender: Address::from(item.sender().bytes),
input_data: item.input().cloned(),
value: item.value(),
code: item.code().cloned().unwrap_or_default(),
})
}
}
Expand Down
2 changes: 0 additions & 2 deletions ffi/vmlib/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ fn test_athcon_create() {
input_data: std::ptr::null(),
input_size: 0,
value: 0,
code: code.as_ptr(),
code_size: code.len(),
};

// note: we cannot check for a null instance or message pointer here, as the VM wrapper code
Expand Down
7 changes: 0 additions & 7 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,8 @@ pub struct AthenaMessage {
pub sender: Address,
pub input_data: Option<Vec<u8>>,
pub value: Balance,
// code is currently unused, and it seems redundant.
// it's not in the yellow paper.
// TODO: remove me?
pub code: Vec<u8>,
}

#[allow(clippy::too_many_arguments)]
impl AthenaMessage {
pub fn new(
kind: MessageKind,
Expand All @@ -160,7 +155,6 @@ impl AthenaMessage {
sender: Address,
input_data: Option<Vec<u8>>,
value: Balance,
code: Vec<u8>,
) -> Self {
AthenaMessage {
kind,
Expand All @@ -170,7 +164,6 @@ impl AthenaMessage {
sender,
input_data,
value,
code,
}
}
}
Expand Down
Loading

0 comments on commit b048267

Please sign in to comment.