Files
Dena/docs/zagros/deposit/blockchain.md
2026-08-13 19:50:53 +03:30

52 lines
3.0 KiB
Markdown

# Blockchain Deposit
[please read first](daemon.md)
### file:
- {$Dena_path}/app/workers/daemons/blockchain.rb
### implementation
daemon dockerize by below code and file:
- bash -c "bundle exec ruby lib/daemons/daemons.rb blockchain" (called in Alvand Service up rake)
- {$Dena_path}/lib/daemons/daemons.rb
Blockchain daemon as one **thread** for each active blockchain, process blocks and filter platform deposits.<br />
this daemon use BlockchainService class in per one thread.<br/>
that class recognizes its `adapter` (coin-connectors or coin-middleman) with help of the parameter that is passed to it when initialized,
this parameter is **the blockchain DB record.**<br/>
also, this class has standard functions that any coin-connectors must obey, so this class has two hands, with one hand connect to local DB and with another hand talk to the relevant Blockchain with help of `the adapter`.<br/>
each coin has itself adapter that can be the Gem.<br />
responsibility of these Adapters that connect to the related blockchain and doing my needed methods.<br/>
this daemon is kept alive by an infinite loop that in every 30 seconds check the active blockchains.<br />
also, in this 30-second time, check updated time of the blockchain or currencies record, if they have been newly updated time,
the blockchain thread will be reset.<br />
when the blockchain thread is reset, the last_seen_block will become **nil**.</br>
BlockchainService class fetches relative blocks with help the Adapter,<br />
and find transactions that included our users addresses (users addresses were saved in local DB).<br />
after each loop of the Blockchain investigation, we update the Variable that has kept the last seen block number.<br />
and also update the Height column of the blockchain record.<br />
We are waiting for the N number of confirmations.<br />
if height column of the blockchain record plus(+) the min confirmation, was bigger than the Adapters last seen block variable, synchronization will be skipped, and try again after 10 seconds<br />
from the blockchain height to Adapters last_seen_block, the `process_block` function of **BlockchainService** class will be executed for each block.<br/>
at last the height column will be updated.
#### process_block function
1. fetch block by Adapter
2. find relative deposits (destination address of blocks in our user address).
3. find relative withdraws (hash transaction of blocks in our user confirming withdraws `txid`).
4. Per deposit Blockchain, we update or create Db record and changing users balances.
5. Per withdraw in Blockchain, we find Db records by `confirming` state, then update record state by the blockchain transaction,
that was fetched before again by its the Adapter. so, at last, we will unlock and update user balances
```mermaid
graph LR
A(Peatio) -->|Blockchain stuffs| B[BlockchainService using adapter]
B --> |Blockchain stuffs| D((Btc Blockchain))
B --> |Blockchain stuffs| E((Eth Blockchain))
B --> |Blockchain stuffs| F((...))
B --> A
D --> B
E --> B
F --> B
```