Skip to content

Commit

Permalink
republishing and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Oct 21, 2023
1 parent b3e4594 commit e5ff0b5
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 86 deletions.
13 changes: 8 additions & 5 deletions impl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func (e EnvironmentVariable) String() string {
}

type Config struct {
ServerConfig ServiceConfig `toml:"server"`
DHTConfig DHTServiceConfig `toml:"dht"`
ServerConfig ServiceConfig `toml:"server"`
DHTConfig DHTServiceConfig `toml:"dht"`
PKARRConfig PKARRServiceConfig `toml:"pkarr"`
}

type ServiceConfig struct {
Expand All @@ -53,9 +54,8 @@ type DHTServiceConfig struct {
BootstrapPeers []string `toml:"bootstrap_peers"`
}

type GossipServiceConfig struct {
// if set, the API will only accept signed messages
EnforceSignedMessages bool `toml:"enforce_signed_messages"`
type PKARRServiceConfig struct {
RepublishCRON string `toml:"republish_cron"`
}

func GetDefaultConfig() Config {
Expand All @@ -71,6 +71,9 @@ func GetDefaultConfig() Config {
DHTConfig: DHTServiceConfig{
BootstrapPeers: GetDefaultBootstrapPeers(),
},
PKARRConfig: PKARRServiceConfig{
RepublishCRON: "0 */2 * * *",
},
}
}

Expand Down
5 changes: 4 additions & 1 deletion impl/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ db_file = "diddht.db"

[dht]
bootstrap_peers = ["router.magnets.im:6881", "router.bittorrent.com:6881", "dht.transmissionbt.com:6881",
"router.utorrent.com:6881", "router.nuh.dev:6881"]
"router.utorrent.com:6881", "router.nuh.dev:6881"]

[pkarr]
republish_cron = "0 */2 * * *" # every 2 hours
3 changes: 3 additions & 0 deletions impl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-co-op/gocron v1.35.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
Expand Down Expand Up @@ -98,6 +99,7 @@ require (
github.com/piprate/json-gold v0.5.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -112,6 +114,7 @@ require (
github.com/swaggo/swag v1.8.12 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions impl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ github.com/glycerine/go-unsnap-stream v0.0.0-20190901134440-81cf024a9e0a/go.mod
github.com/glycerine/goconvey v0.0.0-20180728074245-46e3a41ad493/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
github.com/go-co-op/gocron v1.35.2 h1:lG3rdA9TqBBC/PtT2ukQqgLm6jEepnAzz3+OQetvPTE=
github.com/go-co-op/gocron v1.35.2/go.mod h1:NLi+bkm4rRSy1F8U7iacZOz0xPseMoIOnvabGoSe/no=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -454,9 +456,12 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 h1:Lt9DzQALzHoDwMBGJ6v8ObDPR0dzr2a6sXTB1Fq7IHs=
Expand Down Expand Up @@ -548,6 +553,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
45 changes: 45 additions & 0 deletions impl/internal/dht/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dht

import (
"time"

"github.com/go-co-op/gocron"
"github.com/pkg/errors"
)

// Scheduler runs crob jobs asynchronously, designed to just schedule one job
type Scheduler struct {
scheduler *gocron.Scheduler
job *gocron.Job
}

// NewScheduler creates a new scheduler
func NewScheduler() Scheduler {
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
return Scheduler{scheduler: s}
}

// Schedule schedules a job to run and starts it asynchronously
func (s *Scheduler) Schedule(cron string, job func()) error {
if s.job != nil {
return errors.New("job already scheduled")
}
j, err := s.scheduler.Cron(cron).Do(job)
if err != nil {
return err
}
s.job = j
s.Start()
return nil
}

// Start starts the scheduler
func (s *Scheduler) Start() {
s.scheduler.StartAsync()
}

// Stop stops the scheduler
func (s *Scheduler) Stop() {
s.scheduler.Stop()
}
12 changes: 4 additions & 8 deletions impl/pkg/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/anacrolix/dht/v2/bep44"
"github.com/anacrolix/dht/v2/exts/getput"
"github.com/anacrolix/torrent/types/infohash"
"github.com/sirupsen/logrus"

dhtint "github.com/TBD54566975/did-dht-method/internal/dht"
"github.com/TBD54566975/did-dht-method/internal/util"
Expand All @@ -25,8 +24,7 @@ func NewDHT(bootstrapPeers []string) (*DHT, error) {
c.StartingNodes = func() ([]dht.Addr, error) { return dht.ResolveHostPorts(bootstrapPeers) }
s, err := dht.NewServer(c)
if err != nil {
logrus.WithError(err).Error("failed to create dht server")
return nil, err
return nil, errutil.LoggingErrorMsg(err, "failed to create dht server")
}
return &DHT{Server: s}, nil
}
Expand All @@ -47,8 +45,7 @@ func (d *DHT) Put(ctx context.Context, request bep44.Put) (string, error) {
func (d *DHT) Get(ctx context.Context, key string) (*getput.GetResult, error) {
z32Decoded, err := util.Z32Decode(key)
if err != nil {
logrus.WithError(err).Error("failed to decode key")
return nil, err
return nil, errutil.LoggingErrorMsg(err, "failed to decode key")
}
res, t, err := getput.Get(ctx, infohash.HashBytes(z32Decoded), d.Server, nil, nil)
if err != nil {
Expand All @@ -58,13 +55,12 @@ func (d *DHT) Get(ctx context.Context, key string) (*getput.GetResult, error) {
}

// GetFull returns the full BEP-44 result for the given key from the DHT, using our modified
// implementation of getput.Get. IT should only be used when it's needed to get the signature
// implementation of getput.Get. It should ONLY be used when it's needed to get the signature
// data for a record.
func (d *DHT) GetFull(ctx context.Context, key string) (*dhtint.FullGetResult, error) {
z32Decoded, err := util.Z32Decode(key)
if err != nil {
logrus.WithError(err).Error("failed to decode key")
return nil, err
return nil, errutil.LoggingErrorMsg(err, "failed to decode key")
}
res, t, err := dhtint.Get(ctx, infohash.HashBytes(z32Decoded), d.Server, nil, nil)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions impl/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Server struct {
shutdown chan os.Signal

cfg *config.Config
svc *service.DIDService
svc *service.PKARRService
}

// NewServer returns a new instance of Server with the given db and host.
Expand All @@ -47,10 +47,6 @@ func NewServer(cfg *config.Config, shutdown chan os.Signal) (*Server, error) {
if err != nil {
return nil, util.LoggingErrorMsg(err, "could not instantiate pkarr service")
}
didDHTService, err := service.NewDIDService(cfg, db, *pkarrService)
if err != nil {
return nil, util.LoggingErrorMsg(err, "could not instantiate did dht service")
}

handler.GET("/health", Health)

Expand All @@ -73,7 +69,7 @@ func NewServer(cfg *config.Config, shutdown chan os.Signal) (*Server, error) {
WriteTimeout: time.Second * 5,
},
cfg: cfg,
svc: didDHTService,
svc: pkarrService,
handler: handler,
shutdown: shutdown,
}, nil
Expand Down
91 changes: 40 additions & 51 deletions impl/pkg/service/did.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
package service

import (
"context"

"github.com/TBD54566975/ssi-sdk/did"
"github.com/TBD54566975/ssi-sdk/util"
"github.com/pkg/errors"

"github.com/TBD54566975/did-dht-method/config"
"github.com/TBD54566975/did-dht-method/pkg/storage"
)

// DIDService is the DID DHT service responsible for managing the DHT and reading/writing records
type DIDService struct {
cfg *config.Config
db *storage.Storage
pkarr PKARRService
}

// NewDIDService returns a new instance of the DHT service
func NewDIDService(cfg *config.Config, db *storage.Storage, pkarr PKARRService) (*DIDService, error) {
if cfg == nil {
return nil, util.LoggingNewError("config is required")
}
if db == nil && !db.IsOpen() {
return nil, util.LoggingNewError("storage is required be non-nil and to be open")
}
return &DIDService{
cfg: cfg,
db: db,
pkarr: pkarr,
}, nil
}

type PublishDIDRequest struct {
}

func (s *DIDService) PublishDID(_ context.Context, _ PublishDIDRequest) error {
return errors.New("unimplemented")
}

func (s *DIDService) GetDID(_ context.Context, did string) (*did.Document, error) {
return nil, errors.New("unimplemented")
}

func (s *DIDService) ListDIDs(_ context.Context) ([]did.Document, error) {
return nil, errors.New("unimplemented")
}

func (s *DIDService) DeleteDID(_ context.Context, _ string) error {
return errors.New("unimplemented")
}
// // DIDService is the DID DHT service responsible for managing the DHT and reading/writing records
// type DIDService struct {
// cfg *config.Config
// db *storage.Storage
// pkarr PKARRService
// }
//
// // NewDIDService returns a new instance of the DHT service
// func NewDIDService(cfg *config.Config, db *storage.Storage, pkarr PKARRService) (*DIDService, error) {
// if cfg == nil {
// return nil, util.LoggingNewError("config is required")
// }
// if db == nil && !db.IsOpen() {
// return nil, util.LoggingNewError("storage is required be non-nil and to be open")
// }
// return &DIDService{
// cfg: cfg,
// db: db,
// pkarr: pkarr,
// }, nil
// }
//
// type PublishDIDRequest struct {
// }
//
// func (s *DIDService) PublishDID(_ context.Context, _ PublishDIDRequest) error {
// return errors.New("unimplemented")
// }
//
// func (s *DIDService) GetDID(_ context.Context, did string) (*did.Document, error) {
// return nil, errors.New("unimplemented")
// }
//
// func (s *DIDService) ListDIDs(_ context.Context) ([]did.Document, error) {
// return nil, errors.New("unimplemented")
// }
//
// func (s *DIDService) DeleteDID(_ context.Context, _ string) error {
// return errors.New("unimplemented")
// }
Loading

0 comments on commit e5ff0b5

Please sign in to comment.