bitshares / bitshares-core

BitShares Blockchain node and command-line wallet
https://bitshares.github.io/
Other
1.17k stars 643 forks source link

Unable to adjust debt position when individual settlement fund is not empty #2582

Closed abitmore closed 2 years ago

abitmore commented 2 years ago

Bug Description

Unable to adjust debt position when individual settlement fund is not empty

Assert Exception: enable_black_swan: Black swan was detected during a margin update which is not allowed to trigger a blackswan

3128356ms th_a       db_market.cpp:161             check_for_blackswan  ] *call_ptr: {"id":"1.8.117222","borrower":"1.2.458173","collateral":"32522288519","debt":449905151,"call_price":{"base":{"amount":1,"asset_id":"1.3.0"},"quote":{"amount":1,"asset_id":"1.3.113"}}}
3128357ms th_a       db_market.cpp:171             check_for_blackswan  ] Black Swan detected on asset CNY (1.3.113) at block 65662393:
   Least collateralized call: 72.28698859462491555  0.01383374822276602
   Settle Price:              68.18559658342681473  0.01466585393553717
   Max:                       73.61204268292682684  0.01358473374128951

3128358ms th_a       db_market.cpp:172             check_for_blackswan  ] enable_black_swan: false
  "median_feed": {
    "settlement_price": {
      "base": {
        "amount": 328,
        "asset_id": "1.3.113"
      },
      "quote": {
        "amount": 22995,
        "asset_id": "1.3.0"
      }
    },
    "maintenance_collateral_ratio": 1600,
    "maximum_short_squeeze_ratio": 1050,
    "core_exchange_rate": {
      "base": {
        "amount": 8506627,
        "asset_id": "1.3.113"
      },
      "quote": {
        "amount": 500000000,
        "asset_id": "1.3.0"
      }
    },
    "initial_collateral_ratio": 1750
  },
  "current_feed": {
    "settlement_price": {
      "base": {
        "amount": "504696031977",
        "asset_id": "1.3.113"
      },
      "quote": {
        "amount": "34413000033640",
        "asset_id": "1.3.0"
      }
    },
    "maintenance_collateral_ratio": 1600,
    "maximum_short_squeeze_ratio": 1050,
    "core_exchange_rate": {
      "base": {
        "amount": 8506627,
        "asset_id": "1.3.113"
      },
      "quote": {
        "amount": 500000000,
        "asset_id": "1.3.0"
      }
    },
    "initial_collateral_ratio": 1750
  },
  "current_feed_publication_time": "2022-01-22T16:40:12",
  "current_maintenance_collateralization": {
    "base": {
      "amount": 4599,
      "asset_id": "1.3.0"
    },
    "quote": {
      "amount": 41,
      "asset_id": "1.3.113"
    }
  },
  "current_initial_collateralization": {
    "base": {
      "amount": 160965,
      "asset_id": "1.3.0"
    },
    "quote": {
      "amount": 1312,
      "asset_id": "1.3.113"
    }
  },
  "options": {
    "feed_lifetime_sec": 86400,
    "minimum_feeds": 3,
    "force_settlement_delay_sec": 86400,
    "force_settlement_offset_percent": 200,
    "maximum_force_settlement_volume": 50,
    "short_backing_asset": "1.3.0",
    "extensions": {
      "initial_collateral_ratio": 1750,
      "maintenance_collateral_ratio": 1600,
      "maximum_short_squeeze_ratio": 1050,
      "margin_call_fee_ratio": 30,
      "force_settle_fee_percent": 300,
      "black_swan_response_method": 2
    }
  },
  "force_settled_volume": 0,
  "is_prediction_market": false,
  "settlement_price": {
    "base": {
      "amount": 0,
      "asset_id": "1.3.0"
    },
    "quote": {
      "amount": 0,
      "asset_id": "1.3.0"
    }
  },
  "settlement_fund": 0,
  "individual_settlement_debt": "49480003135",
  "individual_settlement_fund": "3441300003364",

Impacts Describe which portion(s) of BitShares Core may be impacted by this bug. Please tick at least one box.

CORE TEAM TASK LIST

abitmore commented 2 years ago

Found the reason: https://github.com/bitshares/bitshares-core/blob/e52b9ffa3cb5c0a0dcdd3d3610486ce08925deba/libraries/chain/asset_evaluator.cpp#L1432-L1433 and (else) https://github.com/bitshares/bitshares-core/blob/e52b9ffa3cb5c0a0dcdd3d3610486ce08925deba/libraries/chain/asset_evaluator.cpp#L1467

When the individual settlement fund is not empty, and the price of the backing asset continues to fall, after a new price feed is published, current_feed will not change, according to the code, return void_result() will be executed, this means that check_call_orders() will not be executed, so new individual settlement(s) will not happen although it (or they) should. In this case, when trying to update a debt position, the check_call_orders() call will fail.

https://github.com/bitshares/bitshares-core/blob/e52b9ffa3cb5c0a0dcdd3d3610486ce08925deba/libraries/chain/market_evaluator.cpp#L383

Similar suspicious code: https://github.com/bitshares/bitshares-core/blob/e52b9ffa3cb5c0a0dcdd3d3610486ce08925deba/libraries/chain/db_update.cpp#L498 and https://github.com/bitshares/bitshares-core/blob/e52b9ffa3cb5c0a0dcdd3d3610486ce08925deba/libraries/chain/asset_evaluator.cpp#L1004

abitmore commented 2 years ago

To work around this issue, asset owners can temporarily or regularly update MCR (to a slightly lower value) to trigger individual settlements then update it back. See https://bitsharestalk.org/index.php?topic=33637.0.

abitmore commented 2 years ago

When this occurs, these notes in the code are no longer true: https://github.com/bitshares/bitshares-core/blob/2c2e0ef4cc4e22b11630f0ca0ac8334760b52f10/libraries/chain/asset_evaluator.cpp#L1291-L1295

For the first note, although it is not true, but it just means that there is still a check_call_orders() call missing, so nothing changes.

For the second note, in short, it is not true but also not critical. The details are as follows.

abitmore commented 2 years ago

It should have been fixed by #2599. But missing test cases.

abitmore commented 2 years ago

Added tests in #2606.