Skip to content

Commit 46112c2

Browse files
authored
Fix chain comparison bug and add tests (#734)
Return false if chains of same length are not equal and assert assert it in unit tests.
1 parent 12514c1 commit 46112c2

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

gpbft/chain.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (ts *TipSet) Equal(b *TipSet) bool {
8383
}
8484
return ts.Epoch == b.Epoch &&
8585
bytes.Equal(ts.Key, b.Key) &&
86-
ts.PowerTable == b.PowerTable &&
86+
ts.PowerTable.Equals(b.PowerTable) &&
8787
ts.Commitments == b.Commitments
8888
}
8989

@@ -216,7 +216,9 @@ func (c ECChain) Eq(other ECChain) bool {
216216
return false
217217
}
218218
for i := range c {
219-
c[i].Equal(&other[i])
219+
if !c[i].Equal(&other[i]) {
220+
return false
221+
}
220222
}
221223
return true
222224
}

gpbft/chain_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/filecoin-project/go-f3/gpbft"
7+
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -107,3 +108,79 @@ func TestECChain(t *testing.T) {
107108
require.Equal(t, second[1], gpbft.TipSet{Epoch: 1, Key: []byte{2}})
108109
})
109110
}
111+
112+
func TestECChain_Eq(t *testing.T) {
113+
t.Parallel()
114+
var (
115+
commitThis = [32]byte{0x01}
116+
commitThat = [32]byte{0x02}
117+
ptThis = gpbft.MakeCid([]byte("fish"))
118+
ptThat = gpbft.MakeCid([]byte("lobster"))
119+
ts1 = gpbft.TipSet{Epoch: 1, Key: []byte("barreleye1"), PowerTable: ptThat, Commitments: commitThis}
120+
ts2 = gpbft.TipSet{Epoch: 2, Key: []byte("barreleye2"), PowerTable: ptThat, Commitments: commitThis}
121+
ts3 = gpbft.TipSet{Epoch: 3, Key: []byte("barreleye3"), PowerTable: ptThat, Commitments: commitThis}
122+
ts1DifferentCommitments = gpbft.TipSet{1, []byte("barreleye1"), ptThat, commitThat}
123+
ts1DifferentPowerTable = gpbft.TipSet{1, []byte("barreleye1"), ptThis, commitThis}
124+
)
125+
for _, tt := range []struct {
126+
name string
127+
one *gpbft.ECChain
128+
other *gpbft.ECChain
129+
expect bool
130+
}{
131+
{
132+
name: "Equal chains",
133+
one: &gpbft.ECChain{ts1, ts2},
134+
other: &gpbft.ECChain{ts1, ts2},
135+
expect: true,
136+
},
137+
{
138+
name: "Different chains",
139+
one: &gpbft.ECChain{ts1, ts2},
140+
other: &gpbft.ECChain{ts1, ts3},
141+
expect: false,
142+
},
143+
{
144+
name: "Same chain compared with itself",
145+
one: &gpbft.ECChain{ts1, ts2},
146+
other: &gpbft.ECChain{ts1, ts2},
147+
expect: true,
148+
},
149+
{
150+
name: "Different lengths",
151+
one: &gpbft.ECChain{ts1},
152+
other: &gpbft.ECChain{ts1, ts2},
153+
expect: false,
154+
},
155+
{
156+
name: "Zero chains (empty chains)",
157+
one: &gpbft.ECChain{},
158+
other: &gpbft.ECChain{},
159+
expect: true,
160+
},
161+
{
162+
name: "One zero chain",
163+
one: &gpbft.ECChain{ts1},
164+
other: &gpbft.ECChain{},
165+
expect: false,
166+
},
167+
{
168+
name: "Different commitments",
169+
one: &gpbft.ECChain{ts1},
170+
other: &gpbft.ECChain{ts1DifferentCommitments},
171+
expect: false,
172+
},
173+
{
174+
name: "Different power table",
175+
one: &gpbft.ECChain{ts1},
176+
other: &gpbft.ECChain{ts1DifferentPowerTable},
177+
expect: false,
178+
},
179+
} {
180+
t.Run(tt.name, func(t *testing.T) {
181+
t.Parallel()
182+
assert.Equal(t, tt.expect, tt.one.Eq(*tt.other), "Unexpected equality result for one compared to other: %s", tt.name)
183+
assert.Equal(t, tt.expect, tt.other.Eq(*tt.one), "Unexpected equality result for other compared to one: %s", tt.name)
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)