From 0bbdc8476452215e035852359185bfbca83b05b1 Mon Sep 17 00:00:00 2001 From: Gabe <7622243+decentralgabe@users.noreply.github.com> Date: Fri, 29 Mar 2024 13:00:04 -0700 Subject: [PATCH] Fix put (#149) * update deps * fix put * remove print * change err to warning * update codeql version --- .github/workflows/codeql-analysis.yml | 6 +++--- impl/cmd/main.go | 7 +++---- impl/go.mod | 2 +- impl/go.sum | 4 ++-- impl/internal/cli/store.go | 16 ++++++++-------- impl/internal/did/client_test.go | 1 + impl/pkg/dht/dht.go | 11 +++++++++-- impl/pkg/pkarr/record.go | 3 ++- impl/pkg/service/pkarr.go | 8 +++++++- 9 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 7dcb290e..9f65f63c 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -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 \ No newline at end of file + uses: github/codeql-action/analyze@v3 \ No newline at end of file diff --git a/impl/cmd/main.go b/impl/cmd/main.go index 229b62ed..f143354e 100644 --- a/impl/cmd/main.go +++ b/impl/cmd/main.go @@ -11,6 +11,7 @@ import ( "syscall" "time" + "github.com/TBD54566975/ssi-sdk/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -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) diff --git a/impl/go.mod b/impl/go.mod index 8ab21b22..07816d5b 100644 --- a/impl/go.mod +++ b/impl/go.mod @@ -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 diff --git a/impl/go.sum b/impl/go.sum index 71f5abd0..fac6c8fc 100644 --- a/impl/go.sum +++ b/impl/go.sum @@ -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= diff --git a/impl/internal/cli/store.go b/impl/internal/cli/store.go index f41c35e2..2ba0819f 100644 --- a/impl/internal/cli/store.go +++ b/impl/internal/cli/store.go @@ -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") } @@ -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} @@ -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") } diff --git a/impl/internal/did/client_test.go b/impl/internal/did/client_test.go index e693a7e4..ffc52a70 100644 --- a/impl/internal/did/client_test.go +++ b/impl/internal/did/client_test.go @@ -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) diff --git a/impl/pkg/dht/dht.go b/impl/pkg/dht/dht.go index 76681b04..0f0b580a 100644 --- a/impl/pkg/dht/dht.go +++ b/impl/pkg/dht/dht.go @@ -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 } @@ -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 }) diff --git a/impl/pkg/pkarr/record.go b/impl/pkg/pkarr/record.go index ac2628a4..0f250f9d 100644 --- a/impl/pkg/pkarr/record.go +++ b/impl/pkg/pkarr/record.go @@ -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 { @@ -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 { diff --git a/impl/pkg/service/pkarr.go b/impl/pkg/service/pkarr.go index ea5a645d..a560c31f 100644 --- a/impl/pkg/service/pkarr.go +++ b/impl/pkg/service/pkarr.go @@ -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" @@ -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") } }() @@ -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++