-
Notifications
You must be signed in to change notification settings - Fork 311
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
Replace trait AnchorFromBlockPosition
with new struct
#1594
Conversation
@evanlinjin @LLFourn, is this what you meant in #1266? |
f2f74da
to
26c5282
Compare
I am not sure about the failing test. Is there some flakiness? It ran alright in my fork and also my machine.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK.
crates/chain/src/tx_data_traits.rs
Outdated
/// Minimal set of parameters that are needed to construct a generic [`Anchor`]. | ||
/// Typically used as an additional constraint on anchor: | ||
/// `A: Anchor + From<BlockPosition<'b>>`. | ||
pub struct BlockPosition<'b> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be called BlockAndHeight
and should be documented simply as that. I'm not sure it should be described as the "minimal" set of parameters. It's more like like the maximal amount of detail about the block that could be needed to make an anchor i.e. any anchor should be able to be made from a BlockPosition
. We could even type constraint that Anchor: for<'a> From<BlockPosition<'a>>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to have a merkle-proof anchor (which I think we should have), don't we also need a tx position in block? I'm not too great with naming, but I'm thinking TxPosInBlock
?
pub struct TxPosInBlock<'b> {
block: &'b Block,
height: u32,
tx_pos: usize,
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we are missing some derives. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
I think makes sense for this type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we are missing some derives. #[derive(Debug, Clone, Copy, PartialEq, Eq)] I think makes sense for this type.
Added, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All fields should be public as well, otherwise external modules/crates can't impl From<BlockPosition>
.
Ohh I see u have a constructor and methods on it. I guess you did it this way instead of...
pub struct BlockPosition<'a> {
pub block: &'a Block,
pub height: u32,
pub tx_pos: usize,
}
So that the blockhash can be precalculated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it is more of my habit to start with hidden fields (in order to hide internals) and use getters. The blockhash was a bonus.
However, as BDK typically exposes publicly all fields, I guess it would be appropriate to follow also in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could even type constraint that
Anchor: for<'a> From<BlockPosition<'a>>
.
From<BlockPosition>
is currently used only by two methods. Wouldn't that be too strict to require that of every future implementation of Anchor?
I'm not sure it should be described as the "minimal" set of parameters. It's more like like the maximal amount of detail about the block that could be needed to make an anchor
Mmm, yeah, I see how it can be unclear. My idea was “if you have at least these parameters at disposal, you can create any anchor.“ I will reword.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @evanlinjin's design looks right to me.
From is currently used only by two methods. Wouldn't that be too strict to require that of every future implementation of Anchor?
I think if you can't get your anchor from the full block and position you may be using anchors in a way you probably shouldn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to have a merkle-proof anchor (which I think we should have), don't we also need a tx position in block?
If we decide this new struct should include the position of the tx in block, then the original name BlockPosition
would again makes sense to me but I won't belabor the point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BlockPosition
may be interpreted as position of block in the Blockchain though
26c5282
to
d1bd5d6
Compare
I reflected some of the comments but have not figured out the name yet. I will sleep on it and will see tomorrow if I come with a better one. Until then, I will keep it as draft. |
d1bd5d6
to
7fa3043
Compare
Thanks everybody for the comments! |
7fa3043
to
4f46aeb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
I agree, the naming is hard. I wasn't able to come up with any other idea than TxBlockId
(which I don't think really suits 😅)
crates/chain/src/indexed_tx_graph.rs
Outdated
graph.merge(self.graph.insert_anchor( | ||
tx.compute_txid(), | ||
TxPosInBlock::new(block, height, tx_pos).into(), | ||
)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means we are constructing TxPosInBlock
on every tx, thus re-computing the block hash for every tx... I have a couple of ideas to fix this.
- Make all fields of
TxPositionInBlock
public and get rid of construction and getter methods. - Add a method to change the
tx_pos
. I.e.fn with_tx_pos(self, tx_pos: usize) -> Self
.
I'm in favor of 1. because it is simpler and I don't see much value in having an API that enforces the consistency of the type. I don't see the caller constructing this type. The main usecase of this type (as I understand) is for implementing From<TxPositionInBlock>
for anchors types that support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make all fields of TxPositionInBlock public and get rid of construction and getter methods.
Made fields public as suggested.
Although that puts us back to the beginning (suggestion to pass only height, not BlockId), the repeated calculation of block hash should be avoided. Most likely why the creation of BlockId
was in the original code in a first place.
07a8a27
to
f96b705
Compare
I think it makes sense to include this change in v1.0 since this is a minor breaking change and the work is done. The worst case scenario is that users that created custom anchors that used methods which required If we deem this breaking change as unacceptable, there is a way to make this non-breaking. We can keep I'm in favor of just making this change breaking since it's so minor. |
f96b705
to
424c05a
Compare
How about calling it |
How about |
Maybe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just some doc changes and I'll happy to ACK.
There is a bit of bike-shedding going on with the naming.
I'm happy with how it is currently, or BlockTxPosition
, or PositionInBlock
, or TxBlockPosition
. Whatever you choose, I will ACK.
Alongside Evan's comments, please do mind the commit message to follow https://www.conventionalcommits.org/en/v1.0.0/ too. I'm fine with the name suggestions above, although I tend more toward either: |
…ruct This change replaces a way of creating new generic anchor from block, its height and transaction position. Previous way was using conversion trait, newly it is a struct and `From`.
424c05a
to
ab8068b
Compare
Addressed @evanlinjin's comments and changed commit message (hope it's right). Apologies for the delay. |
Nit: I would probably reword the commit description to be something like:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK ab8068b
a trait is deleted I ack. Would like @evanlinjin and @LagginTimes to ACK too.
ACK ab8068b |
Description
This change replaces a way of creating new generic anchor from block and its height. Previous way was using conversion trait, newly it is a struct and
From
.Closes #1266.
Notes to the reviewers
tx_pos
: I did not see its relevance (Anchor
does not give access to it). Was it a relic from the past or something I overlooked?BlockPosition
intotx_data_traits.rs
because I believe it should be next toAnchor
. But then it's not a module just for traits. What would be better place?Checklists
All Submissions:
cargo fmt
andcargo clippy
before committing