Просмотр исходного кода

tokenlist: Move the token list into an Arc, and share around.

parazyd 4 лет назад
Родитель
Сommit
695d7ed084

+ 14 - 27
bin/darkfid2/src/main.rs

@@ -146,7 +146,6 @@ pub struct Darkfid {
     sync_p2p: Option<P2pPtr>,
     client: Arc<Client>,
     validator_state: ValidatorStatePtr,
-    tokenlist: DrkTokenList,
 }
 
 // JSON-RPC methods
@@ -186,20 +185,12 @@ impl Darkfid {
         validator_state: ValidatorStatePtr,
         consensus_p2p: Option<P2pPtr>,
         sync_p2p: Option<P2pPtr>,
-        tokenlist: DrkTokenList,
     ) -> Result<Self> {
         debug!("Waiting for validator state lock");
         let client = validator_state.read().await.client.clone();
         debug!("Released validator state lock");
 
-        Ok(Self {
-            synced: Mutex::new(false),
-            consensus_p2p,
-            sync_p2p,
-            client,
-            validator_state,
-            tokenlist,
-        })
+        Ok(Self { synced: Mutex::new(false), consensus_p2p, sync_p2p, client, validator_state })
     }
 }
 
@@ -233,9 +224,18 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         }
     };
 
+    debug!("Parsing token lists...");
+    let tokenlist = Arc::new(DrkTokenList::new(&[
+        ("drk", include_bytes!("../../../contrib/token/darkfi_token_list.min.json")),
+        ("btc", include_bytes!("../../../contrib/token/bitcoin_token_list.min.json")),
+        ("eth", include_bytes!("../../../contrib/token/erc20_token_list.min.json")),
+        ("sol", include_bytes!("../../../contrib/token/solana_token_list.min.json")),
+    ])?);
+    debug!("Finished parsing token lists");
+
     // TODO: sqldb init cleanup
     // Initialize Client
-    let client = Arc::new(Client::new(wallet).await?);
+    let client = Arc::new(Client::new(wallet, tokenlist).await?);
 
     // Parse cashier addresses
     let mut cashier_pubkeys = vec![];
@@ -264,15 +264,6 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     )
     .await?;
 
