Skip to content

Commit

Permalink
core/types/vote: introduce RawVoteEnvelop (axieinfinity#308)
Browse files Browse the repository at this point in the history
RawVoteEnvelop is the same as VoteEnvelop without cached hash
  • Loading branch information
minh-bq committed Sep 12, 2023
1 parent d23d0cb commit 827c622
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
16 changes: 13 additions & 3 deletions core/types/vote.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
"github.com/ethereum/go-ethereum/params"
"sync/atomic"

"github.com/ethereum/go-ethereum/params"

"github.com/ethereum/go-ethereum/crypto/bls"
"github.com/pkg/errors"

Expand All @@ -23,16 +24,25 @@ type VoteData struct {
// Hash returns the hash of the vote data.
func (d *VoteData) Hash() common.Hash { return rlpHash(d) }

// VoteEnvelope represents the vote of a single validator.
type VoteEnvelope struct {
// RawVoteEnvelope is VoteEnvelop without cached hash
type RawVoteEnvelope struct {
PublicKey BLSPublicKey // The BLS public key of the validator.
Signature BLSSignature // Validator's signature for the vote data.
Data *VoteData // The vote data for fast finality.
}

// VoteEnvelope represents the vote of a single validator.
type VoteEnvelope struct {
RawVoteEnvelope

// caches
hash atomic.Value
}

func (v *VoteEnvelope) Raw() *RawVoteEnvelope {
return &v.RawVoteEnvelope
}

// Hash returns the vote's hash.
func (v *VoteEnvelope) Hash() common.Hash {
if hash := v.hash.Load(); hash != nil {
Expand Down
4 changes: 3 additions & 1 deletion core/vote/vote_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func (voteManager *VoteManager) loop() {
TargetHash: curHead.Hash(),
}
voteMessage := &types.VoteEnvelope{
Data: vote,
RawVoteEnvelope: types.RawVoteEnvelope{
Data: vote,
},
}

// Put Vote into journal and VotesPool if we are active validator and allow to sign it.
Expand Down
26 changes: 17 additions & 9 deletions core/vote/vote_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ func testVotePool(t *testing.T, isValidRules bool) {

// Test invalid vote whose number larger than latestHeader + 13
invalidVote := &types.VoteEnvelope{
Data: &types.VoteData{
TargetNumber: 1000,
RawVoteEnvelope: types.RawVoteEnvelope{
Data: &types.VoteData{
TargetNumber: 1000,
},
},
}
voteManager.pool.PutVote(invalidVote)
Expand All @@ -220,8 +222,10 @@ func testVotePool(t *testing.T, isValidRules bool) {

// Test future votes scenario: votes number within latestBlockHeader ~ latestBlockHeader + 13
futureVote := &types.VoteEnvelope{
Data: &types.VoteData{
TargetNumber: 279,
RawVoteEnvelope: types.RawVoteEnvelope{
Data: &types.VoteData{
TargetNumber: 279,
},
},
}
if err := voteManager.signer.SignVote(futureVote); err != nil {
Expand All @@ -235,8 +239,10 @@ func testVotePool(t *testing.T, isValidRules bool) {

// Test duplicate vote case, shouldn'd be put into vote pool
duplicateVote := &types.VoteEnvelope{
Data: &types.VoteData{
TargetNumber: 279,
RawVoteEnvelope: types.RawVoteEnvelope{
Data: &types.VoteData{
TargetNumber: 279,
},
},
}
if err := voteManager.signer.SignVote(duplicateVote); err != nil {
Expand All @@ -250,9 +256,11 @@ func testVotePool(t *testing.T, isValidRules bool) {

// Test future votes larger than latestBlockNumber + 13 should be rejected
futureVote = &types.VoteEnvelope{
Data: &types.VoteData{
TargetNumber: 282,
TargetHash: common.Hash{},
RawVoteEnvelope: types.RawVoteEnvelope{
Data: &types.VoteData{
TargetNumber: 282,
TargetHash: common.Hash{},
},
},
}
voteManager.pool.PutVote(futureVote)
Expand Down

0 comments on commit 827c622

Please sign in to comment.