Skip to content

Commit

Permalink
[rooch-networkgh-2287] refactor transaction_validator.move.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Sep 15, 2024
1 parent b2dae5f commit 05dd8f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use crate::binding_test;
use bitcoin::hex::DisplayHex;
use move_core_types::account_address::AccountAddress;
use move_core_types::ident_str;
use move_core_types::language_storage::ModuleId;
Expand All @@ -11,7 +10,6 @@ use move_core_types::vm_status::{AbortLocation, KeptVMStatus, VMStatus};
use moveos_types::module_binding::MoveFunctionCaller;
use moveos_types::move_std::string::MoveString;
use moveos_types::move_types::FunctionId;
use moveos_types::state::MoveState;
use moveos_types::{module_binding::ModuleBinding, transaction::MoveAction};
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_key::keystore::memory_keystore::InMemKeystore;
Expand Down
139 changes: 55 additions & 84 deletions frameworks/rooch-framework/sources/transaction_validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ module rooch_framework::transaction_validator {
use rooch_framework::address_mapping;
use rooch_framework::account_coin_store;
use rooch_framework::builtin_validators;
// use rooch_framework::bitcoin_address;
// use std::vector;
// use std::string;
// use std::bcs;
use rooch_framework::bitcoin_address;
use std::vector;
use std::string;
use std::bcs;

const MAX_U64: u128 = 18446744073709551615;

Expand Down Expand Up @@ -180,84 +180,55 @@ module rooch_framework::transaction_validator {
};
}

// #[test]
// fun test_validate_success() {
// // chain id
// let chain_id = 3;
// std::debug::print(&chain_id);
// // auth validator id
// let session_validator_id = session_validator::auth_validator_id();
// std::debug::print(&session_validator_id);
// // let bitcoin_validator_id = bitcoin_validator::auth_validator_id();
// let authenticator_payload = vector::empty<u8>();
// // bytes: [55, 20, 57, 24, 237, 39, 212, 99, 80, 253, 158, 152, 69, 44, 186, 5, 149, 156, 255, 112, 63, 122, 253, 78, 174, 126, 212, 106, 62, 176, 54, 107]
// vector::push_back(&mut authenticator_payload, 55);
// vector::push_back(&mut authenticator_payload, 20);
// vector::push_back(&mut authenticator_payload, 57);
// vector::push_back(&mut authenticator_payload, 24);
// vector::push_back(&mut authenticator_payload, 237);
// vector::push_back(&mut authenticator_payload, 39);
// vector::push_back(&mut authenticator_payload, 212);
// vector::push_back(&mut authenticator_payload, 99);
// vector::push_back(&mut authenticator_payload, 80);
// vector::push_back(&mut authenticator_payload, 253);
// vector::push_back(&mut authenticator_payload, 158);
// vector::push_back(&mut authenticator_payload, 152);
// vector::push_back(&mut authenticator_payload, 69);
// vector::push_back(&mut authenticator_payload, 44);
// vector::push_back(&mut authenticator_payload, 186);
// vector::push_back(&mut authenticator_payload, 5);
// vector::push_back(&mut authenticator_payload, 149);
// vector::push_back(&mut authenticator_payload, 156);
// vector::push_back(&mut authenticator_payload, 255);
// vector::push_back(&mut authenticator_payload, 112);
// vector::push_back(&mut authenticator_payload, 63);
// vector::push_back(&mut authenticator_payload, 122);
// vector::push_back(&mut authenticator_payload, 253);
// vector::push_back(&mut authenticator_payload, 78);
// vector::push_back(&mut authenticator_payload, 174);
// vector::push_back(&mut authenticator_payload, 126);
// vector::push_back(&mut authenticator_payload, 212);
// vector::push_back(&mut authenticator_payload, 106);
// vector::push_back(&mut authenticator_payload, 62);
// vector::push_back(&mut authenticator_payload, 176);
// vector::push_back(&mut authenticator_payload, 54);
// vector::push_back(&mut authenticator_payload, 107);
// std::debug::print(&authenticator_payload);
// rooch_framework::genesis::init_for_test();

