quantadex / distributed_quanta_bridge

The distributed version of the quanta bridge
1 stars 0 forks source link

Confirm blocks, to prevent orphaned blocks scenarios #43

Open quocble opened 5 years ago

quocble commented 5 years ago

Motivation

We need to check the # of confirmations before we issue assets. It must meet 3 general requirements:

1) Provide user the most recent information 2) Robust enough to handle change of block hash, and top block 3) Safely handle minimum confirmations

https://testnet.litecore.io/block/1d098a4dfc4f45aa6b637394add9551c0ea6ac5c6c8a43a196cc515208ff4de6 https://www.blockchain.com/btc/orphaned-blocks

Tech design

Introduce configurations: EthereumMinConfirm, BtcMinConfirm, LtcMinConfirm

State transitions: -->[Pending]-->[Wait_for_Confirm]---->[Consensus]--->[Success]

Pending state is optional, and supported by BTC. Other chains go straight to wait for confirm

At wait_for_confirm, record blockhash for all ETH, LTC, and BTC Every update API now needs to pass in 3 piece of unique information, Type, TX, BlockHash

At the begin of the block update:

BTC, LTC, BCH

find_and_confirms:

  For all wait_for_confirm tx's
   Lookup blocks with getBlock(blockhash)
   confirm = top_block - block_number
   if block_number == = -1
       mark tx as "orphan"
   else if confirms > min_block_confirm   (configure for each blockchain)
       mark tx as "consensus"
   else
       println("tx has x ", confirms)
func FindAndConfirm(tx db.Transaction, data GetBlockVerboseResult):
   Lookup blocks with getBlock(blockhash)
   confirm = top_block - block_number
   if block_number == = -1
       mark tx as "orphan"
   else if confirms > min_block_confirm   (configure for each blockchain)
       mark tx as "consensus"
   else
       println("tx has x ", confirms)

func FindAllAndConfirm():
    for tx in all confirm:
        data = getBlock(tx.blockHash)
        FindAndConfirm(tx, data)

ETH

For all wait_for_confirm tx's
   latest <- Lookup blocks with getTransaction(txhash)
  Get all tx for <deposit,txhash>

   for all txs found in db:
       if tx.blockhash ==   latest.blockhash
           confirm = top_block - latest.block_number
           if confirms > min_block_confirm   (configure for each blockchain)
                mark tx as "consensus"
          else
               println("tx has x ", confirms)
       else:
              // blockhash changed on the transaction, this one is now an orphan 
             println("blockhash is different for this tx=tx, it is now an orphan) 
             mark tx as "orphan"

Useful debug

truffle console --network mainnet
truffle(mainnet)> web3.eth.getTransaction(tx,(err,msg) => console.log(err,msg))

Links

  1. LTC orphans https://chainz.cryptoid.info/ltc/orphans.dws
  2. BTC orphans https://www.blockchain.com/btc/orphaned-blocks
  3. ETH uncles https://etherscan.io/uncles