Skip to content
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
13 changes: 6 additions & 7 deletions txsystem/fc/unit_data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type FeeCreditRecord struct {

func NewFeeCreditRecord(balance uint64, ownerPredicate []byte, minLifetime uint64) *FeeCreditRecord {
return &FeeCreditRecord{
Version: 1,
Balance: balance,
OwnerPredicate: ownerPredicate,
MinLifetime: minLifetime,
Expand Down Expand Up @@ -67,18 +68,16 @@ func (b *FeeCreditRecord) GetVersion() types.Version {

func (b *FeeCreditRecord) MarshalCBOR() ([]byte, error) {
type alias FeeCreditRecord
if b.Version == 0 {
b.Version = b.GetVersion()
cp := *b
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(b))
return types.Cbor.Marshal((*alias)(&cp))
}

func (b *FeeCreditRecord) UnmarshalCBOR(data []byte) error {
type alias FeeCreditRecord
if err := types.Cbor.Unmarshal(data, (*alias)(b)); err != nil {
return err
}
return types.EnsureVersion(b, b.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(b), b)
}

func (b *FeeCreditRecord) IsExpired(currentRoundNumber uint64) bool {
Expand Down
13 changes: 6 additions & 7 deletions txsystem/money/unit_data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewUnitData(unitID types.UnitID, unitTypeExtractor types.UnitTypeExtractor)

func NewBillData(value uint64, ownerPredicate []byte) *BillData {
return &BillData{
Version: 1,
Value: value,
OwnerPredicate: ownerPredicate,
}
Expand Down Expand Up @@ -72,16 +73,14 @@ func (b *BillData) GetVersion() types.Version {

func (b *BillData) MarshalCBOR() ([]byte, error) {
type alias BillData
if b.Version == 0 {
b.Version = b.GetVersion()
cp := *b
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(b))
return types.Cbor.Marshal((*alias)(&cp))
}

func (b *BillData) UnmarshalCBOR(data []byte) error {
type alias BillData
if err := types.Cbor.Unmarshal(data, (*alias)(b)); err != nil {
return err
}
return types.EnsureVersion(b, b.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(b), b)
}
16 changes: 9 additions & 7 deletions txsystem/orchestration/unit_data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ func (b *VarData) GetVersion() types.Version {
return 1
}

func NewVarData(epochNumber uint64) *VarData {
return &VarData{Version: 1, EpochNumber: epochNumber}
}

func (b *VarData) MarshalCBOR() ([]byte, error) {
type alias VarData
if b.Version == 0 {
b.Version = b.GetVersion()
cp := *b
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(b))
return types.Cbor.Marshal((*alias)(&cp))
}

func (b *VarData) UnmarshalCBOR(data []byte) error {
type alias VarData
if err := types.Cbor.Unmarshal(data, (*alias)(b)); err != nil {
return err
}
return types.EnsureVersion(b, b.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(b), b)
}
52 changes: 24 additions & 28 deletions txsystem/tokens/unit_data_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type FungibleTokenData struct {

func NewFungibleTokenTypeData(attr *DefineFungibleTokenAttributes) types.UnitData {
return &FungibleTokenTypeData{
Version: 1,
Symbol: attr.Symbol,
Name: attr.Name,
Icon: attr.Icon,
Expand All @@ -77,6 +78,7 @@ func NewFungibleTokenTypeData(attr *DefineFungibleTokenAttributes) types.UnitDat

func NewNonFungibleTokenTypeData(attr *DefineNonFungibleTokenAttributes) types.UnitData {
return &NonFungibleTokenTypeData{
Version: 1,
Symbol: attr.Symbol,
Name: attr.Name,
Icon: attr.Icon,
Expand All @@ -90,6 +92,7 @@ func NewNonFungibleTokenTypeData(attr *DefineNonFungibleTokenAttributes) types.U

func NewNonFungibleTokenData(typeID types.UnitID, attr *MintNonFungibleTokenAttributes) types.UnitData {
return &NonFungibleTokenData{
Version: 1,
TypeID: typeID,
Name: attr.Name,
URI: attr.URI,
Expand All @@ -101,6 +104,7 @@ func NewNonFungibleTokenData(typeID types.UnitID, attr *MintNonFungibleTokenAttr

func NewFungibleTokenData(typeID types.UnitID, value uint64, ownerPredicate []byte, minLifetime uint64) types.UnitData {
return &FungibleTokenData{
Version: 1,
TypeID: typeID,
Value: value,
OwnerPredicate: ownerPredicate,
Expand Down Expand Up @@ -141,18 +145,16 @@ func (n *NonFungibleTokenTypeData) GetVersion() types.Version {

func (n *NonFungibleTokenTypeData) MarshalCBOR() ([]byte, error) {
type alias NonFungibleTokenTypeData
if n.Version == 0 {
n.Version = n.GetVersion()
cp := *n
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(n))
return types.Cbor.Marshal((*alias)(&cp))
}

func (n *NonFungibleTokenTypeData) UnmarshalCBOR(data []byte) error {
type alias NonFungibleTokenTypeData
if err := types.Cbor.Unmarshal(data, (*alias)(n)); err != nil {
return err
}
return types.EnsureVersion(n, n.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(n), n)
}

func (n *NonFungibleTokenTypeData) Owner() []byte {
Expand Down Expand Up @@ -191,18 +193,16 @@ func (n *NonFungibleTokenData) GetVersion() types.Version {

func (n *NonFungibleTokenData) MarshalCBOR() ([]byte, error) {
type alias NonFungibleTokenData
if n.Version == 0 {
n.Version = n.GetVersion()
cp := *n
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(n))
return types.Cbor.Marshal((*alias)(&cp))
}

func (n *NonFungibleTokenData) UnmarshalCBOR(data []byte) error {
type alias NonFungibleTokenData
if err := types.Cbor.Unmarshal(data, (*alias)(n)); err != nil {
return err
}
return types.EnsureVersion(n, n.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(n), n)
}

func (n *NonFungibleTokenData) GetCounter() uint64 {
Expand Down Expand Up @@ -250,18 +250,16 @@ func (f *FungibleTokenTypeData) GetVersion() types.Version {

func (b *FungibleTokenTypeData) MarshalCBOR() ([]byte, error) {
type alias FungibleTokenTypeData
if b.Version == 0 {
b.Version = b.GetVersion()
cp := *b
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(b))
return types.Cbor.Marshal((*alias)(&cp))
}

func (b *FungibleTokenTypeData) UnmarshalCBOR(data []byte) error {
type alias FungibleTokenTypeData
if err := types.Cbor.Unmarshal(data, (*alias)(b)); err != nil {
return err
}
return types.EnsureVersion(b, b.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(b), b)
}

func (f *FungibleTokenData) Write(hasher abhash.Hasher) {
Expand Down Expand Up @@ -302,16 +300,14 @@ func (f *FungibleTokenData) GetVersion() types.Version {

func (f *FungibleTokenData) MarshalCBOR() ([]byte, error) {
type alias FungibleTokenData
if f.Version == 0 {
f.Version = f.GetVersion()
cp := *f
if cp.Version == 0 {
cp.Version = 1
}
return types.Cbor.Marshal((*alias)(f))
return types.Cbor.Marshal((*alias)(&cp))
}

func (f *FungibleTokenData) UnmarshalCBOR(data []byte) error {
type alias FungibleTokenData
if err := types.Cbor.Unmarshal(data, (*alias)(f)); err != nil {
return err
}
return types.EnsureVersion(f, f.Version, 1)
return types.UnmarshalVersioned(1, data, (*alias)(f), f)
}
15 changes: 10 additions & 5 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,25 @@ func (h *Header) GetVersion() Version {
return 1
}

func NewHeader() *Header {
return &Header{Version: 1}
}

func (h *Header) MarshalCBOR() ([]byte, error) {
type alias Header
if h.Version == 0 {
h.Version = h.GetVersion()
cp := *h
if cp.Version == 0 {
cp.Version = 1
}
return Cbor.MarshalTaggedValue(BlockTag, (*alias)(h))
return Cbor.MarshalTaggedValue(BlockTag, (*alias)(&cp))
}

func (h *Header) UnmarshalCBOR(data []byte) error {
type alias Header
if err := Cbor.UnmarshalTaggedValue(BlockTag, data, (*alias)(h)); err != nil {
if err := UnmarshalTaggedVersioned(BlockTag, 1, data, (*alias)(h), h); err != nil {
return fmt.Errorf("failed to unmarshal block header: %w", err)
}
return EnsureVersion(h, h.Version, 1)
return nil
}

func (h *Header) Hash(algorithm crypto.Hash) ([]byte, error) {
Expand Down
7 changes: 5 additions & 2 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,14 @@ func TestBlock_CBOR(t *testing.T) {
})
t.Run("block with unicity certificate", func(t *testing.T) {
uc := &UnicityCertificate{
Version: 1,
InputRecord: &InputRecord{
Version: 1, // if version is not set here, the test fails (despite the fact it's a pointer)
Version: 1,
Hash: []byte{1, 1, 1},
PreviousHash: []byte{1, 1, 1},
}}
},
ShardTreeCertificate: NewShardTreeCertificate(),
}
ucBytes, err := (uc).MarshalCBOR()
require.NoError(t, err)
b := Block{
Expand Down
16 changes: 9 additions & 7 deletions types/input_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,20 @@ func (x *InputRecord) GetVersion() Version {
return 1
}

func NewInputRecord() *InputRecord {
return &InputRecord{Version: 1}
}

func (x *InputRecord) MarshalCBOR() ([]byte, error) {
type alias InputRecord
if x.Version == 0 {
x.Version = x.GetVersion()
cp := *x
if cp.Version == 0 {
cp.Version = 1
}
return Cbor.MarshalTaggedValue(InputRecordTag, (*alias)(x))
return Cbor.MarshalTaggedValue(InputRecordTag, (*alias)(&cp))
}

func (x *InputRecord) UnmarshalCBOR(data []byte) error {
type alias InputRecord
if err := Cbor.UnmarshalTaggedValue(InputRecordTag, data, (*alias)(x)); err != nil {
return err
}
return EnsureVersion(x, x.Version, 1)
return UnmarshalTaggedVersioned(InputRecordTag, 1, data, (*alias)(x), x)
}
4 changes: 2 additions & 2 deletions types/input_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func TestInputRecord_AddToHasher(t *testing.T) {
ir.AddToHasher(abhasher)
hash := hasher.Sum(nil)

expectedHash := []byte{0x65, 0x33, 0x67, 0xb2, 0xf2, 0xff, 0x9d, 0xa5, 0x2, 0x86, 0x2, 0x65, 0x46, 0xf6, 0x62,
0x77, 0x89, 0x83, 0x10, 0x63, 0x60, 0x6b, 0x23, 0x60, 0xf2, 0x16, 0x61, 0x5a, 0x60, 0x16, 0x1, 0xbf}
expectedHash := []byte{0xc0, 0x8e, 0x82, 0x36, 0x7b, 0xe0, 0x7b, 0xd, 0xf7, 0xfd, 0xa4, 0x72, 0xde, 0xf0, 0xed,
0xb5, 0xd9, 0xc5, 0x63, 0x48, 0xc1, 0x65, 0xfb, 0xfc, 0x12, 0x8e, 0x6, 0xd6, 0x67, 0xad, 0xbb, 0xe8}
require.Equal(t, expectedHash, hash)
}

Expand Down
15 changes: 10 additions & 5 deletions types/partition_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,25 @@ func (pdr *PartitionDescriptionRecord) ExtractUnitType(id UnitID) (uint32, error
return v & mask, nil
}

func NewPartitionDescriptionRecord() *PartitionDescriptionRecord {
return &PartitionDescriptionRecord{Version: 1}
}

func (pdr *PartitionDescriptionRecord) MarshalCBOR() ([]byte, error) {
type alias PartitionDescriptionRecord
if pdr.Version == 0 {
pdr.Version = pdr.GetVersion()
cp := *pdr
if cp.Version == 0 {
cp.Version = 1
}
return Cbor.MarshalTaggedValue(PartitionDescriptionRecordTag, (*alias)(pdr))
return Cbor.MarshalTaggedValue(PartitionDescriptionRecordTag, (*alias)(&cp))
}

func (pdr *PartitionDescriptionRecord) UnmarshalCBOR(data []byte) error {
type alias PartitionDescriptionRecord
if err := Cbor.UnmarshalTaggedValue(PartitionDescriptionRecordTag, data, (*alias)(pdr)); err != nil {
if err := UnmarshalTaggedVersioned(PartitionDescriptionRecordTag, 1, data, (*alias)(pdr), pdr); err != nil {
return fmt.Errorf("failed to unmarshal partition description record: %w", err)
}
return EnsureVersion(pdr, pdr.Version, 1)
return nil
}

func (pdr *PartitionDescriptionRecord) FindValidator(nodeID string) *NodeInfo {
Expand Down
15 changes: 10 additions & 5 deletions types/root_trust_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,25 @@ func (r *RootTrustBaseV1) GetEpochStart() uint64 {
return r.EpochStart
}

func NewRootTrustBaseV1() *RootTrustBaseV1 {
return &RootTrustBaseV1{Version: 1}
}

func (r *RootTrustBaseV1) MarshalCBOR() ([]byte, error) {
type alias RootTrustBaseV1
if r.Version == 0 {
r.Version = r.GetVersion()
cp := *r
if cp.Version == 0 {
cp.Version = 1
}
return Cbor.MarshalTaggedValue(RootTrustBaseTag, (*alias)(r))
return Cbor.MarshalTaggedValue(UnicityTrustBaseTag, (*alias)(&cp))
}

func (r *RootTrustBaseV1) UnmarshalCBOR(data []byte) error {
type alias RootTrustBaseV1
if err := Cbor.UnmarshalTaggedValue(RootTrustBaseTag, data, (*alias)(r)); err != nil {
if err := UnmarshalTaggedVersioned(UnicityTrustBaseTag, 1, data, (*alias)(r), r); err != nil {
return fmt.Errorf("failed to unmarshal root trust base: %w", err)
}
return EnsureVersion(r, r.Version, 1)
return nil
}

func (r *RootTrustBaseV1) getRootNode(nodeID string) *NodeInfo {
Expand Down
Loading
Loading