Skip to content

Commit

Permalink
Fix saving hole punch attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Sep 1, 2022
1 parent 3a36c40 commit 33c1246
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions cmd/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (s Server) TrackHolePunch(ctx context.Context, req *pb.TrackHolePunchReques
}
defer db.DeferRollback(txn)

dbAttempts := make([]*models.HolePunchAttempt, len(req.HolePunchAttempts))
dbhpas := make([]*models.HolePunchAttempt, len(req.HolePunchAttempts))
for i, hpa := range req.HolePunchAttempts {
if hpa.OpenedAt == nil {
return nil, errors.Wrapf(err, "opened at in attempt %d is nil", i)
Expand All @@ -371,21 +371,7 @@ func (s Server) TrackHolePunch(ctx context.Context, req *pb.TrackHolePunchReques
startedAt = &t
}

maddrs := make([]multiaddr.Multiaddr, len(hpa.MultiAddresses))
for j, maddrBytes := range hpa.MultiAddresses {
maddr, err := multiaddr.NewMultiaddrBytes(maddrBytes)
if err != nil {
return nil, errors.Wrap(err, "hole punch attempt multi addr from bytes")
}
maddrs[j] = maddr
}

dbMaddrs, err := s.DBClient.UpsertMultiAddresses(ctx, txn, maddrs)
if err != nil {
return nil, errors.Wrap(err, "hole punch attempt multi addresses")
}

dbhpa := &models.HolePunchAttempt{
dbhpas[i] = &models.HolePunchAttempt{
OpenedAt: time.Unix(0, int64(*hpa.OpenedAt)),
StartedAt: null.TimeFromPtr(startedAt),
EndedAt: time.Unix(0, int64(*hpa.EndedAt)),
Expand All @@ -395,12 +381,6 @@ func (s Server) TrackHolePunch(ctx context.Context, req *pb.TrackHolePunchReques
Error: null.NewString(hpa.GetError(), hpa.GetError() != ""),
DirectDialError: null.NewString(hpa.GetDirectDialError(), hpa.GetDirectDialError() != ""),
}

if err = dbhpa.SetMultiAddresses(ctx, txn, false, dbMaddrs...); err != nil {
return nil, errors.Wrap(err, "upsert listen multi addresses")
}

dbAttempts[i] = dbhpa
}

lmaddrs := make([]multiaddr.Multiaddr, len(req.ListenMultiAddresses))
Expand Down Expand Up @@ -438,10 +418,30 @@ func (s Server) TrackHolePunch(ctx context.Context, req *pb.TrackHolePunchReques
return nil, errors.Wrap(err, "insert hole punch result")
}

if err = hpr.AddHolePunchAttempts(ctx, txn, true, dbAttempts...); err != nil {
if err = hpr.AddHolePunchAttempts(ctx, txn, true, dbhpas...); err != nil {
return nil, errors.Wrap(err, "add attempts to hole punch result")
}

for i, hpa := range req.HolePunchAttempts {
hpamaddrs := make([]multiaddr.Multiaddr, len(hpa.MultiAddresses))
for j, maddrBytes := range hpa.MultiAddresses {
maddr, err := multiaddr.NewMultiaddrBytes(maddrBytes)
if err != nil {
return nil, errors.Wrap(err, "hole punch attempt multi addr from bytes")
}
hpamaddrs[j] = maddr
}

dbHPAMaddrs, err := s.DBClient.UpsertMultiAddresses(ctx, txn, hpamaddrs)
if err != nil {
return nil, errors.Wrap(err, "hole punch attempt multi addresses")
}

if err = dbhpas[i].SetMultiAddresses(ctx, txn, false, dbHPAMaddrs...); err != nil {
return nil, errors.Wrap(err, "upsert hole punch attempt multi addresses")
}
}

omaddrs := make([]multiaddr.Multiaddr, len(req.OpenMultiAddresses))
for i, maddrBytes := range req.OpenMultiAddresses {
maddr, err := multiaddr.NewMultiaddrBytes(maddrBytes)
Expand Down

0 comments on commit 33c1246

Please sign in to comment.