Skip to content

Commit

Permalink
fix(logbook): use AddChild so oplogs set ancestry
Browse files Browse the repository at this point in the history
also, refactored SignedFlatbufferBytes, more useful if signing is a separate function
  • Loading branch information
b5 committed Jan 29, 2020
1 parent 35158d5 commit be633b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
18 changes: 10 additions & 8 deletions logbook/logbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ func (book Book) initName(ctx context.Context, name string) *oplog.Log {
Timestamp: NewTimestamp(),
})

dsLog.Logs = append(dsLog.Logs, branch)
dsLog.AddChild(branch)

ns := book.authorLog(ctx)
ns.Logs = append(ns.Logs, dsLog)
nameLog := book.authorLog(ctx)
nameLog.AddChild(dsLog)
return branch
}

Expand Down Expand Up @@ -564,10 +564,9 @@ func (book Book) UserDatasetRef(ctx context.Context, ref dsref.Ref) (*oplog.Log,
}

// construct a sparse oplog of just user, dataset, and branches
return &oplog.Log{
Ops: author.Ops,
Logs: []*oplog.Log{ds},
}, nil
sparseLog := &oplog.Log{Ops: author.Ops}
sparseLog.AddChild(ds)
return sparseLog, nil
}

// DatasetRef gets a dataset log and all branches. Dataset logs describe
Expand Down Expand Up @@ -605,7 +604,10 @@ func (book Book) BranchRef(ctx context.Context, ref dsref.Ref) (*oplog.Log, erro

// LogBytes signs a log with this book's private key and writes to a flatbuffer
func (book Book) LogBytes(log *oplog.Log) ([]byte, error) {
return log.SignedFlatbufferBytes(book.pk)
if err := log.Sign(book.pk); err != nil {
return nil, err
}
return log.FlatbufferBytes(), nil
}

// DsrefAliasForLog parses log data into a dataset alias reference, populating
Expand Down
12 changes: 4 additions & 8 deletions logbook/oplog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ LOOP:
}
}
// no match, append!
lg.Logs = append(lg.Logs, x)
lg.AddChild(x)
}
}

Expand Down Expand Up @@ -567,16 +567,12 @@ func (lg Log) SigningBytes() []byte {
return hasher.Sum(nil)
}

// SignedFlatbufferBytes signs a log then marshals it to a flatbuffer
func (lg Log) SignedFlatbufferBytes(pk crypto.PrivKey) ([]byte, error) {
if err := lg.Sign(pk); err != nil {
return nil, err
}

// FlatbufferBytes marshals a log to flabuffer-formatted bytes
func (lg Log) FlatbufferBytes() []byte {
builder := flatbuffers.NewBuilder(0)
log := lg.MarshalFlatbuffer(builder)
builder.Finish(log)
return builder.FinishedBytes(), nil
return builder.FinishedBytes()
}

// MarshalFlatbuffer writes log to a flatbuffer, returning the ending byte
Expand Down
8 changes: 5 additions & 3 deletions logbook/oplog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ func TestJournalSignLog(t *testing.T) {
}, 400)

pk := tr.PrivKey
data, err := lg.SignedFlatbufferBytes(pk)
if err != nil {
if err := lg.Sign(pk); err != nil {
t.Fatal(err)
}
data := lg.FlatbufferBytes()

received, err := FromFlatbufferBytes(data)
if err != nil {
Expand Down Expand Up @@ -274,6 +274,8 @@ func TestLogNameTracking(t *testing.T) {
}
}

// NB: This test currently doesn't / can't confirm merging sets Log.parent.
// the cmp package can't deal with cyclic references
func TestLogMerge(t *testing.T) {
left := &Log{
Signature: []byte{1, 2, 3},
Expand Down Expand Up @@ -383,7 +385,7 @@ func TestLogMerge(t *testing.T) {
},
}

if diff := cmp.Diff(expect, left, allowUnexported); diff != "" {
if diff := cmp.Diff(expect, left, allowUnexported, cmpopts.IgnoreUnexported(Log{})); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}
Expand Down

0 comments on commit be633b9

Please sign in to comment.