OSDN Git Service

edit dup sup link struct (#1988)
authorPaladz <yzhu101@uottawa.ca>
Thu, 24 Jun 2021 04:24:05 +0000 (12:24 +0800)
committerGitHub <noreply@github.com>
Thu, 24 Jun 2021 04:24:05 +0000 (12:24 +0800)
Co-authored-by: paladz <colt@ColtdeMacBook-Pro.local>
database/store.go
netsync/consensusmgr/handle.go
protocol/apply_block.go
protocol/auth_verification.go
protocol/auth_verification_test.go
protocol/bc/types/sup_link.go
protocol/block.go
protocol/protocol.go
protocol/state/checkpoint.go
protocol/verfication.go

index d8682a0..70b6fed 100644 (file)
@@ -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,
-               })
-       }
-}
index ed29287..3a67dc3 100644 (file)
@@ -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())
index 2ee17de..07238a6 100644 (file)
@@ -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,
                })
        }
index 76a4480..a06a4e1 100644 (file)
@@ -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,
index 2e9dc9e..c6217b0 100644 (file)
@@ -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}},
                                },
                        },
                },
index be69564..4aff938 100644 (file)
@@ -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
index 088bcc1..242c718 100644 (file)
@@ -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,
        })
 }
 
index 36a3ad2..c895dc4 100644 (file)
@@ -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,
        })
 }
 
index 0576a03..1455a26 100644 (file)
@@ -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],
                        })
                }
        }
index 54bae5c..1e65345 100644 (file)
@@ -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
        }