github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/common/consensus.go (about)

     1  package common
     2  
     3  import (
     4  	comm "github.com/sixexorg/magnetic-ring/common"
     5  	sink "github.com/sixexorg/magnetic-ring/common/sink"
     6  	"github.com/sixexorg/magnetic-ring/crypto"
     7  )
     8  
     9  type P2PConsPld struct {
    10  	Version         uint32
    11  	PrevHash        comm.Hash
    12  	Height          uint32
    13  	BookkeeperIndex uint16
    14  	Timestamp       uint32
    15  	Data            []byte
    16  	Owner           []byte
    17  	Signature       []byte
    18  	PeerId          uint64
    19  	hash            comm.Hash
    20  }
    21  
    22  type Consensus struct {
    23  	Cons ConsensusPayload
    24  }
    25  
    26  func ConsensusToP2PConsPld(cons *Consensus) *P2PConsPld{
    27  	res := new(P2PConsPld)
    28  	res.Version = cons.Cons.Version
    29  	res.PrevHash = cons.Cons.PrevHash
    30  	res.Height = cons.Cons.Height
    31  	res.BookkeeperIndex = cons.Cons.BookkeeperIndex
    32  	res.Timestamp = cons.Cons.Timestamp
    33  	res.Data = cons.Cons.Data
    34  	res.Owner = cons.Cons.Owner.Bytes()
    35  	res.Signature = cons.Cons.Signature
    36  	res.PeerId = cons.Cons.PeerId
    37  	res.hash = cons.Cons.hash
    38  
    39  	return res
    40  }
    41  
    42  func P2PConsPldToConsensus(cons *P2PConsPld) (*Consensus,error){
    43  	own,err := crypto.UnmarshalPubkey(cons.Owner) 
    44  	// keypair.DeserializePublicKey(cons.Owner)
    45  	if err != nil {
    46  		return nil,err
    47  	}
    48  	res := ConsensusPayload{
    49  		Version: cons.Version,
    50  		PrevHash: cons.PrevHash,
    51  		Height: cons.Height,
    52  		BookkeeperIndex: cons.BookkeeperIndex,
    53  		Timestamp: cons.Timestamp,
    54  		Data: cons.Data,
    55  		Owner: own,
    56  		Signature: cons.Signature,
    57  		PeerId: cons.PeerId,
    58  		hash: cons.hash,
    59  	}
    60  
    61  	return &Consensus{
    62  		Cons:res,
    63  	},nil
    64  }
    65  
    66  func (this *P2PConsPld) Serialization(sink *sink.ZeroCopySink) error {
    67  	return nil
    68  }
    69  
    70  func (this *P2PConsPld) CmdType() string {
    71  	return CONSENSUS_TYPE
    72  }
    73  
    74  //Deserialize message payload
    75  func (this *P2PConsPld) Deserialization(source *sink.ZeroCopySource) error {
    76  	return nil
    77  }
    78  
    79  //Serialize message payload
    80  func (this *Consensus) Serialization(sink *sink.ZeroCopySink) error {
    81  	return this.Cons.Serialization(sink)
    82  }
    83  
    84  func (this *Consensus) CmdType() string {
    85  	return CONSENSUS_TYPE
    86  }
    87  
    88  //Deserialize message payload
    89  func (this *Consensus) Deserialization(source *sink.ZeroCopySource) error {
    90  	return this.Cons.Deserialization(source)
    91  }
    92  
    93  type PeerAnn struct {
    94  	PeerId  uint64
    95  	Height  uint64
    96  }
    97  
    98  type NotifyBlk struct {
    99  	BlkHeight uint64
   100  	BlkHash   comm.Hash
   101  }