DeFiCh / ain

DeFi Blockchain - enabling decentralized finance on Bitcoin
https://defichain.com
MIT License
399 stars 118 forks source link

Anchor Bugs - reward payout order, missing anchor (failed anchor detection) #524

Closed DefichainHelper33 closed 3 years ago

DefichainHelper33 commented 3 years ago

I discovered two anchor bugs since the eunos hard fork:

First Bug

the newer anchor received the bigger anchor reward (wrong order of anchor reward payment)

on 9.06.2021 two valid anchors were created:

The second anchor references the first anchor as prev-anchor => they are both valid in the same chain

The second anchor received the higher reward of 376.768208 DFI - reward tx 73ffa06a11c57868df878711fed5a2fb2cc6e3ee5555e42bbbe0a1edeb7e8d52 (block: 909843) The first anchor received the reward afterwards of 0.081008 DFI - reward tx 9a638b3e3ed2823981ebdee24d1c7050f0d717111570e2eb9a352085f1afb06e (block 909844)

Expected behavior: First anchor should receive the full reward and second anchor should receive the 0.08 DFI. This was also the behavior before the hard fork (maybe it was random and the correct anchor got to rewards by luck)

Second Bug

Not all anchors are discovered in time. (reward went to wrong anchor because of undiscovered anchor)

The anchor tx: e950a67fb3636ddf83d3b72ff9e2c34a743c0a1c5de4ff9814a0fbdcb9b00ac6 was created on 11.06 13:14 but only discovered by the nodes a few hours later. (Note: this anchor was/is invalid because it breaks the anchor chain)

The anchor tx: b17b2919c807cbd61e82f4faa4464850c4b2a06f2002b0808c0fb31f42000000 was created on 11.06 17:26 but has not yet been discovered by the nodes. This anchor is valid and would have received a reward of 80 DFI.

The anchor tx: d6d001203d24fb495d70ce0e01a762bffa9dbf765268114478ec73049a28f1e4 was created on 11.06 18:19 and has been discovered by the nodes about 30min later. This anchor received the reward which would normally go to the b17b... anchor.

Before Eunos: Anchors were discovered immediately -> this is also the expected behavior.

defichain-bot commented 3 years ago

@DefichainHelper33: Thanks for opening an issue, it is currently awaiting triage.

The triage/accepted label can be added by foundation members by writing /triage accepted in a comment.

