Skip to content

Commit

Permalink
Fix timeout (#33)
Browse files Browse the repository at this point in the history
* bump timeout

* embracing asynchronicity
  • Loading branch information
decentralgabe authored Oct 31, 2023
1 parent 4abb58c commit d35f96f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion impl/pkg/server/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (r *PKARRRouter) PutRecord(c *gin.Context) {
Sig: sigBytes,
Seq: seq,
}
if _, err = r.service.PublishPKARR(c, request); err != nil {
if err = r.service.PublishPKARR(c, request); err != nil {
LoggingRespondErrWithMsg(c, err, "failed to publish pkarr record", http.StatusInternalServerError)
return
}
Expand Down
6 changes: 3 additions & 3 deletions impl/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func NewServer(cfg *config.Config, shutdown chan os.Signal) (*Server, error) {
Server: &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.ServerConfig.APIHost, cfg.ServerConfig.APIPort),
Handler: handler,
ReadTimeout: time.Second * 5,
ReadHeaderTimeout: time.Second * 5,
WriteTimeout: time.Second * 5,
ReadTimeout: time.Second * 15,
ReadHeaderTimeout: time.Second * 15,
WriteTimeout: time.Second * 15,
},
cfg: cfg,
svc: pkarrService,
Expand Down
33 changes: 28 additions & 5 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"encoding/base64"
"errors"

"github.com/TBD54566975/ssi-sdk/util"
"github.com/anacrolix/dht/v2/bep44"
Expand Down Expand Up @@ -56,6 +57,23 @@ type PublishPKARRRequest struct {
Seq int64 `validate:"required"`
}

// isValid returns an error if the request is invalid
// also validates the signature
func (p PublishPKARRRequest) isValid() error {
if err := util.IsValidStruct(p); err != nil {
return err
}
// validate the signature
bv, err := bencode.Marshal(p.V)
if err != nil {
return err
}
if !bep44.Verify(p.K[:], nil, p.Seq, bv, p.Sig[:]) {
return errors.New("signature is invalid")
}
return nil
}

func (p PublishPKARRRequest) toRecord() storage.PKARRRecord {
encoding := base64.RawURLEncoding
return storage.PKARRRecord{
Expand All @@ -67,20 +85,25 @@ func (p PublishPKARRRequest) toRecord() storage.PKARRRecord {
}

// PublishPKARR stores the record in the db, publishes the given PKARR to the DHT, and returns the z-base-32 encoded ID
func (s *PKARRService) PublishPKARR(ctx context.Context, request PublishPKARRRequest) (string, error) {
if err := util.IsValidStruct(request); err != nil {
return "", err
func (s *PKARRService) PublishPKARR(ctx context.Context, request PublishPKARRRequest) error {
if err := request.isValid(); err != nil {
return err
}

// TODO(gabe): if putting to the DHT fails we should note that in the db and retry later
if err := s.db.WriteRecord(request.toRecord()); err != nil {
return "", err
return err
}
return s.dht.Put(ctx, bep44.Put{

// return here and put it in the DHT asynchronously
go s.dht.Put(ctx, bep44.Put{
V: request.V,
K: &request.K,
Sig: request.Sig,
Seq: request.Seq,
})

return nil
}

// GetPKARRResponse is the response to a get PKARR request
Expand Down
48 changes: 43 additions & 5 deletions impl/pkg/service/pkarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestPKARRService(t *testing.T) {
require.NotEmpty(t, svc)

t.Run("test put bad record", func(t *testing.T) {
_, err := svc.PublishPKARR(context.Background(), PublishPKARRRequest{})
err := svc.PublishPKARR(context.Background(), PublishPKARRRequest{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "validation for 'V' failed on the 'required' tag")
})
Expand All @@ -29,30 +29,68 @@ func TestPKARRService(t *testing.T) {
assert.Nil(t, got)
})

t.Run("test record with a bad signature", func(t *testing.T) {
// create a did doc as a packet to store
sk, doc, err := did.GenerateDIDDHT(did.CreateDIDDHTOpts{})
require.NoError(t, err)
require.NotEmpty(t, doc)

d := did.DHT(doc.ID)
packet, err := d.ToDNSPacket(*doc, nil)
assert.NoError(t, err)
assert.NotEmpty(t, packet)

putMsg, err := dht.CreatePKARRPublishRequest(sk, *packet)
require.NoError(t, err)
require.NotEmpty(t, putMsg)

err = svc.PublishPKARR(context.Background(), PublishPKARRRequest{
V: putMsg.V.([]byte),
K: *putMsg.K,
Sig: putMsg.Sig,
Seq: putMsg.Seq,
})
assert.NoError(t, err)

// invalidate the signature
putMsg.Sig[0] = 0
err = svc.PublishPKARR(context.Background(), PublishPKARRRequest{
V: putMsg.V.([]byte),
K: *putMsg.K,
Sig: putMsg.Sig,
Seq: putMsg.Seq,
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "signature is invalid")
})

t.Run("test put and get record", func(t *testing.T) {
// create a did doc as a packet to store
sk, doc, err := did.GenerateDIDDHT(did.CreateDIDDHTOpts{})
require.NoError(t, err)
require.NotEmpty(t, doc)

packet, err := did.DHT(doc.ID).ToDNSPacket(*doc, nil)
d := did.DHT(doc.ID)
packet, err := d.ToDNSPacket(*doc, nil)
assert.NoError(t, err)
assert.NotEmpty(t, packet)

putMsg, err := dht.CreatePKARRPublishRequest(sk, *packet)
require.NoError(t, err)
require.NotEmpty(t, putMsg)

id, err := svc.PublishPKARR(context.Background(), PublishPKARRRequest{
err = svc.PublishPKARR(context.Background(), PublishPKARRRequest{
V: putMsg.V.([]byte),
K: *putMsg.K,
Sig: putMsg.Sig,
Seq: putMsg.Seq,
})
assert.NoError(t, err)
assert.NotEmpty(t, id)

got, err := svc.GetPKARR(context.Background(), id)
suffix, err := d.Suffix()
assert.NoError(t, err)

got, err := svc.GetPKARR(context.Background(), suffix)
assert.NoError(t, err)
assert.NotEmpty(t, got)
assert.Equal(t, putMsg.V, got.V)
Expand Down

0 comments on commit d35f96f

Please sign in to comment.