Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BUMP): implementation of NewBUMPFromStream that returns bytes used #84

Merged
merged 3 commits into from
May 24, 2024
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
5 changes: 0 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ linters-settings:
line-length: 150
# tab width in spaces. Default to 1.
tab-width: 1
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
Expand Down Expand Up @@ -339,7 +336,6 @@ linters:
- revive
- unconvert
- dupl
- maligned
- misspell
- dogsled
- prealloc
Expand Down Expand Up @@ -388,7 +384,6 @@ issues:
- goconst
- dupl
- gosec
- maligned
- lll

# Exclude known linters from partially hard-vendored code,
Expand Down
21 changes: 16 additions & 5 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ type leaf struct {
Duplicate *bool `json:"duplicate,omitempty"`
}

// NewBUMPFromBytes creates a new BUMP from a byte slice.
func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
// NewBUMPFromStream takes an array of bytes and contructs a BUMP from it, returning the BUMP
// and the bytes used. Despite the name, this is not actually reading a stream in the true sense:
// it is a byte slice that contains many BUMPs one after another.
func NewBUMPFromStream(bytes []byte) (*BUMP, int, error) {
if len(bytes) < 37 {
return nil, errors.New("BUMP bytes do not contain enough data to be valid")
return nil, 0, errors.New("BUMP bytes do not contain enough data to be valid")
}
bump := &BUMP{}

Expand All @@ -54,7 +56,7 @@ func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
skip += size
nLeavesAtThisHeight := uint64(n)
if nLeavesAtThisHeight == 0 {
return nil, errors.New("There are no leaves at height: " + fmt.Sprint(lv) + " which makes this invalid")
return nil, 0, errors.New("There are no leaves at height: " + fmt.Sprint(lv) + " which makes this invalid")
}
bump.Path[lv] = make([]leaf, nLeavesAtThisHeight)
for lf := uint64(0); lf < nLeavesAtThisHeight; lf++ {
Expand All @@ -72,7 +74,7 @@ func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
l.Duplicate = &dup
} else {
if len(bytes) < skip+32 {
return nil, errors.New("BUMP bytes do not contain enough data to be valid")
return nil, 0, errors.New("BUMP bytes do not contain enough data to be valid")
}
h := StringFromBytesReverse(bytes[skip : skip+32])
l.Hash = &h
Expand All @@ -92,6 +94,15 @@ func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
})
}

return bump, skip, nil
}

// NewBUMPFromBytes creates a new BUMP from a byte slice.
func NewBUMPFromBytes(bytes []byte) (*BUMP, error) {
bump, _, err := NewBUMPFromStream(bytes)
if err != nil {
return nil, err
}
return bump, nil
}

Expand Down
Loading