Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

fix: operate on block header only #24

Merged
merged 1 commit into from
Jul 19, 2018
Merged

fix: operate on block header only #24

merged 1 commit into from
Jul 19, 2018

Conversation

vmx
Copy link
Member

@vmx vmx commented Jul 17, 2018

The current implementation was wrong. You were able to add a full
block (including transactions), but the CID was only calculated
based on the header.

The correct implementation is to work on the block header only.

Transaction support needs to be implemented separately.

@@ -6,6 +6,8 @@ const multihashes = require('multihashes')
const multihashing = require('multihashing-async')
const waterfall = require('async/waterfall')

const BITCOIN_BLOCK_HEADER_SIZE = 80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it standard practice to SHOUT CONSTANTS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I'm used to shout constants. I couldn't find any pointers to a standard, but I'm used to do that in all the languages I code.

@codecov
Copy link

codecov bot commented Jul 17, 2018

Codecov Report

Merging #24 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #24   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines          83     84    +1     
=====================================
+ Hits           83     84    +1
Impacted Files Coverage Δ
src/util.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ce5f66...aee2024. Read the comment docs.

.util.BITCOIN_BLOCK_HEADER_SIZE

const headerFromHexBlock = (hex) => {
return Buffer.from(hex.toString(), 'hex').slice(0, BITCOIN_BLOCK_HEADER_SIZE)
Copy link
Contributor

@richardschneider richardschneider Jul 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems inefficient in time and memory. What is the hex type? Why the toString()?

If hex is a string, then it would be better to first take the BITCOIN_BLOCK_HEADER_SIZE * 2 characters and then create the Buffer from it.

If this method is just used by tests, then you can ignore the above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'ts just for tests. The Bitcoin blocks are serialized as "hex", it's bytes encoded as hex put into a file. That's what the Go implementation is using and i didn't want to convert the files, but just use the same ones for the JS implementation as well.

Copy link
Contributor

@richardschneider richardschneider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks that you can ignore.

However, it looks like you are only changing the tests and not any actual code. So my question is, if the tests already pass why change them?

@vmx
Copy link
Member Author

vmx commented Jul 17, 2018

@richardschneider The actual change is pretty subtle. Things worked accidentally. Prior to this change you could put a full Bitcoin block (which consists of the header and transaction) into IPLD and the tests would pass.

Though this is wrong. Using only the header is the correct way. So this change is mostly changing the tests so that they only use the header of the block.

It will make more sense, once we switch the cid() call to work on the binary encoding and not and the deserialized version. Then the full object (which is just the header now) will be used for the CID. Prior to this change the full block will be used, hence be the wrong result.

The current code was wrong. You could pass in a full block (with
transactions) and only the header was used. The correct behaviour
is, to use the header only.

This means that this library does support block headers only, there
is no support for transactions.
@vmx
Copy link
Member Author

vmx commented Jul 19, 2018

@richardschneider Have I cleared up your concerns? If yes, please approve this PR.

Copy link
Contributor

@richardschneider richardschneider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vmx vmx merged commit ef05359 into master Jul 19, 2018
@vmx vmx deleted the header-only branch July 19, 2018 10:23
@alanshaw alanshaw removed the status/in-progress In progress label Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants