From d6dad2190093e9e0939dd5573abd498d31087077 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 31 Jan 2025 17:05:03 +0000 Subject: [PATCH] std: make produce version required --- dv/dv/advert_data.go | 3 +-- dv/table/prefix_table.go | 5 ++--- fw/mgmt/thread.go | 1 + std/ndn/client.go | 17 +++++++++++++++-- std/object/client_produce.go | 23 +++++++++++++++-------- tools/putchunks.go | 1 + 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/dv/dv/advert_data.go b/dv/dv/advert_data.go index 05c4c7ed..05944301 100644 --- a/dv/dv/advert_data.go +++ b/dv/dv/advert_data.go @@ -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() { @@ -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 { diff --git a/dv/table/prefix_table.go b/dv/table/prefix_table.go index 27795038..6844f3fe 100644 --- a/dv/table/prefix_table.go +++ b/dv/table/prefix_table.go @@ -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 @@ -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) @@ -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) diff --git a/fw/mgmt/thread.go b/fw/mgmt/thread.go index 73713ee4..aaed62b8 100644 --- a/fw/mgmt/thread.go +++ b/fw/mgmt/thread.go @@ -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) diff --git a/std/ndn/client.go b/std/ndn/client.go index 3ecde94f..ce9d39ef 100644 --- a/std/ndn/client.go +++ b/std/ndn/client.go @@ -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. @@ -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 @@ -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). diff --git a/std/object/client_produce.go b/std/object/client_produce.go index 4fa9ff33..0c157060 100644 --- a/std/object/client_produce.go +++ b/std/object/client_produce.go @@ -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) diff --git a/tools/putchunks.go b/tools/putchunks.go index 9851e548..83596057 100644 --- a/tools/putchunks.go +++ b/tools/putchunks.go @@ -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)