Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit 0da8fda

Browse files
committed
WIP: fix mutator bug
1 parent a7f163d commit 0da8fda

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

core/monitor/verify.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/google/keytransparency/core/mutator/entry"
3434
ktpb "github.com/google/keytransparency/core/proto/keytransparency_v1_types"
35+
"fmt"
3536
)
3637

3738
var (
@@ -108,13 +109,15 @@ func (m *Monitor) VerifyMutationsResponse(in *ktpb.GetMutationsResponse) []error
108109
// previous epoch. Storage always stores the mutations response independent
109110
// from if the checks succeeded or not.
110111
var oldRoot []byte
111-
if m.store.LatestEpoch() > 1 {
112+
if m.store.LatestEpoch() > 0 {
113+
fmt.Println("Called")
112114
// retrieve the old root hash from storage!
113115
monRes, err := m.store.Get(in.Epoch - 1)
114116
if err != nil {
115117
glog.Infof("Could not retrieve previous monitoring result: %v", err)
116118
}
117119
oldRoot = monRes.Response.GetSmr().GetRootHash()
120+
118121
if err := m.verifyMutations(in.GetMutations(), oldRoot,
119122
in.GetSmr().GetRootHash(), in.GetSmr().GetMapId()); len(err) > 0 {
120123
errList = append(errList, err...)
@@ -129,7 +132,7 @@ func (m *Monitor) verifyMutations(muts []*ktpb.Mutation, oldRoot, expectedNewRoo
129132
mutator := entry.New()
130133
oldProofNodes := make(map[string][]byte)
131134
newLeaves := make([]merkle.HStar2LeafHash, 0, len(muts))
132-
135+
glog.Infof("verifyMutations() called with %v mutations.", len(muts))
133136
for _, mut := range muts {
134137
oldLeaf, err := entry.FromLeafValue(mut.GetProof().GetLeaf().GetLeafValue())
135138
if err != nil {
@@ -146,8 +149,12 @@ func (m *Monitor) verifyMutations(muts []*ktpb.Mutation, oldRoot, expectedNewRoo
146149
}
147150

148151
// compute the new leaf
152+
fmt.Println("old leaf: ")
153+
fmt.Println(mut.GetProof().GetLeaf().GetLeafValue())
154+
fmt.Println(oldLeaf)
149155
newLeaf, err := mutator.Mutate(oldLeaf, mut.GetUpdate())
150156
if err != nil {
157+
glog.Infof("Mutation did not verify: %v", err)
151158
errList = append(errList, ErrInvalidMutation)
152159
}
153160
newLeafnID := storage.NewNodeIDFromPrefixSuffix(index, storage.Suffix{}, m.mapHasher.BitLen())

core/mutation/mutation.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,24 @@ func (s *Server) GetMutations(ctx context.Context, in *tpb.GetMutationsRequest)
106106
}
107107
// Get leaf proofs.
108108
// TODO: allow leaf proofs to be optional.
109-
proofs, err := s.inclusionProofs(ctx, indexes, in.Epoch)
109+
var epoch int64
110+
if in.Epoch > 1 {
111+
epoch = in.Epoch-1
112+
} else {
113+
epoch = 1
114+
}
115+
proofs, err := s.inclusionProofs(ctx, indexes, epoch)
110116
if err != nil {
111117
return nil, err
112118
}
113119
for i, p := range proofs {
114120
mutations[i].Proof = p
115121
}
116122

117-
// MapRevisions start at 1. Log leave's index starts at 0.
123+
// MapRevisions start at 1. Log leave's index starts at 1.
118124
// MapRevision should be at least 1 since the Signer is
119125
// supposed to create at least one revision on startup.
120-
respEpoch := resp.GetMapRoot().GetMapRevision() - 1
126+
respEpoch := resp.GetMapRoot().GetMapRevision()
121127
// Fetch log proofs.
122128
logRoot, logConsistency, logInclusion, err := s.logProofs(ctx, in.GetFirstTreeSize(), respEpoch)
123129
if err != nil {

integration/monitor_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"github.com/google/trillian/crypto/keys/pem"
3030

3131
"github.com/google/keytransparency/core/fake"
32+
"github.com/google/keytransparency/core/crypto/signatures"
33+
"github.com/google/keytransparency/cmd/keytransparency-client/grpcc"
3234
)
3335

3436
const (
@@ -73,7 +75,6 @@ func TestMonitorEmptyStart(t *testing.T) {
7375
if err != nil {
7476
t.Fatalf("Could not query mutations: %v", err)
7577
}
76-
_ = mon
7778
if err := mon.Process(mutResp); err != nil {
7879
t.Fatalf("Monitor could process mutations: %v", err)
7980
}
@@ -85,5 +86,30 @@ func TestMonitorEmptyStart(t *testing.T) {
8586
t.Errorf("Got error: %v", err)
8687
}
8788

88-
// TODO client sends one mutation, sequencer "signs", monitor verifies
89+
// client sends one mutation, sequencer "signs", monitor verifies
90+
userID := "test@test.com"
91+
signers := []signatures.Signer{createSigner(t, testPrivKey1)}
92+
authorizedKeys := []*kpb.PublicKey{getAuthorizedKey(testPubKey1)}
93+
94+
_, err = env.Client.Update(GetNewOutgoingContextWithFakeAuth("test@test.com"), userID, appID, []byte("testProfile"), signers, authorizedKeys)
95+
if err != grpcc.ErrRetry {
96+
t.Fatalf("Could not send update request: %v", err)
97+
}
98+
if err := env.Signer.CreateEpoch(bctx, false); err != nil {
99+
t.Errorf("CreateEpoch(_): %v", err)
100+
}
101+
mutResp, err = mutCli.PollMutations(bctx, 2)
102+
if err != nil {
103+
t.Fatalf("Could not query mutations: %v", err)
104+
}
105+
if err := mon.Process(mutResp); err != nil {
106+
t.Fatalf("Monitor could not process mutations: %v", err)
107+
}
108+
mresp, err = store.Get(2)
109+
if err != nil {
110+
t.Fatalf("Could not read monitoring response: %v", err)
111+
}
112+
for _, err := range mresp.Errors {
113+
t.Errorf("Got error: %v", err)
114+
}
89115
}

0 commit comments

Comments
 (0)