Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions token/services/selector/sherdlock/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func TestRistrettoCache_Integration(t *testing.T) {

// Clear cache
c.Clear()
c.Wait()

// Verify cleared
_, ok = c.Get("key1")
Expand Down
27 changes: 20 additions & 7 deletions token/services/tokens/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/events"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver"
"go.uber.org/zap/zapcore"
)

const (
Expand Down Expand Up @@ -163,13 +164,19 @@ func (t *DBTransaction) DeleteToken(ctx context.Context, tokenID token2.ID, dele

// The token is not in the local store, so there are no owners to notify.
if tok == nil {
logger.DebugfContext(ctx, "nothing further to delete for [%s]", tokenID)
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "nothing further to delete for [%s]", tokenID)
}

return nil
}
logger.DebugfContext(ctx, "Notify owners")
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "Notify owners")
}
for _, owner := range owners {
logger.DebugfContext(ctx, "post new delete-token event [%s:%s]", tokenID, owner)
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "post new delete-token event [%s:%s]", tokenID, owner)
}
t.Notify(ctx, DeleteToken, t.TMSID, owner, tok.Type, tokenID.TxId, tokenID.Index)
}

Expand All @@ -190,7 +197,7 @@ func (t *DBTransaction) DeleteTokens(ctx context.Context, deletedBy string, ids
// AppendToken records a new token in the database and records an add-token event for
// each of its owners. The events are published only after the transaction commits,
// see Notify and FlushEvents.
func (t *DBTransaction) AppendToken(ctx context.Context, tta TokenToAppend) error {
func (t *DBTransaction) AppendToken(ctx context.Context, tta *TokenToAppend) error {
q, err := token2.ToQuantity(tta.Tok.Quantity, tta.Precision)
if err != nil {
return errors.Wrapf(err, "cannot covert [%s] with precision [%d]", tta.Tok.Quantity, tta.Precision)
Expand Down Expand Up @@ -230,7 +237,9 @@ func (t *DBTransaction) AppendToken(ctx context.Context, tta TokenToAppend) erro
return errors.Wrapf(err, "cannot store token in db")
}

logger.DebugfContext(ctx, "Notify owners")
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "Notify owners")
}
for _, id := range tta.Owners {
if len(id) == 0 {
continue
Expand Down Expand Up @@ -261,7 +270,9 @@ func (t *DBTransaction) Notify(ctx context.Context, topic string, tmsID token.TM
Index: index,
})

logger.DebugfContext(ctx, "record new event %v", e)
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "record new event %v", e)
}
t.pending = append(t.pending, e)
}

Expand All @@ -277,7 +288,9 @@ func (t *DBTransaction) FlushEvents(ctx context.Context) {
pending := t.pending
t.pending = nil
for _, e := range pending {
logger.DebugfContext(ctx, "publish new event %v", e)
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.DebugfContext(ctx, "publish new event %v", e)
}
t.Notifier.Publish(e)
}
}
Expand Down
14 changes: 7 additions & 7 deletions token/services/tokens/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestTransaction_AppendToken(t *testing.T) {
Owners: []string{}, // no owners
Flags: tokens.Flags{Mine: false},
}
err = tx.AppendToken(ctx, tta)
err = tx.AppendToken(ctx, &tta)
require.NoError(t, err)
// commit to flush the (empty) event buffer — without this the assertion is vacuous
require.NoError(t, tx.Commit(ctx))
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestTransaction_AppendToken_Notify(t *testing.T) {
Owners: []string{"wallet1"},
Flags: tokens.Flags{Mine: true},
}
err = tx.AppendToken(ctx, tta)
err = tx.AppendToken(ctx, &tta)
require.NoError(t, err)
// the transaction is still open, nothing may be published yet
assert.Equal(t, 0, pub.PublishCallCount())
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestTransaction_AppendToken_NoEventBeforeCommit(t *testing.T) {
Owners: []string{"wallet1"},
Flags: tokens.Flags{Mine: true},
}
require.NoError(t, tx.AppendToken(ctx, tta))
require.NoError(t, tx.AppendToken(ctx, &tta))
require.Equal(t, 0, pub.PublishCallCount())

// the owner of the transaction decides to roll back
Expand All @@ -166,7 +166,7 @@ func TestTransaction_Commit_PublishesRecordedEventsInOrder(t *testing.T) {
tx, err := tokens.NewTransaction(pub, &tokendb.Transaction{TokenStoreTransaction: mockTx}, tmsID)
require.NoError(t, err)

require.NoError(t, tx.AppendToken(ctx, tokens.TokenToAppend{
require.NoError(t, tx.AppendToken(ctx, &tokens.TokenToAppend{
TxID: "tx1",
Index: 0,
Tok: &token2.Token{Type: "TOK", Owner: []byte("alice"), Quantity: "0x64"},
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestTransaction_Commit_Error_PublishesNothing(t *testing.T) {
tx, err := tokens.NewTransaction(pub, &tokendb.Transaction{TokenStoreTransaction: mockTx}, tmsID)
require.NoError(t, err)

require.NoError(t, tx.AppendToken(ctx, tokens.TokenToAppend{
require.NoError(t, tx.AppendToken(ctx, &tokens.TokenToAppend{
TxID: "tx1",
Index: 0,
Tok: &token2.Token{Type: "TOK", Owner: []byte("alice"), Quantity: "0x64"},
Expand All @@ -231,7 +231,7 @@ func TestTransaction_FlushEvents_Idempotent(t *testing.T) {
tx, err := tokens.NewTransaction(pub, &tokendb.Transaction{TokenStoreTransaction: mockTx}, tmsID)
require.NoError(t, err)

require.NoError(t, tx.AppendToken(ctx, tokens.TokenToAppend{
require.NoError(t, tx.AppendToken(ctx, &tokens.TokenToAppend{
TxID: "tx1",
Index: 0,
Tok: &token2.Token{Type: "TOK", Owner: []byte("alice"), Quantity: "0x64"},
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestTransaction_AppendToken_NoNotify(t *testing.T) {
Issuer: false,
},
}
err = tx.AppendToken(ctx, tta)
err = tx.AppendToken(ctx, &tta)
require.NoError(t, err)
// commit to flush the (empty) event buffer — without this the assertion is vacuous
require.NoError(t, tx.Commit(ctx))
Expand Down
Loading
Loading