Skip to content

Commit b6405d6

Browse files
Merge branch 'main' of github.com:hyperledger-labs/firefly into enums
2 parents 0efffb7 + 6213b08 commit b6405d6

File tree

8 files changed

+90
-61
lines changed

8 files changed

+90
-61
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/blockchain/ethereum/ethereum.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ func (e *Ethereum) handleBatchPinEvent(ctx context.Context, msgJSON fftypes.JSON
324324
}
325325

326326
// If there's an error dispatching the event, we must return the error and shutdown
327+
delete(msgJSON, "data")
327328
return e.callbacks.BatchPinComplete(batch, authorAddress, sTransactionHash, msgJSON)
328329
}
329330

internal/blockchain/ethereum/ethereum_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,27 @@ func TestHandleMessageBatchPinOK(t *testing.T) {
549549
assert.Equal(t, "68e4da79f805bca5b912bcda9c63d03e6e867108dabb9b944109aea541ef522a", b.Contexts[0].String())
550550
assert.Equal(t, "19b82093de5ce92a01e333048e877e2374354bf846dd034864ef6ffbd6438771", b.Contexts[1].String())
551551

552+
info1 := fftypes.JSONObject{
553+
"address": "0x1C197604587F046FD40684A8f21f4609FB811A7b",
554+
"blockNumber": "38011",
555+
"logIndex": "50",
556+
"signature": "BatchPin(address,uint256,string,bytes32,bytes32,string,bytes32[])",
557+
"subID": "sb-b5b97a4e-a317-4053-6400-1474650efcb5",
558+
"transactionHash": "0xc26df2bf1a733e9249372d61eb11bd8662d26c8129df76890b1beb2f6fa72628",
559+
"transactionIndex": "0x0",
560+
}
561+
assert.Equal(t, info1, em.Calls[0].Arguments[3])
562+
info2 := fftypes.JSONObject{
563+
"address": "0x1C197604587F046FD40684A8f21f4609FB811A7b",
564+
"blockNumber": "38011",
565+
"logIndex": "51",
566+
"signature": "BatchPin(address,uint256,string,bytes32,bytes32,string,bytes32[])",
567+
"subID": "sb-b5b97a4e-a317-4053-6400-1474650efcb5",
568+
"transactionHash": "0x0c50dff0893e795293189d9cc5ba0d63c4020d8758ace4a69d02c9d6d43cb695",
569+
"transactionIndex": "0x1",
570+
}
571+
assert.Equal(t, info2, em.Calls[1].Arguments[3])
572+
552573
em.AssertExpectations(t)
553574

554575
}

internal/tokens/https/https.go

Lines changed: 10 additions & 14 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 {
@@ -124,7 +121,7 @@ func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObje
124121
clientID := data.GetString("clientId")
125122
tokenType := data.GetString("type")
126123
protocolID := data.GetString("poolId")
127-
authorAddress := data.GetString("author")
124+
operatorAddress := data.GetString("operator")
128125
tx := data.GetObject("transaction")
129126
txHash := tx.GetString("transactionHash")
130127

@@ -133,7 +130,7 @@ func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObje
133130
clientID == "" ||
134131
tokenType == "" ||
135132
protocolID == "" ||
136-
authorAddress == "" ||
133+
operatorAddress == "" ||
137134
txHash == "" {
138135
log.L(ctx).Errorf("TokenPool event is not valid - missing data: %+v", data)
139136
return nil // move on
@@ -162,7 +159,7 @@ func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObje
162159
}
163160

164161
// If there's an error dispatching the event, we must return the error and shutdown
165-
return h.callbacks.TokenPoolCreated(h, pool, authorAddress, txHash, data)
162+
return h.callbacks.TokenPoolCreated(h, pool, operatorAddress, txHash, tx)
166163
}
167164

168165
func (h *HTTPS) eventLoop() {
@@ -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: 8 additions & 6 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

@@ -192,7 +193,7 @@ func TestEvents(t *testing.T) {
192193
assert.Equal(t, `{"data":{"id":"5"},"event":"ack"}`, string(msg))
193194

194195
// token-pool: invalid uuids
195-
fromServer <- `{"id":"6","event":"token-pool","data":{"namespace":"ns1","name":"test-pool","clientId":"bad","type":"fungible","poolId":"F1","author":"0x0","transaction":{"transactionHash":"abc"}}}`
196+
fromServer <- `{"id":"6","event":"token-pool","data":{"namespace":"ns1","name":"test-pool","clientId":"bad","type":"fungible","poolId":"F1","operator":"0x0","transaction":{"transactionHash":"abc"}}}`
196197
msg = <-toServer
197198
assert.Equal(t, `{"data":{"id":"6"},"event":"ack"}`, string(msg))
198199

@@ -205,8 +206,9 @@ func TestEvents(t *testing.T) {
205206
// token-pool: success
206207
mcb.On("TokenPoolCreated", h, mock.MatchedBy(func(pool *fftypes.TokenPool) bool {
207208
return pool.Namespace == "ns1" && pool.Name == "test-pool" && pool.Type == fftypes.TokenTypeFungible && pool.ProtocolID == "F1" && *pool.ID == *id2 && *pool.TX.ID == *id1
208-
}), "0x0", "abc", mock.Anything).Return(nil)
209-
fromServer <- `{"id":"7","event":"token-pool","data":{"namespace":"ns1","name":"test-pool","clientId":"` + hex.EncodeToString(uuids[0:32]) + `","type":"fungible","poolId":"F1","author":"0x0","transaction":{"transactionHash":"abc"}}}`
209+
}), "0x0", "abc", fftypes.JSONObject{"transactionHash": "abc"},
210+
).Return(nil)
211+
fromServer <- `{"id":"7","event":"token-pool","data":{"namespace":"ns1","name":"test-pool","clientId":"` + hex.EncodeToString(uuids[0:32]) + `","type":"fungible","poolId":"F1","operator":"0x0","transaction":{"transactionHash":"abc"}}}`
210212
msg = <-toServer
211213
assert.Equal(t, `{"data":{"id":"7"},"event":"ack"}`, string(msg))
212214

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)