From: Paladz Date: Thu, 24 Jun 2021 04:24:05 +0000 (+0800) Subject: edit dup sup link struct (#1988) X-Git-Tag: bytom2-prerelease~1^2~87 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;ds=sidebyside;h=98c3baac82cb06ce60cb06b146f6375af2d9d141;p=bytom%2Fbytom.git edit dup sup link struct (#1988) Co-authored-by: paladz --- diff --git a/database/store.go b/database/store.go index d8682a04..70b6fed5 100644 --- a/database/store.go +++ b/database/store.go @@ -2,7 +2,6 @@ package database import ( "encoding/binary" - "encoding/hex" "encoding/json" "time" @@ -269,7 +268,7 @@ func (s *Store) GetCheckpoint(hash *bc.Hash) (*state.Checkpoint, error) { return nil, err } - setSupLinkToCheckpoint(checkpoint, header.SupLinks) + checkpoint.SupLinks = append(checkpoint.SupLinks, header.SupLinks...) return checkpoint, nil } @@ -314,7 +313,7 @@ func (s *Store) loadCheckpointsFromIter(iter dbm.Iterator) ([]*state.Checkpoint, return nil, err } - setSupLinkToCheckpoint(checkpoint, header.SupLinks) + checkpoint.SupLinks = append(checkpoint.SupLinks, header.SupLinks...) checkpoints = append(checkpoints, checkpoint) } return checkpoints, nil @@ -360,18 +359,3 @@ func (s *Store) saveCheckpoints(batch dbm.Batch, checkpoints []*state.Checkpoint } return nil } - -func setSupLinkToCheckpoint(c *state.Checkpoint, supLinks types.SupLinks) { - for _, supLink := range supLinks { - var signatures [consensus.MaxNumOfValidators]string - for i, signature := range supLink.Signatures { - signatures[i] = hex.EncodeToString(signature) - } - - c.SupLinks = append(c.SupLinks, &state.SupLink{ - SourceHeight: supLink.SourceHeight, - SourceHash: supLink.SourceHash, - Signatures: signatures, - }) - } -} diff --git a/netsync/consensusmgr/handle.go b/netsync/consensusmgr/handle.go index ed29287c..3a67dc3d 100644 --- a/netsync/consensusmgr/handle.go +++ b/netsync/consensusmgr/handle.go @@ -114,7 +114,7 @@ func (m *Manager) handleBlockVerificationMsg(peerID string, msg *BlockVerificati TargetHash: msg.TargetHash, SourceHeight: msg.SourceHeight, TargetHeight: msg.TargetHeight, - Signature: hex.EncodeToString(msg.Signature), + Signature: msg.Signature, PubKey: hex.EncodeToString(msg.PubKey), }); err != nil { m.peers.ProcessIllegal(peerID, security.LevelMsgIllegal, err.Error()) diff --git a/protocol/apply_block.go b/protocol/apply_block.go index 2ee17dea..07238a69 100644 --- a/protocol/apply_block.go +++ b/protocol/apply_block.go @@ -195,12 +195,7 @@ func (c *Casper) applyMyVerification(target *state.Checkpoint, block *types.Bloc return nil, nil } - signature, err := hex.DecodeString(v.Signature) - if err != nil { - return nil, err - } - - block.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, signature, validators[v.PubKey].Order) + block.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, v.Signature, validators[v.PubKey].Order) return v, c.store.SaveBlockHeader(&block.BlockHeader) } @@ -313,7 +308,7 @@ func supLinkToVerifications(supLink *types.SupLink, validators map[string]*state TargetHash: targetHash, SourceHeight: supLink.SourceHeight, TargetHeight: targetHeight, - Signature: hex.EncodeToString(signature), + Signature: signature, PubKey: validatorList[i].PubKey, }) } diff --git a/protocol/auth_verification.go b/protocol/auth_verification.go index 76a44806..a06a4e19 100644 --- a/protocol/auth_verification.go +++ b/protocol/auth_verification.go @@ -1,13 +1,13 @@ package protocol import ( - "encoding/hex" "fmt" log "github.com/sirupsen/logrus" "github.com/bytom/bytom/consensus" "github.com/bytom/bytom/protocol/bc" + "github.com/bytom/bytom/protocol/bc/types" "github.com/bytom/bytom/protocol/state" ) @@ -100,12 +100,7 @@ func (c *Casper) saveVerificationToHeader(v *Verification, validatorOrder int) e return err } - signature, err := hex.DecodeString(v.Signature) - if err != nil { - return err - } - - blockHeader.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, signature, validatorOrder) + blockHeader.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, v.Signature, validatorOrder) return c.store.SaveBlockHeader(blockHeader) } @@ -191,7 +186,7 @@ func (c *Casper) verifySameHeight(v *Verification, validatorOrder int, trackEvil for _, checkpoint := range checkpoints { for _, supLink := range checkpoint.SupLinks { - if supLink.Signatures[validatorOrder] != "" && checkpoint.Hash != v.TargetHash { + if len(supLink.Signatures[validatorOrder]) != 0 && checkpoint.Hash != v.TargetHash { if trackEvilValidator { c.evilValidators[v.PubKey] = []*Verification{v, makeVerification(supLink, checkpoint, v.PubKey, validatorOrder)} } @@ -210,7 +205,7 @@ func (c *Casper) verifySpanHeight(v *Verification, validatorOrder int, trackEvil } for _, supLink := range checkpoint.SupLinks { - if supLink.Signatures[validatorOrder] != "" { + if len(supLink.Signatures[validatorOrder]) != 0 { if (checkpoint.Height < v.TargetHeight && supLink.SourceHeight > v.SourceHeight) || (checkpoint.Height > v.TargetHeight && supLink.SourceHeight < v.SourceHeight) { if trackEvilValidator { @@ -227,7 +222,7 @@ func (c *Casper) verifySpanHeight(v *Verification, validatorOrder int, trackEvil return nil } -func makeVerification(supLink *state.SupLink, checkpoint *state.Checkpoint, pubKey string, validatorOrder int) *Verification { +func makeVerification(supLink *types.SupLink, checkpoint *state.Checkpoint, pubKey string, validatorOrder int) *Verification { return &Verification{ SourceHash: supLink.SourceHash, TargetHash: checkpoint.Hash, diff --git a/protocol/auth_verification_test.go b/protocol/auth_verification_test.go index 2e9dc9e7..c6217b07 100644 --- a/protocol/auth_verification_test.go +++ b/protocol/auth_verification_test.go @@ -32,11 +32,11 @@ var ( ParentHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"), Hash: testutil.MustDecodeHash("104da9966d3ef81d20a67a0aa79d3979b2542adeb52666deff64f63ecbfe2535"), Status: state.Unjustified, - SupLinks: []*state.SupLink{ + SupLinks: []*types.SupLink{ { SourceHeight: 0, SourceHash: testutil.MustDecodeHash("a1770c17493b87c43e85c2ab811023a8566907838c2237e7d72071c5f4713c5b"), - Signatures: [consensus.MaxNumOfValidators]string{"aaa"}, + Signatures: [consensus.MaxNumOfValidators][]byte{[]byte{0xaa}}, }, }, }, diff --git a/protocol/bc/types/sup_link.go b/protocol/bc/types/sup_link.go index be69564e..4aff938c 100644 --- a/protocol/bc/types/sup_link.go +++ b/protocol/bc/types/sup_link.go @@ -65,6 +65,17 @@ type SupLink struct { Signatures [consensus.MaxNumOfValidators][]byte } +// IsMajority if at least 2/3 of validators have published votes with sup link +func (s *SupLink) IsMajority(numOfValidators int) bool { + numOfSignatures := 0 + for _, signature := range s.Signatures { + if len(signature) >= 0 { + numOfSignatures++ + } + } + return numOfSignatures > numOfValidators*2/3 +} + func (s *SupLink) readFrom(r *blockchain.Reader) (err error) { if s.SourceHeight, err = blockchain.ReadVarint63(r); err != nil { return err diff --git a/protocol/block.go b/protocol/block.go index 088bcc15..242c7186 100644 --- a/protocol/block.go +++ b/protocol/block.go @@ -226,18 +226,13 @@ func (c *Chain) broadcastVerification(v *Verification) error { return err } - signature, err := hex.DecodeString(v.Signature) - if err != nil { - return err - } - return c.eventDispatcher.Post(event.BlockVerificationEvent{ SourceHeight: v.SourceHeight, SourceHash: v.SourceHash, TargetHeight: v.TargetHeight, TargetHash: v.TargetHash, PubKey: pubKey, - Signature: signature, + Signature: v.Signature, }) } diff --git a/protocol/protocol.go b/protocol/protocol.go index 36a3ad2a..c895dc4f 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -30,8 +30,8 @@ type Chain struct { processRollbackCh chan *rollbackMsg eventDispatcher *event.Dispatcher - cond sync.Cond - bestBlockHeader *types.BlockHeader // the last block on current main chain + cond sync.Cond + bestBlockHeader *types.BlockHeader // the last block on current main chain } // NewChain returns a new Chain using store as the underlying storage. @@ -124,14 +124,13 @@ func (c *Chain) ProcessBlockVerification(v *Verification) error { } pubKey, _ := hex.DecodeString(v.PubKey) - signature, _ := hex.DecodeString(v.Signature) return c.eventDispatcher.Post(event.BlockVerificationEvent{ SourceHeight: v.SourceHeight, SourceHash: v.SourceHash, TargetHeight: v.TargetHeight, TargetHash: v.TargetHash, PubKey: pubKey, - Signature: signature, + Signature: v.Signature, }) } diff --git a/protocol/state/checkpoint.go b/protocol/state/checkpoint.go index 0576a03d..1455a26e 100644 --- a/protocol/state/checkpoint.go +++ b/protocol/state/checkpoint.go @@ -29,25 +29,6 @@ const ( var errIncreaseCheckpoint = errors.New("invalid block for increase checkpoint") -// SupLink is an ordered pair of checkpoints (a, b), also written a → b, -// such that at least 2/3 of validators have published votes with source a and target b. -type SupLink struct { - SourceHeight uint64 - SourceHash bc.Hash - Signatures [consensus.MaxNumOfValidators]string -} - -// IsMajority if at least 2/3 of validators have published votes with sup link -func (s *SupLink) IsMajority(numOfValidators int) bool { - numOfSignatures := 0 - for _, signature := range s.Signatures { - if signature != "" { - numOfSignatures++ - } - } - return numOfSignatures > numOfValidators*2/3 -} - // Checkpoint represent the block/hash under consideration for finality for a given epoch. // This block is the last block of the previous epoch. Rather than dealing with every block, // Casper only considers checkpoints for finalization. When a checkpoint is explicitly finalized, @@ -59,11 +40,11 @@ type Checkpoint struct { // only save in the memory, not be persisted Parent *Checkpoint `json:"-"` Timestamp uint64 - SupLinks []*SupLink `json:"-"` + SupLinks []*types.SupLink `json:"-"` Status CheckpointStatus - Rewards map[string]uint64 // controlProgram -> num of reward - Votes map[string]uint64 // pubKey -> num of vote + Rewards map[string]uint64 // controlProgram -> num of reward + Votes map[string]uint64 // pubKey -> num of vote MergeCheckpoint func(bc.Hash) `json:"-"` } @@ -85,14 +66,15 @@ func NewCheckpoint(parent *Checkpoint) *Checkpoint { } // AddVerification add a valid verification to checkpoint's supLink -func (c *Checkpoint) AddVerification(sourceHash bc.Hash, sourceHeight uint64, validatorOrder int, signature string) *SupLink { +func (c *Checkpoint) AddVerification(sourceHash bc.Hash, sourceHeight uint64, validatorOrder int, signature []byte) *types.SupLink { for _, supLink := range c.SupLinks { if supLink.SourceHash == sourceHash { supLink.Signatures[validatorOrder] = signature return supLink } } - supLink := &SupLink{ + + supLink := &types.SupLink{ SourceHeight: sourceHeight, SourceHash: sourceHash, } @@ -105,7 +87,7 @@ func (c *Checkpoint) AddVerification(sourceHash bc.Hash, sourceHeight uint64, va // sourceHash not as filter if is nil, func (c *Checkpoint) ContainsVerification(validatorOrder int, sourceHash *bc.Hash) bool { for _, supLink := range c.SupLinks { - if (sourceHash == nil || supLink.SourceHash == *sourceHash) && supLink.Signatures[validatorOrder] != "" { + if (sourceHash == nil || supLink.SourceHash == *sourceHash) && len(supLink.Signatures[validatorOrder]) != 0 { return true } } @@ -134,9 +116,9 @@ func (c *Checkpoint) Increase(block *types.Block) error { // Validator represent the participants of the PoS network // Responsible for block generation and verification type Validator struct { - PubKey string - Order int - Vote uint64 + PubKey string + Order int + Vote uint64 } // Validators return next epoch of validators, if the status of checkpoint is growing, return empty @@ -149,8 +131,8 @@ func (c *Checkpoint) Validators() map[string]*Validator { for pubKey, voteNum := range c.Votes { if voteNum >= consensus.ActiveNetParams.MinValidatorVoteNum { validators = append(validators, &Validator{ - PubKey: pubKey, - Vote: c.Votes[pubKey], + PubKey: pubKey, + Vote: c.Votes[pubKey], }) } } diff --git a/protocol/verfication.go b/protocol/verfication.go index 54bae5c5..1e65345f 100644 --- a/protocol/verfication.go +++ b/protocol/verfication.go @@ -21,7 +21,7 @@ type Verification struct { TargetHash bc.Hash SourceHeight uint64 TargetHeight uint64 - Signature string + Signature []byte PubKey string } @@ -32,7 +32,7 @@ func (v *Verification) Sign(xPrv chainkd.XPrv) error { return err } - v.Signature = hex.EncodeToString(xPrv.Sign(message)) + v.Signature = xPrv.Sign(message) return nil } @@ -43,11 +43,6 @@ func (v *Verification) VerifySignature() error { return err } - signature, err := hex.DecodeString(v.Signature) - if err != nil { - return err - } - message, err := v.encodeMessage() if err != nil { return err @@ -55,7 +50,7 @@ func (v *Verification) VerifySignature() error { var xPub chainkd.XPub copy(xPub[:], pubKey) - if !xPub.Verify(message, signature) { + if !xPub.Verify(message, v.Signature) { return errVerifySignature }