forked from ghe0/brain-mock
switch language from tokens to LP
This commit is contained in:
parent
928c68f550
commit
7dfdf4844e
@ -28,7 +28,7 @@ message Contract {
|
|||||||
string dtrfs_sha = 12;
|
string dtrfs_sha = 12;
|
||||||
string created_at = 13;
|
string created_at = 13;
|
||||||
string updated_at = 14;
|
string updated_at = 14;
|
||||||
// total nanotoken cost per minute (for all units)
|
// total nanoLP cost per minute (for all units)
|
||||||
uint64 nano_per_minute = 15;
|
uint64 nano_per_minute = 15;
|
||||||
uint64 locked_nano = 16;
|
uint64 locked_nano = 16;
|
||||||
string collected_at = 17;
|
string collected_at = 17;
|
||||||
@ -59,7 +59,7 @@ message RegisterNodeReq {
|
|||||||
string country = 4;
|
string country = 4;
|
||||||
string region = 5;
|
string region = 5;
|
||||||
string city = 6;
|
string city = 6;
|
||||||
// nanotokens per unit per minute
|
// nanoLP per unit per minute
|
||||||
uint64 price = 7;
|
uint64 price = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ message NodeListResp {
|
|||||||
string ip = 5; // required for latency test
|
string ip = 5; // required for latency test
|
||||||
uint32 server_rating = 6;
|
uint32 server_rating = 6;
|
||||||
uint32 provider_rating = 7;
|
uint32 provider_rating = 7;
|
||||||
// nanotokens per unit per minute
|
// nanoLP per unit per minute
|
||||||
uint64 price = 8;
|
uint64 price = 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
src/data.rs
70
src/data.rs
@ -10,7 +10,7 @@ use tokio::sync::oneshot::Sender as OneshotSender;
|
|||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("We do not allow locking of more than 100000 tokens.")]
|
#[error("We do not allow locking of more than 100000 LP.")]
|
||||||
TxTooBig,
|
TxTooBig,
|
||||||
#[error("Account has insufficient funds for this operation")]
|
#[error("Account has insufficient funds for this operation")]
|
||||||
InsufficientFunds,
|
InsufficientFunds,
|
||||||
@ -21,13 +21,13 @@ pub enum Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AccountNanoTokens {
|
pub struct AccountNanoLP {
|
||||||
pub balance: u64,
|
pub balance: u64,
|
||||||
pub tmp_locked: u64,
|
pub tmp_locked: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AccountNanoTokens> for grpc::AccountBalance {
|
impl From<AccountNanoLP> for grpc::AccountBalance {
|
||||||
fn from(value: AccountNanoTokens) -> Self {
|
fn from(value: AccountNanoLP) -> Self {
|
||||||
grpc::AccountBalance {
|
grpc::AccountBalance {
|
||||||
balance: value.balance,
|
balance: value.balance,
|
||||||
tmp_locked: value.tmp_locked,
|
tmp_locked: value.tmp_locked,
|
||||||
@ -50,7 +50,7 @@ pub struct Node {
|
|||||||
pub avail_ipv6: u32,
|
pub avail_ipv6: u32,
|
||||||
pub avail_ports: u32,
|
pub avail_ports: u32,
|
||||||
pub max_ports_per_vm: u32,
|
pub max_ports_per_vm: u32,
|
||||||
// nanotokens per unit per minute
|
// nanoLP per unit per minute
|
||||||
pub price: u64,
|
pub price: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ impl Contract {
|
|||||||
+ (!self.public_ipv4.is_empty() as u64 * 10)
|
+ (!self.public_ipv4.is_empty() as u64 * 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns price per minute in nanotokens
|
// Returns price per minute in nanoLP
|
||||||
fn price_per_minute(&self) -> u64 {
|
fn price_per_minute(&self) -> u64 {
|
||||||
self.total_units() * self.price_per_unit
|
self.total_units() * self.price_per_unit
|
||||||
}
|
}
|
||||||
@ -136,8 +136,8 @@ impl Into<grpc::Contract> for Contract {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct BrainData {
|
pub struct BrainData {
|
||||||
// amount of nanotokens in each account
|
// amount of nanoLP in each account
|
||||||
accounts: DashMap<String, AccountNanoTokens>,
|
accounts: DashMap<String, AccountNanoLP>,
|
||||||
nodes: RwLock<Vec<Node>>,
|
nodes: RwLock<Vec<Node>>,
|
||||||
contracts: RwLock<Vec<Contract>>,
|
contracts: RwLock<Vec<Contract>>,
|
||||||
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
|
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
|
||||||
@ -164,11 +164,11 @@ impl BrainData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_balance(&self, account: &str) -> AccountNanoTokens {
|
pub fn get_balance(&self, account: &str) -> AccountNanoLP {
|
||||||
if let Some(account) = self.accounts.get(account) {
|
if let Some(account) = self.accounts.get(account) {
|
||||||
return account.value().clone();
|
return account.value().clone();
|
||||||
} else {
|
} else {
|
||||||
let balance = AccountNanoTokens {
|
let balance = AccountNanoLP {
|
||||||
balance: 0,
|
balance: 0,
|
||||||
tmp_locked: 0,
|
tmp_locked: 0,
|
||||||
};
|
};
|
||||||
@ -180,13 +180,13 @@ impl BrainData {
|
|||||||
self.add_nano_to_wallet(account, 1000_000000000);
|
self.add_nano_to_wallet(account, 1000_000000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_nano_to_wallet(&self, account: &str, nanotokens: u64) {
|
fn add_nano_to_wallet(&self, account: &str, nano_lp: u64) {
|
||||||
log::debug!("Adding {nanotokens} nanotokens to {account}");
|
log::debug!("Adding {nano_lp} nanoLP to {account}");
|
||||||
self.accounts
|
self.accounts
|
||||||
.entry(account.to_string())
|
.entry(account.to_string())
|
||||||
.and_modify(|tokens| tokens.balance += nanotokens)
|
.and_modify(|d| d.balance += nano_lp)
|
||||||
.or_insert(AccountNanoTokens {
|
.or_insert(AccountNanoLP {
|
||||||
balance: nanotokens,
|
balance: nano_lp,
|
||||||
tmp_locked: 0,
|
tmp_locked: 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -205,17 +205,17 @@ impl BrainData {
|
|||||||
let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64;
|
let minutes_to_collect = (Utc::now() - c.collected_at).num_minutes() as u64;
|
||||||
c.collected_at = Utc::now();
|
c.collected_at = Utc::now();
|
||||||
log::debug!("{minutes_to_collect}");
|
log::debug!("{minutes_to_collect}");
|
||||||
let mut nanotokens_to_collect =
|
let mut nanolp_to_collect =
|
||||||
c.price_per_minute().saturating_mul(minutes_to_collect);
|
c.price_per_minute().saturating_mul(minutes_to_collect);
|
||||||
if nanotokens_to_collect > c.locked_nano {
|
if nanolp_to_collect > c.locked_nano {
|
||||||
nanotokens_to_collect = c.locked_nano;
|
nanolp_to_collect = c.locked_nano;
|
||||||
}
|
}
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Removing {nanotokens_to_collect} nanotokens from {}",
|
"Removing {nanolp_to_collect} nanoLP from {}",
|
||||||
c.uuid
|
c.uuid
|
||||||
);
|
);
|
||||||
c.locked_nano -= nanotokens_to_collect;
|
c.locked_nano -= nanolp_to_collect;
|
||||||
self.add_nano_to_wallet(&owner_key, nanotokens_to_collect);
|
self.add_nano_to_wallet(&owner_key, nanolp_to_collect);
|
||||||
if c.locked_nano == 0 {
|
if c.locked_nano == 0 {
|
||||||
deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone()));
|
deleted_contracts.push((c.uuid.clone(), c.node_pubkey.clone()));
|
||||||
}
|
}
|
||||||
@ -250,29 +250,29 @@ impl BrainData {
|
|||||||
nodes.push(node);
|
nodes.push(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
|
pub fn lock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
|
||||||
if nanotokens > 100_000_000_000_000 {
|
if nano_lp > 100_000_000_000_000 {
|
||||||
return Err(Error::TxTooBig);
|
return Err(Error::TxTooBig);
|
||||||
}
|
}
|
||||||
if let Some(mut account) = self.accounts.get_mut(account) {
|
if let Some(mut account) = self.accounts.get_mut(account) {
|
||||||
if nanotokens > account.balance {
|
if nano_lp > account.balance {
|
||||||
return Err(Error::InsufficientFunds);
|
return Err(Error::InsufficientFunds);
|
||||||
}
|
}
|
||||||
account.balance = account.balance.saturating_sub(nanotokens);
|
account.balance = account.balance.saturating_sub(nano_lp);
|
||||||
account.tmp_locked = account.tmp_locked.saturating_add(nanotokens);
|
account.tmp_locked = account.tmp_locked.saturating_add(nano_lp);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::InsufficientFunds)
|
Err(Error::InsufficientFunds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unlock_nanotockens(&self, account: &str, nanotokens: u64) -> Result<(), Error> {
|
pub fn unlock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
|
||||||
if let Some(mut account) = self.accounts.get_mut(account) {
|
if let Some(mut account) = self.accounts.get_mut(account) {
|
||||||
if nanotokens > account.tmp_locked {
|
if nano_lp > account.tmp_locked {
|
||||||
return Err(Error::ImpossibleError);
|
return Err(Error::ImpossibleError);
|
||||||
}
|
}
|
||||||
account.balance = account.balance.saturating_add(nanotokens);
|
account.balance = account.balance.saturating_add(nano_lp);
|
||||||
account.tmp_locked = account.tmp_locked.saturating_sub(nanotokens);
|
account.tmp_locked = account.tmp_locked.saturating_sub(nano_lp);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ImpossibleError)
|
Err(Error::ImpossibleError)
|
||||||
@ -283,9 +283,9 @@ impl BrainData {
|
|||||||
&self,
|
&self,
|
||||||
uuid: &str,
|
uuid: &str,
|
||||||
account: &str,
|
account: &str,
|
||||||
nanotokens: u64,
|
nano_lp: u64,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if nanotokens > 100_000_000_000_000 {
|
if nano_lp > 100_000_000_000_000 {
|
||||||
return Err(Error::TxTooBig);
|
return Err(Error::TxTooBig);
|
||||||
}
|
}
|
||||||
let mut account = match self.accounts.get_mut(account) {
|
let mut account = match self.accounts.get_mut(account) {
|
||||||
@ -300,11 +300,11 @@ impl BrainData {
|
|||||||
.find(|c| c.uuid == uuid)
|
.find(|c| c.uuid == uuid)
|
||||||
{
|
{
|
||||||
Some(contract) => {
|
Some(contract) => {
|
||||||
if account.balance + contract.locked_nano < nanotokens {
|
if account.balance + contract.locked_nano < nano_lp {
|
||||||
return Err(Error::InsufficientFunds);
|
return Err(Error::InsufficientFunds);
|
||||||
}
|
}
|
||||||
account.balance = account.balance + contract.locked_nano - nanotokens;
|
account.balance = account.balance + contract.locked_nano - nano_lp;
|
||||||
contract.locked_nano = nanotokens;
|
contract.locked_nano = nano_lp;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
None => Err(Error::ContractNotFound(uuid.to_string())),
|
None => Err(Error::ContractNotFound(uuid.to_string())),
|
||||||
|
Loading…
Reference in New Issue
Block a user