Skip to content

Commit

Permalink
Add Header key in Block
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Oct 27, 2023
1 parent 1e11b8a commit 1eb0501
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion benches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
34 changes: 17 additions & 17 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
}
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions builder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -261,7 +261,7 @@ func (v *ValidationBlockBuilder) SlotCommitmentID(commitmentID iotago.Commitment
return v
}

v.protocolBlock.SlotCommitmentID = commitmentID
v.protocolBlock.Header.SlotCommitmentID = commitmentID

return v
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion nodeclient/http_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions tpkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1eb0501

Please sign in to comment.