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

Commit

Permalink
feat(BUX-250): block height added to TuBUMP method
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain authored and arkadiuszos4chain committed Nov 13, 2023
1 parent d0e1394 commit 4307026
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion beef_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func addGrandpaTx(ctx context.Context, t *testing.T, client ClientInterface) *Tr
},
}
grandpaTx.MerkleProof = MerkleProof(grandpaTxMp)
grandpaTx.BUMP = grandpaTx.MerkleProof.ToBUMP()
grandpaTx.BUMP = grandpaTx.MerkleProof.ToBUMP(grandpaTx.BlockHeight)
err := grandpaTx.Save(ctx)
require.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions db_model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ func (m *Transaction) migrateBUMP() error {
return err
}
for _, tx := range txs {
bump := tx.MerkleProof.ToBUMP()
bump.BlockHeight = tx.BlockHeight
bump := tx.MerkleProof.ToBUMP(tx.BlockHeight)
tx.BUMP = bump
_ = tx.Save(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion model_bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ func TestBUMPModel_CalculateMergedBUMPAndHex(t *testing.T) {
// when
bumps := make([]BUMP, 0)
for _, mp := range merkleProof {
bumps = append(bumps, mp.ToBUMP())
bumps = append(bumps, mp.ToBUMP(0))
}
bump, err := CalculateMergedBUMP(bumps)
actualHex := bump.Hex()
Expand Down
4 changes: 2 additions & 2 deletions model_merkle_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (m MerkleProof) Value() (driver.Value, error) {
}

// ToBUMP transform Merkle Proof to BUMP
func (m *MerkleProof) ToBUMP() BUMP {
bump := BUMP{}
func (m *MerkleProof) ToBUMP(blockHeight uint64) BUMP {
bump := BUMP{BlockHeight: blockHeight}

height := len(m.Nodes)
if height == 0 {
Expand Down
32 changes: 27 additions & 5 deletions model_merkle_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
t.Parallel()

t.Run("Valid Merkle Proof #1", func(t *testing.T) {
// given
blockHeight := uint64(0)
mp := MerkleProof{
Index: 1,
TxOrID: "txId",
Nodes: []string{"node0", "node1", "node2", "node3"},
}
expectedBUMP := BUMP{
BlockHeight: blockHeight,
Path: [][]BUMPLeaf{
{
{Offset: 0, Hash: "node0"},
Expand All @@ -33,17 +36,24 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
},
},
}
actualBUMP := mp.ToBUMP()

// when
actualBUMP := mp.ToBUMP(blockHeight)

// then
assert.Equal(t, expectedBUMP, actualBUMP)
})

t.Run("Valid Merkle Proof #2", func(t *testing.T) {
// given
blockHeight := uint64(0)
mp := MerkleProof{
Index: 14,
TxOrID: "txId",
Nodes: []string{"node0", "node1", "node2", "node3", "node4"},
}
expectedBUMP := BUMP{
BlockHeight: blockHeight,
Path: [][]BUMPLeaf{
{
{Offset: 14, Hash: "txId", TxID: true},
Expand All @@ -63,17 +73,24 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
},
},
}
actualBUMP := mp.ToBUMP()

// when
actualBUMP := mp.ToBUMP(blockHeight)

// then
assert.Equal(t, expectedBUMP, actualBUMP)
})

t.Run("Valid Merkle Proof #3 - with *", func(t *testing.T) {
// given
blockHeight := uint64(0)
mp := MerkleProof{
Index: 14,
TxOrID: "txId",
Nodes: []string{"*", "node1", "node2", "node3", "node4"},
}
expectedBUMP := BUMP{
BlockHeight: blockHeight,
Path: [][]BUMPLeaf{
{
{Offset: 14, Hash: "txId", TxID: true},
Expand All @@ -93,13 +110,18 @@ func TestMerkleProofModel_ToBUMP(t *testing.T) {
},
},
}
actualBUMP := mp.ToBUMP()

// when
actualBUMP := mp.ToBUMP(blockHeight)

// then
assert.Equal(t, expectedBUMP, actualBUMP)
})

t.Run("Empty Merkle Proof", func(t *testing.T) {
blockHeight := uint64(0)
mp := MerkleProof{}
actualBUMP := mp.ToBUMP()
assert.Equal(t, BUMP{}, actualBUMP)
actualBUMP := mp.ToBUMP(blockHeight)
assert.Equal(t, BUMP{BlockHeight: blockHeight}, actualBUMP)
})
}
3 changes: 1 addition & 2 deletions model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ func (m *Transaction) setMerkleRoot(txInfo *chainstate.TransactionInfo) {
mp := MerkleProof(*txInfo.MerkleProof)
m.MerkleProof = mp

bump := mp.ToBUMP()
bump.BlockHeight = uint64(txInfo.BlockHeight)
bump := mp.ToBUMP(uint64(txInfo.BlockHeight))
m.BUMP = bump
}
}
Expand Down

0 comments on commit 4307026

Please sign in to comment.