Polkadex icon indicating copy to clipboard operation
Polkadex copied to clipboard

Implement off-chain worker for OCEX pallet

Open Gauthamastro opened this issue 3 years ago • 2 comments

User story

OCEX pallet will allow enclave to store balance snapshots of each trading pair registered on-chain. The balance snapshots are stored in IPFS, and only the CIDs of those storages are stored in the OCEX Pallet.

Logic Flow

Enclave sends a vector of CIDs to the OCEX pallet --> Off-chain workers take these CIDs and pins the data locally on each full node registered with the OCEX pallet --> Off-chain workers check the integrity of the snapshot --> submits their vote of approval on the snapshot. --> once the OCEX pallet receives enough votes, CIDs are considered accepted.

If the operator of the exchange is found to be compromised, or blockchain is not receiving CIDs for a certain period of time or on-chain governance decides to do an emergency shutdown of the off-chain orderbook. The confirmed snapshots are used to recover user funds.

This feature lays the crux of the security of Polkadex Orderbook where user funds are secured by on-chain validators and not by the operator.

Implementation Details

The off-chain worker of the OCEX pallet will embed an IPFS node and track CIDs registered by Enclaves.

  • [ ] register_worker(origin) dispatchable function registers the given AccountID of the worker.
  • [ ] stake_worker(origin, worker: T::AccountID, stake_amount: T::Balance) allows an account to stake PDEX for a worker
  • [ ] claim_payout(origin, ) allows an account who staked for a worker to claim their payout ( payout includes a portion of inflation and withdrawal fees collected)
  • [ ] update_CID(origin, cid: T::Hash, trading_pair: (AssetID,AssetID)): This is called by all registered enclave on periodic intervals to update their snapshots on-chain. (#298)
  • [ ] Implement sessions pallet to track epochs of workers like BABE's session.
  • [ ] Implement ipfs offchain worker
  • [ ] claim_funds dispatchable function can be called only when ocex is in emergency shutdown mode, which takes the latest balance of an asset id via off-chain workers from IPFS. the workers will vote for the claim, and if the majority of workers agree, then the claim is credited

Gauthamastro avatar Jun 08 '21 06:06 Gauthamastro

The idea behind the Off-chain worker of the OCEX pallet is to keep the operator of the orderbook accountable by ensuring the periodic snapshot of user balances of every trading pair. The off-chain worker will loop through all the users and verify the balances. The enclaves will register new CIDs for each trading pair periodically which should be pinned by the off-chain workers. They can unpin the previous CID to conserve space

Gauthamastro avatar Jul 03 '21 07:07 Gauthamastro

In this way, in case the fraud by the operator of the orderbook, the users can issue a dispatchable call that gets the latest balance from the last snapshot via off-chain workers and credit the user on-chain, hence the user is secured from operator fraud.

Gauthamastro avatar Jul 03 '21 07:07 Gauthamastro