From f326b6e8772766cc33cdffd013b8375571488589 Mon Sep 17 00:00:00 2001 From: Onyeka Obi Date: Tue, 24 Jan 2023 14:11:11 -0800 Subject: [PATCH 1/5] Beginning proof wiring --- Cargo.lock | 54 +++++++++++++++++++-------- Cargo.toml | 17 +++++++++ src/many-ledger/Cargo.toml | 1 + src/many-ledger/src/module/ledger.rs | 5 ++- src/many-ledger/test-utils/Cargo.toml | 1 + src/many-ledger/test-utils/src/lib.rs | 5 ++- src/many-ledger/tests/storage.rs | 4 ++ 7 files changed, 69 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a0ac3f5..0a86aa00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -817,6 +817,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "core-foundation" version = "0.9.3" @@ -1246,8 +1252,10 @@ version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ + "convert_case", "proc-macro2", "quote", + "rustc_version", "syn", ] @@ -2483,7 +2491,7 @@ dependencies = [ [[package]] name = "many-client" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "anyhow", "async-trait", @@ -2526,7 +2534,7 @@ dependencies = [ [[package]] name = "many-client-macros" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "proc-macro2", "quote", @@ -2536,7 +2544,7 @@ dependencies = [ [[package]] name = "many-error" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "minicbor", "num-derive", @@ -2547,7 +2555,7 @@ dependencies = [ [[package]] name = "many-identity" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "base32", "coset", @@ -2567,7 +2575,7 @@ dependencies = [ [[package]] name = "many-identity-dsa" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "asn1 0.8.7", "base32", @@ -2595,7 +2603,7 @@ dependencies = [ [[package]] name = "many-identity-hsm" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "asn1 0.10.0", "coset", @@ -2615,7 +2623,7 @@ dependencies = [ [[package]] name = "many-identity-webauthn" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "authenticator-ctap2-2021", "base64 0.13.1", @@ -2681,6 +2689,7 @@ dependencies = [ name = "many-ledger" version = "0.1.0" dependencies = [ + "async-channel", "async-trait", "base64 0.20.0", "bip39-dict", @@ -2741,6 +2750,7 @@ dependencies = [ name = "many-ledger-test-utils" version = "0.1.0" dependencies = [ + "async-channel", "coset", "cucumber", "itertools", @@ -2765,8 +2775,9 @@ dependencies = [ [[package]] name = "many-macros" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ + "async-channel", "inflections", "proc-macro2", "quote", @@ -2778,7 +2789,7 @@ dependencies = [ [[package]] name = "many-migration" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "many-error", "minicbor", @@ -2791,8 +2802,9 @@ dependencies = [ [[package]] name = "many-modules" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ + "async-channel", "async-trait", "coset", "derive_builder 0.11.2", @@ -2812,8 +2824,9 @@ dependencies = [ [[package]] name = "many-protocol" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ + "async-channel", "base64 0.13.1", "coset", "derive_builder 0.10.2", @@ -2836,7 +2849,7 @@ dependencies = [ [[package]] name = "many-server" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "anyhow", "asn1 0.11.0", @@ -2885,10 +2898,11 @@ dependencies = [ [[package]] name = "many-types" version = "0.1.0" -source = "git+https://github.com/liftedinit/many-rs.git?rev=ce03b88dea51232ca578d19d52cfc263cfc4080b#ce03b88dea51232ca578d19d52cfc263cfc4080b" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" dependencies = [ "base64 0.20.0", "coset", + "derive_more", "fixed", "hex", "many-error", @@ -4000,9 +4014,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.8.0" +version = "2.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645926f31b250a2dca3c232496c2d898d91036e45ca0e97e0e2390c54e11be36" +checksum = "7c4437699b6d34972de58652c68b98cb5b53a4199ab126db8e20ec8ded29a721" dependencies = [ "bitflags", "core-foundation", @@ -5412,3 +5426,13 @@ dependencies = [ "syn", "synstructure", ] + +[[patch.unused]] +name = "many" +version = "0.1.0" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" + +[[patch.unused]] +name = "many-mock" +version = "0.1.0" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" diff --git a/Cargo.toml b/Cargo.toml index 1c107a3d..ddb8a5c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,23 @@ incremental = false #many-server = { path = "../many-rs/src/many-server" } #many-types = { path = "../many-rs/src/many-types" } +[patch."https://github.com/liftedinit/many-rs.git"] +many = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-client = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-client-macros = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-error = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-identity = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-identity-dsa = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-identity-hsm = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-identity-webauthn = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-macros = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-migration = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-mock = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-modules = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-protocol = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-server = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } +many-types = { git = "https://github.com/MavenRain/many-rs/", branch = "oobi/proof" } + [patch.crates-io] ciborium = { git = "https://github.com/enarx/ciborium", rev = "2ca375e6b33d1ade5a5798792278b35a807b748e" } ciborium-io = { git = "https://github.com/enarx/ciborium", rev = "2ca375e6b33d1ade5a5798792278b35a807b748e" } diff --git a/src/many-ledger/Cargo.toml b/src/many-ledger/Cargo.toml index 3a112373..ffbed2e0 100644 --- a/src/many-ledger/Cargo.toml +++ b/src/many-ledger/Cargo.toml @@ -17,6 +17,7 @@ name = "many-ledger" doc = false [dependencies] +async-channel = "1.8" async-trait = "0.1.51" base64 = "0.20.0-alpha.1" bip39-dict = "0.1" diff --git a/src/many-ledger/src/module/ledger.rs b/src/many-ledger/src/module/ledger.rs index 86b353aa..fc735e79 100644 --- a/src/many-ledger/src/module/ledger.rs +++ b/src/many-ledger/src/module/ledger.rs @@ -2,6 +2,7 @@ use crate::module::LedgerModuleImpl; use many_error::ManyError; use many_identity::Address; use many_modules::ledger; +use many_protocol::context::Context; use std::collections::BTreeSet; use tracing::info; @@ -34,9 +35,9 @@ impl ledger::LedgerModuleBackend for LedgerModuleImpl { fn balance( &self, sender: &Address, - args: ledger::BalanceArgs, + ledger::BalanceArgs { account, symbols }: ledger::BalanceArgs, + _: Context ) -> Result { - let ledger::BalanceArgs { account, symbols } = args; let identity = account.as_ref().unwrap_or(sender); diff --git a/src/many-ledger/test-utils/Cargo.toml b/src/many-ledger/test-utils/Cargo.toml index f0413c16..a0f395c1 100644 --- a/src/many-ledger/test-utils/Cargo.toml +++ b/src/many-ledger/test-utils/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/liftedinit/many-framework" publish = false [dependencies] +async-channel = "1.8" coset = "0.3" cucumber = { version = "0.17.0", features = ["libtest"] } itertools = "0.10.3" diff --git a/src/many-ledger/test-utils/src/lib.rs b/src/many-ledger/test-utils/src/lib.rs index 2c374458..1fc75764 100644 --- a/src/many-ledger/test-utils/src/lib.rs +++ b/src/many-ledger/test-utils/src/lib.rs @@ -1,5 +1,6 @@ pub mod cucumber; +use async_channel::unbounded; use coset::CborSerializable; use itertools::Itertools; use many_error::ManyError; @@ -22,7 +23,7 @@ use many_modules::ledger::{ BalanceArgs, LedgerCommandsModuleBackend, LedgerModuleBackend, TokenCreateArgs, }; use many_modules::{account, events, ledger}; -use many_protocol::ResponseMessage; +use many_protocol::{RequestMessage, ResponseMessage}; use many_types::ledger::{ LedgerTokensAddressMap, Symbol, TokenAmount, TokenInfoSummary, TokenMaybeOwner, }; @@ -249,6 +250,7 @@ impl Setup { account: None, symbols: Some(vec![symbol].into()), }, + (RequestMessage::default(), unbounded().0).into() )? .balances .get(&symbol) @@ -756,6 +758,7 @@ pub fn verify_balance( account: Some(id), symbols: Some(vec![symbol].into()), }, + (RequestMessage::default(), unbounded().0).into() ); assert!(result.is_ok()); let balances = result.unwrap(); diff --git a/src/many-ledger/tests/storage.rs b/src/many-ledger/tests/storage.rs index 9e3bcf34..e85ea950 100644 --- a/src/many-ledger/tests/storage.rs +++ b/src/many-ledger/tests/storage.rs @@ -1,3 +1,4 @@ +use async_channel::unbounded; use many_identity::testing::identity; use many_identity::Address; use many_ledger::migration::tokens::TOKEN_MIGRATION; @@ -8,6 +9,7 @@ use many_modules::account::features::FeatureInfo; use many_modules::account::AccountModuleBackend; use many_modules::ledger::{LedgerModuleBackend, LedgerTokensModuleBackend, TokenInfoArgs}; use many_modules::{account, ledger}; +use many_protocol::RequestMessage; use std::collections::{BTreeMap, BTreeSet}; /// Verify persistent storage can be re-loaded @@ -57,6 +59,7 @@ fn load() { account: Some(identity(5)), symbols: Some(vec![identity(1000)].into()), }, + (RequestMessage::default(), unbounded().0).into() ) .unwrap(); assert_eq!( @@ -150,6 +153,7 @@ fn load_symbol_meta() { account: Some(identity(5)), symbols: Some(vec![identity(1000)].into()), }, + (RequestMessage::default(), unbounded().0).into() ) .unwrap(); assert_eq!( From 113af00071e864ff12695d4890a533dcc32bd6e8 Mon Sep 17 00:00:00 2001 From: Onyeka Obi Date: Wed, 25 Jan 2023 14:51:25 -0800 Subject: [PATCH 2/5] Now calculating the relevant proof for balance --- Cargo.lock | 52 +++++++++---------- src/many-ledger/src/module/ledger.rs | 10 ++-- src/many-ledger/src/module/ledger_mintburn.rs | 12 +++-- src/many-ledger/src/storage/ledger.rs | 52 +++++++++++-------- .../src/storage/ledger_mintburn.rs | 7 ++- src/many-ledger/test-utils/src/lib.rs | 4 +- src/many-ledger/tests/storage.rs | 4 +- 7 files changed, 80 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a86aa00..4be35363 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -671,9 +671,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.1.3" +version = "4.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8d93d855ce6a0aa87b8473ef9169482f40abaa2e9e0993024c35c902cbd5920" +checksum = "f13b9c79b5d1dd500d20ef541215a6423c75829ef43117e1b4d17fd8af0b5d76" dependencies = [ "bitflags", "clap_derive 4.1.0", @@ -841,9 +841,9 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "coset" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "604cb30f7b6f4fee05b9ddf88cf6d3c0af0f98d9099e6f6a050e81e7a7cb938b" +checksum = "c765a4e852cef25c69a48e9fcd60995a7fecabf0134a0021e7181452c4a60f95" dependencies = [ "ciborium", "ciborium-io", @@ -981,7 +981,7 @@ dependencies = [ "anyhow", "async-trait", "atty", - "clap 4.1.3", + "clap 4.1.4", "console", "cucumber-codegen", "cucumber-expressions", @@ -1512,9 +1512,9 @@ dependencies = [ [[package]] name = "fixed" -version = "1.21.0" +version = "1.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1965703169d4c3581f1d853e6a0bacf2ad9ba5d161f58ac331de95b37584a50" +checksum = "ad45f1b686656281ac6a3b7eaf487be0e9e69fe4678f8497c84beb5f0fab7dae" dependencies = [ "az", "bytemuck", @@ -2491,7 +2491,7 @@ dependencies = [ [[package]] name = "many-client" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "anyhow", "async-trait", @@ -2534,7 +2534,7 @@ dependencies = [ [[package]] name = "many-client-macros" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "proc-macro2", "quote", @@ -2544,7 +2544,7 @@ dependencies = [ [[package]] name = "many-error" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "minicbor", "num-derive", @@ -2555,7 +2555,7 @@ dependencies = [ [[package]] name = "many-identity" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "base32", "coset", @@ -2575,7 +2575,7 @@ dependencies = [ [[package]] name = "many-identity-dsa" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "asn1 0.8.7", "base32", @@ -2603,7 +2603,7 @@ dependencies = [ [[package]] name = "many-identity-hsm" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "asn1 0.10.0", "coset", @@ -2623,7 +2623,7 @@ dependencies = [ [[package]] name = "many-identity-webauthn" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "authenticator-ctap2-2021", "base64 0.13.1", @@ -2775,7 +2775,7 @@ dependencies = [ [[package]] name = "many-macros" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "async-channel", "inflections", @@ -2789,7 +2789,7 @@ dependencies = [ [[package]] name = "many-migration" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "many-error", "minicbor", @@ -2802,7 +2802,7 @@ dependencies = [ [[package]] name = "many-modules" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "async-channel", "async-trait", @@ -2824,7 +2824,7 @@ dependencies = [ [[package]] name = "many-protocol" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "async-channel", "base64 0.13.1", @@ -2849,7 +2849,7 @@ dependencies = [ [[package]] name = "many-server" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "anyhow", "asn1 0.11.0", @@ -2898,7 +2898,7 @@ dependencies = [ [[package]] name = "many-types" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" dependencies = [ "base64 0.20.0", "coset", @@ -3130,18 +3130,18 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.5.7" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9" +checksum = "8d829733185c1ca374f17e52b762f24f535ec625d2cc1f070e34c8a9068f341b" dependencies = [ "num_enum_derive", ] [[package]] name = "num_enum_derive" -version = "0.5.7" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce" +checksum = "2be1598bf1c313dcdd12092e3f1920f463462525a21b7b4e11b4168353d0123e" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -5430,9 +5430,9 @@ dependencies = [ [[patch.unused]] name = "many" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" [[patch.unused]] name = "many-mock" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#27e611fd11572038817fe84951469a4ba00ff729" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" diff --git a/src/many-ledger/src/module/ledger.rs b/src/many-ledger/src/module/ledger.rs index fc735e79..a75c0139 100644 --- a/src/many-ledger/src/module/ledger.rs +++ b/src/many-ledger/src/module/ledger.rs @@ -36,16 +36,18 @@ impl ledger::LedgerModuleBackend for LedgerModuleImpl { &self, sender: &Address, ledger::BalanceArgs { account, symbols }: ledger::BalanceArgs, - _: Context + context: Context, ) -> Result { - let identity = account.as_ref().unwrap_or(sender); let storage = &self.storage; let symbols = symbols.unwrap_or_default().0; - let balances = storage - .get_multiple_balances(identity, &BTreeSet::from_iter(symbols.clone().into_iter()))?; + let balances = storage.get_multiple_balances( + identity, + &BTreeSet::from_iter(symbols.clone().into_iter()), + context, + )?; info!("balance({}, {:?}): {:?}", identity, &symbols, &balances); Ok(ledger::BalanceReturns { balances }) } diff --git a/src/many-ledger/src/module/ledger_mintburn.rs b/src/many-ledger/src/module/ledger_mintburn.rs index 271691e9..f0933d80 100644 --- a/src/many-ledger/src/module/ledger_mintburn.rs +++ b/src/many-ledger/src/module/ledger_mintburn.rs @@ -7,15 +7,17 @@ use many_identity::Address; use many_modules::events::EventInfo; use many_modules::ledger; use many_modules::ledger::{TokenBurnArgs, TokenBurnReturns, TokenMintArgs, TokenMintReturns}; +use many_protocol::context::Context; use many_types::ledger::Symbol; use std::collections::BTreeSet; /// Check if a symbol exists in the storage fn check_symbol_exists(symbol: &Symbol, symbols: BTreeSet) -> Result<(), ManyError> { if !symbols.contains(symbol) { - return Err(error::symbol_not_found(symbol.to_string())); + Err(error::symbol_not_found(symbol.to_string())) + } else { + Ok(()) } - Ok(()) } impl ledger::LedgerMintBurnModuleBackend for LedgerModuleImpl { @@ -23,6 +25,7 @@ impl ledger::LedgerMintBurnModuleBackend for LedgerModuleImpl { &mut self, sender: &Address, args: TokenMintArgs, + context: Context, ) -> Result { if !self.storage.migrations().is_active(&TOKEN_MIGRATION) { return Err(ManyError::invalid_method_name("tokens.mint")); @@ -44,7 +47,7 @@ impl ledger::LedgerMintBurnModuleBackend for LedgerModuleImpl { check_symbol_exists(&symbol, self.storage.get_symbols()?)?; // Mint into storage - self.storage.mint_token(symbol, &distribution)?; + self.storage.mint_token(symbol, &distribution, context)?; // Log event self.storage.log_event(EventInfo::TokenMint { @@ -60,6 +63,7 @@ impl ledger::LedgerMintBurnModuleBackend for LedgerModuleImpl { &mut self, sender: &Address, args: TokenBurnArgs, + context: Context, ) -> Result { if !self.storage.migrations().is_active(&TOKEN_MIGRATION) { return Err(ManyError::invalid_method_name("tokens.burn")); @@ -89,7 +93,7 @@ impl ledger::LedgerMintBurnModuleBackend for LedgerModuleImpl { } // Burn from storage - self.storage.burn_token(symbol, &distribution)?; + self.storage.burn_token(symbol, &distribution, context)?; // Log event self.storage.log_event(EventInfo::TokenBurn { diff --git a/src/many-ledger/src/storage/ledger.rs b/src/many-ledger/src/storage/ledger.rs index f6453998..0bf3176f 100644 --- a/src/many-ledger/src/storage/ledger.rs +++ b/src/many-ledger/src/storage/ledger.rs @@ -2,8 +2,9 @@ use crate::error; use crate::storage::{key_for_account_balance, LedgerStorage}; use many_error::ManyError; use many_identity::Address; +use many_protocol::context::Context; use many_types::ledger::{Symbol, TokenAmount}; -use merk::{BatchEntry, Op}; +use merk::{proofs::Query, BatchEntry, Op}; use std::collections::{BTreeMap, BTreeSet}; impl LedgerStorage { @@ -36,42 +37,51 @@ impl LedgerStorage { fn get_all_balances( &self, identity: &Address, + context: impl AsRef, ) -> Result, ManyError> { - if identity.is_anonymous() { + Ok(if identity.is_anonymous() { // Anonymous cannot hold funds. - Ok(BTreeMap::new()) + BTreeMap::new() } else { let mut result = BTreeMap::new(); for symbol in self.get_symbols()? { - match self - .persistent_store - .get(&key_for_account_balance(identity, &symbol)) - .map_err(error::storage_get_failed)? - { - None => {} - Some(value) => { - result.insert(symbol, TokenAmount::from(value)); - } - } + match context.as_ref().prove(|| { + self.persistent_store + .prove({ + let mut query = Query::new(); + query.insert_key(key_for_account_balance(identity, &symbol)); + query + }) + .map_err(|error| ManyError::unknown(error.to_string())) + }) { + Some(error) => Err(ManyError::unknown(error.to_string())), + None => Ok(self + .persistent_store + .get(&key_for_account_balance(identity, &symbol)) + .map_err(error::storage_get_failed)? + .map(|value| result.insert(symbol, TokenAmount::from(value))) + .map(|_| ()) + .unwrap_or_default()), + }? } - Ok(result) - } + result + }) } pub fn get_multiple_balances( &self, identity: &Address, symbols: &BTreeSet, + context: impl AsRef, ) -> Result, ManyError> { - if symbols.is_empty() { - Ok(self.get_all_balances(identity)?) + Ok(if symbols.is_empty() { + self.get_all_balances(identity, context)? } else { - Ok(self - .get_all_balances(identity)? + self.get_all_balances(identity, context)? .into_iter() .filter(|(k, _v)| symbols.contains(k)) - .collect()) - } + .collect() + }) } } diff --git a/src/many-ledger/src/storage/ledger_mintburn.rs b/src/many-ledger/src/storage/ledger_mintburn.rs index 27ef6028..a5d106a3 100644 --- a/src/many-ledger/src/storage/ledger_mintburn.rs +++ b/src/many-ledger/src/storage/ledger_mintburn.rs @@ -3,6 +3,7 @@ use crate::storage::ledger_tokens::key_for_symbol; use crate::storage::{key_for_account_balance, LedgerStorage}; use many_error::ManyError; use many_modules::ledger::TokenInfoArgs; +use many_protocol::context::Context; use many_types::ledger::{LedgerTokensAddressMap, Symbol, TokenAmount, TokenInfoSupply}; use merk::{BatchEntry, Op}; use std::collections::BTreeSet; @@ -22,6 +23,7 @@ impl LedgerStorage { &mut self, symbol: Symbol, distribution: &LedgerTokensAddressMap, + context: impl AsRef, ) -> Result<(), ManyError> { let mut batch: Vec = Vec::new(); let mut circulating = TokenAmount::zero(); @@ -44,7 +46,7 @@ impl LedgerStorage { // Store the new balance to the DB let new_balance = self - .get_multiple_balances(address, &BTreeSet::from([symbol]))? + .get_multiple_balances(address, &BTreeSet::from([symbol]), context.as_ref())? .get(&symbol) .map_or(amount.clone(), |b| b + amount); let key = key_for_account_balance(address, &symbol); @@ -79,6 +81,7 @@ impl LedgerStorage { &mut self, symbol: Symbol, distribution: &LedgerTokensAddressMap, + context: impl AsRef, ) -> Result<(), ManyError> { let mut batch: Vec = Vec::new(); let mut circulating = TokenAmount::zero(); @@ -90,7 +93,7 @@ impl LedgerStorage { // Check if we have enough funds let balance_amount = match self - .get_multiple_balances(address, &BTreeSet::from_iter([symbol]))? + .get_multiple_balances(address, &BTreeSet::from_iter([symbol]), context.as_ref())? .get(&symbol) { Some(x) if x < amount => Err(error::missing_funds(symbol, amount, x)), diff --git a/src/many-ledger/test-utils/src/lib.rs b/src/many-ledger/test-utils/src/lib.rs index 1fc75764..f7c79304 100644 --- a/src/many-ledger/test-utils/src/lib.rs +++ b/src/many-ledger/test-utils/src/lib.rs @@ -250,7 +250,7 @@ impl Setup { account: None, symbols: Some(vec![symbol].into()), }, - (RequestMessage::default(), unbounded().0).into() + (RequestMessage::default(), unbounded().0).into(), )? .balances .get(&symbol) @@ -758,7 +758,7 @@ pub fn verify_balance( account: Some(id), symbols: Some(vec![symbol].into()), }, - (RequestMessage::default(), unbounded().0).into() + (RequestMessage::default(), unbounded().0).into(), ); assert!(result.is_ok()); let balances = result.unwrap(); diff --git a/src/many-ledger/tests/storage.rs b/src/many-ledger/tests/storage.rs index e85ea950..ca96cf9b 100644 --- a/src/many-ledger/tests/storage.rs +++ b/src/many-ledger/tests/storage.rs @@ -59,7 +59,7 @@ fn load() { account: Some(identity(5)), symbols: Some(vec![identity(1000)].into()), }, - (RequestMessage::default(), unbounded().0).into() + (RequestMessage::default(), unbounded().0).into(), ) .unwrap(); assert_eq!( @@ -153,7 +153,7 @@ fn load_symbol_meta() { account: Some(identity(5)), symbols: Some(vec![identity(1000)].into()), }, - (RequestMessage::default(), unbounded().0).into() + (RequestMessage::default(), unbounded().0).into(), ) .unwrap(); assert_eq!( From ef5013fde3a7aa5740723d85936b4b49bc0470b5 Mon Sep 17 00:00:00 2001 From: Onyeka Obi Date: Wed, 25 Jan 2023 17:57:56 -0800 Subject: [PATCH 3/5] End-to-end proof wiring example for ledger.balance --- Cargo.lock | 30 ++++++------- src/many-ledger/src/storage/ledger.rs | 65 +++++++++++++++++++-------- 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4be35363..59d18808 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2491,7 +2491,7 @@ dependencies = [ [[package]] name = "many-client" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "anyhow", "async-trait", @@ -2534,7 +2534,7 @@ dependencies = [ [[package]] name = "many-client-macros" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "proc-macro2", "quote", @@ -2544,7 +2544,7 @@ dependencies = [ [[package]] name = "many-error" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "minicbor", "num-derive", @@ -2555,7 +2555,7 @@ dependencies = [ [[package]] name = "many-identity" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "base32", "coset", @@ -2575,7 +2575,7 @@ dependencies = [ [[package]] name = "many-identity-dsa" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "asn1 0.8.7", "base32", @@ -2603,7 +2603,7 @@ dependencies = [ [[package]] name = "many-identity-hsm" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "asn1 0.10.0", "coset", @@ -2623,7 +2623,7 @@ dependencies = [ [[package]] name = "many-identity-webauthn" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "authenticator-ctap2-2021", "base64 0.13.1", @@ -2775,7 +2775,7 @@ dependencies = [ [[package]] name = "many-macros" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "async-channel", "inflections", @@ -2789,7 +2789,7 @@ dependencies = [ [[package]] name = "many-migration" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "many-error", "minicbor", @@ -2802,7 +2802,7 @@ dependencies = [ [[package]] name = "many-modules" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "async-channel", "async-trait", @@ -2824,7 +2824,7 @@ dependencies = [ [[package]] name = "many-protocol" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "async-channel", "base64 0.13.1", @@ -2849,7 +2849,7 @@ dependencies = [ [[package]] name = "many-server" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "anyhow", "asn1 0.11.0", @@ -2898,7 +2898,7 @@ dependencies = [ [[package]] name = "many-types" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" dependencies = [ "base64 0.20.0", "coset", @@ -5430,9 +5430,9 @@ dependencies = [ [[patch.unused]] name = "many" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" [[patch.unused]] name = "many-mock" version = "0.1.0" -source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#bdc5cf9e58a9cc4657e0df0f9f2aa73af4ac76d5" +source = "git+https://github.com/MavenRain/many-rs/?branch=oobi/proof#576f50d5c4718c890bae54fd00a51a71bf3e65f3" diff --git a/src/many-ledger/src/storage/ledger.rs b/src/many-ledger/src/storage/ledger.rs index 0bf3176f..b1bb3db7 100644 --- a/src/many-ledger/src/storage/ledger.rs +++ b/src/many-ledger/src/storage/ledger.rs @@ -3,8 +3,19 @@ use crate::storage::{key_for_account_balance, LedgerStorage}; use many_error::ManyError; use many_identity::Address; use many_protocol::context::Context; -use many_types::ledger::{Symbol, TokenAmount}; -use merk::{proofs::Query, BatchEntry, Op}; +use many_types::{ + ledger::{Symbol, TokenAmount}, + ProofOperation, +}; +use merk::{ + proofs::{ + Decoder, + Node::{Hash, KVHash, KV}, + Op::{Child, Parent, Push}, + Query, + }, + BatchEntry, Op, +}; use std::collections::{BTreeMap, BTreeSet}; impl LedgerStorage { @@ -44,26 +55,44 @@ impl LedgerStorage { BTreeMap::new() } else { let mut result = BTreeMap::new(); + let mut query = Query::new(); for symbol in self.get_symbols()? { - match context.as_ref().prove(|| { + let key = key_for_account_balance(identity, &symbol); + self.persistent_store + .get(&key) + .map_err(error::storage_get_failed)? + .map(|value| result.insert(symbol, TokenAmount::from(value))) + .map(|_| ()) + .unwrap_or_default(); + query.insert_key(key) + } + context + .as_ref() + .prove(|| { self.persistent_store - .prove({ - let mut query = Query::new(); - query.insert_key(key_for_account_balance(identity, &symbol)); - query + .prove(query) + .and_then(|proof| { + Decoder::new(proof.as_slice()) + .into_iter() + .map(|fallible_operation| { + fallible_operation.map(|operation| match operation { + Child => ProofOperation::Child, + Parent => ProofOperation::Parent, + Push(Hash(hash)) => ProofOperation::NodeHash(hash.to_vec()), + Push(KV(key, value)) => { + ProofOperation::KeyValuePair(key.into(), value.into()) + } + Push(KVHash(hash)) => { + ProofOperation::KeyValueHash(hash.to_vec()) + } + }) + }) + .collect::, _>>() }) .map_err(|error| ManyError::unknown(error.to_string())) - }) { - Some(error) => Err(ManyError::unknown(error.to_string())), - None => Ok(self - .persistent_store - .get(&key_for_account_balance(identity, &symbol)) - .map_err(error::storage_get_failed)? - .map(|value| result.insert(symbol, TokenAmount::from(value))) - .map(|_| ()) - .unwrap_or_default()), - }? - } + }) + .map(|error| Err(ManyError::unknown(error.to_string()))) + .unwrap_or(Ok(()))?; result }) From ef6f1ecc4901ccf78fb29415ae06f60cf219d176 Mon Sep 17 00:00:00 2001 From: Onyeka Obi Date: Wed, 25 Jan 2023 18:34:30 -0800 Subject: [PATCH 4/5] Redundant into_iter --- src/many-ledger/src/storage/ledger.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/many-ledger/src/storage/ledger.rs b/src/many-ledger/src/storage/ledger.rs index b1bb3db7..faa80440 100644 --- a/src/many-ledger/src/storage/ledger.rs +++ b/src/many-ledger/src/storage/ledger.rs @@ -73,7 +73,6 @@ impl LedgerStorage { .prove(query) .and_then(|proof| { Decoder::new(proof.as_slice()) - .into_iter() .map(|fallible_operation| { fallible_operation.map(|operation| match operation { Child => ProofOperation::Child, From 9aa2d0729970f109419330792e92de5066feaac3 Mon Sep 17 00:00:00 2001 From: Onyeka Obi Date: Wed, 25 Jan 2023 18:52:38 -0800 Subject: [PATCH 5/5] Refactor get_all_balances --- src/many-ledger/src/storage/ledger.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/many-ledger/src/storage/ledger.rs b/src/many-ledger/src/storage/ledger.rs index faa80440..2831fa9e 100644 --- a/src/many-ledger/src/storage/ledger.rs +++ b/src/many-ledger/src/storage/ledger.rs @@ -103,10 +103,11 @@ impl LedgerStorage { symbols: &BTreeSet, context: impl AsRef, ) -> Result, ManyError> { + let balances = self.get_all_balances(identity, context)?; Ok(if symbols.is_empty() { - self.get_all_balances(identity, context)? + balances } else { - self.get_all_balances(identity, context)? + balances .into_iter() .filter(|(k, _v)| symbols.contains(k)) .collect()