Skip to content

Commit

Permalink
Make smoke tests check if CBOR tag nums are reserved
Browse files Browse the repository at this point in the history
This commit checks if CBOR tag number is reserved by atree
for internal use before smoke tests use it to encode Cadence
values as elements in atree containers.

As of Aug 15, 2024:
- Atree reserves CBOR tag numbers [240, 255] for internal use.
- Smoke tests reserves CBOR tag numbers [161, 239] for encoding
  elements in atree containers.
  • Loading branch information
fxamacker committed Aug 15, 2024
1 parent 141ec5c commit 273a611
Showing 2 changed files with 51 additions and 16 deletions.
51 changes: 51 additions & 0 deletions cmd/stress/storable.go
Original file line number Diff line number Diff line change
@@ -32,12 +32,63 @@ import (
// This file contains value implementations for testing purposes.

const (
reservedMinTagNum = 161
reservedMinTagNumForContainerType = 230
reservedMaxTagNum = 239
)

const (
// CBOR tag numbers used to encode elements.

cborTagUInt8Value = 161
cborTagUInt16Value = 162
cborTagUInt32Value = 163
cborTagUInt64Value = 164

// CBOR tag numbers in this block cannot exceed 230 (reservedMinTagNumForContainerType).
)

const (
// CBOR tag numbers used to encode container types.
// Replace _ when new tag number is needed (use lower tag numbers first).

arrayTypeTagNum = reservedMinTagNumForContainerType + iota
compositeTypeTagNum
mapTypeTagNum
_
_
_
_
_
_
_
)

func init() {
// Check if the CBOR tag number range is reserved for internal use by atree.
// Smoke tests must only use available (unreserved by atree) CBOR tag numbers
// to encode elements in atree managed containers.

// As of Aug 15, 2024:
// - Atree reserves CBOR tag numbers [240, 255] for atree internal use.
// - Smoke tests reserve CBOR tag numbers [161, 239] to encode elements.

tagNumOK, err := atree.IsCBORTagNumberRangeAvailable(reservedMinTagNum, reservedMaxTagNum)
if err != nil {
panic(err)
}

if !tagNumOK {
atreeMinTagNum, atreeMaxTagNum := atree.ReservedCBORTagNumberRange()
panic(fmt.Errorf(
"smoke test tag numbers [%d, %d] overlaps with atree internal tag numbers [%d, %d]",
reservedMinTagNum,
reservedMaxTagNum,
atreeMinTagNum,
atreeMaxTagNum))
}
}

type Uint8Value uint8

var _ atree.Value = Uint8Value(0)
16 changes: 0 additions & 16 deletions cmd/stress/typeinfo.go
Original file line number Diff line number Diff line change
@@ -29,10 +29,6 @@ import (
const (
maxArrayTypeValue = 10
maxMapTypeValue = 10

arrayTypeTagNum = 246
mapTypeTagNum = 245
compositeTypeTagNum = 244
)

type arrayTypeInfo struct {
@@ -53,10 +49,6 @@ func (i arrayTypeInfo) IsComposite() bool {
return false
}

func (i arrayTypeInfo) Identifier() string {
return fmt.Sprintf("array(%d)", i)
}

func (i arrayTypeInfo) Encode(e *cbor.StreamEncoder) error {
err := e.EncodeTagHead(arrayTypeTagNum)
if err != nil {
@@ -88,10 +80,6 @@ func (i mapTypeInfo) IsComposite() bool {
return false
}

func (i mapTypeInfo) Identifier() string {
return fmt.Sprintf("map(%d)", i)
}

func (i mapTypeInfo) Encode(e *cbor.StreamEncoder) error {
err := e.EncodeTagHead(mapTypeTagNum)
if err != nil {
@@ -153,10 +141,6 @@ func (i compositeTypeInfo) IsComposite() bool {
return true
}

func (i compositeTypeInfo) Identifier() string {
return fmt.Sprintf("composite(%d_%d)", i.fieldStartIndex, i.fieldEndIndex)
}

func (i compositeTypeInfo) Encode(e *cbor.StreamEncoder) error {
err := e.EncodeTagHead(compositeTypeTagNum)
if err != nil {

0 comments on commit 273a611

Please sign in to comment.