Skip to content

Commit

Permalink
Expose a new Slot() method on Block and Attestation to avoid calculat…
Browse files Browse the repository at this point in the history
…ing the ID just go get the slot.

Also added a new IDWithBlockIdentifier so we don’t encode the block if we already have the bytes available elsewhere
  • Loading branch information
alexsporn committed Mar 22, 2024
1 parent 9ab858c commit b8308a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
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

0 comments on commit b8308a3

Please sign in to comment.