// // session key
// let sender_addr = tx_context::sender();
// let sender = moveos_std::account::create_signer_for_testing(sender_addr);
// let scope = session_key::new_session_scope(@0x1, std::string::utf8(b"*"), std::string::utf8(b"*"));
// let authentication_key = bcs::to_bytes(&sender_addr);
// let max_inactive_interval = 10;
// let app_name = std::string::utf8(b"test");
// let app_url = std::string::utf8(b"https://test.rooch.network");
// session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);

// // validate function
// // TODO: let scheme = vector::borrow(authenticator_payload, 0);
// let tx_validate_result = validate(chain_id, session_validator_id, authenticator_payload);
// std::debug::print(&tx_validate_result);

// let id = 1;
// let module_address = @000000000003;
// let module_name = string::utf8(b"rooch_framework::empty");
// let expected_auth_validator = auth_validator::new_auth_validator(id, module_address, module_name);
// std::debug::print(&expected_auth_validator);
// let expected_session_key = vector::empty<u8>();
// let expected_bitcoin_addr = bitcoin_address::from_string(&string::utf8(b"bc1p72fvqwm9w4wcsd205maky9qejf6dwa6qeku5f5vnu4phpp3vvpws0p2f4g"));
// std::debug::print(&expected_bitcoin_addr);

// let auth_validator_id = 1;
// let auth_validator = option::some(expected_auth_validator);
// let session_key = option::some(expected_session_key);
// let bitcoin_address = expected_bitcoin_addr;

// let expected_tx_validate_result = auth_validator::new_tx_validate_result(auth_validator_id, auth_validator, session_key, bitcoin_address);
// std::debug::print(&expected_tx_validate_result);

// assert!(tx_validate_result == expected_tx_validate_result, 1000);
// }
// TODO: tx validator test
#[test]
fun test_validate_success() {
// chain id
let chain_id = 3;
std::debug::print(&chain_id);
// auth validator id
let session_validator_id = session_validator::auth_validator_id();
std::debug::print(&session_validator_id);
let authenticator_payload = x"008b91427a5eaea2970a093a5a996a947fd7073d0155eaf85db9367715572662b20ddeb9fbb56a3d2a9ebd9d41c8cfc0089e7f7c6b2b2da954c4764527a8a2bd077495252f4c098ae79597de12da55e446256953a5a05d95e54adcd5e1e561cbe0";
std::debug::print(&authenticator_payload);
rooch_framework::genesis::init_for_test();

// session key
let sender_addr = tx_context::sender();
let sender = moveos_std::account::create_signer_for_testing(sender_addr);
let scope = session_key::new_session_scope(@0x1, std::string::utf8(b"*"), std::string::utf8(b"*"));
let authentication_key = bcs::to_bytes(&sender_addr);
let max_inactive_interval = 10;
let app_name = std::string::utf8(b"test");
let app_url = std::string::utf8(b"https://test.rooch.network");
session_key::create_session_key(&sender, app_name, app_url, authentication_key, vector::singleton(scope), max_inactive_interval);
let session_key_opt = session_key::get_session_key(sender_addr, authentication_key);
assert!(option::is_some(&session_key_opt), 1000);

// session validator


// validate function
let tx_validate_result = validate(chain_id, session_validator_id, authenticator_payload);
std::debug::print(&tx_validate_result);

let id = 1;
let module_address = @000000000003;
let module_name = string::utf8(b"rooch_framework::empty");
let expected_auth_validator = auth_validator::new_auth_validator(id, module_address, module_name);
std::debug::print(&expected_auth_validator);
let expected_session_key = vector::empty<u8>();
let expected_bitcoin_addr = bitcoin_address::from_string(&string::utf8(b"bc1p72fvqwm9w4wcsd205maky9qejf6dwa6qeku5f5vnu4phpp3vvpws0p2f4g"));
std::debug::print(&expected_bitcoin_addr);

let auth_validator_id = 1;
let auth_validator = option::some(expected_auth_validator);
let session_key = option::some(expected_session_key);
let bitcoin_address = expected_bitcoin_addr;

let expected_tx_validate_result = auth_validator::new_tx_validate_result(auth_validator_id, auth_validator, session_key, bitcoin_address);
std::debug::print(&expected_tx_validate_result);

assert!(tx_validate_result == expected_tx_validate_result, 1000);
}
}

0 comments on commit 05dd8f5

Please sign in to comment.