Browse Source

storage-node: balance checks before submitting transactions

Mokhtar Naamani 4 years ago
parent
commit
64b2eeed5e

+ 6 - 1
storage-node/packages/colossus/bin/cli.js

@@ -216,12 +216,17 @@ async function announcePublicUrl(api, publicUrl) {
   }
 
   const chainIsSyncing = await api.chainIsSyncing()
-
   if (chainIsSyncing) {
     debug('Chain is syncing. Postponing announcing public url.')
     return reannounce(10 * 60 * 1000)
   }
 
+  const sufficientBalance = await api.providerHasMinimumBalance(1)
+  if (!sufficientBalance) {
+    debug('Provider role account does not have sufficient balance. Postponing announcing public url.')
+    return reannounce(10 * 60 * 1000)
+  }
+
   debug('announcing public url')
   const { publish } = require('@joystream/service-discovery')
 

+ 11 - 1
storage-node/packages/colossus/lib/sync.js

@@ -94,12 +94,22 @@ async function syncPeriodic(api, flags, storage) {
     debug('Starting sync run...')
 
     const chainIsSyncing = await api.chainIsSyncing()
-
     if (chainIsSyncing) {
       debug('Chain is syncing. Postponing sync run.')
       return setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
     }
 
+    const recommendedBalance = await api.providerHasMinimumBalance(300)
+    if (!recommendedBalance) {
+      debug('Warning: Provider role account is running low on balance.')
+    }
+
+    const sufficientBalance = await api.providerHasMinimumBalance(100)
+    if (!sufficientBalance) {
+      debug('Provider role account does not have sufficient balance. Postponing sync run!')
+      return setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
+    }
+
     await syncCallback(api, storage)
     debug('sync run complete')
   } catch (err) {

+ 7 - 0
storage-node/packages/colossus/paths/asset/v0/{id}.js

@@ -91,6 +91,13 @@ module.exports = function (storage, runtime) {
         return
       }
 
+      const sufficientBalance = await runtime.providerHasMinimumBalance(3)
+
+      if (!sufficientBalance) {
+        errorHandler(res, 'Insufficient balance to process upload!', 503)
+        return
+      }
+
       // We'll open a write stream to the backend, but reserve the right to
       // abort upload if the filters don't smell right.
       let stream

+ 5 - 0
storage-node/packages/runtime-api/index.js

@@ -105,6 +105,11 @@ class RuntimeApi {
     return isSyncing.isTrue
   }
 
+  async providerHasMinimumBalance(minimumBalance) {
+    const providerAccountId = this.identities.key.address
+    return this.balances.hasMinimumBalanceOf(providerAccountId, minimumBalance)
+  }
+
   executeWithAccountLock(accountId, func) {
     return this.asyncLock.acquire(`${accountId}`, func)
   }