-
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.
DKG migration tool from v1.* to v2.0.0 (#1215)
* DKG migration tool from v1.* to v2.0.0 * implemented the migration * added a CLI command for running it automagically for each beaconID * reload beacon upon migration * added integration test for restoring node state from migration
- Loading branch information
1 parent
488f0a1
commit 9969ec6
Showing
17 changed files
with
500 additions
and
52 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
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
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,183 @@ | ||
package dkg | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/drand/drand/crypto" | ||
"github.com/drand/drand/key" | ||
"github.com/drand/kyber" | ||
"github.com/drand/kyber/share" | ||
"github.com/drand/kyber/share/dkg" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNilGroupFails(t *testing.T) { | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
err = store.MigrateFromGroupfile("some-beacon", nil, fakeShare()) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestNilKeyShareFails(t *testing.T) { | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
group := fakeGroup() | ||
|
||
err = store.MigrateFromGroupfile("some-beacon", group, nil) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestEmptyBeaconIDFails(t *testing.T) { | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
keyShare := fakeShare() | ||
group := fakeGroup() | ||
|
||
err = store.MigrateFromGroupfile("", group, keyShare) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestStateAlreadyInDBForBeaconIDFails(t *testing.T) { | ||
// create a new store | ||
beaconID := "banana" | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
// save an existing state in it | ||
now := time.Now() | ||
err = store.SaveFinished(beaconID, &DBState{ | ||
BeaconID: beaconID, | ||
Epoch: 1, | ||
State: Complete, | ||
Threshold: 1, | ||
Timeout: now, | ||
SchemeID: crypto.DefaultSchemeID, | ||
GenesisTime: now, | ||
GenesisSeed: []byte("deadbeef"), | ||
TransitionTime: now, | ||
CatchupPeriod: 1, | ||
BeaconPeriod: 3, | ||
Leader: nil, | ||
Remaining: nil, | ||
Joining: nil, | ||
Leaving: nil, | ||
Acceptors: nil, | ||
Rejectors: nil, | ||
FinalGroup: nil, | ||
KeyShare: nil, | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = store.MigrateFromGroupfile(beaconID, fakeGroup(), fakeShare()) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestStateInDBForDifferentBeaconIDDoesntFail(t *testing.T) { | ||
// create a new store | ||
beaconID := "banana" | ||
aDifferentBeaconID := "different-beacon-id" | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
// save an existing state but for a differen beacon ID | ||
now := time.Now() | ||
err = store.SaveFinished(aDifferentBeaconID, &DBState{ | ||
BeaconID: aDifferentBeaconID, | ||
Epoch: 1, | ||
State: Complete, | ||
Threshold: 1, | ||
Timeout: now, | ||
SchemeID: crypto.DefaultSchemeID, | ||
GenesisTime: now, | ||
GenesisSeed: []byte("deadbeef"), | ||
TransitionTime: now, | ||
CatchupPeriod: 1, | ||
BeaconPeriod: 3, | ||
Leader: nil, | ||
Remaining: nil, | ||
Joining: nil, | ||
Leaving: nil, | ||
Acceptors: nil, | ||
Rejectors: nil, | ||
FinalGroup: nil, | ||
KeyShare: nil, | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = store.MigrateFromGroupfile(beaconID, fakeGroup(), fakeShare()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestValidMigrationIsRetrievable(t *testing.T) { | ||
// create a new store | ||
beaconID := "banana" | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
// perform the migration | ||
err = store.MigrateFromGroupfile(beaconID, fakeGroup(), fakeShare()) | ||
require.NoError(t, err) | ||
|
||
// get the finished migrated state and check some of its fields | ||
state, err := store.GetFinished(beaconID) | ||
require.NoError(t, err) | ||
require.Equal(t, state.BeaconID, beaconID) | ||
require.Equal(t, state.State, Complete) | ||
} | ||
|
||
func TestInvalidMigrationIsNotRetrievable(t *testing.T) { | ||
// create a new store | ||
beaconID := "banana" | ||
store, err := NewDKGStore(t.TempDir(), nil) | ||
require.NoError(t, err) | ||
|
||
// perform an invalid migration | ||
err = store.MigrateFromGroupfile(beaconID, nil, nil) | ||
require.Error(t, err) | ||
|
||
// get the finished migrated state and check some of its fields | ||
state, err := store.GetFinished(beaconID) | ||
require.NoError(t, err) | ||
require.Nil(t, state) | ||
} | ||
|
||
func fakeShare() *key.Share { | ||
sch := crypto.NewPedersenBLSChained() | ||
scalarOne := sch.KeyGroup.Scalar().One() | ||
s := &share.PriShare{I: 2, V: scalarOne} | ||
return &key.Share{DistKeyShare: dkg.DistKeyShare{Share: s}, Scheme: sch} | ||
} | ||
|
||
func fakeGroup() *key.Group { | ||
sch := crypto.NewPedersenBLSChained() | ||
return &key.Group{ | ||
Threshold: 1, | ||
Period: 3, | ||
Scheme: sch, | ||
ID: "default", | ||
CatchupPeriod: 2, | ||
Nodes: []*key.Node{{ | ||
Index: 0, | ||
Identity: &key.Identity{ | ||
Key: sch.KeyGroup.Point(), | ||
Addr: "localhost:1234", | ||
TLS: false, | ||
Signature: []byte("abcd1234"), | ||
Scheme: sch, | ||
}, | ||
}}, | ||
GenesisTime: time.Now().Unix(), | ||
GenesisSeed: []byte("deadbeef"), | ||
TransitionTime: time.Now().Unix(), | ||
PublicKey: fakePublic(), | ||
} | ||
} | ||
|
||
func fakePublic() *key.DistPublic { | ||
sch := crypto.NewPedersenBLSChained() | ||
return &key.DistPublic{Coefficients: []kyber.Point{sch.KeyGroup.Point()}} | ||
} |
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
Oops, something went wrong.