Details I am a bot created to help the [DeFiCh](https://github.com/DeFiCh) developers manage community feedback and contributions. You can check out my [manifest file](https://github.com/DeFiCh/ain/blob/master/.github/governance.yml) to understand my behavior and what I can do. If you want to use this for your project, you can check out the [DeFiCh/oss-governance-bot](https://github.com/DeFiCh/oss-governance-bot) repository.
DefichainHelper33 commented 3 years ago

On the 12.06 and the 13.06 there were again a lot of undiscovered anchors.

The latest anchor reward on 13.06 was also paid to an invalid anchor. The anchor used a non-legacy address as payment address which normaly results in the error rewardAddress (d....) does not refer to a P2PKH or P2WPKH address This anchor received 284.58 DFI which would normal have gone to the first of the 6 undiscovered anchors.

The undiscovered anchors can be seen here: https://www.blockchain.com/btc/address/1FtZwEZKknoquUb6DyQHFZ6g6oomXJYEcb. Two of this anchors belong two me and I can guarantee that they were valid and build on the same anchor chain as the latest selected anchor.

Here is the list of undiscovered anchors which competed for the latest anchor reward:

There were also 2 undiscovered anchors, two anchor rewards earlier. 3 anchors were within the same bitcoin block (687207) and only the anchor with the worst defiBlockHeight was discovered and received the reward:

Sebz84 commented 3 years ago

I can confirm the above mentioned issues (I have created some of the above mentioned anchors).

Additional comments:

  1. It is possible for the user to create anchors with non-legacy addresses because SPV Anchor commands are accepting non-legacy addresses. If those addresses are not supported, the command should throw an error. Example:

defi-cli spv_createanchortemplate df1q9j8trl4xvrldf6fcusf39x3t2tfr308qxjy349 { "txHex": "02000000000ca086010000000000.....", "defiHash": "6d01da77a20cbc4bf08ec92a6300e0324c2f687e35490d3cd19c1e0ecaa62d13", "defiHeight": 922080, "estimatedReward": 20.00000000, "anchorAddress": "1FtZwEZKknoquUb6DyQHFZ6g6oomXJYEcb" }

When an anchor with a non-legacy address is created, the reward is never recieved. Example Btc Tx 71d606c1951fd8dcb27886707ecbf1dc0a23375d70ee103c38119e90c312b36d -> Anchor is active but reward not received.

Bushstar commented 3 years ago

Legacy and Bech32 addresses are accepted and will be paid if they are an active anchor. The anchor above was paid, here's the entry for it from listanchors. When an anchor becomes part of the active anchor chain this makes it valid for payment, but this process can take time as this requires a quorum of five masternodes to validate the anchor and send signed confirmation messages to the network. The masternodes that will create these confirmations are chosen randomly evrry 120 blocks from active masternodes that have staked a block in the last two weeks. However we cannot be sure that all five masternodes will be online or even following anchors, this can mean that anchors take a while to get paid once active. For more information on anchors take a look at this Wiki entry.

Thanks for the report on missing anchors, I'm looking into it now.

  {
    "anchorHeight": 920580,
    "anchorHash": "d56641f3958dd6e56f49d105f2d44ccedd7f8f066961b55c933df2459868d4ae",
    "rewardAddress": "df1q9j8trl4xvrldf6fcusf39x3t2tfr308qxjy349",
    "dfiRewardHash": "7cf64e09990a82e246d76a0ada9b50dbd9a3448756f5d1defefb5160079d2353",
    "btcAnchorHeight": 687434,
    "btcAnchorHash": "71d606c1951fd8dcb27886707ecbf1dc0a23375d70ee103c38119e90c312b36d",
    "confirmSignHash": "297afd0fed7fa0170f8c062cf51b63d2e3a63705519c5c2a7f32f9f6a7c9ca55"
  }
Sebz84 commented 3 years ago

The anchor above was paid, here's the entry for it from listanchors

Thanks for the information! I got the impression that the reward is not paid, because the reward is not showing up via getbalances listaddressgroupings. Is this a normal behaviour?

./defi-cli listaddressgroupings
[
  [
    [
      "8RmVH1BUCD3F2BvuuVB4ynTX4A7t2Wmcjj",
      0.00000000,
    ],
    [
      "df1q9j8trl4xvrldf6fcusf39x3t2tfr308qxjy349",
      0.00000000
    ]
    ...
  ],
  [
   ...
  ]
]

./defi-cli getblockcount
922558
Bushstar commented 3 years ago

It is a UTXO reward paid here.

https://explorer.defichain.io/#/DFI/mainnet/tx/7cf64e09990a82e246d76a0ada9b50dbd9a3448756f5d1defefb5160079d2353

The funds have already been spent sending them to the account layer.

https://chainz.cryptoid.info/dfi/address.dws?df1q9j8trl4xvrldf6fcusf39x3t2tfr308qxjy349.htm

Bushstar commented 3 years ago

@DefichainHelper33 about the second bug, you said about anchors that were created but not discovered for hours. Can you confirm that the anchor that was not being seen was confirmed on the Bitcoin network, in that it had made its way into a Bitcoin block and was still not seen by your node?

Anchors are not accepted for payment until they are included in a block and have six confirmations, an anchor paying a high fee could be added after another anchor and still be confirmed by Bitcoin and paid by DeFi chain first.

There is a certain inconsistent experience expected with anchors due to their decentralised nature that relies on masternodes to create the anchor data, read and confirm anchors and then pay them.

Bushstar commented 3 years ago

Those undiscovered anchors were found but were invalid, the DFI anchor block time should be at least three hours before the Bitcoin block time the anchor is embedded in. This can be changed in future releases to make sure anchor data is created further back.

2021-06-14T06:54:32Z Anchor too new. DeFi: 1623414453 Bitcoin: 1623425013 Anchor: b17b2919c807cbd61e82f4faa4464850c4b2a06f2002b0808c0fb31f42000000
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623437670 Bitcoin: 1623447834 Anchor: 16482f3be3d819c052f0beaf7b561efa1fb6e309bf8b4bfad94381e33e060000
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623437670 Bitcoin: 1623447834 Anchor: 4e85cba054daff4f7626b619ac24da78b4daa5e9d34cca21c649cf4f2ee5e7a0
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623496330 Bitcoin: 1623506982 Anchor: a38d673c83ee37ccecb3d84dd86d2d9c1e0967610109a3b5ff02e5cff30a0000
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623496330 Bitcoin: 1623506982 Anchor: 1ebecc6617811b106402ed93b74dac1e7e5eb237859b5e921da1ff2c675bc7c9
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623509474 Bitcoin: 1623520226 Anchor: 6b5c300275ea9c665061897abb60e7a9203f6cb11dfe95d67817dede2e0f0000
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623512299 Bitcoin: 1623523089 Anchor: bb897cb8ebc38720f8cab86dbc57801e80bdaee3437099d65db1e4e504009d0e
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623532368 Bitcoin: 1623542640 Anchor: 05ebdd4623c670bfb696fd28a8b38bd46ab02c7e6499d75c95950c20a09ec576
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623566631 Bitcoin: 1623577370 Anchor: 192353c4f8e4607fcd82894a3767624f1232ce7431eb0d0bd49f56ae5f000000
2021-06-14T06:54:32Z Anchor too new. DeFi: 1623621557 Bitcoin: 1623632083 Anchor: 767c6d7877c7b1326f3bbfb3b47e25b257d62687ec1827fe868b79bf4b3e98b8
DefichainHelper33 commented 3 years ago

@Bushstar Yes I can confirm. All anchors were confirmed by the bitcoin network within one block and had more then 6 confirmations without being added to spv_listanchors.

Thanks for the info, this means that createanchortemplate does not check the time difference and creates invalid anchors. Is that correct?

uzyn commented 3 years ago

/triage accepted

Bushstar commented 3 years ago

The masternodes create anchor data for use in anchors, the data includes the block to be anchors, it is created every 15 blocks and goes back 3 hours from that blocks time and then a further number of blocks to a 15 block interval which may be no blocks at all.

With the super miner update blocks are being created in the future up to five minutes, I expect this is responsible to the anchors falling foul of the time rule. In the next major update we will increase the block sample time to go further back in the DFI chain.

Bushstar commented 3 years ago

Please note that 1.7.11 has been updated to not select anchor data less than three hours older than the local time, this should help with missing anchors. 1.7.11 has also been updated to pay anchors in Bitcoin height order though this will not take effect until MNs have updated.