-
Notifications
You must be signed in to change notification settings - Fork 14
/
consensus_payload.go
145 lines (125 loc) · 3.89 KB
/
consensus_payload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2019 The TesraSupernet Authors
* This file is part of The TesraSupernet library.
*
* The TesraSupernet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The TesraSupernet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The TesraSupernet. If not, see <http://www.gnu.org/licenses/>.
*/
package types
import (
"fmt"
"io"
"github.com/TesraSupernet/tesracrypto/keypair"
"github.com/TesraSupernet/Tesra/common"
"github.com/TesraSupernet/Tesra/core/signature"
"github.com/TesraSupernet/Tesra/errors"
)
type ConsensusPayload struct {
Version uint32
PrevHash common.Uint256
Height uint32
BookkeeperIndex uint16
Timestamp uint32
Data []byte
Owner keypair.PublicKey
Signature []byte
PeerId uint64
hash common.Uint256
}
//get the consensus payload hash
func (this *ConsensusPayload) Hash() common.Uint256 {
return common.Uint256{}
}
//Check whether header is correct
func (this *ConsensusPayload) Verify() error {
sink := common.NewZeroCopySink(nil)
this.SerializationUnsigned(sink)
err := signature.Verify(this.Owner, sink.Bytes(), this.Signature)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetVerifyFail, fmt.Sprintf("signature verify error. buf:%v", sink.Bytes()))
}
return nil
}
//serialize the consensus payload
func (this *ConsensusPayload) ToArray() []byte {
return common.SerializeToBytes(this)
}
//return inventory type
func (this *ConsensusPayload) InventoryType() common.InventoryType {
return common.CONSENSUS
}
func (this *ConsensusPayload) GetMessage() []byte {
//TODO: GetMessage
//return sig.GetHashData(cp)
return []byte{}
}
func (this *ConsensusPayload) Type() common.InventoryType {
//TODO:Temporary add for Interface signature.SignableData use.
return common.CONSENSUS
}
func (this *ConsensusPayload) Serialization(sink *common.ZeroCopySink) {
this.SerializationUnsigned(sink)
buf := keypair.SerializePublicKey(this.Owner)
sink.WriteVarBytes(buf)
sink.WriteVarBytes(this.Signature)
}
//Deserialize message payload
func (this *ConsensusPayload) Deserialization(source *common.ZeroCopySource) error {
err := this.DeserializationUnsigned(source)
if err != nil {
return err
}
buf, _, irregular, eof := source.NextVarBytes()
if eof {
return io.ErrUnexpectedEOF
}
if irregular {
return common.ErrIrregularData
}
this.Owner, err = keypair.DeserializePublicKey(buf)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "deserialize publickey error")
}
this.Signature, _, irregular, eof = source.NextVarBytes()
if irregular {
return common.ErrIrregularData
}
if eof {
return io.ErrUnexpectedEOF
}
return nil
}
func (this *ConsensusPayload) SerializationUnsigned(sink *common.ZeroCopySink) {
sink.WriteUint32(this.Version)
sink.WriteHash(this.PrevHash)
sink.WriteUint32(this.Height)
sink.WriteUint16(this.BookkeeperIndex)
sink.WriteUint32(this.Timestamp)
sink.WriteVarBytes(this.Data)
}
func (this *ConsensusPayload) DeserializationUnsigned(source *common.ZeroCopySource) error {
var irregular, eof bool
this.Version, eof = source.NextUint32()
this.PrevHash, eof = source.NextHash()
this.Height, eof = source.NextUint32()
this.BookkeeperIndex, eof = source.NextUint16()
this.Timestamp, eof = source.NextUint32()
this.Data, _, irregular, eof = source.NextVarBytes()
if eof {
return io.ErrUnexpectedEOF
}
if irregular {
return common.ErrIrregularData
}
return nil
}