-
Notifications
You must be signed in to change notification settings - Fork 107
/
store_test.go
66 lines (52 loc) 路 1.84 KB
/
store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package beacon
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/drand/drand/chain"
"github.com/drand/drand/chain/boltdb"
"github.com/drand/drand/common/scheme"
"github.com/drand/drand/test"
)
func TestSchemeStore(t *testing.T) {
sch, _ := scheme.ReadSchemeByEnv()
dir := t.TempDir()
ctx := context.Background()
l := test.Logger(t)
bstore, err := boltdb.NewBoltStore(l, dir, nil)
require.NoError(t, err)
genesisBeacon := chain.GenesisBeacon(&chain.Info{GenesisSeed: []byte("genesis_signature")})
err = bstore.Put(ctx, genesisBeacon)
require.NoError(t, err)
ss := NewSchemeStore(bstore, sch)
newBeacon := &chain.Beacon{
Round: 1,
Signature: []byte("signature_1"),
PreviousSig: []byte("genesis_signature"),
}
err = ss.Put(ctx, newBeacon)
require.NoError(t, err)
beaconSaved, err := ss.Last(ctx)
require.NoError(t, err)
// test if store sets to nil prev signature depending on scheme
// with chained scheme, it should keep the consistency between prev signature and signature
if sch.DecouplePrevSig && beaconSaved.PreviousSig != nil {
t.Errorf("previous signature should be nil")
} else if !sch.DecouplePrevSig && !bytes.Equal(beaconSaved.PreviousSig, genesisBeacon.Signature) {
t.Errorf("previous signature on last beacon [%s] should be equal to previous beacon signature [%s]",
beaconSaved.PreviousSig, genesisBeacon.PreviousSig)
}
newBeacon = &chain.Beacon{
Round: 2,
Signature: []byte("signature_2"),
PreviousSig: nil,
}
err = ss.Put(ctx, newBeacon)
// test if store checks consistency between signature and prev signature depending on the scheme
if sch.DecouplePrevSig && err != nil {
t.Errorf("new beacon should be allow to be put on store")
} else if !sch.DecouplePrevSig && err == nil {
t.Errorf("new beacon should not be allow to be put on store")
}
}