mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-03 05:43:08 +00:00
minor change
This commit is contained in:
parent
6030e54cd7
commit
4f7bde100c
@ -21,6 +21,7 @@ impl Program {
|
||||
pub fn id(&self) -> ProgramId {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub(crate) fn execute(
|
||||
&self,
|
||||
pre_states: &[AccountWithMetadata],
|
||||
|
||||
@ -39,7 +39,7 @@ impl Message {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct WitnessSet {
|
||||
pub signatures_and_public_keys: Vec<(Signature, PublicKey)>,
|
||||
pub(crate) signatures_and_public_keys: Vec<(Signature, PublicKey)>,
|
||||
}
|
||||
|
||||
impl WitnessSet {
|
||||
@ -53,6 +53,10 @@ impl WitnessSet {
|
||||
signatures_and_public_keys,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter_signatures(&self) -> impl Iterator<Item = &(Signature, PublicKey)> {
|
||||
self.signatures_and_public_keys.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
|
||||
@ -57,6 +57,7 @@ impl V01State {
|
||||
let current_account = self.get_account_by_address_mut(address);
|
||||
current_account.nonce += 1;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -92,11 +93,7 @@ impl V01State {
|
||||
}
|
||||
|
||||
let mut authorized_addresses = Vec::new();
|
||||
for ((signature, public_key), nonce) in witness_set
|
||||
.signatures_and_public_keys
|
||||
.iter()
|
||||
.zip(message.nonces.iter())
|
||||
{
|
||||
for ((signature, public_key), nonce) in witness_set.iter_signatures().zip(&message.nonces) {
|
||||
// Check the signature is valid
|
||||
if !signature.is_valid_for(message, public_key) {
|
||||
return Err(NssaError::InvalidInput(
|
||||
@ -148,7 +145,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{public_transaction, signature::PrivateKey};
|
||||
|
||||
fn transfer_transaction_for_tests(
|
||||
fn transfer_transaction(
|
||||
from: Address,
|
||||
from_key: PrivateKey,
|
||||
nonce: u128,
|
||||
@ -164,54 +161,75 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
fn transition_from_authenticated_transfer_program_invocation_default_account_destination() {
|
||||
let initial_data = [([1; 32], 100)];
|
||||
let mut state = V01State::new_with_genesis_accounts(&initial_data);
|
||||
let from = Address::new(initial_data[0].0);
|
||||
let from_key = PrivateKey(1);
|
||||
let to = Address::new([2; 32]);
|
||||
assert_eq!(state.get_account_by_address(&to), Account::default());
|
||||
let balance_to_move = 5;
|
||||
let tx =
|
||||
transfer_transaction_for_tests(from.clone(), from_key, 0, to.clone(), balance_to_move);
|
||||
|
||||
let tx = transfer_transaction(from.clone(), from_key, 0, to.clone(), balance_to_move);
|
||||
state.transition_from_public_transaction(&tx).unwrap();
|
||||
|
||||
assert_eq!(state.get_account_by_address(&to).balance, 5);
|
||||
assert_eq!(state.get_account_by_address(&from).balance, 95);
|
||||
assert_eq!(state.get_account_by_address(&to).balance, 5);
|
||||
assert_eq!(state.get_account_by_address(&from).nonce, 1);
|
||||
assert_eq!(state.get_account_by_address(&to).nonce, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
fn transition_from_authenticated_transfer_program_invocation_insuficient_balance() {
|
||||
let initial_data = [([1; 32], 100)];
|
||||
let mut state = V01State::new_with_genesis_accounts(&initial_data);
|
||||
let from = Address::new(initial_data[0].0);
|
||||
let from_key = PrivateKey(1);
|
||||
let to = Address::new([2; 32]);
|
||||
let balance_to_move = 101;
|
||||
assert!(state.get_account_by_address(&from).balance < balance_to_move);
|
||||
|
||||
let tx = transfer_transaction(from.clone(), from_key, 0, to.clone(), balance_to_move);
|
||||
let result = state.transition_from_public_transaction(&tx);
|
||||
|
||||
assert!(matches!(result, Err(NssaError::ProgramExecutionFailed(_))));
|
||||
assert_eq!(state.get_account_by_address(&from).balance, 100);
|
||||
assert_eq!(state.get_account_by_address(&to).balance, 0);
|
||||
assert_eq!(state.get_account_by_address(&from).nonce, 0);
|
||||
assert_eq!(state.get_account_by_address(&to).nonce, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transition_from_authenticated_transfer_program_invocation_non_default_account_destination() {
|
||||
let initial_data = [([1; 32], 100), ([99; 32], 200)];
|
||||
let mut state = V01State::new_with_genesis_accounts(&initial_data);
|
||||
let from = Address::new(initial_data[1].0);
|
||||
let from_key = PrivateKey(99);
|
||||
let to = Address::new(initial_data[0].0);
|
||||
assert_ne!(state.get_account_by_address(&to), Account::default());
|
||||
let balance_to_move = 8;
|
||||
let tx =
|
||||
transfer_transaction_for_tests(from.clone(), from_key, 0, to.clone(), balance_to_move);
|
||||
|
||||
let tx = transfer_transaction(from.clone(), from_key, 0, to.clone(), balance_to_move);
|
||||
state.transition_from_public_transaction(&tx).unwrap();
|
||||
|
||||
assert_eq!(state.get_account_by_address(&to).balance, 108);
|
||||
assert_eq!(state.get_account_by_address(&from).balance, 192);
|
||||
assert_eq!(state.get_account_by_address(&to).balance, 108);
|
||||
assert_eq!(state.get_account_by_address(&from).nonce, 1);
|
||||
assert_eq!(state.get_account_by_address(&to).nonce, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_3() {
|
||||
fn transition_from_chained_authenticated_transfer_program_invocations() {
|
||||
let initial_data = [([1; 32], 100)];
|
||||
let mut state = V01State::new_with_genesis_accounts(&initial_data);
|
||||
let address_1 = Address::new(initial_data[0].0);
|
||||
let key_1 = PrivateKey(1);
|
||||
let address_2 = Address::new([2; 32]);
|
||||
|
||||
let key_2 = PrivateKey(2);
|
||||
let address_3 = Address::new([3; 32]);
|
||||
|
||||
let balance_to_move = 5;
|
||||
let tx = transfer_transaction_for_tests(
|
||||
|
||||
let tx = transfer_transaction(
|
||||
address_1.clone(),
|
||||
key_1,
|
||||
0,
|
||||
@ -219,9 +237,8 @@ mod tests {
|
||||
balance_to_move,
|
||||
);
|
||||
state.transition_from_public_transaction(&tx).unwrap();
|
||||
|
||||
let balance_to_move = 3;
|
||||
let tx = transfer_transaction_for_tests(
|
||||
let tx = transfer_transaction(
|
||||
address_2.clone(),
|
||||
key_2,
|
||||
0,
|
||||
@ -233,7 +250,6 @@ mod tests {
|
||||
assert_eq!(state.get_account_by_address(&address_1).balance, 95);
|
||||
assert_eq!(state.get_account_by_address(&address_2).balance, 2);
|
||||
assert_eq!(state.get_account_by_address(&address_3).balance, 3);
|
||||
|
||||
assert_eq!(state.get_account_by_address(&address_1).nonce, 1);
|
||||
assert_eq!(state.get_account_by_address(&address_2).nonce, 1);
|
||||
assert_eq!(state.get_account_by_address(&address_3).nonce, 0);
|
||||
|
||||
@ -64,8 +64,7 @@ impl SequencerCore {
|
||||
// Stateless checks here
|
||||
if tx
|
||||
.witness_set()
|
||||
.signatures_and_public_keys
|
||||
.iter()
|
||||
.iter_signatures()
|
||||
.all(|(signature, public_key)| signature.is_valid_for(tx.message(), public_key))
|
||||
{
|
||||
Ok(tx)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user