Skip to content

Commit

Permalink
std: make produce version required
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Jan 31, 2025
1 parent cc2b875 commit d6dad21
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
3 changes: 1 addition & 2 deletions dv/dv/advert_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
enc "github.com/named-data/ndnd/std/encoding"
"github.com/named-data/ndnd/std/log"
"github.com/named-data/ndnd/std/ndn"
"github.com/named-data/ndnd/std/utils"
)

func (a *advertModule) generate() {
Expand All @@ -23,7 +22,7 @@ func (a *advertModule) generate() {
enc.NewTimestampComponent(a.bootTime),
),
Content: a.dv.rib.Advert().Encode(),
Version: utils.IdPtr(a.seq),
Version: a.seq,
FreshnessPeriod: 10 * time.Second,
})
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions dv/table/prefix_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/named-data/ndnd/std/ndn"
"github.com/named-data/ndnd/std/object"
ndn_sync "github.com/named-data/ndnd/std/sync"
"github.com/named-data/ndnd/std/utils"
)

const PrefixSnapThreshold = 100
Expand Down Expand Up @@ -232,7 +231,7 @@ func (pt *PrefixTable) publishOp(content enc.Wire) {
enc.NewSequenceNumComponent(seq),
),
Content: content,
Version: utils.IdPtr(uint64(0)), // immutable
Version: ndn.VersionImmutable,
})
if err != nil {
log.Error(pt, "Failed to produce op", "err", err)
Expand Down Expand Up @@ -269,7 +268,7 @@ func (pt *PrefixTable) publishSnap() {
enc.NewKeywordComponent(PrefixSnapKeyword),
),
Content: snap.Encode(),
Version: utils.IdPtr(pt.me.Latest),
Version: pt.me.Latest,
})
if err != nil {
log.Error(pt, "Failed to produce snap", "err", err)
Expand Down
1 change: 1 addition & 0 deletions fw/mgmt/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (m *Thread) sendStatusDataset(interest *Interest, name enc.Name, dataset en
objName, err := object.Produce(ndn.ProduceArgs{
Name: name,
Content: dataset,
Version: ndn.VersionUnixMicro,
FreshnessPeriod: time.Millisecond,
NoMetadata: true,
}, m.store, m.signer)
Expand Down
17 changes: 15 additions & 2 deletions std/ndn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import (
enc "github.com/named-data/ndnd/std/encoding"
)

// ObjectVersionImmutable is the version number for immutable objects.
// A version number of 0 will be used on the wire.
const VersionImmutable = uint64(1<<63 - 15)

// ObjectVersionUnixMicro is the version number for objects with a unix timestamp.
// A version number of microseconds since the unix epoch will be used on the wire.
// Current unix time must be positive to use this.
const VersionUnixMicro = uint64(1<<63 - 16)

// Client is the interface for the Object Client API
type Client interface {
// String is the instance log identifier.
Expand All @@ -20,6 +29,7 @@ type Client interface {
Store() Store
// Produce generates and signs data, and inserts into the client's store.
// The input data will be freed as the object is segmented.
// Returns the final versioned name of the object.
Produce(args ProduceArgs) (enc.Name, error)
// Remove removes an object from the client's store by name.
Remove(name enc.Name) error
Expand Down Expand Up @@ -47,8 +57,11 @@ type ProduceArgs struct {
// Content is the raw data wire.
// Content can be larger than a single packet and will be segmented.
Content enc.Wire
// Version of the object (defaults to unix timestamp, 0 for immutable).
Version *uint64
// Version of the object. This option is required.
// Available magic values are:
// VersionImmutable for v=0
// VersionUnixMicro for v=current unix time in microseconds
Version uint64
// Time for which the object version can be cached (default 4s).
FreshnessPeriod time.Duration
// NoMetadata disables RDR metadata (advanced usage).
Expand Down
23 changes: 15 additions & 8 deletions std/object/client_produce.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,27 @@ func Produce(args ndn.ProduceArgs, store ndn.Store, signer ndn.Signer) (enc.Name
content := args.Content
contentSize := content.Length()

now := time.Now().UnixNano()
if now < 0 { // > 1970
return nil, errors.New("current unix time is negative")
}

version := uint64(now)
if args.Version != nil {
version = *args.Version
// Get the correct version
version := args.Version
switch args.Version {
case 0: // nothing
return nil, errors.New("object version is not specified or zero")
case ndn.VersionImmutable:
version = 0
case ndn.VersionUnixMicro:
if now := time.Now().UnixMicro(); now > 0 { // > 1970
version = uint64(now)
} else {
return nil, errors.New("current unix time is negative")
}
}

// Use freshness period or default
if args.FreshnessPeriod == 0 {
args.FreshnessPeriod = 4 * time.Second
}

// Compute final block ID with segment count
lastSeg := uint64(0)
if contentSize > 0 {
lastSeg = uint64((contentSize - 1) / pSegmentSize)
Expand Down
1 change: 1 addition & 0 deletions tools/putchunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (pc *PutChunks) run(_ *cobra.Command, args []string) {
vname, err := cli.Produce(ndn.ProduceArgs{
Name: name,
Content: content,
Version: ndn.VersionUnixMicro,
})
if err != nil {
log.Fatal(pc, "Unable to produce object", "err", err)
Expand Down

0 comments on commit d6dad21

Please sign in to comment.