1

Could you tell me if an RPC is executed atomically?

For example making a transaction between two accounts, I would have an RPC such:

1. client.rpc.provide('xfer', (data, response) => {
2.   var srcWallet = getRecord(data.srcWalletId);
3.   var dstWallet = getRecord(data.dstWalletId);
4.   if (srcWallet.get('balance') >= data.xferAmount) {
5.     srcWallet.set('balance', srcWallet.get('balance') - xferAmount);
6.     dstWallet.set('balance', dstWallet.get('balance') + xferAmount);
7.   }

Is it certain that the srcWallet balance cannot change between line 4 and 5?

Perky
  • 43
  • 1
  • 4

1 Answers1

0

Just to clarify: deepstream does RPC's (request/response) using ds.rpc.make/ds.rpc.provide, your example refers to records.

The above example would work temporarily for JavaScript implementations that are single threaded, however you can't be sure that there isn't an incoming transaction on the way to the client that changes the wallet's balance. To enforce rules like the above, use a Valve rule on the server, e.g.

_('src-wallet').balance >= data.xferAmount

Please find more at

https://deepstream.io/tutorials/core/permission-conf-simple/ https://deepstream.io/tutorials/core/permission-conf-advanced/ https://deepstream.io/tutorials/core/permissions-dynamic/

wolframhempel
  • 1,094
  • 10
  • 12
  • I left out the RPC provide above for brevity, I ask because on the RPC page (https://deepstream.io/tutorials/core/request-response-rpc/) it mentions 'Securely combining multi step record transactions', from your answer it seems this only true with one RPC provider in a single thread? – Perky Oct 14 '16 at 19:16