Skip to content

Commit

Permalink
Merge pull request #154 from new0nebit/mangrove
Browse files Browse the repository at this point in the history
Add test coverage for ElementAccumulator.DecodeFrom
  • Loading branch information
lukechampine committed May 15, 2024
2 parents d5632c5 + 91dc7c6 commit ce7fca8
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions consensus/merkle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consensus

import (
"bytes"
"testing"

"go.sia.tech/core/types"
Expand Down Expand Up @@ -42,6 +43,44 @@ func TestElementAccumulatorEncoding(t *testing.T) {
}
}

func TestElementAccumulatorRoundTrip(t *testing.T) {
leafData := []byte{0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C}
leafHash := types.HashBytes(leafData)

for _, numLeaves := range []uint64{0, 1, 2, 3, 10, 1 << 16, 1 << 32, 1 << 63} {
acc := ElementAccumulator{NumLeaves: numLeaves}

for i := 0; i < 64; i++ {
if acc.hasTreeAtHeight(i) {
acc.Trees[i] = leafHash
}
}

var buf bytes.Buffer
e := types.NewEncoder(&buf)
acc.EncodeTo(e)
if err := e.Flush(); err != nil {
t.Fatalf("Unexpected error during encoding: %v", err)
}

encodedData := buf.Bytes()

d := types.NewBufDecoder(encodedData)
var decodedAcc ElementAccumulator
decodedAcc.DecodeFrom(d)

if decodedAcc.NumLeaves != acc.NumLeaves {
t.Errorf("NumLeaves mismatch: got %d, expected %d", decodedAcc.NumLeaves, acc.NumLeaves)
}

for i, tree := range decodedAcc.Trees {
if tree != acc.Trees[i] {
t.Errorf("Tree mismatch at %d: got %v, expected %v", i, tree, acc.Trees[i])
}
}
}
}

func TestStorageProofRoot(t *testing.T) {
leafHash := types.Hash256{0x01, 0x02, 0x03}
validProof := []types.Hash256{
Expand Down

0 comments on commit ce7fca8

Please sign in to comment.