-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Prevent double spending: UTXOs created and spent in the same b…
…lock (#173)
- Loading branch information
Showing
9 changed files
with
254 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
scarb 2.8.0 | ||
scarb 2.8.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
use consensus::types::block::Block; | ||
use consensus::types::chain_state::{ChainState, BlockValidator}; | ||
use consensus::types::state::State; | ||
use consensus::types::chain_state::BlockValidator; | ||
use consensus::types::utxo_set::UtxoSet; | ||
|
||
/// Raito program arguments. | ||
#[derive(Serde)] | ||
struct Args { | ||
/// Current (initial) chain state | ||
chain_state: ChainState, | ||
/// Current (initial) state | ||
state: State, | ||
/// Batch of blocks that have to be applied to the current chain state | ||
blocks: Array<Block>, | ||
} | ||
|
||
/// Raito program entrypoint. | ||
/// | ||
/// Receives current chain state and pending blocks, | ||
/// Receives current state (chain state + utreexo state) and pending blocks, | ||
/// then validates and applies them one by one. | ||
/// Returns new chain state in case of succes, otherwise raises an error. | ||
fn main(mut arguments: Span<felt252>) -> ChainState { | ||
let Args { mut chain_state, blocks, } = Serde::deserialize(ref arguments) | ||
/// Returns new state in case of succes, otherwise raises an error. | ||
fn main(mut arguments: Span<felt252>) -> State { | ||
let Args { mut state, blocks, } = Serde::deserialize(ref arguments) | ||
.expect('Failed to deserialize'); | ||
|
||
let mut utxo_set = UtxoSet { utreexo_state: state.utreexo_state, cache: Default::default(), }; | ||
|
||
for block in blocks { | ||
chain_state = chain_state.validate_and_apply(block).expect('Validation failed'); | ||
state | ||
.chain_state = state | ||
.chain_state | ||
.validate_and_apply(block, ref utxo_set) | ||
.expect('Validation failed'); | ||
}; | ||
chain_state | ||
|
||
state.utreexo_state = utxo_set.utreexo_state; | ||
state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ pub mod types { | |
pub mod block; | ||
pub mod transaction; | ||
pub mod utxo_set; | ||
pub mod state; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! State is a top level struct containing the chain state and the utxo set | ||
|
||
use crate::types::utreexo::{UtreexoState, UtreexoStateDefault}; | ||
use super::chain_state::ChainState; | ||
|
||
#[derive(Default, Drop, Copy, Debug, Serde)] | ||
pub struct State { | ||
pub chain_state: ChainState, | ||
pub utreexo_state: UtreexoState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.