Skip to content

Commit

Permalink
Fix put (#149)
Browse files Browse the repository at this point in the history
* update deps

* fix put

* remove print

* change err to warning

* update codeql version
  • Loading branch information
decentralgabe authored Mar 29, 2024
1 parent 5176956 commit 0bbdc84
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
7 changes: 3 additions & 4 deletions impl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"syscall"
"time"

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

Expand Down Expand Up @@ -81,14 +82,12 @@ func run() error {

d, err := dht.NewDHT(cfg.DHTConfig.BootstrapPeers)
if err != nil {
logrus.WithError(err).Error("failed to instantiate dht")
return err
return util.LoggingErrorMsg(err, "failed to instantiate dht")
}

s, err := server.NewServer(cfg, shutdown, d)
if err != nil {
logrus.WithError(err).Error("could not start http services")
return err
return util.LoggingErrorMsg(err, "could not start http services")
}

serverErrors := make(chan error, 1)
Expand Down
2 changes: 1 addition & 1 deletion impl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/TBD54566975/ssi-sdk v0.0.4-alpha.0.20240321215515-97ccd06a631d
github.com/allegro/bigcache/v3 v3.1.0
github.com/anacrolix/dht/v2 v2.20.0
github.com/anacrolix/dht/v2 v2.21.1
github.com/anacrolix/log v0.14.0
github.com/anacrolix/torrent v1.52.5
github.com/gin-contrib/cors v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions impl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3b
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/anacrolix/chansync v0.3.0 h1:lRu9tbeuw3wl+PhMu/r+JJCRu5ArFXIluOgdF0ao6/U=
github.com/anacrolix/chansync v0.3.0/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=
github.com/anacrolix/dht/v2 v2.20.0 h1:eDx9lfE9iCSf5sPK0290GToHURNhEFuUGN8iyvhvJDk=
github.com/anacrolix/dht/v2 v2.20.0/go.mod h1:SDGC+sEs1pnO2sJGYuhvIis7T8749dDHNfcjtdH4e3g=
github.com/anacrolix/dht/v2 v2.21.1 h1:s1rKkfLLcmBHKv4v/mtMkIeHIEptzEFiB6xVu54+5/o=
github.com/anacrolix/dht/v2 v2.21.1/go.mod h1:SDGC+sEs1pnO2sJGYuhvIis7T8749dDHNfcjtdH4e3g=
github.com/anacrolix/envpprof v0.0.0-20180404065416-323002cec2fa/go.mod h1:KgHhUaQMc8cC0+cEflSgCFNFbKwi5h54gqtVn8yhP7c=
github.com/anacrolix/envpprof v1.0.0/go.mod h1:KgHhUaQMc8cC0+cEflSgCFNFbKwi5h54gqtVn8yhP7c=
github.com/anacrolix/envpprof v1.1.0/go.mod h1:My7T5oSqVfEn4MD4Meczkw/f5lSIndGAKu/0SM/rkf4=
Expand Down
16 changes: 8 additions & 8 deletions impl/internal/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

const (
diddhtDir = "/.diddht"
diddhtPath = diddhtDir + "/diddht.json"
didDHTDir = "/.diddht"
didDHTPath = didDHTDir + "/diddht.json"
)

// Read reads the diddht file and returns the identities.
func Read() (internal.Identities, error) {
homeDir, _ := os.UserHomeDir()
diddhtFile := homeDir + diddhtPath
diddhtFile := homeDir + didDHTPath
if _, err := os.Stat(diddhtFile); os.IsNotExist(err) {
return nil, util.LoggingErrorMsg(err, "failed to find diddht file")
}
Expand All @@ -33,14 +33,14 @@ func Read() (internal.Identities, error) {
// Write writes the given identity to the diddht file.
func Write(id string, identity internal.Identity) error {
homeDir, _ := os.UserHomeDir()
diddhtFile := homeDir + diddhtPath
didDHTFile := homeDir + didDHTPath
var identities internal.Identities
var err error
if _, err = os.Stat(diddhtFile); os.IsNotExist(err) {
if err = os.Mkdir(homeDir+diddhtDir, 0700); err != nil {
if _, err = os.Stat(didDHTFile); os.IsNotExist(err) {
if err = os.Mkdir(homeDir+didDHTDir, 0700); err != nil {
return util.LoggingErrorMsg(err, "failed to create diddht directory")
}
if _, err = os.Create(homeDir + diddhtPath); err != nil {
if _, err = os.Create(homeDir + didDHTPath); err != nil {
return util.LoggingErrorMsg(err, "failed to create diddht file")
}
identities = internal.Identities{id: identity}
Expand All @@ -60,7 +60,7 @@ func Write(id string, identity internal.Identity) error {
return util.LoggingErrorMsg(err, "failed to marshal identities")
}

f, _ := os.OpenFile(diddhtFile, os.O_WRONLY, os.ModeAppend)
f, _ := os.OpenFile(didDHTFile, os.O_WRONLY, os.ModeAppend)
if _, err = f.WriteString(string(identitiesBytes)); err != nil {
return util.LoggingErrorMsg(err, "failed to write identities to diddht file")
}
Expand Down
1 change: 1 addition & 0 deletions impl/internal/did/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

func TestClient(t *testing.T) {
client, err := NewGatewayClient("https://diddht.tbddev.org")

require.NoError(t, err)
require.NotNil(t, client)

Expand Down
11 changes: 9 additions & 2 deletions impl/pkg/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func NewDHT(bootstrapPeers []string) (*DHT, error) {
if err != nil {
return nil, errutil.LoggingErrorMsg(err, "failed to create dht server")
}
if _, err = s.Bootstrap(); err != nil {
return nil, errutil.LoggingErrorMsg(err, "failed to bootstrap the dht")
if tried, err := s.Bootstrap(); err != nil {
return nil, errutil.LoggingErrorMsg(err, "error bootstrapping")
} else {
logrus.WithField("bootstrap_peers", tried.NumResponses).Info("bootstrapped DHT successfully")
}
return &DHT{Server: s}, nil
}
Expand Down Expand Up @@ -77,6 +79,11 @@ func (d *DHT) Put(ctx context.Context, request bep44.Put) (string, error) {
ctx, span := telemetry.GetTracer().Start(ctx, "DHT.Put")
defer span.End()

// Check if there are any nodes in the DHT
if len(d.Server.Nodes()) == 0 {
logrus.Warn("no nodes available in the DHT for publishing")
}

t, err := getput.Put(ctx, request.Target(), d.Server, nil, func(int64) bep44.Put {
return request
})
Expand Down
3 changes: 2 additions & 1 deletion impl/pkg/pkarr/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/TBD54566975/ssi-sdk/util"
"github.com/anacrolix/dht/v2/bep44"
"github.com/anacrolix/torrent/bencode"
"github.com/tv42/zbase32"
)

type Record struct {
Expand Down Expand Up @@ -85,7 +86,7 @@ func (r Record) BEP44() bep44.Put {

func (r Record) String() string {
e := base64.RawURLEncoding
return fmt.Sprintf("pkarr.Record{K=%s V=%s Sig=%s Seq=%d}", e.EncodeToString(r.Key[:]), e.EncodeToString(r.Value), e.EncodeToString(r.Signature[:]), r.SequenceNumber)
return fmt.Sprintf("pkarr.Record{K=%s V=%s Sig=%s Seq=%d}", zbase32.EncodeToString(r.Key[:]), e.EncodeToString(r.Value), e.EncodeToString(r.Signature[:]), r.SequenceNumber)
}

func RecordFromBEP44(putMsg *bep44.Put) Record {
Expand Down
8 changes: 7 additions & 1 deletion impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/goccy/go-json"
"github.com/tv42/zbase32"

ssiutil "github.com/TBD54566975/ssi-sdk/util"
"github.com/allegro/bigcache/v3"
Expand Down Expand Up @@ -88,7 +89,11 @@ func (s *PkarrService) PublishPkarr(ctx context.Context, id string, record pkarr
// return here and put it in the DHT asynchronously
// TODO(gabe): consider a background process to monitor failures
go func() {
if _, err = s.dht.Put(ctx, record.BEP44()); err != nil {
// Create a new context with a timeout so that the parent context does not cancel the put
putCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if _, err = s.dht.Put(putCtx, record.BEP44()); err != nil {
logrus.WithError(err).Error("error from dht.Put")
}
}()
Expand Down Expand Up @@ -197,6 +202,7 @@ func (s *PkarrService) republish() {
logrus.WithField("record_count", len(allRecords)).Info("Republishing record")

for _, record := range allRecords {
logrus.Infof("Republishing record: %s", zbase32.EncodeToString(record.Key[:]))
if _, err = s.dht.Put(ctx, record.BEP44()); err != nil {
logrus.WithError(err).Error("failed to republish record")
errCnt++
Expand Down

0 comments on commit 0bbdc84

Please sign in to comment.