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

Expose a new Slot() method on Block and Attestation to avoid calculating the ID just go get the slot. #715

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (a *Attestation) Compare(other *Attestation) int {
}
}

func (a *Attestation) Slot() SlotIndex {
return a.API.TimeProvider().SlotFromTime(a.Header.IssuingTime)
}

func (a *Attestation) BlockID() (BlockID, error) {
signatureBytes, err := a.API.Encode(a.Signature)
if err != nil {
Expand All @@ -83,9 +87,8 @@ func (a *Attestation) BlockID() (BlockID, error) {
}

id := blockIdentifier(headerHash, a.BodyHash, signatureBytes)
slot := a.API.TimeProvider().SlotFromTime(a.Header.IssuingTime)

return NewBlockID(slot, id), nil
return NewBlockID(a.Slot(), id), nil
}

func (a *Attestation) signingMessage() ([]byte, error) {
Expand Down
24 changes: 11 additions & 13 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func (b *Block) VerifySignature() (valid bool, err error) {
return hiveEd25519.Verify(edSig.PublicKey[:], signingMessage, edSig.Signature[:]), nil
}

// Slot returns the SlotIndex of the Block.
func (b *Block) Slot() SlotIndex {
return b.API.TimeProvider().SlotFromTime(b.Header.IssuingTime)
}

// ID computes the ID of the Block.
func (b *Block) ID() (BlockID, error) {
data, err := b.API.Encode(b)
Expand All @@ -189,9 +194,11 @@ func (b *Block) ID() (BlockID, error) {
return BlockID{}, ierrors.Wrap(err, "failed to compute blockID")
}

slot := b.API.TimeProvider().SlotFromTime(b.Header.IssuingTime)
return b.IDWithBlockIdentifier(id), nil
}

return NewBlockID(slot, id), nil
func (b *Block) IDWithBlockIdentifier(blockIdentifier Identifier) BlockID {
return NewBlockID(b.Slot(), blockIdentifier)
}

// MustID works like ID but panics if the BlockID can't be computed.
Expand Down Expand Up @@ -297,17 +304,12 @@ func (b *Block) syntacticallyValidate() error {
}
}

blockID, err := b.ID()
if err != nil {
return ierrors.Wrap(err, "failed to syntactically validate block")
}

protocolParams := b.API.ProtocolParameters()
genesisSlot := protocolParams.GenesisSlot()
minCommittableAge := protocolParams.MinCommittableAge()
maxCommittableAge := protocolParams.MaxCommittableAge()
commitmentSlot := b.Header.SlotCommitmentID.Slot()
blockSlot := blockID.Slot()
blockSlot := b.Slot()

// check that commitment is not too recent.
if commitmentSlot > genesisSlot && // Don't filter commitments to genesis based on being too recent.
Expand Down Expand Up @@ -408,11 +410,7 @@ func (b *BasicBlockBody) Size() int {
// syntacticallyValidate syntactically validates the BasicBlock.
func (b *BasicBlockBody) syntacticallyValidate(block *Block) error {
if b.Payload != nil && b.Payload.PayloadType() == PayloadSignedTransaction {
blockID, err := block.ID()
if err != nil {
return ierrors.Wrap(err, "failed to calculate basic block ID during syntactical validation")
}
blockSlot := blockID.Slot()
blockSlot := block.Slot()

minCommittableAge := block.API.ProtocolParameters().MinCommittableAge()
maxCommittableAge := block.API.ProtocolParameters().MaxCommittableAge()
Expand Down
Loading