Skip to content

Commit 6213b08

Browse files
Merge pull request #179 from kaleido-io/request
Supply requestId when creating token pools
2 parents 824840b + b987b00 commit 6213b08

File tree

6 files changed

+61
-55
lines changed

6 files changed

+61
-55
lines changed

internal/assets/manager.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ func (am *assetManager) CreateTokenPool(ctx context.Context, ns string, typeName
7676
}
7777

7878
func (am *assetManager) CreateTokenPoolWithID(ctx context.Context, ns string, id *fftypes.UUID, typeName string, pool *fftypes.TokenPool, waitConfirm bool) (*fftypes.TokenPool, error) {
79-
pool.ID = id
80-
pool.Namespace = ns
81-
8279
if err := am.data.VerifyNamespaceExists(ctx, ns); err != nil {
8380
return nil, err
8481
}
@@ -97,28 +94,19 @@ func (am *assetManager) CreateTokenPoolWithID(ctx context.Context, ns string, id
9794
}
9895

9996
if waitConfirm {
100-
return am.syncasync.SendConfirmTokenPool(ctx, pool.Namespace, func(requestID *fftypes.UUID) error {
97+
return am.syncasync.SendConfirmTokenPool(ctx, ns, func(requestID *fftypes.UUID) error {
10198
_, err := am.CreateTokenPoolWithID(ctx, ns, requestID, typeName, pool, false)
10299
return err
103100
})
104101
}
105102

106-
pool.TX = fftypes.TransactionRef{
107-
ID: fftypes.NewUUID(),
108-
Type: fftypes.TransactionTypeTokenPool,
109-
}
110-
trackingID, err := plugin.CreateTokenPool(ctx, author, pool)
111-
if err != nil {
112-
return nil, err
113-
}
114-
115103
tx := &fftypes.Transaction{
116-
ID: pool.TX.ID,
104+
ID: fftypes.NewUUID(),
117105
Subject: fftypes.TransactionSubject{
118-
Namespace: pool.Namespace,
119-
Type: pool.TX.Type,
106+
Namespace: ns,
107+
Type: fftypes.TransactionTypeTokenPool,
120108
Signer: author.OnChain, // The transaction records on the on-chain identity
121-
Reference: pool.ID,
109+
Reference: id,
122110
},
123111
Created: fftypes.Now(),
124112
Status: fftypes.OpStatusPending,
@@ -132,12 +120,23 @@ func (am *assetManager) CreateTokenPoolWithID(ctx context.Context, ns string, id
132120
op := fftypes.NewTXOperation(
133121
plugin,
134122
ns,
135-
fftypes.NewUUID(),
136-
trackingID,
123+
tx.ID,
124+
"",
137125
fftypes.OpTypeTokensCreatePool,
138126
fftypes.OpStatusPending,
139127
author.Identifier)
140-
return pool, am.database.UpsertOperation(ctx, op, false)
128+
err = am.database.UpsertOperation(ctx, op, false)
129+
if err != nil {
130+
return nil, err
131+
}
132+
133+
pool.ID = id
134+
pool.Namespace = ns
135+
pool.TX = fftypes.TransactionRef{
136+
ID: tx.ID,
137+
Type: tx.Subject.Type,
138+
}
139+
return pool, plugin.CreateTokenPool(ctx, author, pool)
141140
}
142141

143142
func (am *assetManager) scopeNS(ns string, filter database.AndFilter) database.AndFilter {

internal/assets/manager_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ func TestCreateTokenPoolFail(t *testing.T) {
102102
am, cancel := newTestAssets(t)
103103
defer cancel()
104104

105+
mdi := am.database.(*databasemocks.Plugin)
105106
mdm := am.data.(*datamocks.Manager)
106107
mti := am.tokens["magic-tokens"].(*tokenmocks.Plugin)
107108
mdm.On("VerifyNamespaceExists", context.Background(), "ns1").Return(nil)
108-
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.Anything).Return("", fmt.Errorf("pop"))
109+
mdi.On("UpsertTransaction", context.Background(), mock.MatchedBy(func(tx *fftypes.Transaction) bool {
110+
return tx.Subject.Type == fftypes.TransactionTypeTokenPool
111+
}), false).Return(nil)
112+
mdi.On("UpsertOperation", mock.Anything, mock.Anything, false).Return(nil)
113+
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))
109114

110115
_, err := am.CreateTokenPool(context.Background(), "ns1", "magic-tokens", &fftypes.TokenPool{}, false)
111116
assert.Regexp(t, "pop", err)
@@ -117,15 +122,29 @@ func TestCreateTokenPoolTransactionFail(t *testing.T) {
117122

118123
mdi := am.database.(*databasemocks.Plugin)
119124
mdm := am.data.(*datamocks.Manager)
120-
mti := am.tokens["magic-tokens"].(*tokenmocks.Plugin)
121125
mdm.On("VerifyNamespaceExists", context.Background(), "ns1").Return(nil)
122-
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.Anything).Return("tx12345", nil)
123126
mdi.On("UpsertTransaction", context.Background(), mock.Anything, false).Return(fmt.Errorf("pop"))
124127

125128
_, err := am.CreateTokenPool(context.Background(), "ns1", "magic-tokens", &fftypes.TokenPool{}, false)
126129
assert.Regexp(t, "pop", err)
127130
}
128131

132+
func TestCreateTokenPoolOperationFail(t *testing.T) {
133+
am, cancel := newTestAssets(t)
134+
defer cancel()
135+
136+
mdi := am.database.(*databasemocks.Plugin)
137+
mdm := am.data.(*datamocks.Manager)
138+
mdm.On("VerifyNamespaceExists", context.Background(), "ns1").Return(nil)
139+
mdi.On("UpsertTransaction", context.Background(), mock.MatchedBy(func(tx *fftypes.Transaction) bool {
140+
return tx.Subject.Type == fftypes.TransactionTypeTokenPool
141+
}), false).Return(nil)
142+
mdi.On("UpsertOperation", mock.Anything, mock.Anything, false).Return(fmt.Errorf("pop"))
143+
144+
_, err := am.CreateTokenPool(context.Background(), "ns1", "magic-tokens", &fftypes.TokenPool{}, false)
145+
assert.Regexp(t, "pop", err)
146+
}
147+
129148
func TestCreateTokenPoolSuccess(t *testing.T) {
130149
am, cancel := newTestAssets(t)
131150
defer cancel()
@@ -134,7 +153,7 @@ func TestCreateTokenPoolSuccess(t *testing.T) {
134153
mdm := am.data.(*datamocks.Manager)
135154
mti := am.tokens["magic-tokens"].(*tokenmocks.Plugin)
136155
mdm.On("VerifyNamespaceExists", context.Background(), "ns1").Return(nil)
137-
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.Anything).Return("tx12345", nil)
156+
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.Anything).Return(nil)
138157
mdi.On("UpsertTransaction", context.Background(), mock.MatchedBy(func(tx *fftypes.Transaction) bool {
139158
return tx.Subject.Type == fftypes.TransactionTypeTokenPool
140159
}), false).Return(nil)
@@ -157,13 +176,11 @@ func TestCreateTokenPoolConfirm(t *testing.T) {
157176
mdm.On("VerifyNamespaceExists", context.Background(), "ns1").Return(nil).Times(2)
158177
mti.On("CreateTokenPool", context.Background(), mock.Anything, mock.MatchedBy(func(pool *fftypes.TokenPool) bool {
159178
return pool.ID == requestID
160-
})).Return("tx12345", nil).Times(1)
179+
})).Return(nil).Times(1)
161180
mdi.On("UpsertTransaction", context.Background(), mock.MatchedBy(func(tx *fftypes.Transaction) bool {
162181
return tx.Subject.Type == fftypes.TransactionTypeTokenPool
163182
}), false).Return(nil)
164-
mdi.On("UpsertOperation", mock.Anything, mock.MatchedBy(func(op *fftypes.Operation) bool {
165-
return op.BackendID == "tx12345"
166-
}), false).Return(nil).Times(1)
183+
mdi.On("UpsertOperation", mock.Anything, mock.Anything, false).Return(nil).Times(1)
167184
msa.On("SendConfirmTokenPool", context.Background(), "ns1", mock.Anything).
168185
Run(func(args mock.Arguments) {
169186
send := args[2].(syncasync.RequestSender)

internal/tokens/https/https.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@ const (
5252
messageTokenPool msgType = "token-pool"
5353
)
5454

55-
type responseData struct {
56-
RequestID string `json:"id"`
57-
}
58-
5955
type createPool struct {
60-
ID string `json:"clientId"`
56+
ClientID string `json:"clientId"`
6157
Type fftypes.TokenType `json:"type"`
6258
Namespace string `json:"namespace"`
6359
Name string `json:"name"`
60+
RequestID string `json:"requestId"`
6461
}
6562

6663
func (h *HTTPS) Name() string {
@@ -215,23 +212,22 @@ func (h *HTTPS) eventLoop() {
215212
}
216213
}
217214

218-
func (h *HTTPS) CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) (txTrackingID string, err error) {
215+
func (h *HTTPS) CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) error {
219216
var uuids fftypes.Bytes32
220217
copy(uuids[0:16], (*pool.TX.ID)[:])
221218
copy(uuids[16:32], (*pool.ID)[:])
222219

223-
var response responseData
224220
res, err := h.client.R().SetContext(ctx).
225221
SetBody(&createPool{
226-
ID: hex.EncodeToString(uuids[0:32]),
222+
ClientID: hex.EncodeToString(uuids[0:32]),
227223
Type: pool.Type,
228224
Namespace: pool.Namespace,
229225
Name: pool.Name,
226+
RequestID: pool.TX.ID.String(),
230227
}).
231-
SetResult(&response).
232228
Post("/api/v1/pool")
233229
if err != nil || !res.IsSuccess() {
234-
return "", restclient.WrapRestErr(ctx, res, err, i18n.MsgTokensRESTErr)
230+
return restclient.WrapRestErr(ctx, res, err, i18n.MsgTokensRESTErr)
235231
}
236-
return response.RequestID, nil
232+
return nil
237233
}

internal/tokens/https/https_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func TestCreateTokenPool(t *testing.T) {
119119
body := make(fftypes.JSONObject)
120120
err := json.NewDecoder(req.Body).Decode(&body)
121121
assert.NoError(t, err)
122+
assert.Contains(t, body, "requestId")
123+
delete(body, "requestId")
122124
assert.Equal(t, fftypes.JSONObject{
123125
"clientId": hex.EncodeToString(uuids[0:32]),
124126
"namespace": "ns1",
@@ -136,9 +138,8 @@ func TestCreateTokenPool(t *testing.T) {
136138
return res, nil
137139
})
138140

139-
trackingID, err := h.CreateTokenPool(context.Background(), &fftypes.Identity{}, pool)
141+
err := h.CreateTokenPool(context.Background(), &fftypes.Identity{}, pool)
140142
assert.NoError(t, err)
141-
assert.Equal(t, "1", trackingID)
142143
}
143144

144145
func TestCreateTokenPoolError(t *testing.T) {
@@ -156,7 +157,7 @@ func TestCreateTokenPoolError(t *testing.T) {
156157
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/pool", httpURL),
157158
httpmock.NewJsonResponderOrPanic(500, fftypes.JSONObject{}))
158159

159-
_, err := h.CreateTokenPool(context.Background(), &fftypes.Identity{}, pool)
160+
err := h.CreateTokenPool(context.Background(), &fftypes.Identity{}, pool)
160161
assert.Regexp(t, "FF10274", err)
161162
}
162163

mocks/tokenmocks/plugin.go

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/tokens/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Plugin interface {
4242

4343
// CreateTokenPool creates a new (fungible or non-fungible) pool of tokens
4444
// The returned tracking ID will be used to correlate with any subsequent transaction tracking updates
45-
CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) (txTrackingID string, err error)
45+
CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) error
4646
}
4747

4848
// Callbacks is the interface provided to the tokens plugin, to allow it to pass events back to firefly.

0 commit comments

Comments
 (0)