Skip to content

Commit e6adc69

Browse files
committed
refactor: turn witnet_stack crate into module inside witnet_data_structures
1 parent 7f00e25 commit e6adc69

File tree

15 files changed

+32
-58
lines changed

15 files changed

+32
-58
lines changed

Cargo.lock

Lines changed: 1 addition & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description = "An in-progress open source implementation of the Witnet protocol
1111
edition = "2021"
1212

1313
[workspace]
14-
members = ["config", "node", "crypto", "data_structures", "p2p", "storage", "wallet", "validations", "protected", "reputation", "net", "toolkit", "stack", "bridges/ethereum", "bridges/centralized-ethereum", "futures_utils"]
14+
members = ["config", "node", "crypto", "data_structures", "p2p", "storage", "wallet", "validations", "protected", "reputation", "net", "toolkit", "bridges/ethereum", "bridges/centralized-ethereum", "futures_utils"]
1515

1616
[features]
1717
default = ["wallet", "node", "telemetry"]
@@ -47,7 +47,6 @@ witnet_crypto = { path = "./crypto" }
4747
witnet_data_structures = { path = "./data_structures" }
4848
witnet_node = { path = "./node", optional = true }
4949
witnet_rad = { path = "./rad" }
50-
witnet_stack = { path = "./stack" }
5150
witnet_util = { path = "./util" }
5251
witnet_validations = { path = "./validations" }
5352
witnet_wallet = { path = "./wallet", optional = true }

data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ serde = { version = "1.0.104", features = ["derive"] }
2929
serde_cbor = "0.11.1"
3030
serde_json = "1.0.48"
3131
vrf = "0.2.3"
32+
scriptful = { version = "0.4", features = ["use_serde"] }
3233

3334
witnet_crypto = { path = "../crypto" }
3435
witnet_reputation = { path = "../reputation", features = ["serde"] }

data_structures/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub mod radon_error;
5959
/// Module containing RadonReport structures
6060
pub mod radon_report;
6161

62+
/// Module containing scripting logic
63+
pub mod stack;
64+
6265
/// Serialization boilerplate to allow serializing some data structures as
6366
/// strings or bytes depending on the serializer.
6467
mod serialization_helpers;
File renamed without changes.

stack/src/lib.rs renamed to data_structures/src/stack/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use scriptful::{core::machine::Machine, core::Script, core::ScriptRef};
22

3-
pub use crate::error::ScriptError;
4-
pub use crate::operators::{MyOperator, MyValue};
3+
pub use self::error::ScriptError;
4+
pub use self::operators::{MyOperator, MyValue};
5+
use crate::chain::Hash;
56
pub use scriptful::core::item::Item;
6-
use witnet_data_structures::chain::Hash;
77

88
mod error;
99
mod operators;

stack/src/operators.rs renamed to data_structures/src/stack/operators.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{ScriptContext, ScriptError};
1+
use super::{ScriptContext, ScriptError};
2+
use crate::chain::{Hash, PublicKey, Secp256k1Signature, Signature};
3+
use crate::chain::{KeyedSignature, PublicKeyHash};
24
use scriptful::prelude::{ConditionStack, Stack};
35
use serde::{Deserialize, Serialize};
46
use witnet_crypto::hash::{calculate_sha256, Sha256};
5-
use witnet_data_structures::chain::{Hash, PublicKey, Secp256k1Signature, Signature};
6-
use witnet_data_structures::chain::{KeyedSignature, PublicKeyHash};
77

88
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
99
// TODO: Include more operators

stack/src/parser.rs renamed to data_structures/src/stack/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{MyOperator, MyValue};
1+
use super::{MyOperator, MyValue};
22
use scriptful::prelude::Item;
33
use std::str::FromStr;
44

