Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sequence number to advertisements #159

Draft
wants to merge 1 commit into
base: release-v0.5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions ingest/schema/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/record"
"github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
)

var log = logging.Logger("indexer/schema")
Expand Down Expand Up @@ -93,9 +94,14 @@ func signaturePayload(ad *Advertisement, oldFormat bool) ([]byte, error) {
addrsLen += len(addr)
}

var seqLen int
if ad.SeqNum != nil {
seqLen = varint.UvarintSize(*ad.SeqNum)
}

// Signature data is previousID+entries+metadata+isRm
var sigBuf bytes.Buffer
sigBuf.Grow(len(bindex) + len(ent) + len(ad.Provider) + addrsLen + len(ad.Metadata) + 1)
sigBuf.Grow(len(bindex) + len(ent) + len(ad.Provider) + addrsLen + len(ad.Metadata) + 1 + seqLen)
sigBuf.Write(bindex)
sigBuf.Write(ent)
sigBuf.WriteString(ad.Provider)
Expand All @@ -108,6 +114,9 @@ func signaturePayload(ad *Advertisement, oldFormat bool) ([]byte, error) {
} else {
sigBuf.WriteByte(0)
}
if ad.SeqNum != nil {
sigBuf.Write(varint.ToUvarint(*ad.SeqNum))
}

// Generates the old (incorrect) data payload used for signature. This is
// only for compatibility with existing advertisements that have the old
Expand Down Expand Up @@ -147,8 +156,11 @@ func extendedProviderSignaturePayload(ad *Advertisement, p *Provider) ([]byte, e
for _, addr := range p.Addresses {
addrsLen += len(addr)
}

sigBuf.Grow(len(bindex) + len(ent) + len(ad.Provider) + len(ad.ContextID) + len(p.ID) + addrsLen + len(p.Metadata) + 1)
var seqLen int
if ad.SeqNum != nil {
seqLen = varint.UvarintSize(*ad.SeqNum)
}
sigBuf.Grow(len(bindex) + len(ent) + len(ad.Provider) + len(ad.ContextID) + len(p.ID) + addrsLen + len(p.Metadata) + 1 + seqLen)
sigBuf.Write(bindex)
sigBuf.Write(ent)
sigBuf.WriteString(ad.Provider)
Expand All @@ -163,6 +175,9 @@ func extendedProviderSignaturePayload(ad *Advertisement, p *Provider) ([]byte, e
} else {
sigBuf.WriteByte(0)
}
if ad.SeqNum != nil {
sigBuf.Write(varint.ToUvarint(*ad.SeqNum))
}

return multihash.Sum(sigBuf.Bytes(), multihash.SHA2_256, -1)
}
Expand Down
3 changes: 2 additions & 1 deletion ingest/schema/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func testSignAndVerify(t *testing.T, signer func(*stischema.Advertisement, crypt
require.NoError(t, err)
elnk, err := lsys.Store(ipld.LinkContext{}, stischema.Linkproto, node)
require.NoError(t, err)

seq := uint64(123)
adv := stischema.Advertisement{
Provider: "12D3KooWKRyzVWW6ChFjQjK4miCty85Niy48tpPV95XdKu1BcvMA",
Addresses: []string{
Expand All @@ -60,6 +60,7 @@ func testSignAndVerify(t *testing.T, signer func(*stischema.Advertisement, crypt
Entries: elnk,
ContextID: []byte("test-context-id"),
Metadata: []byte("test-metadata"),
SeqNum: &seq,
}
err = signer(&adv, priv)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions ingest/schema/schema.ipldsch
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Advertisement struct {
Metadata Bytes
# IsRm specifies whether this advertisement represents the content are no longer retrievalbe fom the provider.
IsRm Bool
# SeqNum is a monotonically increasing sequence number that gives the advertisement's distance in the chain.
SeqNum optional Int
# ExtendedProvider might optionally specify a set of providers where the ad entries are available from.
# See: https://github.com/ipni/storetheindex/blob/main/doc/ingest.md#extendedprovider
ExtendedProvider optional ExtendedProvider
Expand Down
8 changes: 8 additions & 0 deletions ingest/schema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type (
ContextID []byte
Metadata []byte
IsRm bool
SeqNum *uint64
ExtendedProvider *ExtendedProvider
}
EntryChunk struct {
Expand Down Expand Up @@ -100,6 +101,13 @@ func (a Advertisement) PreviousCid() cid.Cid {
return a.PreviousID.(cidlink.Link).Cid
}

func (a Advertisement) Sequence() uint64 {
if a.SeqNum == nil {
return 0
}
return uint64(*a.SeqNum)
}

func (a Advertisement) Validate() error {
if len(a.ContextID) > MaxContextIDLen {
return errors.New("context id too long")
Expand Down
2 changes: 2 additions & 0 deletions ingest/schema/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func Test_LinkLoadNoEntries(t *testing.T) {
func generateAdvertisement() *stischema.Advertisement {
mhs := test.RandomMultihashes(7)
prev := ipld.Link(cidlink.Link{Cid: cid.NewCidV1(cid.Raw, mhs[0])})
seq := uint64(54321)
return &stischema.Advertisement{
PreviousID: prev,
Provider: mhs[1].String(),
Expand All @@ -280,6 +281,7 @@ func generateAdvertisement() *stischema.Advertisement {
Metadata: mhs[5],
Signature: mhs[6],
IsRm: false,
SeqNum: &seq,
}
}

Expand Down
Loading