import (
"encoding/binary"
- "encoding/hex"
"encoding/json"
"time"
return nil, err
}
- setSupLinkToCheckpoint(checkpoint, header.SupLinks)
+ checkpoint.SupLinks = append(checkpoint.SupLinks, header.SupLinks...)
return checkpoint, nil
}
return nil, err
}
- setSupLinkToCheckpoint(checkpoint, header.SupLinks)
+ checkpoint.SupLinks = append(checkpoint.SupLinks, header.SupLinks...)
checkpoints = append(checkpoints, checkpoint)
}
return checkpoints, nil
}
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,
- })
- }
-}
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())
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)
}
TargetHash: targetHash,
SourceHeight: supLink.SourceHeight,
TargetHeight: targetHeight,
- Signature: hex.EncodeToString(signature),
+ Signature: signature,
PubKey: validatorList[i].PubKey,
})
}
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"
)
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)
}
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)}
}
}
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 {
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,
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}},
},
},
},
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
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,
})
}
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.
}
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,
})
}
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,
// 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:"-"`
}
}
// 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,
}
// 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
}
}
// 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
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],
})
}
}
TargetHash bc.Hash
SourceHeight uint64
TargetHeight uint64
- Signature string
+ Signature []byte
PubKey string
}
return err
}
- v.Signature = hex.EncodeToString(xPrv.Sign(message))
+ v.Signature = xPrv.Sign(message)
return nil
}
return err
}
- signature, err := hex.DecodeString(v.Signature)
- if err != nil {
- return err
- }
-
message, err := v.encodeMessage()
if err != nil {
return err
var xPub chainkd.XPub
copy(xPub[:], pubKey)
- if !xPub.Verify(message, signature) {
+ if !xPub.Verify(message, v.Signature) {
return errVerifySignature
}