@@ -143,7 +143,7 @@ pub fn parse_operator(s: &str) -> Option<MyOperator> {
143143
#[cfg(test)]
144144
mod tests {
145145
use super::*;
146-
use witnet_data_structures::chain::PublicKey;
146+
use crate::chain::PublicKey;
147147

148148
#[test]
149149
fn script_to_string_multisig() {

stack/src/tests.rs renamed to data_structures/src/stack/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{
1+
use super::{
22
encode, execute_complete_script, execute_locking_script, execute_redeem_script, execute_script,
33
Item, MyOperator, MyValue, ScriptContext, ScriptError,
44
};
5+
use crate::chain::KeyedSignature;
56
use witnet_crypto::hash::calculate_sha256;
6-
use witnet_data_structures::chain::KeyedSignature;
77

88
const EQUAL_OPERATOR_HASH: [u8; 20] = [
99
52, 128, 191, 80, 253, 28, 169, 253, 237, 29, 0, 51, 201, 0, 31, 203, 157, 99, 218, 210,
@@ -142,7 +142,7 @@ fn test_ks_id(id: u8) -> KeyedSignature {
142142
let secret_key =
143143
witnet_crypto::secp256k1::SecretKey::from_slice(&mk).expect("32 bytes, within curve order");
144144
let public_key = witnet_crypto::secp256k1::PublicKey::from_secret_key_global(&secret_key);
145-
let public_key = witnet_data_structures::chain::PublicKey::from(public_key);
145+
let public_key = crate::chain::PublicKey::from(public_key);
146146
// TODO: mock this signature, it is not even validated in tests but we need a valid signature to
147147
// test signature deserialization
148148
let signature = witnet_crypto::signature::sign(secret_key, &[0x01; 32]).unwrap();

node/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ witnet_futures_utils = { path = "../futures_utils" }
3636
witnet_p2p = { path = "../p2p" }
3737
witnet_protected = { path = "../protected", features = ["with-serde"] }
3838
witnet_rad = { path = "../rad" }
39-
witnet_stack = { path = "../stack" }
4039
witnet_storage = { path = "../storage", features = ["rocksdb-backend"] }
4140
witnet_util = { path = "../util" }
4241
witnet_validations = { path = "../validations" }

node/src/actors/chain_manager/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ impl Handler<BuildScriptTransaction> for ChainManager {
13061306
// Script transactions are not signed by this method because the witness may need
13071307
// something more aside from a single signature, so script transactions need to be
13081308
// manually signed using other methods.
1309-
let empty_witness = witnet_stack::encode(&[]).unwrap();
1309+
let empty_witness = witnet_data_structures::stack::encode(&[]).unwrap();
13101310
let num_inputs = vtt.inputs.len();
13111311
let transaction = Transaction::ValueTransfer(VTTransaction {
13121312
body: vtt,

src/cli/node/json_rpc_client.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use witnet_crypto::{
2020
hash::calculate_sha256,
2121
key::{ExtendedPK, ExtendedSK},
2222
};
23+
use witnet_data_structures::stack::{Item, MyOperator, MyValue};
2324
use witnet_data_structures::{
2425
chain::{
2526
Block, ConsensusConstants, DataRequestInfo, DataRequestOutput, Environment, Epoch, Hash,
@@ -40,7 +41,6 @@ use witnet_node::actors::{
4041
messages::{BuildScriptTransaction, BuildVtt, GetReputationResult, SignalingInfo},
4142
};
4243
use witnet_rad::types::RadonTypes;
43-
use witnet_stack::{Item, MyOperator, MyValue};
4444
use witnet_util::{credentials::create_credentials_file, timestamp::pretty_print};
4545
use witnet_validations::validations::{
4646
run_tally_panic_safe, validate_data_request_output, validate_rad_request, Wit,
@@ -735,7 +735,8 @@ pub fn create_multisig_address(
735735
Item::Operator(MyOperator::CheckMultiSig),
736736
]);
737737

738-
let script_address = PublicKeyHash::from_script_bytes(&witnet_stack::encode(&redeem_script)?);
738+
let script_address =
739+
PublicKeyHash::from_script_bytes(&witnet_data_structures::stack::encode(&redeem_script)?);
739740

740741
println!(
741742
"Created {}-of-{} multisig address {} composed of {:?}",
@@ -776,7 +777,7 @@ pub fn create_opened_multisig(
776777
Item::Value(MyValue::Integer(i128::from(n))),
777778
Item::Operator(MyOperator::CheckMultiSig),
778779
]);
779-
let redeem_script_bytes = witnet_stack::encode(&redeem_script)?;
780+
let redeem_script_bytes = witnet_data_structures::stack::encode(&redeem_script)?;
780781
let vt_outputs = vec![ValueTransferOutput {
781782
pkh: address,
782783
value,
@@ -907,11 +908,11 @@ pub fn sign_tx(
907908
// TODO: this only works if the witness field represents a script
908909
// It could also represent a signature in the case of normal value transfer
909910
// transactions. It would be nice to also support signing normal transactions here
910-
let mut script = witnet_stack::decode(&vtt.witness[input_index])?;
911+
let mut script = witnet_data_structures::stack::decode(&vtt.witness[input_index])?;
911912

912913
println!(
913914
"-----------------------\nPrevious witness:\n-----------------------\n{}",
914-
witnet_stack::parser::script_to_string(&script)
915+
witnet_data_structures::stack::parser::script_to_string(&script)
915916
);
916917

917918
script.insert(
@@ -921,9 +922,9 @@ pub fn sign_tx(
921922

922923
println!(
923924
"-----------------------\nNew witness:\n-----------------------\n{}",
924-
witnet_stack::parser::script_to_string(&script)
925+
witnet_data_structures::stack::parser::script_to_string(&script)
925926
);
926-
let encoded_script = witnet_stack::encode(&script)?;
927+
let encoded_script = witnet_data_structures::stack::encode(&script)?;
927928

928929
vtt.witness[input_index] = encoded_script;
929930

@@ -988,9 +989,9 @@ pub fn address_to_bytes(address: PublicKeyHash) -> Result<(), failure::Error> {
988989
/// Convert script text file into hex bytes
989990
pub fn encode_script(script_file: &Path) -> Result<(), failure::Error> {
990991
let script_str = std::fs::read_to_string(script_file)?;
991-
let script = witnet_stack::parser::parse_script(&script_str)
992+
let script = witnet_data_structures::stack::parser::parse_script(&script_str)
992993
.map_err(|e| format_err!("Failed to parse script: {:?}", e))?;
993-
let script_bytes = witnet_stack::encode(&script)?;
994+
let script_bytes = witnet_data_structures::stack::encode(&script)?;
994995
let script_hex = hex::encode(&script_bytes);
995996
let script_pkh = PublicKeyHash::from_script_bytes(&script_bytes);
996997
println!("Script address: {}", script_pkh);
@@ -1006,9 +1007,9 @@ pub fn decode_script(hex: String, script_file: Option<&Path>) -> Result<(), fail
10061007
let script_pkh = PublicKeyHash::from_script_bytes(&script_bytes);
10071008
println!("Script address: {}", script_pkh);
10081009

1009-
let script = witnet_stack::decode(&script_bytes)?;
1010+
let script = witnet_data_structures::stack::decode(&script_bytes)?;
10101011

1011-
let script_str = witnet_stack::parser::script_to_string(&script);
1012+
let script_str = witnet_data_structures::stack::parser::script_to_string(&script);
10121013

10131014
match script_file {
10141015
Some(script_file) => {

stack/Cargo.toml

Lines changed: 0 additions & 14 deletions
This file was deleted.

validations/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ log = "0.4.8"
1414
witnet_crypto = { path = "../crypto" }
1515
witnet_data_structures = { path = "../data_structures" }
1616
witnet_rad = { path = "../rad" }
17-
witnet_stack = { path = "../stack" }
1817

1918
[dev-dependencies]
2019
approx = "0.5.0"

validations/src/validations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use witnet_crypto::{
1212
merkle::{merkle_tree_root as crypto_merkle_tree_root, ProgressiveMerkleTree},
1313
signature::{verify, PublicKey, Signature},
1414
};
15+
use witnet_data_structures::stack::{execute_complete_script, Item, ScriptContext};
1516
use witnet_data_structures::{
1617
chain::{
1718
Block, BlockMerkleRoots, CheckpointBeacon, CheckpointVRF, ConsensusConstants,
@@ -44,7 +45,6 @@ use witnet_rad::{
4445
script::{create_radon_script_from_filters_and_reducer, unpack_radon_script},
4546
types::{serial_iter_decode, RadonTypes},
4647
};
47-
use witnet_stack::{execute_complete_script, Item, ScriptContext};
4848

4949
/// Returns the fee of a value transfer transaction.
5050
///
@@ -1256,7 +1256,7 @@ pub fn validate_transaction_signatures(
12561256
&signature,
12571257
);
12581258
} else {
1259-
let witness_script = witnet_stack::decode(witness)?;
1259+
let witness_script = witnet_data_structures::stack::decode(witness)?;
12601260
// Operators are not allowed in witness script
12611261
for item in witness_script {
12621262
match item {

0 commit comments

Comments
 (0)