Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r2r] spv storage optimization #1 #1585

Merged
merged 76 commits into from
Feb 13, 2023
Merged

[r2r] spv storage optimization #1 #1585

merged 76 commits into from
Feb 13, 2023

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Dec 21, 2022

issue: #1483

This PR introduces new SPVConf structures with some fields that's needed to optimize stored block headers when spv proof is enabled

Main Optimization in this PR is allowing node to set max headers that needs to be stored(max_stored_block_headers) and where to start fetching block headers from(block_header).

pub struct SPVConf {
    /// Where to start block headers sync from.
    pub block_header: SPVBlockHeader,
    /// Max number of block headers to be stored in db, when reached, old header will be deleted.
    pub max_stored_block_headers: Option<NonZeroU64>,
    /// The parameters that specify how the coin block headers should be validated. If None,
    /// headers will be saved in DB without validation, can be none if the coin's RPC server is trusted.
    pub validation_params: Option<BlockHeaderValidationParams>,
}

Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
# Conflicts:
#	mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs
#	mm2src/coins/utxo/utxo_builder/utxo_coin_builder.rs
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
@borngraced borngraced changed the title [wip] spv storage optimization 1# [r2r] spv storage optimization #1 Dec 22, 2022
Signed-off-by: borngraced <samiodev@icloud.com>
@borngraced borngraced changed the title [r2r] spv storage optimization #1 [wip] spv storage optimization #1 Dec 22, 2022
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
@borngraced borngraced changed the title [wip] spv storage optimization #1 [r2r] spv storage optimization #1 Dec 22, 2022
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Just a few comments to start the review, I will have more comments in next iterations.

mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
@borngraced borngraced changed the title [r2r] spv storage optimization #1 [wip] spv storage optimization #1 Dec 26, 2022
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
@borngraced borngraced changed the title [wip] spv storage optimization #1 [r2r] spv storage optimization #1 Jan 23, 2023
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Great progress! Next review iteration.
@borngraced Can you please add a test similar to test_btc_block_header_sync but with optimization? you can use a recent retarget header as the starting block header and max_stored_block_headers=RETARGETING_INTERVAL. This test will not be ignored since the block headers sync time will be short.

mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/work.rs Outdated Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_tests.rs Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/storage.rs Outdated Show resolved Hide resolved
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Next review iteration!

mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_tests.rs Outdated Show resolved Hide resolved
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Next Iteration! I believe the next review will be the last one :)

mm2src/coins/utxo/utxo_tests.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

I believe this will be the last review iteration :)
Only some minor changes left

mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/lib.rs Outdated Show resolved Hide resolved
mm2src/mm2_test_helpers/src/for_tests.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/storage.rs Outdated Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/work.rs Outdated Show resolved Hide resolved
shamardy
shamardy previously approved these changes Jan 30, 2023
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Great work! Thanks for your patience with all the fixes @borngraced on this very tricky PR :)

@shamardy
Copy link
Collaborator

@sergeyboyko0791 Can you please take a look at this PR?

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Thank you for the great enhancement!
I have multiple comments

mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
mm2src/coins/utxo/utxo_tests.rs Show resolved Hide resolved
mm2src/mm2_bitcoin/spv_validation/src/conf.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs Outdated Show resolved Hide resolved
Comment on lines +539 to 552
"spv_conf": {
"starting_block_header": {
"height": 764064,
"hash": "00000000000000000006da48b920343944908861fa05b28824922d9e60aaa94d",
"bits": 386375189,
"time": 1668986059,
},
"max_stored_block_headers": 3000,
"validation_params": {
"difficulty_check": true,
"constant_difficulty": false,
"difficulty_algorithm": "Bitcoin Mainnet"
}
}

Choose a reason for hiding this comment

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

Doesn't this break the backward compatibility?
cc: @shamardy

Copy link
Collaborator

Choose a reason for hiding this comment

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

The coins repo should be updated before merging this. Only BTC have spv configuration now in the coins file, and I think this change will make it easier to get the starting block header info from any block explorer unlike how it used to be with block header hex.

Copy link

@sergeyboyko0791 sergeyboyko0791 Jan 31, 2023

Choose a reason for hiding this comment

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

Thanks for the info! Hope there will not be any problem with it

Signed-off-by: borngraced <samiodev@icloud.com>
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

One more change please

mm2src/coins/utxo/utxo_builder/utxo_arc_builder.rs Outdated Show resolved Hide resolved
Signed-off-by: borngraced <samiodev@icloud.com>
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Reapprove! @borngraced can you please post in the related issue the changes required to the coins configs to be merged with this?

@shamardy
Copy link
Collaborator

@ca333 I believe this can be merged to dev. Will require an update to BTC coin config according to this #1483 (comment).

@ca333 ca333 merged commit e60bf64 into dev Feb 13, 2023
@borngraced borngraced mentioned this pull request Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement SPV with minimal storage requirements and fast block headers sync time
4 participants