-
Notifications
You must be signed in to change notification settings - Fork 62
/
message.go
147 lines (122 loc) · 3.75 KB
/
message.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
146
147
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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 go-ethereum library 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/clearmatics/autonity/common"
"github.com/clearmatics/autonity/consensus/tendermint/validator"
"github.com/clearmatics/autonity/log"
"github.com/clearmatics/autonity/rlp"
)
const (
msgProposal uint64 = iota
msgPrevote
msgPrecommit
)
type Message struct {
Code uint64
Msg []byte
Address common.Address
Signature []byte
CommittedSeal []byte
}
// ==============================================
//
// define the functions that needs to be provided for rlp Encoder/Decoder.
// EncodeRLP serializes m into the Ethereum RLP format.
func (m *Message) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{m.Code, m.Msg, m.Address, m.Signature, m.CommittedSeal})
}
func (m *Message) GetCode() uint64 {
return m.Code
}
func (m *Message) GetSignature() []byte {
return m.Signature
}
// DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream.
func (m *Message) DecodeRLP(s *rlp.Stream) error {
var msg struct {
Code uint64
Msg []byte
Address common.Address
Signature []byte
CommittedSeal []byte
}
if err := s.Decode(&msg); err != nil {
return err
}
m.Code, m.Msg, m.Address, m.Signature, m.CommittedSeal = msg.Code, msg.Msg, msg.Address, msg.Signature, msg.CommittedSeal
return nil
}
var ErrUnauthorizedAddress = errors.New("unauthorized address")
// ==============================================
//
// define the functions that needs to be provided for core.
func (m *Message) FromPayload(b []byte, valSet validator.Set, validateFn func(validator.Set, []byte, []byte) (common.Address, error)) (*validator.Validator, error) {
// Decode message
err := rlp.DecodeBytes(b, m)
if err != nil {
return nil, err
}
// Validate message (on a message without Signature)
if validateFn == nil {
log.Error("validateFn is not set")
return nil, nil
}
// Still return the message even the err is not nil
var payload []byte
payload, err = m.PayloadNoSig()
if err != nil {
return nil, err
}
addr, err := validateFn(valSet, payload, m.Signature)
if err != nil {
return nil, err
}
//ensure message was singed by the sender
if !bytes.Equal(m.Address.Bytes(), addr.Bytes()) {
return nil, ErrUnauthorizedAddress
}
_, v := valSet.GetByAddress(addr)
return &v, nil
}
func (m *Message) Payload() ([]byte, error) {
return rlp.EncodeToBytes(m)
}
func (m *Message) PayloadNoSig() ([]byte, error) {
return rlp.EncodeToBytes(&Message{
Code: m.Code,
Msg: m.Msg,
Address: m.Address,
Signature: []byte{},
CommittedSeal: m.CommittedSeal,
})
}
func (m *Message) Decode(val interface{}) error {
return rlp.DecodeBytes(m.Msg, val)
}
func (m *Message) String() string {
return fmt.Sprintf("{Code: %v, Address: %v}", m.Code, m.Address.String())
}
// ==============================================
//
// helper functions
func Encode(val interface{}) ([]byte, error) {
return rlp.EncodeToBytes(val)
}