This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl allow-addr in (many-)ledger and kvstore (#259)
- Loading branch information
Showing
17 changed files
with
289 additions
and
67 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
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
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,37 @@ | ||
use coset::CoseSign1; | ||
use many_error::ManyError; | ||
use many_identity::Address; | ||
use many_modules::{kvstore, ManyModule, ManyModuleInfo}; | ||
use many_protocol::{RequestMessage, ResponseMessage}; | ||
use std::collections::BTreeSet; | ||
use std::fmt::{Debug, Formatter}; | ||
|
||
pub struct AllowAddrsModule<T: kvstore::KvStoreCommandsModuleBackend> { | ||
pub inner: kvstore::KvStoreCommandsModule<T>, | ||
pub allow_addrs: BTreeSet<Address>, | ||
} | ||
|
||
impl<T: kvstore::KvStoreCommandsModuleBackend> Debug for AllowAddrsModule<T> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.write_str("AllowAddrsModule") | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<T: kvstore::KvStoreCommandsModuleBackend> ManyModule for AllowAddrsModule<T> { | ||
fn info(&self) -> &ManyModuleInfo { | ||
self.inner.info() | ||
} | ||
|
||
fn validate(&self, message: &RequestMessage, envelope: &CoseSign1) -> Result<(), ManyError> { | ||
self.inner.validate(message, envelope) | ||
} | ||
|
||
async fn execute(&self, message: RequestMessage) -> Result<ResponseMessage, ManyError> { | ||
if !self.allow_addrs.contains(&message.from()) { | ||
return Err(ManyError::invalid_from_identity()); | ||
} | ||
|
||
self.inner.execute(message).await | ||
} | ||
} |
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
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,45 @@ | ||
GIT_ROOT="$BATS_TEST_DIRNAME/../../../" | ||
|
||
load '../../test_helper/load' | ||
load '../../test_helper/kvstore' | ||
|
||
function setup() { | ||
mkdir "$BATS_TEST_ROOTDIR" | ||
|
||
skip_if_missing_background_utilities | ||
|
||
if ! [ $CI ]; then | ||
( | ||
cd "$GIT_ROOT" | ||
cargo build | ||
) | ||
fi | ||
|
||
local ALLOW_ADDRS_CONFIG=$(generate_allow_addrs_config 1 2) | ||
|
||
start_kvstore --pem "$(pem 0)" \ | ||
--allow-addrs "$ALLOW_ADDRS_CONFIG" | ||
} | ||
|
||
function teardown() { | ||
stop_background_run | ||
} | ||
|
||
@test "$SUITE: allow addrs" { | ||
call_kvstore --pem=1 --port=8000 put "01" "one" | ||
call_kvstore --pem=1 --port=8000 get "01" | ||
assert_output --partial "one" | ||
|
||
call_kvstore --pem=2 --port=8000 put "02" "two" | ||
call_kvstore --pem=2 --port=8000 get "02" | ||
assert_output --partial "two" | ||
|
||
call_kvstore --pem=3 --port=8000 put "03" "three" | ||
assert_output --partial "The identity of the from field is invalid or unexpected." | ||
|
||
call_kvstore --pem=3 --port=8000 get "02" | ||
assert_output --partial "two" | ||
|
||
call_kvstore --pem=3 --port=8000 get "03" | ||
assert_output --partial "None" | ||
} |
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,49 @@ | ||
GIT_ROOT="$BATS_TEST_DIRNAME/../../../" | ||
START_BALANCE=1000000 | ||
MFX_ADDRESS=mqbfbahksdwaqeenayy2gxke32hgb7aq4ao4wt745lsfs6wiaaaaqnz | ||
|
||
load '../../test_helper/load' | ||
load '../../test_helper/ledger' | ||
|
||
function setup() { | ||
mkdir "$BATS_TEST_ROOTDIR" | ||
|
||
skip_if_missing_background_utilities | ||
|
||
if ! [ $CI ]; then | ||
( | ||
cd "$GIT_ROOT" | ||
cargo build | ||
) | ||
fi | ||
|
||
local ALLOW_ADDRS_CONFIG=$(generate_allow_addrs_config 1 2) | ||
|
||
start_ledger --pem "$(pem 0)" \ | ||
--allow-addrs "$ALLOW_ADDRS_CONFIG" \ | ||
"--balance-only-for-testing=$(identity 1):$START_BALANCE:$MFX_ADDRESS" | ||
} | ||
|
||
function teardown() { | ||
stop_background_run | ||
} | ||
|
||
@test "$SUITE: allow addrs" { | ||
check_consistency --pem=1 --balance=1000000 8000 | ||
|
||
call_ledger --pem=1 --port=8000 send "$(identity 2)" 2000 MFX | ||
check_consistency --pem=1 --balance=998000 8000 | ||
check_consistency --pem=2 --balance=2000 8000 | ||
|
||
call_ledger --pem=2 --port=8000 send "$(identity 3)" 1000 MFX | ||
check_consistency --pem=1 --balance=998000 8000 | ||
check_consistency --pem=2 --balance=1000 8000 | ||
check_consistency --pem=3 --balance=1000 8000 | ||
|
||
call_ledger --pem=3 --port=8000 send "$(identity 1)" 500 MFX | ||
assert_output --partial "The identity of the from field is invalid or unexpected." | ||
|
||
check_consistency --pem=1 --balance=998000 8000 | ||
check_consistency --pem=2 --balance=1000 8000 | ||
check_consistency --pem=3 --balance=1000 8000 | ||
} |
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
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
File renamed without changes.
Oops, something went wrong.