Skip to content

Commit

Permalink
feat(BUMP): implementation of NewBUMPFromStream that returns bytes used
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed May 22, 2024
1 parent 186145d commit ab6eefb
Showing 1 changed file with 16 additions and 5 deletions.
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 ab6eefb

Please sign in to comment.