/
pulsar.go
221 lines (186 loc) · 6.44 KB
/
pulsar.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package pulsar
import (
"context"
"sync"
"time"
"github.com/insolar/insolar/certificate"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/platformpolicy"
"github.com/insolar/insolar/pulsar/entropygenerator"
"go.opencensus.io/stats"
"github.com/insolar/insolar/configuration"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/log"
)
// Pulsar is a base struct for pulsar's node
// It contains all the stuff, which is needed for working of a pulsar
type Pulsar struct {
Config configuration.Pulsar
PublicKeyRaw string
EntropyGenerator entropygenerator.EntropyGenerator
Certificate certificate.Certificate
CryptographyService insolar.CryptographyService
PlatformCryptographyScheme insolar.PlatformCryptographyScheme
KeyProcessor insolar.KeyProcessor
PulseDistributor insolar.PulseDistributor
lastPNMutex sync.RWMutex
lastPN insolar.PulseNumber
}
// NewPulsar creates a new pulse with using of custom GeneratedEntropy Generator
func NewPulsar(
configuration configuration.Pulsar,
cryptographyService insolar.CryptographyService,
scheme insolar.PlatformCryptographyScheme,
keyProcessor insolar.KeyProcessor,
pulseDistributor insolar.PulseDistributor,
entropyGenerator entropygenerator.EntropyGenerator,
) *Pulsar {
log.Info("[NewPulsar]")
pulsar := &Pulsar{
CryptographyService: cryptographyService,
PlatformCryptographyScheme: scheme,
KeyProcessor: keyProcessor,
PulseDistributor: pulseDistributor,
Config: configuration,
EntropyGenerator: entropyGenerator,
}
pubKey, err := cryptographyService.GetPublicKey()
if err != nil {
log.Fatal(err)
}
pubKeyRaw, err := keyProcessor.ExportPublicKeyPEM(pubKey)
if err != nil {
log.Fatal(err)
}
pulsar.PublicKeyRaw = string(pubKeyRaw)
return pulsar
}
func (p *Pulsar) Send(ctx context.Context, pulseNumber insolar.PulseNumber) error {
logger := inslogger.FromContext(ctx)
logger.Infof("before sending new pulseNumber: %v", pulseNumber)
entropy, _, err := p.generateNewEntropyAndSign()
if err != nil {
logger.Error(err)
return err
}
pulseForSending := insolar.Pulse{
PulseNumber: pulseNumber,
Entropy: entropy,
NextPulseNumber: pulseNumber + insolar.PulseNumber(p.Config.NumberDelta),
PrevPulseNumber: p.lastPN,
EpochPulseNumber: int(pulseNumber),
OriginID: [16]byte{206, 41, 229, 190, 7, 240, 162, 155, 121, 245, 207, 56, 161, 67, 189, 0},
PulseTimestamp: time.Now().UnixNano(),
Signs: map[string]insolar.PulseSenderConfirmation{},
}
payload := PulseSenderConfirmationPayload{PulseSenderConfirmation: insolar.PulseSenderConfirmation{
ChosenPublicKey: p.PublicKeyRaw,
Entropy: entropy,
PulseNumber: pulseNumber,
}}
hasher := platformpolicy.NewPlatformCryptographyScheme().IntegrityHasher()
hash, err := payload.Hash(hasher)
if err != nil {
return err
}
signature, err := p.CryptographyService.Sign(hash)
if err != nil {
return err
}
pulseForSending.Signs[p.PublicKeyRaw] = insolar.PulseSenderConfirmation{
ChosenPublicKey: p.PublicKeyRaw,
Signature: signature.Bytes(),
Entropy: entropy,
PulseNumber: pulseNumber,
}
logger.Debug("Start a process of sending pulse")
go func() {
logger.Debug("Before sending to network")
p.PulseDistributor.Distribute(ctx, pulseForSending)
}()
p.lastPNMutex.Lock()
p.lastPN = pulseNumber
p.lastPNMutex.Unlock()
logger.Infof("set latest pulse: %v", pulseForSending.PulseNumber)
stats.Record(ctx, statPulseGenerated.M(1), statCurrentPulse.M(int64(pulseNumber.AsUint32())))
return nil
}
func (p *Pulsar) LastPN() insolar.PulseNumber {
p.lastPNMutex.RLock()
defer p.lastPNMutex.RUnlock()
return p.lastPN
}
func (p *Pulsar) generateNewEntropyAndSign() (insolar.Entropy, []byte, error) {
e := p.EntropyGenerator.GenerateEntropy()
sign, err := p.CryptographyService.Sign(e[:])
if err != nil {
return insolar.Entropy{}, nil, err
}
return e, sign.Bytes(), nil
}
// PulseSenderConfirmationPayload is a struct with info about pulse's confirmations
type PulseSenderConfirmationPayload struct {
insolar.PulseSenderConfirmation
}
// Hash calculates hash of payload
func (ps *PulseSenderConfirmationPayload) Hash(hashProvider insolar.Hasher) ([]byte, error) {
_, err := hashProvider.Write(ps.PulseNumber.Bytes())
if err != nil {
return nil, err
}
_, err = hashProvider.Write([]byte(ps.ChosenPublicKey))
if err != nil {
return nil, err
}
_, err = hashProvider.Write(ps.Entropy[:])
if err != nil {
return nil, err
}
return hashProvider.Sum(nil), nil
}
/*
if currentPulsar.isStandalone() {
currentPulsar.SetCurrentSlotEntropy(currentPulsar.GetGeneratedEntropy())
currentPulsar.CurrentSlotPulseSender = currentPulsar.PublicKeyRaw
payload := PulseSenderConfirmationPayload{insolar.PulseSenderConfirmation{
ChosenPublicKey: currentPulsar.CurrentSlotPulseSender,
Entropy: *currentPulsar.GetCurrentSlotEntropy(),
PulseNumber: currentPulsar.ProcessingPulseNumber,
}}
hashProvider := currentPulsar.PlatformCryptographyScheme.IntegrityHasher()
hash, err := payload.Hash(hashProvider)
if err != nil {
currentPulsar.StateSwitcher.SwitchToState(ctx, Failed, err)
return
}
signature, err := currentPulsar.CryptographyService.Sign(hash)
if err != nil {
currentPulsar.StateSwitcher.SwitchToState(ctx, Failed, err)
return
}
currentPulsar.currentSlotSenderConfirmationsLock.Lock()
currentPulsar.CurrentSlotSenderConfirmations[currentPulsar.PublicKeyRaw] = insolar.PulseSenderConfirmation{
ChosenPublicKey: currentPulsar.CurrentSlotPulseSender,
Signature: signature.Bytes(),
Entropy: *currentPulsar.GetCurrentSlotEntropy(),
PulseNumber: currentPulsar.ProcessingPulseNumber,
}
currentPulsar.currentSlotSenderConfirmationsLock.Unlock()
currentPulsar.StateSwitcher.SwitchToState(ctx, SendingPulse, nil)
return
}*/