Skip to content

Commit 48a95f0

Browse files
authored
Remove the code for signing log roots if the last one is too old. (#266)
* Remove the code for signing log roots if the last one is too old. This is leftover from when we thought app layers wouldn't have their own storage. I don't think it's being used and probably doesn't do what we need anyway. * Create a root in sequencing if none already exists. Could be better to move this to log create API when we write it. Then we can assume a root exists and raise an error if it doesn't. TODO added.
1 parent b2ea828 commit 48a95f0

File tree

4 files changed

+17
-77
lines changed

4 files changed

+17
-77
lines changed

log/sequencer.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ type Sequencer struct {
4040
// the subtrees.
4141
const maxTreeDepth = 64
4242

43-
// CurrentRootExpiredFunc examines a signed log root and decides if it has expired with respect
44-
// to a max age duration and a given time source
45-
// TODO(Martin2112): This is all likely to go away when we switch to application STHs
46-
type CurrentRootExpiredFunc func(trillian.SignedLogRoot) bool
47-
4843
// NewSequencer creates a new Sequencer instance for the specified inputs.
4944
func NewSequencer(hasher merkle.TreeHasher, timeSource util.TimeSource, logStorage storage.LogStorage, km crypto.KeyManager) *Sequencer {
5045
return &Sequencer{hasher: hasher, timeSource: timeSource, logStorage: logStorage, keyManager: km}
@@ -156,7 +151,7 @@ func (s Sequencer) createRootSignature(ctx context.Context, root trillian.Signed
156151
// TODO(Martin2112): Can possibly improve by deferring a function that attempts to rollback,
157152
// which will fail if the tx was committed. Should only do this if we can hide the details of
158153
// the underlying storage transactions and it doesn't create other problems.
159-
func (s Sequencer) SequenceBatch(ctx context.Context, limit int, expiryFunc CurrentRootExpiredFunc) (int, error) {
154+
func (s Sequencer) SequenceBatch(ctx context.Context, limit int) (int, error) {
160155
tx, err := s.logStorage.Begin()
161156

162157
if err != nil {
@@ -184,22 +179,17 @@ func (s Sequencer) SequenceBatch(ctx context.Context, limit int, expiryFunc Curr
184179
}
185180

186181
// TODO(al): Have a better detection mechanism for there being no stored root.
182+
// TODO(mhs): Might be better to create empty root in provisioning API when it exists
187183
if currentRoot.RootHash == nil {
188184
glog.Warningf("%s: Fresh log - no previous TreeHeads exist.", util.LogIDPrefix(ctx))
185+
return 0, s.SignRoot(ctx)
189186
}
190187

191188
// There might be no work to be done. But we possibly still need to create an STH if the
192189
// current one is too old. If there's work to be done then we'll be creating a root anyway.
193190
if len(leaves) == 0 {
194191
// We have nothing to integrate into the tree
195-
tx.Commit()
196-
if expiryFunc(currentRoot) {
197-
// Current root is too old, sign one. Will use a new TX, safe as we have no writes
198-
// pending in this one.
199-
glog.V(2).Infof("%s: Current root is too old, create new STH", util.LogIDPrefix(ctx))
200-
return 0, s.SignRoot(ctx)
201-
}
202-
return 0, nil
192+
return 0, tx.Commit()
203193
}
204194

205195
merkleTree, err := s.initMerkleTreeFromStorage(ctx, currentRoot, tx)

log/sequencer_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ import (
1717
"github.com/google/trillian/util"
1818
)
1919

20-
// Func that says root never expires, so we can be sure tests will only sign / sequence when we
21-
// expect them to
22-
func rootNeverExpiresFunc(trillian.SignedLogRoot) bool {
23-
return false
24-
}
25-
2620
var treeHasher = merkle.NewRFC6962TreeHasher(crypto.NewSHA256())
2721

2822
// These can be shared between tests as they're never modified
@@ -230,7 +224,7 @@ func TestBeginTXFails(t *testing.T) {
230224
params := testParameters{beginFails: true, skipDequeue: true, skipStoreSignedRoot: true}
231225
c, ctx := createTestContext(ctrl, params)
232226

233-
leaves, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
227+
leaves, err := c.sequencer.SequenceBatch(ctx, 1)
234228
if leaves != 0 {
235229
t.Fatalf("Unexpectedly sequenced %d leaves on error", leaves)
236230
}
@@ -245,7 +239,7 @@ func TestSequenceWithNothingQueued(t *testing.T) {
245239

246240
c, ctx := createTestContext(ctrl, params)
247241

248-
leaves, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
242+
leaves, err := c.sequencer.SequenceBatch(ctx, 1)
249243
if leaves != 0 {
250244
t.Fatalf("Unexpectedly sequenced %d leaves on error", leaves)
251245
}
@@ -268,7 +262,7 @@ func TestGuardWindowPassthrough(t *testing.T) {
268262
c, ctx := createTestContext(ctrl, params)
269263
c.sequencer.SetGuardWindow(guardInterval)
270264

271-
leaves, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
265+
leaves, err := c.sequencer.SequenceBatch(ctx, 1)
272266
if leaves != 0 {
273267
t.Fatalf("Expected no leaves sequenced when in guard interval but got: %d", leaves)
274268
}
@@ -285,7 +279,7 @@ func TestDequeueError(t *testing.T) {
285279
params := testParameters{dequeueLimit: 1, shouldRollback: true, dequeuedError: errors.New("dequeue")}
286280
c, ctx := createTestContext(ctrl, params)
287281

288-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
282+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
289283
testonly.EnsureErrorContains(t, err, "dequeue")
290284
if leafCount != 0 {
291285
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
@@ -301,7 +295,7 @@ func TestLatestRootError(t *testing.T) {
301295
latestSignedRoot: &testRoot16, latestSignedRootError: errors.New("root")}
302296
c, ctx := createTestContext(ctrl, params)
303297

304-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
298+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
305299
if leafCount != 0 {
306300
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
307301
}
@@ -319,7 +313,7 @@ func TestUpdateSequencedLeavesError(t *testing.T) {
319313
updatedLeavesError: errors.New("unsequenced")}
320314
c, ctx := createTestContext(ctrl, params)
321315

322-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
316+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
323317
if leafCount != 0 {
324318
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
325319
}
@@ -337,7 +331,7 @@ func TestSetMerkleNodesError(t *testing.T) {
337331
merkleNodesSetError: errors.New("setmerklenodes")}
338332
c, ctx := createTestContext(ctrl, params)
339333

340-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
334+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
341335
if leafCount != 0 {
342336
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
343337
}
@@ -358,7 +352,7 @@ func TestStoreSignedRootError(t *testing.T) {
358352
signingResult: []byte("signed")}
359353
c, ctx := createTestContext(ctrl, params)
360354

361-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
355+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
362356
if leafCount != 0 {
363357
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
364358
}
@@ -378,7 +372,7 @@ func TestStoreSignedRootKeyManagerFails(t *testing.T) {
378372
keyManagerError: errors.New("keymanagerfailed")}
379373
c, ctx := createTestContext(ctrl, params)
380374

381-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
375+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
382376
if leafCount != 0 {
383377
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
384378
}
@@ -399,7 +393,7 @@ func TestStoreSignedRootSignerFails(t *testing.T) {
399393
signingError: errors.New("signerfailed")}
400394
c, ctx := createTestContext(ctrl, params)
401395

402-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
396+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
403397
if leafCount != 0 {
404398
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
405399
}
@@ -421,7 +415,7 @@ func TestCommitFails(t *testing.T) {
421415
signingResult: []byte("signed")}
422416
c, ctx := createTestContext(ctrl, params)
423417

424-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
418+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
425419
if leafCount != 0 {
426420
t.Fatalf("Unexpectedly sequenced %d leaves on error", leafCount)
427421
}
@@ -444,7 +438,7 @@ func TestSequenceBatch(t *testing.T) {
444438
signingResult: []byte("signed")}
445439
c, ctx := createTestContext(ctrl, params)
446440

447-
leafCount, err := c.sequencer.SequenceBatch(ctx, 1, rootNeverExpiresFunc)
441+
leafCount, err := c.sequencer.SequenceBatch(ctx, 1)
448442
if err != nil {
449443
t.Fatalf("Expected sequencing to succeed, but got err: %v", err)
450444
}

server/sequencer_manager.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"time"
55

66
"github.com/golang/glog"
7-
"github.com/google/trillian"
87
"github.com/google/trillian/crypto"
98
"github.com/google/trillian/log"
109
"github.com/google/trillian/merkle"
@@ -18,15 +17,6 @@ type SequencerManager struct {
1817
cachedProvider *cachedLogStorageProvider
1918
}
2019

21-
func isRootTooOld(ts util.TimeSource, maxAge time.Duration) log.CurrentRootExpiredFunc {
22-
return func(root trillian.SignedLogRoot) bool {
23-
rootTime := time.Unix(0, root.TimestampNanos)
24-
rootAge := ts.Now().Sub(rootTime)
25-
26-
return rootAge > maxAge
27-
}
28-
}
29-
3020
// NewSequencerManager creates a new SequencerManager instance based on the provided KeyManager instance
3121
// and guard window.
3222
func NewSequencerManager(km crypto.KeyManager, p LogStorageProviderFunc, gw time.Duration) *SequencerManager {
@@ -71,7 +61,7 @@ func (s SequencerManager) ExecutePass(logIDs []int64, logctx LogOperationManager
7161
sequencer := log.NewSequencer(merkle.NewRFC6962TreeHasher(crypto.NewSHA256()), logctx.timeSource, storage, s.keyManager)
7262
sequencer.SetGuardWindow(s.guardWindow)
7363

74-
leaves, err := sequencer.SequenceBatch(ctx, logctx.batchSize, isRootTooOld(logctx.timeSource, logctx.signInterval))
64+
leaves, err := sequencer.SequenceBatch(ctx, logctx.batchSize)
7565

7666
if err != nil {
7767
glog.Warningf("%s: Error trying to sequence batch for: %v", util.LogIDPrefix(ctx), err)

server/sequencer_manager_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -137,40 +137,6 @@ func TestSequencerManagerSingleLogOneLeaf(t *testing.T) {
137137
sm.ExecutePass([]int64{logID}, createTestContext(provider))
138138
}
139139

140-
// Tests that a new root is signed if it's due even when there is no work to sequence.
141-
// The various failure cases of SignRoot() are tested in the sequencer tests. This is
142-
// an interaction test.
143-
func TestSignsIfNoWorkAndRootExpired(t *testing.T) {
144-
mockCtrl := gomock.NewController(t)
145-
defer mockCtrl.Finish()
146-
147-
mockStorage := storage.NewMockLogStorage(mockCtrl)
148-
mockTx := storage.NewMockLogTX(mockCtrl)
149-
mockKeyManager := crypto.NewMockKeyManager(mockCtrl)
150-
mockKeyManager.EXPECT().SignatureAlgorithm().AnyTimes().Return(trillian.SignatureAlgorithm_ECDSA)
151-
logID := int64(1)
152-
hasher := crypto.NewSHA256()
153-
154-
mockStorage.EXPECT().Begin().AnyTimes().Return(mockTx, nil)
155-
mockTx.EXPECT().WriteRevision().AnyTimes().Return(writeRev)
156-
mockTx.EXPECT().Commit().AnyTimes().Return(nil)
157-
mockTx.EXPECT().LatestSignedLogRoot().AnyTimes().Return(testRoot0, nil)
158-
mockTx.EXPECT().DequeueLeaves(50, fakeTime).Return([]trillian.LogLeaf{}, nil)
159-
mockTx.EXPECT().StoreSignedLogRoot(updatedRootSignOnly).AnyTimes().Return(nil)
160-
161-
mockSigner := crypto.NewMockSigner(mockCtrl)
162-
mockSigner.EXPECT().Sign(gomock.Any(), []byte{0xeb, 0x7d, 0xa1, 0x4f, 0x1e, 0x60, 0x91, 0x24, 0xa, 0xf7, 0x1c, 0xcd, 0xdb, 0xd4, 0xca, 0x38, 0x4b, 0x12, 0xe4, 0xa3, 0xcf, 0x80, 0x5, 0x55, 0x17, 0x71, 0x35, 0xaf, 0x80, 0x11, 0xa, 0x87}, hasher).Return([]byte("signed"), nil)
163-
mockKeyManager.EXPECT().Signer().Return(mockSigner, nil)
164-
165-
provider := mockStorageProviderForSequencer(mockStorage)
166-
sm := NewSequencerManager(mockKeyManager, provider, zeroDuration)
167-
168-
tc := createTestContext(provider)
169-
// Lower the expiry so we can trigger a signing for a root older than 5 seconds
170-
tc.signInterval = time.Second * 5
171-
sm.ExecutePass([]int64{logID}, tc)
172-
}
173-
174140
func TestSequencerManagerGuardWindow(t *testing.T) {
175141
mockCtrl := gomock.NewController(t)
176142
defer mockCtrl.Finish()

0 commit comments

Comments
 (0)