-
Notifications
You must be signed in to change notification settings - Fork 4
/
request.go
67 lines (55 loc) · 1.37 KB
/
request.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
package duobft
import (
"crypto/sha256"
"github.com/gogo/protobuf/proto"
"github.com/ibalajiarun/go-consensus/pkg/command/commandpb"
pb "github.com/ibalajiarun/go-consensus/protocols/duobft/duobftpb"
)
func (p *duobft) onRequest(cmd *commandpb.Command) *instance {
p.logger.Debugf("Replica %v received %v\n", p.id, cmd.Timestamp)
if uint64(p.curView)%uint64(len(p.nodes)) != uint64(p.id) {
p.logger.Errorf("Not the leader of view %d: %d", p.curView, p.id)
return nil
}
p.index++
cmdBytes, err := proto.Marshal(cmd)
if err != nil {
panic(err)
}
cmdHash := sha256.Sum256(cmdBytes)
inst := &instance{
p: p,
is: pb.InstanceState{
View: p.curView,
Index: p.index,
Status: pb.InstanceState_Prepared,
TStatus: pb.InstanceState_TPrepared,
Command: cmd,
CommandHash: cmdHash[:],
},
tcCert: newQuorum(p, p.f+1),
pcCert: newQuorum(p, 2*p.f+1),
cCert: newQuorum(p, 2*p.f+1),
}
p.log[p.index] = inst
pm := &pb.NormalMessage{
View: p.curView,
Index: p.index,
Type: pb.NormalMessage_Prepare,
Command: cmd,
CommandHash: cmdHash[:],
}
mBytes := p.marshall(pm)
p.dispatcher2.Exec(
func() []byte {
sign := p.usigSign(mBytes)
return sign
},
func(sign []byte) {
inst.tcCert.log(p.id, pm)
inst.pcCert.log(p.id, pm)
p.broadcast(mBytes, false, sign)
},
)
return inst
}