-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus.go
129 lines (116 loc) · 3.35 KB
/
consensus.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
/*
* Copyright 2019 Insolar Technologies
*
* 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 logicrunner
import (
"context"
"sync"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/core/message"
"github.com/pkg/errors"
)
type ConsensusRecord struct {
Steps int
Error string
Message core.Parcel
}
// Consensus is an object for one validation process where all validated results will be compared.
type Consensus struct {
sync.Mutex
lr *LogicRunner
ready bool
Have int
Need int
Total int
Results map[Ref]ConsensusRecord
CaseBind CaseBind
Message core.Parcel
}
func newConsensus(lr *LogicRunner, refs []Ref) *Consensus {
c := &Consensus{
lr: lr,
Results: make(map[Ref]ConsensusRecord),
}
for _, r := range refs {
c.Results[r] = ConsensusRecord{}
}
c.Total = len(c.Results)
c.Need = c.Total/2 + 1
return c
}
// AddValidated adds results from validators
func (c *Consensus) AddValidated(ctx context.Context, sm core.Parcel, msg *message.ValidationResults) error {
source := sm.GetSender()
c.Lock()
defer c.Unlock()
if _, ok := c.Results[source]; !ok {
return errors.Errorf("Validation packet from non validation node for %#v", sm)
}
c.Results[source] = ConsensusRecord{
Steps: msg.PassedStepsCount,
Error: msg.Error,
}
c.Have++
c.CheckReady(ctx)
return nil
}
func (c *Consensus) AddExecutor(ctx context.Context, sm core.Parcel, msg *message.ExecutorResults) {
c.Lock()
defer c.Unlock()
c.CaseBind = *NewCaseBindFromExecutorResultsMessage(msg)
c.Message = sm
c.CheckReady(ctx)
}
func (c *Consensus) CheckReady(ctx context.Context) {
if c.Have < c.Need {
return
}
steps := make(map[int]int)
maxSame := 0 // count of nodes with same result
stepsSame := 0 // steps agreed by maximum nodes
for _, r := range c.Results {
steps[r.Steps]++
if maxSame < steps[r.Steps] {
maxSame = steps[r.Steps]
stepsSame = r.Steps
}
}
var err error
if maxSame < c.Need && c.Total == c.Have {
c.ready = true
err = c.lr.ArtifactManager.RegisterValidation(ctx, c.GetReference(), *c.FindRequestBefore(stepsSame), false, c.GetValidatorSignatures())
} else if maxSame >= c.Need && stepsSame == len(c.CaseBind.Requests) {
c.ready = true
err = c.lr.ArtifactManager.RegisterValidation(ctx, c.GetReference(), *c.FindRequestBefore(stepsSame), true, c.GetValidatorSignatures())
}
if err != nil {
panic(err)
}
}
func (c *Consensus) GetReference() Ref {
return c.Message.Message().(*message.ExecutorResults).RecordRef
}
//
func (c *Consensus) GetValidatorSignatures() (messages []core.Message) {
for _, x := range c.Results {
messages = append(messages, x.Message)
}
return messages
}
// FindRequestBefore returns request placed before step (last valid request)
func (c *Consensus) FindRequestBefore(steps int) *core.RecordID {
// TODO: resurrect this part
return nil
}