Skip to content

Commit

Permalink
Merge pull request #557 from iotaledger/feat/code-generation-type-safety
Browse files Browse the repository at this point in the history
Code generation and type safety
  • Loading branch information
muXxer authored Oct 5, 2023
2 parents 4aef333 + 62e6761 commit 4e43c6d
Show file tree
Hide file tree
Showing 38 changed files with 1,825 additions and 938 deletions.
6 changes: 3 additions & 3 deletions attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ func (a *Attestation) Compare(other *Attestation) int {
func (a *Attestation) BlockID() (BlockID, error) {
signatureBytes, err := a.API.Encode(a.Signature)
if err != nil {
return EmptyBlockID(), ierrors.Errorf("failed to create blockID: %w", err)
return EmptyBlockID, ierrors.Errorf("failed to create blockID: %w", err)
}

headerHash, err := a.BlockHeader.Hash(a.API)
if err != nil {
return EmptyBlockID(), ierrors.Errorf("failed to create blockID: %w", err)
return EmptyBlockID, ierrors.Errorf("failed to create blockID: %w", err)
}

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

return NewSlotIdentifier(slot, id), nil
return NewBlockID(slot, id), nil
}

func (a *Attestation) signingMessage() ([]byte, error) {
Expand Down
74 changes: 7 additions & 67 deletions block.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package iotago

import (
"bytes"
"context"
"crypto/ed25519"
"fmt"
"sort"
"time"

"golang.org/x/crypto/blake2b"
Expand All @@ -16,12 +14,9 @@ import (
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/byteutils"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/iota.go/v4/hexutil"
)

const (
// BlockIDLength defines the length of a block ID.
BlockIDLength = SlotIdentifierLength
// MaxBlockSize defines the maximum size of a block.
MaxBlockSize = 32768
// BlockMaxParents defines the maximum amount of parents in a block.
Expand Down Expand Up @@ -52,61 +47,6 @@ const (
BlockTypeValidation BlockType = 1
)

// EmptyBlockID returns an empty BlockID.
func EmptyBlockID() BlockID {
return emptySlotIdentifier
}

// BlockID is the ID of a Block.
type BlockID = SlotIdentifier

// BlockIDs are IDs of blocks.
type BlockIDs []BlockID

// ToHex converts the BlockIDs to their hex representation.
func (ids BlockIDs) ToHex() []string {
hexIDs := make([]string, len(ids))
for i, id := range ids {
hexIDs[i] = hexutil.EncodeHex(id[:])
}

return hexIDs
}

// RemoveDupsAndSort removes duplicated BlockIDs and sorts the slice by the lexical ordering.
func (ids BlockIDs) RemoveDupsAndSort() BlockIDs {
sorted := append(BlockIDs{}, ids...)
sort.Slice(sorted, func(i, j int) bool {
return bytes.Compare(sorted[i][:], sorted[j][:]) == -1
})

var result BlockIDs
var prev BlockID
for i, id := range sorted {
if i == 0 || !bytes.Equal(prev[:], id[:]) {
result = append(result, id)
}
prev = id
}

return result
}

// BlockIDsFromHexString converts the given block IDs from their hex to BlockID representation.
func BlockIDsFromHexString(blockIDsHex []string) (BlockIDs, error) {
result := make(BlockIDs, len(blockIDsHex))

for i, hexString := range blockIDsHex {
blockID, err := SlotIdentifierFromHexString(hexString)
if err != nil {
return nil, err
}
result[i] = blockID
}

return result, nil
}

type BlockPayload interface {
Payload
}
Expand Down Expand Up @@ -255,7 +195,7 @@ func (b *ProtocolBlock) ID() (BlockID, error) {

slot := b.API.TimeProvider().SlotFromTime(b.IssuingTime)

return NewSlotIdentifier(slot, id), nil
return NewBlockID(slot, id), nil
}

// MustID works like ID but panics if the BlockID can't be computed.
Expand Down Expand Up @@ -461,9 +401,9 @@ func (b *BasicBlock) Size() int {
}

return BasicBlockSizeEmptyParentsAndEmptyPayload +
len(b.StrongParents)*SlotIdentifierLength +
len(b.WeakParents)*SlotIdentifierLength +
len(b.ShallowLikeParents)*SlotIdentifierLength +
len(b.StrongParents)*BlockIDLength +
len(b.WeakParents)*BlockIDLength +
len(b.ShallowLikeParents)*BlockIDLength +
payloadSize
}

Expand Down Expand Up @@ -556,9 +496,9 @@ func (b *ValidationBlock) WorkScore(_ *WorkScoreStructure) (WorkScore, error) {

func (b *ValidationBlock) Size() int {
return serializer.OneByte + // block type
serializer.OneByte + len(b.StrongParents)*SlotIdentifierLength + // StrongParents count
serializer.OneByte + len(b.WeakParents)*SlotIdentifierLength + // WeakParents count
serializer.OneByte + len(b.ShallowLikeParents)*SlotIdentifierLength + // ShallowLikeParents count
serializer.OneByte + len(b.StrongParents)*BlockIDLength + // StrongParents count
serializer.OneByte + len(b.WeakParents)*BlockIDLength + // WeakParents count
serializer.OneByte + len(b.ShallowLikeParents)*BlockIDLength + // ShallowLikeParents count
serializer.OneByte + // highest supported version
IdentifierLength // protocol parameters hash
}
Expand Down
201 changes: 201 additions & 0 deletions block_id.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e43c6d

Please sign in to comment.