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

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/sixexorg/magnetic-ring/crypto"	
     9  	sink "github.com/sixexorg/magnetic-ring/common/sink"
    10  	"github.com/sixexorg/magnetic-ring/common"
    11  	"github.com/sixexorg/magnetic-ring/errors"
    12  	"github.com/sixexorg/magnetic-ring/common/serialization"	
    13  	"github.com/sixexorg/magnetic-ring/log"
    14  )
    15  
    16  type ConsensusPayload struct {
    17  	Version         uint32
    18  	PrevHash        common.Hash
    19  	Height          uint32
    20  	BookkeeperIndex uint16
    21  	Timestamp       uint32
    22  	Data            []byte
    23  	Owner           crypto.PublicKey
    24  	Signature       []byte
    25  	PeerId          uint64
    26  	hash            common.Hash
    27  }
    28  
    29  //get the consensus payload hash
    30  func (this *ConsensusPayload) Hash() common.Hash {
    31  	return common.Hash{}
    32  }
    33  
    34  //Check whether header is correct
    35  func (this *ConsensusPayload) Verify() error {
    36  	buf := new(bytes.Buffer)
    37  	err := this.SerializeUnsigned(buf)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	_,err = this.Owner.Verify(common.Sha256(buf.Bytes()), this.Signature)
    42  	// err = signature.Verify(this.Owner, buf.Bytes(), this.Signature)
    43  	if err != nil {
    44  		return errors.NewDetailErr(err, errors.ErrNetVerifyFail, fmt.Sprintf("signature verify error. buf:%v", buf))
    45  	}
    46  	return nil
    47  }
    48  
    49  //serialize the consensus payload
    50  func (this *ConsensusPayload) ToArray() []byte {
    51  	b := new(bytes.Buffer)
    52  	err := this.Serialize(b)
    53  	if err != nil {
    54  		log.Error("consensus payload serialize error in ToArray()","payload", this)
    55  		return nil
    56  	}
    57  	return b.Bytes()
    58  }
    59  
    60  //return inventory type
    61  func (this *ConsensusPayload) InventoryType() common.InventoryType {
    62  	return common.CONSENSUS
    63  }
    64  
    65  func (this *ConsensusPayload) GetMessage() []byte {
    66  	//TODO: GetMessage
    67  	//return sig.GetHashData(cp)
    68  	return []byte{}
    69  }
    70  
    71  func (this *ConsensusPayload) Type() common.InventoryType {
    72  
    73  	//TODO:Temporary add for Interface signature.SignableData use.
    74  	return common.CONSENSUS
    75  }
    76  
    77  func (this *ConsensusPayload) Serialization(sink *sink.ZeroCopySink) error {
    78  	this.serializationUnsigned(sink)
    79  	// buf := keypair.SerializePublicKey(this.Owner)
    80  	buf := this.Owner.Bytes()
    81  	sink.WriteVarBytes(buf)
    82  	sink.WriteVarBytes(this.Signature)
    83  
    84  	return nil
    85  }
    86  
    87  //Serialize message payload
    88  func (this *ConsensusPayload) Serialize(w io.Writer) error {
    89  	err := this.SerializeUnsigned(w)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	// buf := keypair.SerializePublicKey(this.Owner)
    94  	buf := this.Owner.Bytes()
    95  	err = serialization.WriteVarBytes(w, buf)
    96  	if err != nil {
    97  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write publickey error. publickey buf:%v", buf))
    98  	}
    99  
   100  	err = serialization.WriteVarBytes(w, this.Signature)
   101  	if err != nil {
   102  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write Signature error. Signature:%v", this.Signature))
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  //Deserialize message payload
   109  func (this *ConsensusPayload) Deserialization(source *sink.ZeroCopySource) error {
   110  	err := this.deserializationUnsigned(source)
   111  	if err != nil {
   112  		return err
   113  	}
   114  	buf, _, irregular, eof := source.NextVarBytes()
   115  	if eof {
   116  		return io.ErrUnexpectedEOF
   117  	}
   118  	if irregular {
   119  		return sink.ErrIrregularData
   120  	}
   121  
   122  	// this.Owner, err = keypair.DeserializePublicKey(buf)
   123  	this.Owner, err = crypto.UnmarshalPubkey(buf) 
   124  	if err != nil {
   125  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "deserialize publickey error")
   126  	}
   127  
   128  	this.Signature, _, irregular, eof = source.NextVarBytes()
   129  	if irregular {
   130  		return sink.ErrIrregularData
   131  	}
   132  	if eof {
   133  		return io.ErrUnexpectedEOF
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  //Deserialize message payload
   140  func (this *ConsensusPayload) Deserialize(r io.Reader) error {
   141  	err := this.DeserializeUnsigned(r)
   142  	if err != nil {
   143  		return err
   144  	}
   145  	buf, err := serialization.ReadVarBytes(r)
   146  	if err != nil {
   147  
   148  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read buf error")
   149  	}
   150  	// this.Owner, err = keypair.DeserializePublicKey(buf)
   151  	this.Owner, err = crypto.UnmarshalPubkey(buf)
   152  	if err != nil {
   153  
   154  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "deserialize publickey error")
   155  	}
   156  
   157  	this.Signature, err = serialization.ReadVarBytes(r)
   158  	if err != nil {
   159  
   160  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Signature error")
   161  	}
   162  
   163  	return err
   164  }
   165  
   166  func (this *ConsensusPayload) serializationUnsigned(sink *sink.ZeroCopySink) {
   167  	sink.WriteUint32(this.Version)
   168  	sink.WriteHash(this.PrevHash)
   169  	sink.WriteUint32(this.Height)
   170  	sink.WriteUint16(this.BookkeeperIndex)
   171  	sink.WriteUint32(this.Timestamp)
   172  	sink.WriteVarBytes(this.Data)
   173  }
   174  
   175  //Serialize message payload
   176  func (this *ConsensusPayload) SerializeUnsigned(w io.Writer) error {
   177  	err := serialization.WriteUint32(w, this.Version)
   178  	if err != nil {
   179  
   180  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. version:%v", this.Version))
   181  	}
   182  	err = this.PrevHash.Serialize(w)
   183  	if err != nil {
   184  
   185  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("serialize error. PrevHash:%v", this.PrevHash))
   186  	}
   187  	err = serialization.WriteUint32(w, this.Height)
   188  	if err != nil {
   189  
   190  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Height:%v", this.Height))
   191  	}
   192  	err = serialization.WriteUint16(w, this.BookkeeperIndex)
   193  	if err != nil {
   194  
   195  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. BookkeeperIndex:%v", this.BookkeeperIndex))
   196  	}
   197  	err = serialization.WriteUint32(w, this.Timestamp)
   198  	if err != nil {
   199  
   200  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Timestamp:%v", this.Timestamp))
   201  	}
   202  	err = serialization.WriteVarBytes(w, this.Data)
   203  	if err != nil {
   204  
   205  		return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Data:%v", this.Data))
   206  	}
   207  	return nil
   208  }
   209  
   210  func (this *ConsensusPayload) deserializationUnsigned(source *sink.ZeroCopySource) error {
   211  	var irregular, eof bool
   212  	this.Version, eof = source.NextUint32()
   213  	this.PrevHash, eof = source.NextHash()
   214  	this.Height, eof = source.NextUint32()
   215  	this.BookkeeperIndex, eof = source.NextUint16()
   216  	this.Timestamp, eof = source.NextUint32()
   217  	this.Data, _, irregular, eof = source.NextVarBytes()
   218  	if eof {
   219  		return io.ErrUnexpectedEOF
   220  	}
   221  	if irregular {
   222  		return sink.ErrIrregularData
   223  	}
   224  
   225  	return nil
   226  }
   227  
   228  //Deserialize message payload
   229  func (this *ConsensusPayload) DeserializeUnsigned(r io.Reader) error {
   230  	var err error
   231  	this.Version, err = serialization.ReadUint32(r)
   232  	if err != nil {
   233  
   234  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read version error")
   235  	}
   236  
   237  	preBlock := new(common.Hash)
   238  	err = preBlock.Deserialize(r)
   239  	if err != nil {
   240  
   241  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read preBlock error")
   242  	}
   243  	this.PrevHash = *preBlock
   244  
   245  	this.Height, err = serialization.ReadUint32(r)
   246  	if err != nil {
   247  
   248  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Height error")
   249  	}
   250  
   251  	this.BookkeeperIndex, err = serialization.ReadUint16(r)
   252  	if err != nil {
   253  
   254  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read BookkeeperIndex error")
   255  	}
   256  
   257  	this.Timestamp, err = serialization.ReadUint32(r)
   258  	if err != nil {
   259  
   260  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Timestamp error")
   261  	}
   262  
   263  	this.Data, err = serialization.ReadVarBytes(r)
   264  	if err != nil {
   265  
   266  		return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Data error")
   267  	}
   268  
   269  	return nil
   270  }