-    debug!("Parsing token lists...");
-    let tokenlist = DrkTokenList::new(&[
-        ("drk", include_bytes!("../../../contrib/token/darkfi_token_list.min.json")),
-        ("btc", include_bytes!("../../../contrib/token/bitcoin_token_list.min.json")),
-        ("eth", include_bytes!("../../../contrib/token/erc20_token_list.min.json")),
-        ("sol", include_bytes!("../../../contrib/token/solana_token_list.min.json")),
-    ])?;
-    debug!("Finished parsing token lists");
-
     let sync_p2p = {
         info!("Registering block sync P2P protocols...");
         let sync_network_settings = net::Settings {
@@ -288,13 +279,11 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         let registry = p2p.protocol_registry();
 
         let _state = state.clone();
-        let _tokenlist = tokenlist.clone();
         registry
             .register(net::SESSION_ALL, move |channel, p2p| {
                 let state = _state.clone();
-                let tokenlist = _tokenlist.clone();
                 async move {
-                    ProtocolSync::init(channel, state, tokenlist, p2p, args.consensus)
+                    ProtocolSync::init(channel, state, p2p, args.consensus)
                         .await
                         .unwrap()
                 }
@@ -372,9 +361,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     };
 
     // Initialize program state
-    let darkfid =
-        Darkfid::new(state.clone(), consensus_p2p.clone(), sync_p2p.clone(), tokenlist.clone())
-            .await?;
+    let darkfid = Darkfid::new(state.clone(), consensus_p2p.clone(), sync_p2p.clone()).await?;
     let darkfid = Arc::new(darkfid);
 
     // JSON-RPC server
@@ -392,7 +379,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     })
     .detach();
 
-    match block_sync_task(sync_p2p.clone().unwrap(), state.clone(), &tokenlist).await {
+    match block_sync_task(sync_p2p.clone().unwrap(), state.clone()).await {
         Ok(()) => *darkfid.synced.lock().await = true,
         Err(e) => error!("Failed syncing blockchain: {}", e),
     }

+ 11 - 11
bin/darkfid2/src/rpc_tx.rs

@@ -85,18 +85,18 @@ impl Darkfid {
             }
         };
 
-        let token_id = if let Some(tok) = self.tokenlist.by_net[&network].get(token.to_uppercase())
-        {
-            tok.drk_address
-        } else {
-            match generate_id(&network, token) {
-                Ok(v) => v,
-                Err(e) => {
-                    error!("transfer(): Failed generate_id(): {}", e);
-                    return jsonrpc::error(InternalError, None, id).into()
+        let token_id =
+            if let Some(tok) = self.client.tokenlist.by_net[&network].get(token.to_uppercase()) {
+                tok.drk_address
+            } else {
+                match generate_id(&network, token) {
+                    Ok(v) => v,
+                    Err(e) => {
+                        error!("transfer(): Failed generate_id(): {}", e);
+                        return jsonrpc::error(InternalError, None, id).into()
+                    }
                 }
-            }
-        };
+            };
 
         let tx = match self
             .client

+ 2 - 2
bin/darkfid2/src/rpc_wallet.rs

@@ -215,7 +215,7 @@ impl Darkfid {
             let mut amount = BigUint::from(balance.value);
 
             let (net_name, net_addr) =
-                if let Some((net, tok)) = self.tokenlist.by_addr.get(&drk_addr) {
+                if let Some((net, tok)) = self.client.tokenlist.by_addr.get(&drk_addr) {
                     (net, tok.net_address.clone())
                 } else {
                     warn!("Could not find network name and token info for {}", drk_addr);
@@ -223,7 +223,7 @@ impl Darkfid {
                 };
 
             let mut ticker = None;
-            for (k, v) in self.tokenlist.by_net[net_name].0.iter() {
+            for (k, v) in self.client.tokenlist.by_net[net_name].0.iter() {
                 if v.net_address == net_addr {
                     ticker = Some(k.clone());
                     break

+ 16 - 25
bin/faucetd/src/main.rs

@@ -130,7 +130,6 @@ pub struct Faucetd {
     sync_p2p: P2pPtr,
     client: Arc<Client>,
     validator_state: ValidatorStatePtr,
-    tokenlist: DrkTokenList,
     airdrop_timeout: i64,
     airdrop_limit: BigUint,
     airdrop_map: Arc<Mutex<HashMap<Address, i64>>>,
@@ -156,7 +155,6 @@ impl Faucetd {
     pub async fn new(
         validator_state: ValidatorStatePtr,
         sync_p2p: P2pPtr,
-        tokenlist: DrkTokenList,
         timeout: i64,
         limit: BigUint,
     ) -> Result<Self> {
@@ -167,7 +165,6 @@ impl Faucetd {
             sync_p2p,
             client,
             validator_state,
-            tokenlist,
             airdrop_timeout: timeout,
             airdrop_limit: limit,
             airdrop_map: Arc::new(Mutex::new(HashMap::new())),
@@ -228,8 +225,10 @@ impl Faucetd {
         };
         drop(map);
 
-        let token_id =
-            self.tokenlist.by_net[&NetworkName::DarkFi].get("DRK".to_string()).unwrap().drk_address;
+        let token_id = self.client.tokenlist.by_net[&NetworkName::DarkFi]
+            .get("DRK".to_string())
+            .unwrap()
+            .drk_address;
 
         let amnt: u64 = match amount.try_into() {
             Ok(v) => v,
@@ -331,9 +330,16 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         }
     };
 
+    let tokenlist = Arc::new(DrkTokenList::new(&[
+        ("drk", include_bytes!("../../../contrib/token/darkfi_token_list.min.json")),
+        ("btc", include_bytes!("../../../contrib/token/bitcoin_token_list.min.json")),
+        ("eth", include_bytes!("../../../contrib/token/erc20_token_list.min.json")),
+        ("sol", include_bytes!("../../../contrib/token/solana_token_list.min.json")),
+    ])?);
+
     // TODO: sqldb init cleanup
     // Initialize client
-    let client = Arc::new(Client::new(wallet.clone()).await?);
+    let client = Arc::new(Client::new(wallet.clone(), tokenlist).await?);
 
     // Parse cashier addresses
     let mut cashier_pubkeys = vec![];
@@ -362,13 +368,6 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     )
     .await?;
 
-    let tokenlist = DrkTokenList::new(&[
-        ("drk", include_bytes!("../../../contrib/token/darkfi_token_list.min.json")),
-        ("btc", include_bytes!("../../../contrib/token/bitcoin_token_list.min.json")),
-        ("eth", include_bytes!("../../../contrib/token/erc20_token_list.min.json")),
-        ("sol", include_bytes!("../../../contrib/token/solana_token_list.min.json")),
-    ])?;
-
     // P2P network. The faucet doesn't participate in consensus, so we only
     // build the sync protocol.
     let network_settings = net::Settings {
@@ -385,12 +384,10 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
 
     info!("Registering block sync P2P protocols...");
     let _state = state.clone();
-    let _tokenlist = tokenlist.clone();
     registry
         .register(net::SESSION_ALL, move |channel, p2p| {
             let state = _state.clone();
-            let tokenlist = _tokenlist.clone();
-            async move { ProtocolSync::init(channel, state, tokenlist, p2p, false).await.unwrap() }
+            async move { ProtocolSync::init(channel, state, p2p, false).await.unwrap() }
         })
         .await;
 
@@ -406,14 +403,8 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     let airdrop_limit = decode_base10(&args.airdrop_limit, 8, true)?;
 
     // Initialize program state
-    let faucetd = Faucetd::new(
-        state.clone(),
-        sync_p2p.clone(),
-        tokenlist.clone(),
-        airdrop_timeout,
-        airdrop_limit,
-    )
-    .await?;
+    let faucetd =
+        Faucetd::new(state.clone(), sync_p2p.clone(), airdrop_timeout, airdrop_limit).await?;
     let faucetd = Arc::new(faucetd);
 
     // Task to periodically clean up the hashmap of airdrops.
@@ -434,7 +425,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     })
     .detach();
 
-    match block_sync_task(sync_p2p.clone(), state.clone(), &tokenlist).await {
+    match block_sync_task(sync_p2p.clone(), state.clone()).await {
         Ok(()) => *faucetd.synced.lock().await = true,
         Err(e) => error!("Failed syncing blockchain: {}", e),
     }

+ 1 - 11
src/consensus/proto/protocol_sync.rs

@@ -8,7 +8,6 @@ use crate::{
         block::{BlockInfo, BlockOrder, BlockResponse},
         ValidatorState, ValidatorStatePtr,
     },
-    crypto::token_list::DrkTokenList,
     net::{
         ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
         ProtocolJobsManager, ProtocolJobsManagerPtr,
@@ -26,7 +25,6 @@ pub struct ProtocolSync {
     block_sub: MessageSubscription<BlockInfo>,
     jobsman: ProtocolJobsManagerPtr,
     state: ValidatorStatePtr,
-    tokenlist: DrkTokenList,
     p2p: P2pPtr,
     consensus_mode: bool,
 }
@@ -35,7 +33,6 @@ impl ProtocolSync {
     pub async fn init(
         channel: ChannelPtr,
         state: ValidatorStatePtr,
-        tokenlist: DrkTokenList,
         p2p: P2pPtr,
         consensus_mode: bool,
     ) -> Result<ProtocolBasePtr> {
@@ -52,7 +49,6 @@ impl ProtocolSync {
             block_sub,
             jobsman: ProtocolJobsManager::new("SyncProtocol", channel),
             state,
-            tokenlist,
             p2p,
             consensus_mode,
         }))
@@ -137,13 +133,7 @@ impl ProtocolSync {
                     debug!("ProtocolSync::handle_receive_block(): All state transitions passed");
 
                     debug!("ProtocolSync::handle_receive_block(): Updating canon state machine");
-                    match self
-                        .state
-                        .write()
-                        .await
-                        .update_canon_state(state_updates, &self.tokenlist, None)
-                        .await
-                    {
+                    match self.state.write().await.update_canon_state(state_updates, None).await {
                         Ok(()) => {}
                         Err(e) => {
                             error!("handle_receive_block(): Canon statemachine update fail: {}", e);

+ 1 - 3
src/consensus/state.rs

@@ -21,7 +21,6 @@ use crate::{
         address::Address,
         keypair::{PublicKey, SecretKey},
         schnorr::{SchnorrPublic, SchnorrSecret},
-        token_list::DrkTokenList,
     },
     net,
     node::{
@@ -856,7 +855,6 @@ impl ValidatorState {
     pub async fn update_canon_state(
         &self,
         updates: Vec<StateUpdate>,
-        tokenlist: &DrkTokenList,
         notify: Option<async_channel::Sender<(PublicKey, u64)>>,
     ) -> Result<()> {
         let secret_keys: Vec<SecretKey> =
@@ -871,7 +869,7 @@ impl ValidatorState {
                     secret_keys.clone(),
                     notify.clone(),
                     self.client.wallet.clone(),
-                    tokenlist,
+                    self.client.tokenlist.clone(),
                 )
                 .await?;
         }

+ 2 - 7
src/consensus/task/block_sync.rs

@@ -3,7 +3,6 @@ use crate::{
         block::{BlockOrder, BlockResponse},
         ValidatorState, ValidatorStatePtr,
     },
-    crypto::token_list::DrkTokenList,
     net,
     node::MemoryState,
     Result,
@@ -11,11 +10,7 @@ use crate::{
 use log::{debug, info, warn};
 
 /// async task used for block syncing.
-pub async fn block_sync_task(
-    p2p: net::P2pPtr,
-    state: ValidatorStatePtr,
-    tokenlist: &DrkTokenList,
-) -> Result<()> {
+pub async fn block_sync_task(p2p: net::P2pPtr, state: ValidatorStatePtr) -> Result<()> {
     info!("Starting blockchain sync...");
 
     // we retrieve p2p network connected channels, so we can use it to
@@ -63,7 +58,7 @@ pub async fn block_sync_task(
             debug!("block_sync_task(): All state transitions passed");
 
             debug!("block_sync_task(): Updating canon state");
-            state.write().await.update_canon_state(canon_updates, tokenlist, None).await?;
+            state.write().await.update_canon_state(canon_updates, None).await?;
 
             debug!("block_sync_task(): Appending blocks to ledger");
             state.write().await.blockchain.add(&resp.blocks)?;

+ 4 - 1
src/node/client.rs

@@ -11,6 +11,7 @@ use crate::{
         keypair::{Keypair, PublicKey},
         merkle_node::MerkleNode,
         proof::ProvingKey,
+        token_list::DrkTokenList,
         types::DrkTokenId,
         OwnCoin,
     },
@@ -32,12 +33,13 @@ use crate::{
 pub struct Client {
     pub main_keypair: Mutex<Keypair>,
     pub wallet: WalletPtr,
+    pub tokenlist: Arc<DrkTokenList>,
     mint_pk: Lazy<ProvingKey>,
     burn_pk: Lazy<ProvingKey>,
 }
 
 impl Client {
-    pub async fn new(wallet: WalletPtr) -> Result<Self> {
+    pub async fn new(wallet: WalletPtr, tokenlist: Arc<DrkTokenList>) -> Result<Self> {
         // Initialize or load the wallet
         wallet.init_db().await?;
 
@@ -54,6 +56,7 @@ impl Client {
         Ok(Self {
             main_keypair: Mutex::new(main_keypair),
             wallet,
+            tokenlist,
             mint_pk: Lazy::new(),
             burn_pk: Lazy::new(),
         })

+ 3 - 2
src/node/state.rs

@@ -1,3 +1,4 @@
+use async_std::sync::Arc;
 use incrementalmerkletree::{bridgetree::BridgeTree, Frontier, Tree};
 use lazy_init::Lazy;
 use log::{debug, error};
@@ -138,7 +139,7 @@ impl State {
         secret_keys: Vec<SecretKey>,
         notify: Option<async_channel::Sender<(PublicKey, u64)>>,
         wallet: WalletPtr,
-        tokenlist: &DrkTokenList,
+        tokenlist: Arc<DrkTokenList>,
     ) -> Result<()> {
         debug!(target: "state_apply", "Extend nullifier set");
         self.nullifiers.insert(&update.nullifiers)?;
@@ -160,7 +161,7 @@ impl State {
                     let own_coin =
                         OwnCoin { coin, note, secret: *secret, nullifier, leaf_position };
 
-                    wallet.put_own_coin(own_coin, tokenlist).await?;
+                    wallet.put_own_coin(own_coin, tokenlist.clone()).await?;
 
                     if let Some(ch) = notify.clone() {
                         debug!(target: "state_apply", "Send a notification");

+ 5 - 1
src/wallet/walletdb.rs

@@ -285,7 +285,11 @@ impl WalletDb {
         Ok(own_coins)
     }
 
-    pub async fn put_own_coin(&self, own_coin: OwnCoin, tokenlist: &DrkTokenList) -> Result<()> {
+    pub async fn put_own_coin(
+        &self,
+        own_coin: OwnCoin,
+        tokenlist: Arc<DrkTokenList>,
+    ) -> Result<()> {
         debug!("Putting own coin into wallet database");
 
         let coin = serialize(&own_coin.coin.to_bytes());