From 1eb0501a2d5fd15964a51b3fca599bbd39fbc563 Mon Sep 17 00:00:00 2001 From: Philipp Gackstatter Date: Fri, 27 Oct 2023 20:18:23 +0200 Subject: [PATCH] Add `Header` key in `Block` --- attestation.go | 2 +- attestation_test.go | 2 +- benches_test.go | 2 +- block.go | 34 +++++++++++++++--------------- block_test.go | 8 +++---- builder/block_builder.go | 24 ++++++++++----------- nodeclient/http_api_client.go | 2 +- nodeclient/http_api_client_test.go | 6 +++--- tpkg/util.go | 6 +++--- 9 files changed, 43 insertions(+), 43 deletions(-) diff --git a/attestation.go b/attestation.go index 7d0bbffa6..fae3a3ddb 100644 --- a/attestation.go +++ b/attestation.go @@ -23,7 +23,7 @@ type Attestation struct { func NewAttestation(api API, block *Block) *Attestation { return &Attestation{ API: api, - BlockHeader: block.BlockHeader, + BlockHeader: block.Header, BlockHash: lo.PanicOnErr(block.Body.Hash()), Signature: block.Signature, } diff --git a/attestation_test.go b/attestation_test.go index 459498cd1..9129bb4b2 100644 --- a/attestation_test.go +++ b/attestation_test.go @@ -24,7 +24,7 @@ func TestAttestation(t *testing.T) { // Compare fields of block and attestation. { - require.Equal(t, block.BlockHeader, attestation.BlockHeader) + require.Equal(t, block.Header, attestation.BlockHeader) require.Equal(t, lo.PanicOnErr(block.Body.Hash()), attestation.BlockHash) require.Equal(t, block.Signature, attestation.Signature) } diff --git a/benches_test.go b/benches_test.go index 2a30c96d2..467769103 100644 --- a/benches_test.go +++ b/benches_test.go @@ -166,7 +166,7 @@ func BenchmarkSerializeAndHashBlockWithTransactionPayload(b *testing.B) { m := &iotago.Block{ API: tpkg.TestAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: tpkg.TestAPI.Version(), }, Body: &iotago.BasicBlockBody{ diff --git a/block.go b/block.go index bf45973e3..3d039c5dc 100644 --- a/block.go +++ b/block.go @@ -83,10 +83,10 @@ func (b *BlockHeader) Size() int { } type Block struct { - API API - BlockHeader `serix:"0,nest"` - Body BlockBody `serix:"1,mapKey=body"` - Signature Signature `serix:"2,mapKey=signature"` + API API + Header BlockHeader `serix:"0,nest"` + Body BlockBody `serix:"1,mapKey=body"` + Signature Signature `serix:"2,mapKey=signature"` } func BlockFromBytes(apiProvider APIProvider) func(bytes []byte) (block *Block, consumedBytes int, err error) { @@ -132,7 +132,7 @@ func (b *Block) SetDeserializationContext(ctx context.Context) { // The BlockHeader and Block are separately hashed and concatenated to enable the verification of the signature for // an Attestation where only the BlockHeader and the hash of Block is known. func (b *Block) SigningMessage() ([]byte, error) { - headerHash, err := b.BlockHeader.Hash(b.API) + headerHash, err := b.Header.Hash(b.API) if err != nil { return nil, err } @@ -193,7 +193,7 @@ func (b *Block) ID() (BlockID, error) { return BlockID{}, err } - slot := b.API.TimeProvider().SlotFromTime(b.IssuingTime) + slot := b.API.TimeProvider().SlotFromTime(b.Header.IssuingTime) return NewBlockID(slot, id), nil } @@ -246,7 +246,7 @@ func (b *Block) ForEachParent(consumer func(parent Parent)) { func (b *Block) WorkScore() (WorkScore, error) { workScoreParameters := b.API.ProtocolParameters().WorkScoreParameters() - workScoreHeader, err := b.BlockHeader.WorkScore(workScoreParameters) + workScoreHeader, err := b.Header.WorkScore(workScoreParameters) if err != nil { return 0, err } @@ -266,13 +266,13 @@ func (b *Block) WorkScore() (WorkScore, error) { // Size returns the size of the block in bytes. func (b *Block) Size() int { - return b.BlockHeader.Size() + b.Body.Size() + b.Signature.Size() + return b.Header.Size() + b.Body.Size() + b.Signature.Size() } // syntacticallyValidate syntactically validates the Block. func (b *Block) syntacticallyValidate() error { - if b.API.ProtocolParameters().Version() != b.ProtocolVersion { - return ierrors.Wrapf(ErrInvalidBlockVersion, "mismatched protocol version: wanted %d, got %d in block", b.API.ProtocolParameters().Version(), b.ProtocolVersion) + if b.API.ProtocolParameters().Version() != b.Header.ProtocolVersion { + return ierrors.Wrapf(ErrInvalidBlockVersion, "mismatched protocol version: wanted %d, got %d in block", b.API.ProtocolParameters().Version(), b.Header.ProtocolVersion) } block := b.Body @@ -291,7 +291,7 @@ func (b *Block) syntacticallyValidate() error { minCommittableAge := b.API.ProtocolParameters().MinCommittableAge() maxCommittableAge := b.API.ProtocolParameters().MaxCommittableAge() - commitmentSlot := b.SlotCommitmentID.Slot() + commitmentSlot := b.Header.SlotCommitmentID.Slot() blockID, err := b.ID() if err != nil { return ierrors.Wrapf(err, "failed to syntactically validate block") @@ -301,12 +301,12 @@ func (b *Block) syntacticallyValidate() error { // check that commitment is not too recent. if commitmentSlot > 0 && // Don't filter commitments to genesis based on being too recent. blockSlot < commitmentSlot+minCommittableAge { - return ierrors.Wrapf(ErrCommitmentTooRecent, "block at slot %d committing to slot %d", blockSlot, b.SlotCommitmentID.Slot()) + return ierrors.Wrapf(ErrCommitmentTooRecent, "block at slot %d committing to slot %d", blockSlot, b.Header.SlotCommitmentID.Slot()) } // Check that commitment is not too old. if blockSlot > commitmentSlot+maxCommittableAge { - return ierrors.Wrapf(ErrCommitmentTooOld, "block at slot %d committing to slot %d, max committable age %d", blockSlot, b.SlotCommitmentID.Slot(), maxCommittableAge) + return ierrors.Wrapf(ErrCommitmentTooOld, "block at slot %d committing to slot %d, max committable age %d", blockSlot, b.Header.SlotCommitmentID.Slot(), maxCommittableAge) } return b.Body.syntacticallyValidate(b) @@ -438,8 +438,8 @@ func (b *BasicBlockBody) syntacticallyValidate(block *Block) error { return ierrors.Wrapf(ErrCommitmentInputTooOld, "block at slot %d committing to slot %d, max committable age %d", blockSlot, cInput.CommitmentID.Slot(), maxCommittableAge) } - if cInputSlot > block.SlotCommitmentID.Slot() { - return ierrors.Wrapf(ErrCommitmentInputNewerThanCommitment, "transaction in a block contains CommitmentInput to slot %d while max allowed is %d", cInput.CommitmentID.Slot(), block.SlotCommitmentID.Slot()) + if cInputSlot > block.Header.SlotCommitmentID.Slot() { + return ierrors.Wrapf(ErrCommitmentInputNewerThanCommitment, "transaction in a block contains CommitmentInput to slot %d while max allowed is %d", cInput.CommitmentID.Slot(), block.Header.SlotCommitmentID.Slot()) } } } @@ -505,8 +505,8 @@ func (b *ValidationBlockBody) Size() int { // syntacticallyValidate syntactically validates the ValidationBlock. func (b *ValidationBlockBody) syntacticallyValidate(block *Block) error { - if b.HighestSupportedVersion < block.ProtocolVersion { - return ierrors.Errorf("highest supported version %d must be greater equal protocol version %d", b.HighestSupportedVersion, block.ProtocolVersion) + if b.HighestSupportedVersion < block.Header.ProtocolVersion { + return ierrors.Errorf("highest supported version %d must be greater equal protocol version %d", b.HighestSupportedVersion, block.Header.ProtocolVersion) } return nil diff --git a/block_test.go b/block_test.go index fa094c9bf..e3d6eab6e 100644 --- a/block_test.go +++ b/block_test.go @@ -439,7 +439,7 @@ func TestBlock_DeserializationNotEnoughData(t *testing.T) { func TestBasicBlock_MinSize(t *testing.T) { minBlock := &iotago.Block{ API: tpkg.TestAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: tpkg.TestAPI.Version(), IssuingTime: tpkg.RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(tpkg.TestAPI.Version()).MustID(), @@ -467,7 +467,7 @@ func TestBasicBlock_MinSize(t *testing.T) { func TestValidationBlock_MinSize(t *testing.T) { minBlock := &iotago.Block{ API: tpkg.TestAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: tpkg.TestAPI.Version(), IssuingTime: tpkg.RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(tpkg.TestAPI.Version()).MustID(), @@ -495,7 +495,7 @@ func TestValidationBlock_MinSize(t *testing.T) { func TestValidationBlock_HighestSupportedVersion(t *testing.T) { block := &iotago.Block{ API: tpkg.TestAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: tpkg.TestAPI.Version(), IssuingTime: tpkg.RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(tpkg.TestAPI.Version()).MustID(), @@ -549,7 +549,7 @@ func TestBlockJSONMarshalling(t *testing.T) { strongParents := tpkg.SortedRandBlockIDs(1) validationBlock := &iotago.Block{ API: tpkg.TestAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: tpkg.TestAPI.Version(), IssuingTime: issuingTime, IssuerID: issuerID, diff --git a/builder/block_builder.go b/builder/block_builder.go index f7cb6dbd0..df11208e2 100644 --- a/builder/block_builder.go +++ b/builder/block_builder.go @@ -20,7 +20,7 @@ func NewBasicBlockBuilder(api iotago.API) *BasicBlockBuilder { protocolBlock := &iotago.Block{ API: api, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: api.ProtocolParameters().Version(), SlotCommitmentID: iotago.EmptyCommitmentID, IssuingTime: time.Now().UTC(), @@ -58,7 +58,7 @@ func (b *BasicBlockBuilder) ProtocolVersion(version iotago.Version) *BasicBlockB return b } - b.protocolBlock.ProtocolVersion = version + b.protocolBlock.Header.ProtocolVersion = version return b } @@ -68,7 +68,7 @@ func (b *BasicBlockBuilder) IssuingTime(time time.Time) *BasicBlockBuilder { return b } - b.protocolBlock.IssuingTime = time.UTC() + b.protocolBlock.Header.IssuingTime = time.UTC() return b } @@ -79,7 +79,7 @@ func (b *BasicBlockBuilder) SlotCommitmentID(commitment iotago.CommitmentID) *Ba return b } - b.protocolBlock.SlotCommitmentID = commitment + b.protocolBlock.Header.SlotCommitmentID = commitment return b } @@ -90,7 +90,7 @@ func (b *BasicBlockBuilder) LatestFinalizedSlot(slot iotago.SlotIndex) *BasicBlo return b } - b.protocolBlock.LatestFinalizedSlot = slot + b.protocolBlock.Header.LatestFinalizedSlot = slot return b } @@ -100,7 +100,7 @@ func (b *BasicBlockBuilder) Sign(accountID iotago.AccountID, prvKey ed25519.Priv return b } - b.protocolBlock.IssuerID = accountID + b.protocolBlock.Header.IssuerID = accountID signature, err := b.protocolBlock.Sign(iotago.NewAddressKeysForEd25519Address(iotago.Ed25519AddressFromPubKey(prvKey.Public().(ed25519.PublicKey)), prvKey)) if err != nil { @@ -202,7 +202,7 @@ func NewValidationBlockBuilder(api iotago.API) *ValidationBlockBuilder { protocolBlock := &iotago.Block{ API: api, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: api.ProtocolParameters().Version(), SlotCommitmentID: iotago.NewEmptyCommitment(api.ProtocolParameters().Version()).MustID(), IssuingTime: time.Now().UTC(), @@ -240,7 +240,7 @@ func (v *ValidationBlockBuilder) ProtocolVersion(version iotago.Version) *Valida return v } - v.protocolBlock.ProtocolVersion = version + v.protocolBlock.Header.ProtocolVersion = version return v } @@ -250,7 +250,7 @@ func (v *ValidationBlockBuilder) IssuingTime(time time.Time) *ValidationBlockBui return v } - v.protocolBlock.IssuingTime = time.UTC() + v.protocolBlock.Header.IssuingTime = time.UTC() return v } @@ -261,7 +261,7 @@ func (v *ValidationBlockBuilder) SlotCommitmentID(commitmentID iotago.Commitment return v } - v.protocolBlock.SlotCommitmentID = commitmentID + v.protocolBlock.Header.SlotCommitmentID = commitmentID return v } @@ -272,7 +272,7 @@ func (v *ValidationBlockBuilder) LatestFinalizedSlot(slot iotago.SlotIndex) *Val return v } - v.protocolBlock.LatestFinalizedSlot = slot + v.protocolBlock.Header.LatestFinalizedSlot = slot return v } @@ -282,7 +282,7 @@ func (v *ValidationBlockBuilder) Sign(accountID iotago.AccountID, prvKey ed25519 return v } - v.protocolBlock.IssuerID = accountID + v.protocolBlock.Header.IssuerID = accountID signature, err := v.protocolBlock.Sign(iotago.NewAddressKeysForEd25519Address(iotago.Ed25519AddressFromPubKey(prvKey.Public().(ed25519.PublicKey)), prvKey)) if err != nil { diff --git a/nodeclient/http_api_client.go b/nodeclient/http_api_client.go index b60418ea4..e9044efcc 100644 --- a/nodeclient/http_api_client.go +++ b/nodeclient/http_api_client.go @@ -473,7 +473,7 @@ func (client *Client) SubmitBlock(ctx context.Context, m *iotago.Block) (iotago. // no parents were given. The node will first add this missing information and // validate the block afterward. - apiForVersion, err := client.APIForVersion(m.ProtocolVersion) + apiForVersion, err := client.APIForVersion(m.Header.ProtocolVersion) if err != nil { return iotago.EmptyBlockID, err } diff --git a/nodeclient/http_api_client_test.go b/nodeclient/http_api_client_test.go index 0608ab5b8..c0475f70c 100644 --- a/nodeclient/http_api_client_test.go +++ b/nodeclient/http_api_client_test.go @@ -322,7 +322,7 @@ func TestClient_SubmitBlock(t *testing.T) { incompleteBlock := &iotago.Block{ API: mockAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: mockAPI.Version(), SlotCommitmentID: iotago.NewEmptyCommitment(mockAPI.Version()).MustID(), }, @@ -378,7 +378,7 @@ func TestClient_BlockByBlockID(t *testing.T) { originBlock := &iotago.Block{ API: mockAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: mockAPI.Version(), IssuingTime: tpkg.RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(mockAPI.Version()).MustID(), @@ -409,7 +409,7 @@ func TestClient_TransactionIncludedBlock(t *testing.T) { originBlock := &iotago.Block{ API: mockAPI, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: mockAPI.Version(), IssuingTime: tpkg.RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(mockAPI.Version()).MustID(), diff --git a/tpkg/util.go b/tpkg/util.go index b7be03c27..9bee08455 100644 --- a/tpkg/util.go +++ b/tpkg/util.go @@ -710,7 +710,7 @@ func RandBlock(blockBody iotago.BlockBody, api iotago.API, rmc iotago.Mana) *iot return &iotago.Block{ API: api, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: TestAPI.Version(), IssuingTime: RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(api.ProtocolParameters().Version()).MustID(), @@ -723,7 +723,7 @@ func RandBlock(blockBody iotago.BlockBody, api iotago.API, rmc iotago.Mana) *iot return &iotago.Block{ API: api, - BlockHeader: iotago.BlockHeader{ + Header: iotago.BlockHeader{ ProtocolVersion: TestAPI.Version(), IssuingTime: RandUTCTime(), SlotCommitmentID: iotago.NewEmptyCommitment(api.ProtocolParameters().Version()).MustID(), @@ -771,7 +771,7 @@ func RandBasicBlockWithIssuerAndRMC(api iotago.API, issuerID iotago.AccountID, r basicBlock := RandBasicBlock(api, iotago.PayloadSignedTransaction) block := RandBlock(basicBlock, TestAPI, rmc) - block.IssuerID = issuerID + block.Header.IssuerID = issuerID return block }