Skip to content

Commit

Permalink
Group all token IDs to be fetched from vault in one query and reduce …
Browse files Browse the repository at this point in the history
…waiting time

Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Sep 2, 2024
1 parent 611b62d commit 914fa56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
19 changes: 13 additions & 6 deletions token/core/common/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/tracing"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/logging"
"go.opentelemetry.io/otel/trace"

Expand Down Expand Up @@ -37,6 +38,7 @@ type TokenAndMetadataDeserializer[T LedgerToken, M any] interface {

type VaultLedgerTokenLoader[T any] struct {
Logger logging.Logger
tracer trace.Tracer
TokenVault TokenVault
Deserializer TokenDeserializer[T]

Expand All @@ -45,21 +47,25 @@ type VaultLedgerTokenLoader[T any] struct {
RetryDelay time.Duration
}

func NewLedgerTokenLoader[T any](logger logging.Logger, tokenVault TokenVault, deserializer TokenDeserializer[T]) *VaultLedgerTokenLoader[T] {
func NewLedgerTokenLoader[T any](logger logging.Logger, tracerProvider trace.TracerProvider, tokenVault TokenVault, deserializer TokenDeserializer[T]) *VaultLedgerTokenLoader[T] {
return &VaultLedgerTokenLoader[T]{
Logger: logger,
tracer: tracerProvider.Tracer("token_loader", tracing.WithMetricsOpts(tracing.MetricsOpts{Namespace: "tokensdk"})),
TokenVault: tokenVault,
Deserializer: deserializer,
NumRetries: 3,
RetryDelay: 3 * time.Second,
NumRetries: 6,
RetryDelay: 1 * time.Second,
}
}

// GetTokenOutputs takes an array of token identifiers (txID, index) and returns the corresponding token outputs
func (s *VaultLedgerTokenLoader[T]) GetTokenOutputs(ids []*token.ID) ([]T, error) {
func (s *VaultLedgerTokenLoader[T]) GetTokenOutputs(ctx context.Context, ids []*token.ID) (map[string]T, error) {
_, span := s.tracer.Start(ctx, "token_outputs_fetch")
defer span.End()
var err error
for i := 0; i < s.NumRetries; i++ {
tokens := make([]T, len(ids))
span.AddEvent("try_fetch")
tokens := make(map[string]T, len(ids))
counter := 0
err = s.TokenVault.GetTokenOutputs(ids, func(id *token.ID, bytes []byte) error {
if len(bytes) == 0 {
Expand All @@ -69,7 +75,7 @@ func (s *VaultLedgerTokenLoader[T]) GetTokenOutputs(ids []*token.ID) ([]T, error
if err != nil {
return errors.Wrapf(err, "failed deserializing token for id [%v][%s]", id, string(bytes))
}
tokens[counter] = ti
tokens[id.TxId] = ti
counter++
return nil
})
Expand All @@ -81,6 +87,7 @@ func (s *VaultLedgerTokenLoader[T]) GetTokenOutputs(ids []*token.ID) ([]T, error

// check if there is any token id whose corresponding transaction is pending
// if there is, then wait a bit and retry to load the outputs
span.AddEvent("check_any_pending")
anyPending, anyError := s.isAnyPending(ids...)
if anyError != nil {
err = anyError
Expand Down
29 changes: 21 additions & 8 deletions token/core/zkatdlog/nogh/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto/audit"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/crypto/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
token2 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -53,16 +54,28 @@ func (s *AuditorService) AuditorCheck(ctx context.Context, request *driver.Token
newCtx, span := s.tracer.Start(ctx, "auditor_check")
defer span.End()
s.Logger.Debugf("[%s] check token request validity, number of transfer actions [%d]...", txID, len(metadata.Transfers))
var inputTokens [][]*token.Token
span.AddEvent("check_token_validity")

tokenIDs := make([]*token2.ID, 0)
for i, transfer := range metadata.Transfers {
s.Logger.Debugf("[%s] transfer action [%d] contains [%d] inputs", txID, i, len(transfer.TokenIDs))
inputs, err := s.TokenCommitmentLoader.GetTokenOutputs(transfer.TokenIDs)
if err != nil {
return errors.Wrapf(err, "failed getting token outputs to perform auditor check")
tokenIDs = append(tokenIDs, transfer.TokenIDs...)
}

span.AddEvent("load_token_outputs")
tokenMap, err := s.TokenCommitmentLoader.GetTokenOutputs(newCtx, tokenIDs)
if err != nil {
return errors.Wrapf(err, "failed getting token outputs to perform auditor check")
}
s.Logger.Debugf("loaded [%d] corresponding inputs for TX [%s]", len(tokenIDs), txID)

inputTokens := make([][]*token.Token, len(metadata.Transfers))
for i, transfer := range metadata.Transfers {
inputTokens[i] = make([]*token.Token, len(transfer.TokenIDs))
for j, tokenID := range transfer.TokenIDs {
if tok, ok := tokenMap[tokenID.TxId]; ok {
inputTokens[i][j] = tok
}
}
s.Logger.Debugf("[%s] transfer action [%d] contains [%d] inputs, loaded corresponding inputs [%d]", txID, i, len(transfer.TokenIDs), len(inputs))
inputTokens = append(inputTokens, inputs)
}

span.AddEvent("load_public_params")
Expand All @@ -78,7 +91,7 @@ func (s *AuditorService) AuditorCheck(ctx context.Context, request *driver.Token
math.Curves[pp.Curve],
)
span.AddEvent("start_auditor_check")
err := auditor.Check(
err = auditor.Check(
newCtx,
request,
metadata,
Expand Down
2 changes: 1 addition & 1 deletion token/core/zkatdlog/nogh/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (d *Driver) NewTokenService(_ driver.ServiceProvider, networkID string, cha
zkatdlog.NewAuditorService(
logger,
ppm,
common.NewLedgerTokenLoader[*token3.Token](logger, qe, tokDeserializer),
common.NewLedgerTokenLoader[*token3.Token](logger, d.tracerProvider, qe, tokDeserializer),
deserializer,
driverMetrics,
d.tracerProvider,
Expand Down
2 changes: 1 addition & 1 deletion token/core/zkatdlog/nogh/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type TokenCommitmentLoader interface {
GetTokenOutputs(ids []*token2.ID) ([]*token.Token, error)
GetTokenOutputs(ctx context.Context, ids []*token2.ID) (map[string]*token.Token, error)
}

type TokenLoader interface {
Expand Down

0 comments on commit 914fa56

Please sign in to comment.