lbryio / lbcd

An alternative full node implementation of LBRY's blockchain written in Go (golang)
https://lbry.com/
ISC License
38 stars 25 forks source link

Bring `getblock` up to date #52

Closed roylee17 closed 2 years ago

roylee17 commented 2 years ago

Missing chainwork and mediantime

On lbcd:

lbcctl getblock ac280c2a68c57707cc1fedbd025782c76cc2679550aa18cc5bd6e3f64c2a2769
{
  "hash": "ac280c2a68c57707cc1fedbd025782c76cc2679550aa18cc5bd6e3f64c2a2769",
  "confirmations": 1,
  "strippedsize": 16191,
  "size": 16227,
  "weight": 64800,
  "height": 1172332,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "b3717572eb82f625a1de2735fbb119802032d07b59a2c84d25c5d5bcb4a69128",
  "time": 1654577506,
  "nonce": 3245888345,
  "bits": "1a009cca",
  "difficulty": 1795218858097.069,
  "previousblockhash": "9ee97ed1dbaf34fbf40d755ccb9a23b3074e61b1572a8135397a9f6514d741ca",
  "nameclaimroot": "1c6a4517e9254d0becdfc1dd5257b8c66b58899618e9305f1fab57a7054c12cd",
  "nTx": 33,
  "tx": [
    "07c8b6aa16d59f184e6aea71e613b08be52182869b9fdeba422a0dbf47411383",
    "1f0eeaa0480639805849724e835be04f1e041f73ed1f10caceb4db70c7403876",
    "0a999db30bfa1116646347dc8a6c1baf62ae1998c63640f48c33b02117fc1412",
    ...
  ]
}

On lbrycrd:

lbcctl --notls -s $LBRYCRD_NODE getblock ac280c2a68c57707cc1fedbd025782c76cc2679550aa18cc5bd6e3f64c2a2769
{
  "hash": "ac280c2a68c57707cc1fedbd025782c76cc2679550aa18cc5bd6e3f64c2a2769",
  "confirmations": 1,
  "strippedsize": 16191,
  "size": 16227,
  "weight": 64800,
  "height": 1172332,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "b3717572eb82f625a1de2735fbb119802032d07b59a2c84d25c5d5bcb4a69128",
  "time": 1654577506,
  "nonce": 3245888345,
  "bits": "1a009cca",
  "difficulty": 1795218858097.069,
  "previousblockhash": "9ee97ed1dbaf34fbf40d755ccb9a23b3074e61b1572a8135397a9f6514d741ca",
  "nameclaimroot": "1c6a4517e9254d0becdfc1dd5257b8c66b58899618e9305f1fab57a7054c12cd",
  "nTx": 33,
  "tx": [
    "07c8b6aa16d59f184e6aea71e613b08be52182869b9fdeba422a0dbf47411383",
    "1f0eeaa0480639805849724e835be04f1e041f73ed1f10caceb4db70c7403876",
    "0a999db30bfa1116646347dc8a6c1baf62ae1998c63640f48c33b02117fc1412",
    ...

  ],
  "mediantime": 1654577056,
  "chainwork": "000000000000000000000000000000000000000000000964a8366f9822316ea1"
}
moodyjon commented 2 years ago

I was getting worried that chainwork/mediantime had no implementation in "lbcd", but found what I needed here:

https://github.com/lbryio/lbcd/blob/7f9fe4b97058830be58f79e8a54530863d5cd829/blockchain/blockindex.go#L188 https://github.com/lbryio/lbcd/blob/7f9fe4b97058830be58f79e8a54530863d5cd829/blockchain/blockindex.go#L82

roylee17 commented 2 years ago

Yeah, the mediantime is part of consensus, so it must be existing somewhere.

The chainwork, however, in lbcd currently has different semantics - accounting works since each restart of lbcd, instead of historically accumulated numbers. It works for the chain as it only cares about the relative work between branches, if any, for choosing the one with most work.

Currently, the only known external users of that field is block explorers for displaying information. While it probably won't block any functional integration, still very nice to have if we can reconstruct/calculated the accumulated work at start-up.

moodyjon commented 2 years ago

All the code I investigate indicates chainwork (workSum) is predictable, and is initialized during the "Loading block index" phase:

https://github.com/lbryio/lbcd/blob/7f9fe4b97058830be58f79e8a54530863d5cd829/blockchain/chainio.go#L1104

initBlockNode() is where the parent workSum is added. This seems consistent across bcd, lbrycrd, and C++ bitcoin. It's "in memory only" but gets recomputed during startup.

Maybe there is some separate concept with a similar purpose/name that you are thinking of?

roylee17 commented 2 years ago

I didn't dig too deep into that last time, so you maybe right, which would be a good news.

You can test against a public lbrycrd instance I setup for validation when you export it to the rpcserver.

lbcctl --notls -s 18.221.146.233 -u rpcuser -P rpcpass getblock ac280c2a68c57707cc1fedbd025782c76cc2679550aa18cc5bd6e3f64c2a2769