-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save prev signature on db only if chained mode is enabled (#895)
* save prev signature on db only if chained mode is enabled * add extra layer to split functionality in two stores * add test to check scheme store behaviour * add scheme store on follow chain command
- Loading branch information
1 parent
ef80ae4
commit dd45899
Showing
4 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package beacon | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/drand/drand/common/scheme" | ||
|
||
"github.com/drand/drand/chain" | ||
"github.com/drand/drand/chain/boltdb" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSchemeStore(t *testing.T) { | ||
sch, _ := scheme.ReadSchemeByEnv() | ||
|
||
dir, err := os.MkdirTemp("", "*") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
bstore, err := boltdb.NewBoltStore(dir, nil) | ||
require.NoError(t, err) | ||
|
||
genesisBeacon := chain.GenesisBeacon(&chain.Info{GroupHash: []byte("genesis_signature")}) | ||
err = bstore.Put(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(newBeacon) | ||
require.NoError(t, err) | ||
|
||
beaconSaved, err := ss.Last() | ||
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(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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters