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(BUX-296): bump as slice #34

Merged
merged 18 commits into from
Nov 2, 2023
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
15 changes: 13 additions & 2 deletions .github/workflows/test-and-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ jobs:
run: |
curl -sL https://taskfile.dev/install.sh | sh
sudo mv ./bin/task /usr/local/bin
- name: Run Tests
run: task test
- name: Cache code
uses: actions/cache@v3
with:
path: |
~/go/pkg/mod # Module download cache
~/.cache/go-build # Build cache (Linux)
~/Library/Caches/go-build # Build cache (Mac)
'%LocalAppData%\go-build' # Build cache (Windows)
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v2
- name: Run Tests
run: task test
- name: Run Lint
run: task lint
112 changes: 112 additions & 0 deletions bump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package paymail

import (
"errors"
"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
)

// BUMPs represents a slice of BUMPs - BSV Unified Merkle Paths
type BUMPs []BUMP

// BUMP is a struct that represents a whole BUMP format
type BUMP struct {
blockHeight uint64
path [][]BUMPLeaf
}

// BUMPLeaf represents each BUMP path element
type BUMPLeaf struct {
hash string
txId bool
duplicate bool
offset uint64
}

// Flags which are used to determine the type of BUMPLeaf
const (
dataFlag bt.VarInt = iota
duplicateFlag
txIDFlag
)

func (b BUMP) calculateMerkleRoots() ([]string, error) {
merkleRoots := make([]string, 0)

for _, bumpPathElement := range b.path[0] {
merkleRoot, err := calculateMerkleRoot(bumpPathElement, b)
if err != nil {
return nil, err
}
merkleRoots = append(merkleRoots, merkleRoot)
}
return merkleRoots, nil
}

func findLeafByOffset(offset uint64, bumpLeaves []BUMPLeaf) *BUMPLeaf {
for _, bumpTx := range bumpLeaves {
if bumpTx.offset == offset {
return &bumpTx
}
}
return nil
}

// calculateMerkleRoots will calculate one merkle root for tx in the BUMPPath
func calculateMerkleRoot(baseLeaf BUMPLeaf, bump BUMP) (string, error) {
calculatedHash := baseLeaf.hash
offset := baseLeaf.offset

for _, bLevel := range bump.path {
newOffset := getOffsetPair(offset)
leafInPair := findLeafByOffset(newOffset, bLevel)
if leafInPair == nil {
return "", errors.New("could not find pair")
}

leftNode, rightNode := prepareNodes(baseLeaf, offset, *leafInPair, newOffset)

str, err := bc.MerkleTreeParentStr(leftNode, rightNode)
if err != nil {
return "", err
}
calculatedHash = str

offset = offset / 2

baseLeaf = BUMPLeaf{
hash: calculatedHash,
offset: offset,
}
}

return calculatedHash, nil
}

func getOffsetPair(offset uint64) uint64 {
if offset%2 == 0 {
return offset + 1
}
return offset - 1
}

func prepareNodes(baseLeaf BUMPLeaf, offset uint64, leafInPair BUMPLeaf, newOffset uint64) (string, string) {
var baseLeafHash, pairLeafHash string

if baseLeaf.duplicate {
baseLeafHash = leafInPair.hash
} else {
baseLeafHash = baseLeaf.hash
}

if leafInPair.duplicate {
pairLeafHash = baseLeaf.hash
} else {
pairLeafHash = leafInPair.hash
}

if newOffset > offset {
return baseLeafHash, pairLeafHash
}
return pairLeafHash, baseLeafHash
}
18 changes: 0 additions & 18 deletions compound_merkle_path.go

This file was deleted.

Loading
Loading