Skip to content

Commit

Permalink
Merge pull request #84 from kuba-4chain/feat/new-bump-from-stream
Browse files Browse the repository at this point in the history
feat(BUMP): implementation of NewBUMPFromStream that returns bytes used
  • Loading branch information
boecklim authored May 24, 2024
2 parents 186145d + 8f5ad17 commit f6a717f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
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

0 comments on commit f6a717f

Please sign in to comment.