Skip to content

Commit 33059ad

Browse files
authored
chore(lib/grandpa): integrate scale v2 into grandpa (#1811)
1 parent a51e5ab commit 33059ad

23 files changed

+521
-643
lines changed

dot/rpc/modules/grandpa_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestRoundState(t *testing.T) {
7070

7171
for _, k := range kr.Keys {
7272
voters = append(voters, types.GrandpaVoter{
73-
Key: k.Public().(*ed25519.PublicKey),
73+
Key: *k.Public().(*ed25519.PublicKey),
7474
ID: 1,
7575
})
7676
}

dot/rpc/modules/mocks/block_finality_api.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/subscription/listeners_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/ChainSafe/gossamer/lib/grandpa"
3838
"github.com/ChainSafe/gossamer/lib/runtime"
3939
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
40+
"github.com/ChainSafe/gossamer/pkg/scale"
4041
"github.com/gorilla/websocket"
4142
"github.com/stretchr/testify/mock"
4243
"github.com/stretchr/testify/require"
@@ -260,14 +261,14 @@ func TestGrandpaJustification_Listen(t *testing.T) {
260261

261262
mockedJust := grandpa.Justification{
262263
Round: 1,
263-
Commit: &grandpa.Commit{
264+
Commit: grandpa.Commit{
264265
Hash: common.Hash{},
265266
Number: 1,
266267
Precommits: nil,
267268
},
268269
}
269270

270-
mockedJustBytes, err := mockedJust.Encode()
271+
mockedJustBytes, err := scale.Marshal(mockedJust)
271272
require.NoError(t, err)
272273

273274
blockStateMock := new(mocks.MockBlockAPI)

dot/rpc/subscription/websocket_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ChainSafe/gossamer/dot/types"
1515
"github.com/ChainSafe/gossamer/lib/common"
1616
"github.com/ChainSafe/gossamer/lib/grandpa"
17+
"github.com/ChainSafe/gossamer/pkg/scale"
1718
"github.com/gorilla/websocket"
1819
"github.com/stretchr/testify/mock"
1920
"github.com/stretchr/testify/require"
@@ -221,14 +222,14 @@ func TestWSConn_HandleComm(t *testing.T) {
221222
var fCh chan<- *types.FinalisationInfo
222223
mockedJust := grandpa.Justification{
223224
Round: 1,
224-
Commit: &grandpa.Commit{
225+
Commit: grandpa.Commit{
225226
Hash: common.Hash{},
226227
Number: 1,
227228
Precommits: nil,
228229
},
229230
}
230231

231-
mockedJustBytes, err := mockedJust.Encode()
232+
mockedJustBytes, err := scale.Marshal(mockedJust)
232233
require.NoError(t, err)
233234

234235
BlockAPI := new(modulesmocks.MockBlockAPI)

dot/state/grandpa.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/ChainSafe/chaindb"
2626
"github.com/ChainSafe/gossamer/dot/types"
2727
"github.com/ChainSafe/gossamer/lib/common"
28-
"github.com/ChainSafe/gossamer/lib/scale"
28+
"github.com/ChainSafe/gossamer/pkg/scale"
2929
)
3030

3131
var (
@@ -90,7 +90,7 @@ func setIDChangeKey(setID uint64) []byte {
9090

9191
// setAuthorities sets the authorities for a given setID
9292
func (s *GrandpaState) setAuthorities(setID uint64, authorities []types.GrandpaVoter) error {
93-
enc, err := scale.Encode(authorities)
93+
enc, err := types.EncodeGrandpaVoters(authorities)
9494
if err != nil {
9595
return err
9696
}
@@ -306,8 +306,8 @@ func roundAndSetIDToBytes(round, setID uint64) []byte {
306306
}
307307

308308
// SetPrevotes sets the prevotes for a specific round and set ID in the database
309-
func (s *GrandpaState) SetPrevotes(round, setID uint64, pvs []*types.GrandpaSignedVote) error {
310-
data, err := scale.Encode(pvs)
309+
func (s *GrandpaState) SetPrevotes(round, setID uint64, pvs []types.GrandpaSignedVote) error {
310+
data, err := scale.Marshal(pvs)
311311
if err != nil {
312312
return err
313313
}
@@ -316,23 +316,24 @@ func (s *GrandpaState) SetPrevotes(round, setID uint64, pvs []*types.GrandpaSign
316316
}
317317

318318
// GetPrevotes retrieves the prevotes for a specific round and set ID from the database
319-
func (s *GrandpaState) GetPrevotes(round, setID uint64) ([]*types.GrandpaSignedVote, error) {
319+
func (s *GrandpaState) GetPrevotes(round, setID uint64) ([]types.GrandpaSignedVote, error) {
320320
data, err := s.db.Get(prevotesKey(round, setID))
321321
if err != nil {
322322
return nil, err
323323
}
324324

325-
pvs, err := scale.Decode(data, []*types.GrandpaSignedVote{})
325+
pvs := []types.GrandpaSignedVote{}
326+
err = scale.Unmarshal(data, &pvs)
326327
if err != nil {
327328
return nil, err
328329
}
329330

330-
return pvs.([]*types.GrandpaSignedVote), nil
331+
return pvs, nil
331332
}
332333

333334
// SetPrecommits sets the precommits for a specific round and set ID in the database
334-
func (s *GrandpaState) SetPrecommits(round, setID uint64, pcs []*types.GrandpaSignedVote) error {
335-
data, err := scale.Encode(pcs)
335+
func (s *GrandpaState) SetPrecommits(round, setID uint64, pcs []types.GrandpaSignedVote) error {
336+
data, err := scale.Marshal(pcs)
336337
if err != nil {
337338
return err
338339
}
@@ -341,16 +342,17 @@ func (s *GrandpaState) SetPrecommits(round, setID uint64, pcs []*types.GrandpaSi
341342
}
342343

343344
// GetPrecommits retrieves the precommits for a specific round and set ID from the database
344-
func (s *GrandpaState) GetPrecommits(round, setID uint64) ([]*types.GrandpaSignedVote, error) {
345+
func (s *GrandpaState) GetPrecommits(round, setID uint64) ([]types.GrandpaSignedVote, error) {
345346
data, err := s.db.Get(precommitsKey(round, setID))
346347
if err != nil {
347348
return nil, err
348349
}
349350

350-
pcs, err := scale.Decode(data, []*types.GrandpaSignedVote{})
351+
pcs := []types.GrandpaSignedVote{}
352+
err = scale.Unmarshal(data, &pcs)
351353
if err != nil {
352354
return nil, err
353355
}
354356

355-
return pcs.([]*types.GrandpaSignedVote), nil
357+
return pcs, nil
356358
}

dot/state/grandpa_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
var (
3131
kr, _ = keystore.NewEd25519Keyring()
3232
testAuths = []types.GrandpaVoter{
33-
{Key: kr.Alice().Public().(*ed25519.PublicKey), ID: 0},
33+
{Key: *kr.Alice().Public().(*ed25519.PublicKey), ID: 0},
3434
}
3535
)
3636

@@ -57,16 +57,12 @@ func TestGrandpaState_SetNextChange(t *testing.T) {
5757
gs, err := NewGrandpaStateFromGenesis(db, testAuths)
5858
require.NoError(t, err)
5959

60-
testAuths2 := []types.GrandpaVoter{
61-
{Key: kr.Bob().Public().(*ed25519.PublicKey), ID: 0},
62-
}
63-
64-
err = gs.SetNextChange(testAuths2, big.NewInt(1))
60+
err = gs.SetNextChange(testAuths, big.NewInt(1))
6561
require.NoError(t, err)
6662

6763
auths, err := gs.GetAuthorities(genesisSetID + 1)
6864
require.NoError(t, err)
69-
require.Equal(t, testAuths2, auths)
65+
require.Equal(t, testAuths, auths)
7066

7167
atBlock, err := gs.GetSetIDChange(genesisSetID + 1)
7268
require.NoError(t, err)
@@ -91,11 +87,7 @@ func TestGrandpaState_GetSetIDByBlockNumber(t *testing.T) {
9187
gs, err := NewGrandpaStateFromGenesis(db, testAuths)
9288
require.NoError(t, err)
9389

94-
testAuths2 := []types.GrandpaVoter{
95-
{Key: kr.Bob().Public().(*ed25519.PublicKey), ID: 0},
96-
}
97-
98-
err = gs.SetNextChange(testAuths2, big.NewInt(100))
90+
err = gs.SetNextChange(testAuths, big.NewInt(100))
9991
require.NoError(t, err)
10092

10193
setID, err := gs.GetSetIDByBlockNumber(big.NewInt(50))

0 commit comments

Comments
 (0)