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