Skip to content

Commit

Permalink
chore(lib/grandpa): integrate scale v2 into grandpa (#1811)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Sep 30, 2021
1 parent a51e5ab commit 33059ad
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 643 deletions.
2 changes: 1 addition & 1 deletion dot/rpc/modules/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestRoundState(t *testing.T) {

for _, k := range kr.Keys {
voters = append(voters, types.GrandpaVoter{
Key: k.Public().(*ed25519.PublicKey),
Key: *k.Public().(*ed25519.PublicKey),
ID: 1,
})
}
Expand Down
12 changes: 6 additions & 6 deletions dot/rpc/modules/mocks/block_finality_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -260,14 +261,14 @@ func TestGrandpaJustification_Listen(t *testing.T) {

mockedJust := grandpa.Justification{
Round: 1,
Commit: &grandpa.Commit{
Commit: grandpa.Commit{
Hash: common.Hash{},
Number: 1,
Precommits: nil,
},
}

mockedJustBytes, err := mockedJust.Encode()
mockedJustBytes, err := scale.Marshal(mockedJust)
require.NoError(t, err)

blockStateMock := new(mocks.MockBlockAPI)
Expand Down
5 changes: 3 additions & 2 deletions dot/rpc/subscription/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -221,14 +222,14 @@ func TestWSConn_HandleComm(t *testing.T) {
var fCh chan<- *types.FinalisationInfo
mockedJust := grandpa.Justification{
Round: 1,
Commit: &grandpa.Commit{
Commit: grandpa.Commit{
Hash: common.Hash{},
Number: 1,
Precommits: nil,
},
}

mockedJustBytes, err := mockedJust.Encode()
mockedJustBytes, err := scale.Marshal(mockedJust)
require.NoError(t, err)

BlockAPI := new(modulesmocks.MockBlockAPI)
Expand Down
26 changes: 14 additions & 12 deletions dot/state/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/pkg/scale"
)

var (
Expand Down Expand Up @@ -90,7 +90,7 @@ func setIDChangeKey(setID uint64) []byte {

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

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

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

pvs, err := scale.Decode(data, []*types.GrandpaSignedVote{})
pvs := []types.GrandpaSignedVote{}
err = scale.Unmarshal(data, &pvs)
if err != nil {
return nil, err
}

return pvs.([]*types.GrandpaSignedVote), nil
return pvs, nil
}

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

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

pcs, err := scale.Decode(data, []*types.GrandpaSignedVote{})
pcs := []types.GrandpaSignedVote{}
err = scale.Unmarshal(data, &pcs)
if err != nil {
return nil, err
}

return pcs.([]*types.GrandpaSignedVote), nil
return pcs, nil
}
16 changes: 4 additions & 12 deletions dot/state/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
var (
kr, _ = keystore.NewEd25519Keyring()
testAuths = []types.GrandpaVoter{
{Key: kr.Alice().Public().(*ed25519.PublicKey), ID: 0},
{Key: *kr.Alice().Public().(*ed25519.PublicKey), ID: 0},
}
)

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

testAuths2 := []types.GrandpaVoter{
{Key: kr.Bob().Public().(*ed25519.PublicKey), ID: 0},
}

err = gs.SetNextChange(testAuths2, big.NewInt(1))
err = gs.SetNextChange(testAuths, big.NewInt(1))
require.NoError(t, err)

auths, err := gs.GetAuthorities(genesisSetID + 1)
require.NoError(t, err)
require.Equal(t, testAuths2, auths)
require.Equal(t, testAuths, auths)

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

testAuths2 := []types.GrandpaVoter{
{Key: kr.Bob().Public().(*ed25519.PublicKey), ID: 0},
}

err = gs.SetNextChange(testAuths2, big.NewInt(100))
err = gs.SetNextChange(testAuths, big.NewInt(100))
require.NoError(t, err)

setID, err := gs.GetSetIDByBlockNumber(big.NewInt(50))
Expand Down
Loading

0 comments on commit 33059ad

Please sign in to comment.