codex-storage / nim-codex

Decentralized Durability Engine
https://codex.storage
Apache License 2.0
69 stars 25 forks source link

Simpler payment channels #302

Open markspanbroek opened 2 years ago

markspanbroek commented 2 years ago

Replace the Nitro state channel PoC implementation with simpler 1-to-1 payment channels.

Rationale: we need to have a fast execution layer for our smart contracts anyway, might as well use that to open 1-to-1 payment channels, without the complexity of Nitro state channels.

Bulat-Ziganshin commented 2 years ago

Just now in https://github.com/status-im/nim-codex/tests/codex/blockexchange/engine/testblockexc.nim#L161-L173

  1. in setup, we exchange blocks between node1 and node2. These blocks are paid and since they have random prices, we start the test with non-zero balance, so our payment test doesn't test much

  2. In order to fix that, we should assign equal prices to blocks on node1 and node2. And better they will be small, so we don't run out of money when performing Balance.move(): https://github.com/status-im/nim-nitro/blob/main/nitro/wallet/balances.nim#L33 We can return back to:

    pricing1.price = 1
    pricing2.price = 1
  3. We need to add test that balance is 0 without trading:

    test "Balance is 0":
    let channel = !peerCtx1.paymentChannel
    let wallet = nodeCmps2.wallet
    check wallet.balance(channel, Asset) == 0

    So, we need two changes: price=1 and test "Balance is 0". But the later will not work till we will fix that bug around nitro

Extra info: I made pseudo-PR to check balance on unices: https://github.com/status-im/nim-codex/pull/301 is just current branch main with a few extra echo statements (and price:=0)

And these echo statements https://github.com/status-im/nim-codex/blob/7b62e82b3540c2de06f30a0eaacc9ba6aff785ba/tests/codex/blockexchange/engine/testblockexc.nim#L161-L173 print the following:

pricing1 some(0)
pricing2 some(0)
balance0 de0b6b3a7640000

balance after de0b6b3a7640000
pricing1 some(0)
pricing2 some(0)

So, on unices we get an inheritance (nonzero "balance0").

It looks like a bug, isn't it? Moreover, it seems that we always had this bug, but #248 fixed it on windows. I bet that this is an error somewhere deep in libraries.