fix: register vm node creates operator account in db
This commit is contained in:
		
							parent
							
								
									519bd1ed7b
								
							
						
					
					
						commit
						18c647ff3e
					
				| @ -30,6 +30,18 @@ impl Account { | |||||||
|         Ok(account) |         Ok(account) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     pub async fn get_or_create(db: &Surreal<Client>, address: &str) -> Result<Self, Error> { | ||||||
|  |         let id = (ACCOUNT, address); | ||||||
|  | 
 | ||||||
|  |         match db.select(id).await? { | ||||||
|  |             Some(account) => Ok(account), | ||||||
|  |             None => { | ||||||
|  |                 let account: Option<Self> = db.create(id).await?; | ||||||
|  |                 account.ok_or(Error::FailedToCreateDBEntry) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pub async fn airdrop(db: &Surreal<Client>, account: &str, tokens: u64) -> Result<(), Error> { |     pub async fn airdrop(db: &Surreal<Client>, account: &str, tokens: u64) -> Result<(), Error> { | ||||||
|         let tokens = tokens.saturating_mul(1_000_000_000); |         let tokens = tokens.saturating_mul(1_000_000_000); | ||||||
|         let _ = db |         let _ = db | ||||||
|  | |||||||
| @ -22,6 +22,8 @@ pub enum Error { | |||||||
|     StdIo(#[from] std::io::Error), |     StdIo(#[from] std::io::Error), | ||||||
|     #[error(transparent)] |     #[error(transparent)] | ||||||
|     TimeOut(#[from] tokio::time::error::Elapsed), |     TimeOut(#[from] tokio::time::error::Elapsed), | ||||||
|  |     #[error("Failed to create account")] | ||||||
|  |     FailedToCreateDBEntry, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub mod prelude { | pub mod prelude { | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ use super::Error; | |||||||
| use crate::constants::{ | use crate::constants::{ | ||||||
|     ACCOUNT, ACTIVE_VM, DELETED_VM, NEW_VM_REQ, UPDATE_VM_REQ, VM_NODE, VM_UPDATE_EVENT, |     ACCOUNT, ACTIVE_VM, DELETED_VM, NEW_VM_REQ, UPDATE_VM_REQ, VM_NODE, VM_UPDATE_EVENT, | ||||||
| }; | }; | ||||||
| use crate::db::{MeasurementArgs, Report, VmNodeFilters}; | use crate::db::{Account, MeasurementArgs, Report, VmNodeFilters}; | ||||||
| use crate::old_brain; | use crate::old_brain; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use surrealdb::engine::remote::ws::Client; | use surrealdb::engine::remote::ws::Client; | ||||||
| @ -52,6 +52,7 @@ impl VmNodeResources { | |||||||
| 
 | 
 | ||||||
| impl VmNode { | impl VmNode { | ||||||
|     pub async fn register(self, db: &Surreal<Client>) -> Result<(), Error> { |     pub async fn register(self, db: &Surreal<Client>) -> Result<(), Error> { | ||||||
|  |         Account::get_or_create(db, &self.operator.key().to_string()).await?; | ||||||
|         let _: Option<VmNode> = db.upsert(self.id.clone()).content(self).await?; |         let _: Option<VmNode> = db.upsert(self.id.clone()).content(self).await?; | ||||||
|         Ok(()) |         Ok(()) | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user