Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat: BUMP. slice of slices
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalibalashka committed Oct 26, 2023
1 parent b6d63ae commit 5d33aed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
10 changes: 4 additions & 6 deletions model_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import (

// BUMP represents BUMP format
type BUMP struct {
BlockHeight uint64 `json:"blockHeight,string"`
Path []BUMPPathMap `json:"path"`
BlockHeight uint64 `json:"blockHeight,string"`
Path [][]BUMPLeaf `json:"path"`
}

// BUMPPathMap represents map with pathes
type BUMPPathMap map[string]BUMPPathElement

// BUMPPathElement represents each BUMP path element
type BUMPPathElement struct {
type BUMPLeaf struct {
Offset uint64 `json:"offset,omitempty,string"`
Hash string `json:"hash,omitempty"`
TxId bool `json:"txid,omitempty"`
Duplicate bool `json:"duplicate,omitempty"`
Expand Down
17 changes: 10 additions & 7 deletions model_merkle_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"
Expand Down Expand Up @@ -86,17 +87,19 @@ func (m *MerkleProof) ToBUMP() BUMP {
if height == 0 {
return bump
}
path := make([]BUMPPathMap, 0)
txIdPath := make(BUMPPathMap, 2)
path := make([][]BUMPLeaf, 0)
txIdPath := make([]BUMPLeaf, 0)
offset := m.Index
op := offsetPair(offset)
txIdPath[fmt.Sprint(offset)] = BUMPPathElement{Hash: m.TxOrID, TxId: true}
txIdPath[fmt.Sprint(op)] = BUMPPathElement{Hash: m.Nodes[0]}
txIdPath = append(txIdPath, BUMPLeaf{Offset: offset, Hash: m.TxOrID, TxId: true})
txIdPath = append(txIdPath, BUMPLeaf{Offset: offsetPair(offset), Hash: m.Nodes[0]})
sort.Slice(txIdPath , func(i, j int) bool {
return txIdPath[i].Offset < txIdPath[j].Offset
})
path = append(path, txIdPath)
for i := 1; i < height; i++ {
p := make(BUMPPathMap, 1)
p := make([]BUMPLeaf, 0)
offset = parrentOffset(offset)
p[fmt.Sprint(offset)] = BUMPPathElement{Hash: m.Nodes[i]}
p = append(p, BUMPLeaf{Offset: offset, Hash: m.Nodes[i]})
path = append(path, p)
}
bump.Path = path
Expand Down
26 changes: 13 additions & 13 deletions model_merkle_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
Nodes: []string{"node0", "node1", "node2", "node3"},
}
expectedBUMP := BUMP{
Path: []BUMPPathMap{
Path: [][]BUMPLeaf{
{
"0": BUMPPathElement{Hash: "node0"},
"1": BUMPPathElement{Hash: "txId", TxId: true},
{Offset: 0, Hash: "node0"},
{Offset: 1, Hash: "txId", TxId: true},
},
{
"1": BUMPPathElement{Hash: "node1"},
{Offset: 1, Hash: "node1"},
},
{
"1": BUMPPathElement{Hash: "node2"},
{Offset: 1, Hash: "node2"},
},
{
"1": BUMPPathElement{Hash: "node3"},
{Offset: 1, Hash: "node3"},
},
},
}
Expand All @@ -113,22 +113,22 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
Nodes: []string{"node0", "node1", "node2", "node3", "node4"},
}
expectedBUMP := BUMP{
Path: []BUMPPathMap{
Path: [][]BUMPLeaf{
{
"14": BUMPPathElement{Hash: "txId", TxId: true},
"15": BUMPPathElement{Hash: "node0"},
{Offset: 14, Hash: "txId", TxId: true},
{Offset: 15, Hash: "node0"},
},
{
"6": BUMPPathElement{Hash: "node1"},
{Offset: 6, Hash: "node1"},
},
{
"2": BUMPPathElement{Hash: "node2"},
{Offset: 2, Hash: "node2"},
},
{
"0": BUMPPathElement{Hash: "node3"},
{Offset: 0, Hash: "node3"},
},
{
"1": BUMPPathElement{Hash: "node4"},
{Offset: 1, Hash: "node4"},
},
},
}
Expand Down

0 comments on commit 5d33aed

Please sign in to comment.