Skip to content

Commit eafd4e5

Browse files
committed
Add pomHandler to ModuleParams (application defines what to do with them)
1 parent a4b983a commit eafd4e5

43 files changed

Lines changed: 238 additions & 169 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pkg/accountability/simpleacc/accountability.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ type ModuleParams = common.ModuleParams
7878
//
7979
// ATTENTION: This module is intended to be used once per instance
8080
// (to avoid replay attacks) and reinstantiated in a factory.
81-
func NewModule(mc ModuleConfig, params *ModuleParams, logger logging.Logger) (modules.PassiveModule, error) {
81+
func NewModule(
82+
mc ModuleConfig,
83+
params *ModuleParams,
84+
logger logging.Logger) (modules.PassiveModule, error) {
8285
m := dsl.NewModule(mc.Self)
8386

8487
state := &incommon.State{
@@ -87,8 +90,8 @@ func NewModule(mc ModuleConfig, params *ModuleParams, logger logging.Logger) (mo
8790
LocalPredecision: nil,
8891
DecidedCertificate: nil,
8992
Predecided: false,
90-
UnsentPoMs: make([]*accpbtypes.PoM, 0),
91-
SentPoMs: make(map[t.NodeID]*accpbtypes.PoM),
93+
UnhandledPoMs: make([]*accpbtypes.PoM, 0),
94+
HandledPoMs: make(map[t.NodeID]*accpbtypes.PoM),
9295
}
9396

9497
predecisions.IncludePredecisions(m, &mc, params, state, logger)

pkg/accountability/simpleacc/common/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package common
22

33
import (
4+
incommon "github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/common"
5+
"github.com/filecoin-project/mir/pkg/dsl"
6+
"github.com/filecoin-project/mir/pkg/logging"
7+
accpbtypes "github.com/filecoin-project/mir/pkg/pb/accountabilitypb/types"
48
trantorpbtypes "github.com/filecoin-project/mir/pkg/pb/trantorpb/types"
59
t "github.com/filecoin-project/mir/pkg/types"
610
)
@@ -21,4 +25,10 @@ type ModuleConfig struct {
2125
type ModuleParams struct {
2226
Membership *trantorpbtypes.Membership // the list of participating nodes
2327
LightCertificates bool
28+
PomsHandler func(m dsl.Module, // function to be called when PoMs detected
29+
mc *ModuleConfig,
30+
params *ModuleParams,
31+
state *incommon.State,
32+
poms []*accpbtypes.PoM,
33+
logger logging.Logger)
2434
}
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package fullcertificates
22

33
import (
4-
"reflect"
5-
64
"github.com/filecoin-project/mir/pkg/accountability/simpleacc/common"
75
incommon "github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/common"
86
"github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/poms"
@@ -28,9 +26,8 @@ func IncludeFullCertificate(m dsl.Module,
2826
logger logging.Logger,
2927
) {
3028

31-
accpbdsl.UponFullCertificateReceived(m, func(from t.NodeID, certificate map[t.NodeID]*accpbtypes.SignedPredecision) error {
32-
predecision, empty := maputil.AnyVal(certificate)
33-
if empty {
29+
accpbdsl.UponFullCertificateReceived(m, func(from t.NodeID, decision []byte, certificate map[t.NodeID][]byte) error {
30+
if len(certificate) == 0 {
3431
logger.Log(logging.LevelDebug, "Ignoring empty predecision certificate")
3532
return nil
3633
}
@@ -40,42 +37,42 @@ func IncludeFullCertificate(m dsl.Module,
4037
return nil
4138
}
4239

43-
for _, v := range certificate {
44-
if !reflect.DeepEqual(predecision.Predecision, v.Predecision) {
45-
logger.Log(logging.LevelDebug, "Ignoring predecision certificate with different predecisions")
46-
return nil
47-
}
48-
}
49-
5040
// Verify all signatures in certificate.
5141
cryptopbdsl.VerifySigs(
5242
m,
5343
mc.Crypto,
54-
sliceutil.Transform(maputil.GetValues(certificate),
55-
func(i int, sp *accpbtypes.SignedPredecision) *cryptopbtypes.SignedData {
56-
return &cryptopbtypes.SignedData{Data: [][]byte{sp.Predecision, []byte(mc.Self)}}
57-
}),
58-
sliceutil.Transform(maputil.GetValues(certificate),
59-
func(i int, sp *accpbtypes.SignedPredecision) []byte {
60-
return sp.Signature
44+
sliceutil.Generate(
45+
len(certificate),
46+
func(i int) *cryptopbtypes.SignedData {
47+
return &cryptopbtypes.SignedData{
48+
Data: [][]byte{decision},
49+
}
6150
}),
51+
maputil.GetValues(certificate),
6252
maputil.GetKeys(certificate),
6353
&verifySigs{
64-
certificate: certificate,
54+
certificate: &accpbtypes.FullCertificate{
55+
Decision: decision,
56+
Signatures: certificate,
57+
},
6558
},
6659
)
6760
return nil
6861
})
6962

7063
cryptopbdsl.UponSigsVerified(m, func(nodeIds []t.NodeID, errs []error, allOk bool, vsr *verifySigs) error {
7164
for i, nodeID := range nodeIds {
72-
predecisions.ApplySigVerified(m, mc, params, state, nodeID, errs[i], vsr.certificate[nodeID], false, logger)
65+
sp := &accpbtypes.SignedPredecision{
66+
Predecision: vsr.certificate.Decision,
67+
Signature: vsr.certificate.Signatures[nodeID],
68+
}
69+
predecisions.ApplySigVerified(m, mc, params, state, nodeID, errs[i], sp, false, logger)
7370
}
74-
poms.SendPoMs(m, mc, params, state, logger)
71+
poms.HandlePoMs(m, mc, params, state, logger)
7572
return nil
7673
})
7774
}
7875

7976
type verifySigs struct {
80-
certificate map[t.NodeID]*accpbtypes.SignedPredecision
77+
certificate *accpbtypes.FullCertificate
8178
}

pkg/accountability/simpleacc/internal/certificates/lightcertificates/lightcertificates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package lightcertificates
22

33
import (
4+
"reflect"
5+
46
"github.com/filecoin-project/mir/pkg/accountability/simpleacc/common"
57
incommon "github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/common"
68
"github.com/filecoin-project/mir/pkg/dsl"
@@ -9,7 +11,6 @@ import (
911
accpbmsgs "github.com/filecoin-project/mir/pkg/pb/accountabilitypb/msgs"
1012
transportpbdsl "github.com/filecoin-project/mir/pkg/pb/transportpb/dsl"
1113
t "github.com/filecoin-project/mir/pkg/types"
12-
"reflect"
1314
)
1415

1516
// IncludeLightCertificate implements the (optional) light certificate optimization

pkg/accountability/simpleacc/internal/common/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type State struct {
1313
LocalPredecision *LocalPredecision // Decision locally decided
1414
DecidedCertificate *accpbtypes.FullCertificate // Locally decided certificate (predecision and list of signatures with signers as key)
1515
Predecided bool // Whether this process has received a predecided value from calling module.
16-
UnsentPoMs []*accpbtypes.PoM // List of PoMs not yet sent to the application.
17-
SentPoMs map[t.NodeID]*accpbtypes.PoM // List of PoMs already sent to the application with the signer as key.
16+
UnhandledPoMs []*accpbtypes.PoM // List of PoMs not yet sent to the application.
17+
HandledPoMs map[t.NodeID]*accpbtypes.PoM // List of PoMs already sent to the application with the signer as key.
1818
LightCertificates map[t.NodeID][]byte // Map of light certificates with the signer as key, buffered if no local decision made yet.
1919
}
2020

pkg/accountability/simpleacc/internal/poms/poms.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import (
88
"github.com/filecoin-project/mir/pkg/dsl"
99
"github.com/filecoin-project/mir/pkg/logging"
1010
accpbdsl "github.com/filecoin-project/mir/pkg/pb/accountabilitypb/dsl"
11-
accountabilitypbmsgs "github.com/filecoin-project/mir/pkg/pb/accountabilitypb/msgs"
1211
accpbtypes "github.com/filecoin-project/mir/pkg/pb/accountabilitypb/types"
1312
cryptopbdsl "github.com/filecoin-project/mir/pkg/pb/cryptopb/dsl"
1413
cryptopbtypes "github.com/filecoin-project/mir/pkg/pb/cryptopb/types"
15-
transportpbdsl "github.com/filecoin-project/mir/pkg/pb/transportpb/dsl"
1614
t "github.com/filecoin-project/mir/pkg/types"
17-
"github.com/filecoin-project/mir/pkg/util/maputil"
1815
)
1916

2017
// IncludePoMs verifies receives PoMs and sends found PoMs to other members.
@@ -40,7 +37,7 @@ func IncludePoMs(
4037
continue
4138
}
4239

43-
if _, ok := state.SentPoMs[pom.NodeId]; ok {
40+
if _, ok := state.HandledPoMs[pom.NodeId]; ok {
4441
continue
4542
}
4643

@@ -72,43 +69,37 @@ func IncludePoMs(
7269
cryptopbdsl.UponSigsVerified(m, func(nodeIds []t.NodeID, errs []error, allOk bool, vpoms *verifyPoMs) error {
7370
for i := 0; i < len(nodeIds); i += 2 {
7471
if errs[i] == nil && errs[i+1] == nil {
75-
state.UnsentPoMs = append(state.UnsentPoMs, vpoms.poms[i/2])
72+
state.UnhandledPoMs = append(state.UnhandledPoMs, vpoms.poms[i/2])
7673
}
7774
}
7875

79-
SendPoMs(m, mc, params, state, logger)
76+
HandlePoMs(m, mc, params, state, logger)
8077

8178
return nil
8279
})
8380
}
8481

85-
// SendPoMs sends all PoMs in State.UnsentPoMs to all nodes and to the application module (from the POV of this module, i.e. mc.App).
86-
func SendPoMs(
82+
// HandlePoMs sends all PoMs in State.UnhandledPoMs to all nodes and to the application module (from the POV of this module, i.e. mc.App).
83+
func HandlePoMs(
8784
m dsl.Module,
8885
mc *common.ModuleConfig,
8986
params *common.ModuleParams,
9087
state *incommon.State,
9188
logger logging.Logger,
9289
) {
93-
if len(state.UnsentPoMs) == 0 {
90+
if len(state.UnhandledPoMs) == 0 {
9491
return
9592
}
9693
logger.Log(logging.LevelWarn, "Found valid PoMs! sending...")
9794

98-
//TODO do something here (function that will be passed on factory creation)
95+
// Handle PoMs according to the application's logic defined when creating the accountability factory
96+
params.PomsHandler(m, mc, params, state, state.UnhandledPoMs, logger)
9997

100-
transportpbdsl.SendMessage(
101-
m,
102-
mc.Net,
103-
accountabilitypbmsgs.PoMs(mc.Self, state.UnsentPoMs),
104-
maputil.GetKeys(params.Membership.Nodes),
105-
)
106-
107-
for _, pom := range state.UnsentPoMs {
108-
state.SentPoMs[pom.NodeId] = pom
98+
for _, pom := range state.UnhandledPoMs {
99+
state.HandledPoMs[pom.NodeId] = pom
109100
}
110101

111-
state.UnsentPoMs = make([]*accpbtypes.PoM, 0)
102+
state.UnhandledPoMs = make([]*accpbtypes.PoM, 0)
112103
}
113104

114105
type verifyPoMs struct {

pkg/accountability/simpleacc/internal/predecisions/predecisions.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package predecisions
22

33
import (
4+
"reflect"
5+
46
"github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/certificates/lightcertificates"
57
isspbdsl "github.com/filecoin-project/mir/pkg/pb/isspb/dsl"
68
isspbtypes "github.com/filecoin-project/mir/pkg/pb/isspb/types"
79
tt "github.com/filecoin-project/mir/pkg/trantor/types"
8-
"reflect"
910

1011
"github.com/filecoin-project/mir/pkg/accountability/simpleacc/common"
1112
incommon "github.com/filecoin-project/mir/pkg/accountability/simpleacc/internal/common"
@@ -134,16 +135,16 @@ func ApplySigVerified(
134135
if !reflect.DeepEqual(state.SignedPredecisions[nodeID].Predecision, sp.Predecision) {
135136
logger.Log(logging.LevelWarn, "Received conflicting signed predecisions from same node")
136137
// if a PoM for this node has not already been sent.
137-
if _, ok := state.SentPoMs[nodeID]; !ok {
138-
state.UnsentPoMs = append(state.UnsentPoMs,
138+
if _, ok := state.HandledPoMs[nodeID]; !ok {
139+
state.UnhandledPoMs = append(state.UnhandledPoMs,
139140
&accpbtypes.PoM{
140141
NodeId: nodeID,
141142
ConflictingMsg_1: state.SignedPredecisions[nodeID],
142143
ConflictingMsg_2: sp,
143144
})
144145

145146
if flushPoMs {
146-
poms.SendPoMs(m, mc, params, state, logger)
147+
poms.HandlePoMs(m, mc, params, state, logger)
147148
}
148149
}
149150

pkg/modules/mockmodules/internal/mock_internal/impl.mock.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/net/grpc/grpctransport.pb.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/net/grpc/grpctransport_grpc.pb.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)