Skip to content

Commit a4388cc

Browse files
authored
Remove the remaining mechanism for swappable payload marshal for signing (#927)
Previous versions of go-f3 made marshal for signing swappable during tests to help tests run faster. This has caused much confusion in debugging false negative test failures. As a result, the fake marshal for signing was changed to use a format consistent with the real network (#923). As a result we no longer need the types and mechanisms in code to make marshalling of payload for signing swappable. Remove them all.
1 parent 8dc2f7e commit a4388cc

19 files changed

+55
-153
lines changed

certchain/certchain.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (cc *CertChain) signProportionally(ctx context.Context, committee *gpbft.Co
165165
signature []byte
166166
}
167167
var signatures []signatureAt
168-
marshalledPayload := cc.sv.MarshalPayloadForSigning(cc.m.NetworkName, payload)
168+
marshalledPayload := payload.MarshalForSigning(cc.m.NetworkName)
169169
for _, p := range candidateSigners {
170170
scaledPower, key := committee.PowerTable.Get(p.ID)
171171
if scaledPower == 0 {
@@ -220,7 +220,7 @@ func (cc *CertChain) sign(ctx context.Context, committee *gpbft.Committee, paylo
220220
var signingPowerSoFar int64
221221
var signatures [][]byte
222222
var signersMask []int
223-
marshalledPayload := cc.sv.MarshalPayloadForSigning(cc.m.NetworkName, payload)
223+
marshalledPayload := payload.MarshalForSigning(cc.m.NetworkName)
224224
if err := signers.ForEach(
225225
func(signerIndex uint64) error {
226226
p := committee.PowerTable.Entries[signerIndex]

certchain/options.go

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type Option func(*options) error
1313
type SignVerifier interface {
1414
gpbft.Signer
1515
gpbft.Verifier
16-
gpbft.SigningMarshaler
1716
}
1817

1918
type options struct {

certs/certs.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,11 @@ func verifyFinalityCertificateSignature(verifier gpbft.Verifier, powerTable gpbf
184184
Value: cert.ECChain,
185185
}
186186

187-
// We use SigningMarshaler when implemented (for testing), but only require a `Verifier` in
188-
// the function signature to make it easier to use this as a free function.
189-
var signedBytes []byte
190-
if sig, ok := verifier.(gpbft.SigningMarshaler); ok {
191-
signedBytes = sig.MarshalPayloadForSigning(nn, payload)
192-
} else {
193-
signedBytes = payload.MarshalForSigning(nn)
194-
}
195-
196187
aggregate, err := verifier.Aggregate(keys)
197188
if err != nil {
198189
return err
199190
}
200-
191+
signedBytes := payload.MarshalForSigning(nn)
201192
if err := aggregate.VerifyAggregate(mask, signedBytes, cert.Signature); err != nil {
202193
return fmt.Errorf("invalid signature on finality certificate for instance %d: %w", cert.GPBFTInstance, err)
203194
}

emulator/driver.go

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func (d *Driver) prepareMessage(partialMessage *gpbft.GMessage) *gpbft.GMessage
8888

8989
mb := instance.NewMessageBuilder(partialMessage.Vote, partialMessage.Justification, withValidTicket)
9090
mb.NetworkName = d.host.NetworkName()
91-
mb.SigningMarshaler = d.host
9291
msg, err := mb.Build(context.Background(), d.host, partialMessage.Sender)
9392
d.require.NoError(err)
9493
d.require.NotNil(msg)

emulator/instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (i *Instance) NewJustification(round uint64, phase gpbft.Phase, vote *gpbft
146146
}
147147

148148
func (i *Instance) NewJustificationWithPayload(payload gpbft.Payload, from ...gpbft.ActorID) *gpbft.Justification {
149-
msg := i.signing.MarshalPayloadForSigning(networkName, &payload)
149+
msg := payload.MarshalForSigning(networkName)
150150
qr := gpbft.QuorumResult{
151151
Signers: make([]int, len(from)),
152152
Signatures: make([][]byte, len(from)),

emulator/signing.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ var (
1919
type Signing interface {
2020
gpbft.Verifier
2121
gpbft.Signer
22-
gpbft.SigningMarshaler
2322
}
2423

2524
// AdhocSigning marshals, signs and verifies messages on behalf of any given

gpbft/api.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,6 @@ type Signer interface {
123123
Sign(ctx context.Context, sender PubKey, msg []byte) ([]byte, error)
124124
}
125125

126-
type SigningMarshaler interface {
127-
// MarshalPayloadForSigning marshals the given payload into the bytes that should be signed.
128-
// This should usually call `Payload.MarshalForSigning(NetworkName)` except when testing as
129-
// that method is slow (computes a merkle tree that's necessary for testing).
130-
// Implementations must be safe for concurrent use.
131-
// WARNING: the partial message code depends on the structure of signed payload
132-
MarshalPayloadForSigning(NetworkName, *Payload) []byte
133-
}
134-
135126
type Aggregate interface {
136127
// Aggregates signatures from a participants.
137128
//
@@ -155,11 +146,6 @@ type Verifier interface {
155146
Aggregate(pubKeys []PubKey) (Aggregate, error)
156147
}
157148

158-
type Signatures interface {
159-
SigningMarshaler
160-
Verifier
161-
}
162-
163149
type DecisionReceiver interface {
164150
// Receives a finality decision from the instance, with signatures from a strong quorum
165151
// of participants justifying it.
@@ -182,6 +168,6 @@ type Host interface {
182168
CommitteeProvider
183169
Network
184170
Clock
185-
Signatures
171+
Verifier
186172
DecisionReceiver
187173
}

gpbft/gpbft.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,10 @@ func (i *instance) broadcast(round uint64, phase Phase, value *ECChain, createTi
893893
}
894894

895895
mb := &MessageBuilder{
896-
NetworkName: i.participant.host.NetworkName(),
897-
PowerTable: i.powerTable,
898-
SigningMarshaler: i.participant.host,
899-
Payload: p,
900-
Justification: justification,
896+
NetworkName: i.participant.host.NetworkName(),
897+
PowerTable: i.powerTable,
898+
Payload: p,
899+
Justification: justification,
901900
}
902901
if createTicket {
903902
mb.BeaconForTicket = i.beacon

gpbft/message_builder.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
var ErrNoPower = errors.New("no power")
1111

1212
type MessageBuilder struct {
13-
NetworkName NetworkName
14-
PowerTable powerTableAccessor
15-
Payload Payload
16-
BeaconForTicket []byte
17-
Justification *Justification
18-
SigningMarshaler SigningMarshaler
13+
NetworkName NetworkName
14+
PowerTable powerTableAccessor
15+
Payload Payload
16+
BeaconForTicket []byte
17+
Justification *Justification
1918
}
2019

2120
type powerTableAccessor interface {
@@ -64,7 +63,7 @@ func (mb *MessageBuilder) PrepareSigningInputs(id ActorID) (*SignatureBuilder, e
6463
PubKey: pubKey,
6564
}
6665

67-
sb.PayloadToSign = mb.SigningMarshaler.MarshalPayloadForSigning(mb.NetworkName, &mb.Payload)
66+
sb.PayloadToSign = mb.Payload.MarshalForSigning(mb.NetworkName)
6867
if mb.BeaconForTicket != nil {
6968
sb.VRFToSign = vrfSerializeSigInput(mb.BeaconForTicket, mb.Payload.Instance, mb.Payload.Round, mb.NetworkName)
7069
}

gpbft/message_builder_test.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11-
type testSigningMarshaler struct{}
12-
13-
var signingMarshaler SigningMarshaler = testSigningMarshaler{}
14-
15-
func (testSigningMarshaler) MarshalPayloadForSigning(nn NetworkName, p *Payload) []byte {
16-
return p.MarshalForSigning(nn)
17-
}
18-
1911
func TestMessageBuilder(t *testing.T) {
2012
pt := NewPowerTable()
2113
err := pt.Add([]PowerEntry{
@@ -38,10 +30,9 @@ func TestMessageBuilder(t *testing.T) {
3830
nn := NetworkName("test")
3931

4032
mt := &MessageBuilder{
41-
NetworkName: nn,
42-
PowerTable: pt,
43-
Payload: payload,
44-
SigningMarshaler: signingMarshaler,
33+
NetworkName: nn,
34+
PowerTable: pt,
35+
Payload: payload,
4536
}
4637

4738
_, err = mt.PrepareSigningInputs(2)
@@ -88,11 +79,10 @@ func TestMessageBuilderWithVRF(t *testing.T) {
8879

8980
nn := NetworkName("test")
9081
mt := &MessageBuilder{
91-
NetworkName: nn,
92-
PowerTable: pt,
93-
Payload: payload,
94-
SigningMarshaler: signingMarshaler,
95-
BeaconForTicket: []byte{0xbe, 0xac, 0x04},
82+
NetworkName: nn,
83+
PowerTable: pt,
84+
Payload: payload,
85+
BeaconForTicket: []byte{0xbe, 0xac, 0x04},
9686
}
9787

9888
st, err := mt.PrepareSigningInputs(0)

gpbft/mock_host_test.go

-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gpbft/validator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type cachingValidator struct {
2424
committeeLookback uint64
2525
committeeProvider CommitteeProvider
2626
networkName NetworkName
27-
signing Signatures
27+
verifier Verifier
2828
progress Progress
2929
}
3030

@@ -34,7 +34,7 @@ func newValidator(host Host, cp CommitteeProvider, progress Progress, cache *cac
3434
committeeProvider: cp,
3535
committeeLookback: committeeLookback,
3636
networkName: host.NetworkName(),
37-
signing: host,
37+
verifier: host,
3838
progress: progress,
3939
}
4040
}
@@ -128,7 +128,7 @@ func (v *cachingValidator) ValidateMessage(msg *GMessage) (valid ValidatedMessag
128128
if msg.Vote.Value.IsZero() {
129129
return nil, fmt.Errorf("unexpected zero value for converge phase: %w", ErrValidationInvalid)
130130
}
131-
if !VerifyTicket(v.networkName, comt.Beacon, msg.Vote.Instance, msg.Vote.Round, senderPubKey, v.signing, msg.Ticket) {
131+
if !VerifyTicket(v.networkName, comt.Beacon, msg.Vote.Instance, msg.Vote.Round, senderPubKey, v.verifier, msg.Ticket) {
132132
return nil, fmt.Errorf("failed to verify ticket from %v: %w", msg.Sender, ErrValidationInvalid)
133133
}
134134
case DECIDE_PHASE:
@@ -145,8 +145,8 @@ func (v *cachingValidator) ValidateMessage(msg *GMessage) (valid ValidatedMessag
145145
}
146146

147147
// Check vote signature.
148-
sigPayload := v.signing.MarshalPayloadForSigning(v.networkName, &msg.Vote)
149-
if err := v.signing.Verify(senderPubKey, sigPayload, msg.Signature); err != nil {
148+
sigPayload := msg.Vote.MarshalForSigning(v.networkName)
149+
if err := v.verifier.Verify(senderPubKey, sigPayload, msg.Signature); err != nil {
150150
return nil, fmt.Errorf("invalid signature on %v, %v: %w", msg, err, ErrValidationInvalid)
151151
}
152152

@@ -272,7 +272,7 @@ func (v *cachingValidator) validateJustification(msg *GMessage, comt *Committee)
272272
return fmt.Errorf("message %v has justification with insufficient power: %v", msg, justificationPower)
273273
}
274274

275-
payload := v.signing.MarshalPayloadForSigning(v.networkName, &msg.Justification.Vote)
275+
payload := msg.Justification.Vote.MarshalForSigning(v.networkName)
276276
if err := comt.AggregateVerifier.VerifyAggregate(signers, payload, msg.Justification.Signature); err != nil {
277277
return fmt.Errorf("verification of the aggregate failed: %+v: %w", msg.Justification, err)
278278
}

partial_validator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type cachingPartialValidator struct {
4242
committeeLookback uint64
4343
committeeProvider gpbft.CommitteeProvider
4444
networkName gpbft.NetworkName
45-
signing gpbft.Signatures
45+
signing gpbft.Verifier
4646
progress gpbft.Progress
4747
}
4848

sim/adversary/decide.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (i *ImmediateDecide) StartInstanceAt(instance uint64, _when time.Time) erro
8787
if i.supplementalData != nil {
8888
justificationPayload.SupplementalData = *i.supplementalData
8989
}
90-
sigPayload := i.host.MarshalPayloadForSigning(i.host.NetworkName(), &justificationPayload)
90+
sigPayload := justificationPayload.MarshalForSigning(i.host.NetworkName())
9191
signers := bitfield.New()
9292

9393
signers.Set(uint64(committee.PowerTable.Lookup[i.id]))
@@ -129,9 +129,8 @@ func (i *ImmediateDecide) StartInstanceAt(instance uint64, _when time.Time) erro
129129

130130
// Immediately send a DECIDE message
131131
mb := &gpbft.MessageBuilder{
132-
NetworkName: i.host.NetworkName(),
133-
PowerTable: committee.PowerTable,
134-
SigningMarshaler: i.host,
132+
NetworkName: i.host.NetworkName(),
133+
PowerTable: committee.PowerTable,
135134
Payload: gpbft.Payload{
136135
Instance: instance,
137136
Round: 0,

sim/adversary/repeat.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ func (r *Repeat) ReceiveMessage(vmsg gpbft.ValidatedMessage) error {
9595
Value: msg.Vote.Value,
9696
}
9797
mt := &gpbft.MessageBuilder{
98-
NetworkName: r.host.NetworkName(),
99-
PowerTable: committee.PowerTable,
100-
Payload: p,
101-
Justification: msg.Justification,
102-
SigningMarshaler: r.host,
98+
NetworkName: r.host.NetworkName(),
99+
PowerTable: committee.PowerTable,
100+
Payload: p,
101+
Justification: msg.Justification,
103102
}
104103
if len(msg.Ticket) > 0 {
105104
mt.BeaconForTicket = committee.Beacon

sim/adversary/spam.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ func (s *Spam) spamAtInstance(instance uint64) {
7979
Phase: gpbft.COMMIT_PHASE,
8080
}
8181
mt := &gpbft.MessageBuilder{
82-
NetworkName: s.host.NetworkName(),
83-
PowerTable: committee.PowerTable,
84-
Payload: p,
85-
SigningMarshaler: s.host,
82+
NetworkName: s.host.NetworkName(),
83+
PowerTable: committee.PowerTable,
84+
Payload: p,
8685
}
8786
if err := s.host.RequestBroadcast(mt); err != nil {
8887
panic(err)

sim/adversary/withhold.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (w *WithholdCommit) StartInstanceAt(instance uint64, _when time.Time) error
107107

108108
signatures := make([][]byte, 0)
109109
mask := make([]int, 0)
110-
prepareMarshalled := w.host.MarshalPayloadForSigning(w.host.NetworkName(), &preparePayload)
110+
prepareMarshalled := preparePayload.MarshalForSigning(w.host.NetworkName())
111111
for _, signerIndex := range signers {
112112
entry := committee.PowerTable.Entries[signerIndex]
113113
signatures = append(signatures, w.sign(entry.PubKey, prepareMarshalled))
@@ -181,11 +181,10 @@ func (w *WithholdCommit) sign(pubkey gpbft.PubKey, msg []byte) []byte {
181181
func (w *WithholdCommit) synchronousBroadcastRequester(powertable *gpbft.PowerTable) func(gpbft.Payload, *gpbft.Justification) {
182182
return func(payload gpbft.Payload, justification *gpbft.Justification) {
183183
mb := &gpbft.MessageBuilder{
184-
NetworkName: w.host.NetworkName(),
185-
PowerTable: powertable,
186-
Payload: payload,
187-
Justification: justification,
188-
SigningMarshaler: w.host,
184+
NetworkName: w.host.NetworkName(),
185+
PowerTable: powertable,
186+
Payload: payload,
187+
Justification: justification,
189188
}
190189
if err := w.host.RequestSynchronousBroadcast(mb); err != nil {
191190
panic(err)

0 commit comments

Comments
 (0)