Skip to content

Cbor tags refactoring#4

Merged
MastaP merged 4 commits intomainfrom
cbor_tags
Apr 13, 2026
Merged

Cbor tags refactoring#4
MastaP merged 4 commits intomainfrom
cbor_tags

Conversation

@MastaP
Copy link
Copy Markdown
Member

@MastaP MastaP commented Apr 10, 2026

MastaP added 4 commits April 10, 2026 21:44
…nstructors

  - Add UnmarshalTaggedVersioned/UnmarshalVersioned helpers in types/versions.go
    to collapse the decode-then-check-version boilerplate.
  - MarshalCBOR now copies the receiver before defaulting Version=1, so
    marshaling is a pure read operation.
  - Add NewXxx() constructors for top-level versioned types; fix existing
    txsystem constructors that produced Version=0 structs.
@MastaP MastaP requested a review from lploom April 10, 2026 21:48
@MastaP MastaP self-assigned this Apr 10, 2026
@MastaP MastaP added this to Unicity Apr 10, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request standardizes versioning and CBOR serialization across the codebase by introducing explicit version fields, constructor functions, and centralized unmarshaling helpers. It also updates CBOR tags to a new range (39000+). Feedback identifies a logic inconsistency where GetVersion() provides a default value of 1, causing unmarshaling to succeed for missing versions while subsequent IsValid() checks fail because they inspect the raw version field directly.

Comment thread types/versions.go
Comment on lines +51 to +58
func UnmarshalTaggedVersioned[A any](tag CborTag, expectedVersion Version, data []byte, aliasPtr *A, v Versioned) error {
if err := Cbor.UnmarshalTaggedValue(tag, data, aliasPtr); err != nil {
return err
}
if got := v.GetVersion(); got != expectedVersion {
return fmt.Errorf("invalid version (type %T), expected %d, got %d", v, expectedVersion, got)
}
return nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The UnmarshalTaggedVersioned helper relies on v.GetVersion() to validate the version. However, most implementations of GetVersion() in this repository (e.g., in Header, InputRecord, ShardTreeCertificate) return a default value of 1 if the Version field is 0. This means that unmarshaling will succeed even if the version is missing or zero in the CBOR data.

This creates an inconsistency because the IsValid() methods for these types typically check Version == 1 directly and will fail if the field is 0. This results in a situation where unmarshaling succeeds but produces an object that is considered invalid by its own validation logic.

Consider either making GetVersion() return the actual field value (to enforce explicit versioning during unmarshaling) or updating IsValid() to use GetVersion() so that the default value is respected consistently.

Comment on lines +22 to +27
func (cert ShardTreeCertificate) GetVersion() Version {
if cert.Version > 0 {
return cert.Version
}
return 1
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This implementation of GetVersion() returns 1 if cert.Version is 0. While this provides a default, it conflicts with the IsValid() check on line 43 which requires cert.Version to be exactly 1.

If unmarshaling data with a missing or zero version field, UnmarshalTaggedVersioned will succeed (because GetVersion() returns 1), but the resulting object will fail IsValid().

To fix this, you should either:

  1. Make GetVersion() return cert.Version directly to enforce that the version is present in the data.
  2. Update IsValid() to check cert.GetVersion() != 1 instead of cert.Version != 1.
func (cert ShardTreeCertificate) GetVersion() Version {
	return cert.Version
}

@mareksepp mareksepp moved this to In Dev in Unicity Apr 13, 2026
@MastaP MastaP merged commit c8fe6c3 into main Apr 13, 2026
3 checks passed
@github-project-automation github-project-automation bot moved this from In Dev to Done in Unicity Apr 13, 2026
@MastaP MastaP deleted the cbor_tags branch April 13, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants