-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3854e6
commit b99967c
Showing
6 changed files
with
414 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 The Accumulate Authors | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
package healing | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"gitlab.com/accumulatenetwork/accumulate/pkg/api/v3" | ||
"gitlab.com/accumulatenetwork/accumulate/pkg/api/v3/message" | ||
"gitlab.com/accumulatenetwork/accumulate/pkg/errors" | ||
"gitlab.com/accumulatenetwork/accumulate/pkg/types/messaging" | ||
"gitlab.com/accumulatenetwork/accumulate/pkg/url" | ||
"gitlab.com/accumulatenetwork/accumulate/protocol" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type SequencedInfo struct { | ||
Source string | ||
Destination string | ||
Number uint64 | ||
ID *url.TxID | ||
} | ||
|
||
// ResolveSequenced resolves an anchor or synthetic message (a sequenced | ||
// message). If the client's address is non-nil, the query will be sent to that | ||
// address. Otherwise, all of the source partition's nodes will be queried in | ||
// order until one responds. | ||
func ResolveSequenced[T messaging.Message](ctx context.Context, client message.AddressedClient, net *NetworkInfo, srcId, dstId string, seqNum uint64, anchor bool) (*api.MessageRecord[T], error) { | ||
srcUrl := protocol.PartitionUrl(srcId) | ||
dstUrl := protocol.PartitionUrl(dstId) | ||
|
||
var account string | ||
if anchor { | ||
account = protocol.AnchorPool | ||
} else { | ||
account = protocol.Synthetic | ||
} | ||
|
||
// If the client has an address, use that | ||
if client.Address != nil { | ||
slog.InfoCtx(ctx, "Querying node", "address", client.Address) | ||
res, err := client.Private().Sequence(ctx, srcUrl.JoinPath(account), dstUrl, seqNum) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r2, err := api.MessageRecordAs[T](res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r2, nil | ||
} | ||
|
||
// Otherwise try each node until one succeeds | ||
slog.InfoCtx(ctx, "Resolving the message ID", "source", srcId, "destination", dstId, "number", seqNum) | ||
for peer := range net.Peers[strings.ToLower(srcId)] { | ||
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) | ||
defer cancel() | ||
|
||
slog.InfoCtx(ctx, "Querying node", "id", peer) | ||
res, err := client.ForPeer(peer).Private().Sequence(ctx, srcUrl.JoinPath(account), dstUrl, seqNum) | ||
if err != nil { | ||
slog.ErrorCtx(ctx, "Query failed", "error", err) | ||
continue | ||
} | ||
|
||
r2, err := api.MessageRecordAs[T](res) | ||
if err != nil { | ||
slog.ErrorCtx(ctx, "Query failed", "error", err) | ||
continue | ||
} | ||
|
||
return r2, nil | ||
} | ||
|
||
return nil, errors.UnknownError.WithFormat("cannot resolve %s→%s #%d", srcId, dstId, seqNum) | ||
} |
Oops, something went wrong.