Skip to content

Commit 98cf6bc

Browse files
authored
Remove extra anchoring step (#1594)
* nft: Remove extra anchoring task for pending document * deps: Update gocelery to 3b07af1b49a6 * nft-test: Update runner func name * deps: Update Centrifuge Chain submodule * deps: Update chain custom types to v1.0.2 * nft: Rename NFT mint jobs
1 parent 4eb3f50 commit 98cf6bc

File tree

7 files changed

+150
-114
lines changed

7 files changed

+150
-114
lines changed

build/centrifuge-chain

Submodule centrifuge-chain updated 318 files

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ require (
66
github.com/ChainSafe/go-schnorrkel v1.0.0
77
github.com/Masterminds/semver v1.5.0
88
github.com/centrifuge/centrifuge-protobufs v1.0.0
9-
github.com/centrifuge/chain-custom-types v1.0.1
9+
github.com/centrifuge/chain-custom-types v1.0.2
1010
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12
11-
github.com/centrifuge/gocelery/v2 v2.0.0-20210312150236-4ff19a2b741d
11+
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6
1212
github.com/centrifuge/precise-proofs v1.0.0
1313
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2
1414
github.com/ethereum/go-ethereum v1.10.20

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH
126126
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
127127
github.com/centrifuge/centrifuge-protobufs v1.0.0 h1:ZPg0XpkTrGrjQu8scXjMGs7jjqsWPiXmOXdV/bz30ng=
128128
github.com/centrifuge/centrifuge-protobufs v1.0.0/go.mod h1:VL6mcnK6vTRiFljHP39J0WBI3Uu5BHQjhdFkCxY9/9I=
129-
github.com/centrifuge/chain-custom-types v1.0.1 h1:BupYn3o/hpjcAq7+MupyXJ6T1qYAWfqU8mFp3TAGx1s=
130-
github.com/centrifuge/chain-custom-types v1.0.1/go.mod h1:hrD9djuAUOJXxV6iYv0Nexw69V/ywk38pTT4waJaakw=
129+
github.com/centrifuge/chain-custom-types v1.0.2 h1:wlHjlK4phXEMVHXQX8JeUuHhINa25RFUg5FFLs1r5JQ=
130+
github.com/centrifuge/chain-custom-types v1.0.2/go.mod h1:hrD9djuAUOJXxV6iYv0Nexw69V/ywk38pTT4waJaakw=
131131
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b h1:TPvvMcGAc3TVBVgQ4XYYEWTXxYls8YuylZ8JzrVxPzc=
132132
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b/go.mod h1:0voJY6Qzxvr2S0LeDSFQiCnJzGq5gORg2SwCmn8602I=
133133
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12 h1:DCYWIBOalB0mKKfUg2HhtGgIkBbMA1fnlnkZp7fHB18=
134134
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12/go.mod h1:5g1oM4Zu3BOaLpsKQ+O8PAv2kNuq+kPcA1VzFbsSqxE=
135-
github.com/centrifuge/gocelery/v2 v2.0.0-20210312150236-4ff19a2b741d h1:4OIGVat6mHenBP2yHVDQjOtip2+jNx7amsUJl2bOJN4=
136-
github.com/centrifuge/gocelery/v2 v2.0.0-20210312150236-4ff19a2b741d/go.mod h1:q6PvyLLCu1IQcE2dFv8advWprzY5b5GtZYRU/bH9nWE=
135+
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6 h1:0/y+LOu+sAnIlXNsvCYmxUY9n6OiCyhT/37KoYKEFSk=
136+
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6/go.mod h1:q6PvyLLCu1IQcE2dFv8advWprzY5b5GtZYRU/bH9nWE=
137137
github.com/centrifuge/precise-proofs v1.0.0 h1:hcXSK2d5aPEZUa09p+aoyw9ofKI6Twip+UMIx+sJKYc=
138138
github.com/centrifuge/precise-proofs v1.0.0/go.mod h1:MDlIcee+jgzH3IP1GpV6e8eeT57RiuvGfH6UFUTrPYc=
139139
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=

nft/v3/bootstrapper.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/centrifuge/pod/pallets/uniques"
1111
"github.com/centrifuge/pod/pallets/utility"
1212
"github.com/centrifuge/pod/pending"
13+
"github.com/centrifuge/pod/storage"
1314
)
1415

1516
const (
@@ -61,16 +62,25 @@ func (*Bootstrapper) Bootstrap(ctx map[string]interface{}) error {
6162
return errors.New("utility API not initialised")
6263
}
6364

64-
go dispatcher.RegisterRunner(commitAndMintNFTV3Job, &CommitAndMintNFTJobRunner{
65+
ldb, ok := ctx[storage.BootstrappedDB].(storage.Repository)
66+
67+
if !ok {
68+
return errors.New("DB not found in the bootstrapper")
69+
}
70+
71+
repo := pending.NewRepository(ldb)
72+
73+
go dispatcher.RegisterRunner(mintNFTForPendingDocV3Job, &MintNFTForPendingDocJobRunner{
6574
accountsSrv: accountsSrv,
6675
pendingDocsSrv: pendingDocsSrv,
76+
pendingRepo: repo,
6777
docSrv: docSrv,
6878
dispatcher: dispatcher,
6979
utilityAPI: utilityAPI,
7080
ipfsPinningSrv: ipfsPinningSrv,
7181
})
7282

73-
go dispatcher.RegisterRunner(mintNFTV3Job, &MintNFTJobRunner{
83+
go dispatcher.RegisterRunner(mintNFTForCommittedDocV3Job, &MintNFTForCommittedDocJobRunner{
7484
accountsSrv: accountsSrv,
7585
docSrv: docSrv,
7686
dispatcher: dispatcher,

nft/v3/jobs.go

Lines changed: 121 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/centrifuge/pod/pallets/utility"
9-
10-
"github.com/centrifuge/pod/centchain"
11-
128
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
139
"github.com/centrifuge/gocelery/v2"
10+
"github.com/centrifuge/pod/centchain"
1411
"github.com/centrifuge/pod/config"
1512
"github.com/centrifuge/pod/contextutil"
1613
"github.com/centrifuge/pod/documents"
1714
"github.com/centrifuge/pod/ipfs"
1815
"github.com/centrifuge/pod/jobs"
1916
"github.com/centrifuge/pod/pallets/uniques"
17+
"github.com/centrifuge/pod/pallets/utility"
2018
"github.com/centrifuge/pod/pending"
2119
logging "github.com/ipfs/go-log"
2220
"github.com/ipfs/interface-go-ipfs-core/path"
2321
)
2422

2523
const (
26-
commitAndMintNFTV3Job = "Commit and mint NFT V3 Job"
27-
mintNFTV3Job = "Mint NFT V3 Job"
28-
createNFTCollectionV3Job = "Create NFT collection V3 Job"
24+
mintNFTForPendingDocV3Job = "Mint NFT For Pending Doc V3 Job"
25+
mintNFTForCommittedDocV3Job = "Mint NFT For Committed Doc V3 Job"
26+
createNFTCollectionV3Job = "Create NFT collection V3 Job"
2927
)
3028

3129
var (
@@ -108,37 +106,37 @@ func (c *CreateCollectionJobRunner) loadTasks() map[string]jobs.Task {
108106
}
109107
}
110108

111-
type CommitAndMintNFTJobRunner struct {
109+
type MintNFTForPendingDocJobRunner struct {
112110
jobs.Base
113111

114112
accountsSrv config.Service
115113
pendingDocsSrv pending.Service
114+
pendingRepo pending.Repository
116115
docSrv documents.Service
117116
dispatcher jobs.Dispatcher
118117
utilityAPI utility.API
119118
ipfsPinningSrv ipfs.PinningServiceClient
120119
}
121120

122-
// New returns a new instance of MintNFTJobRunner
123-
func (c *CommitAndMintNFTJobRunner) New() gocelery.Runner {
124-
mj := &CommitAndMintNFTJobRunner{
121+
// New returns a new instance of MintNFTForPendingDocJobRunner
122+
func (c *MintNFTForPendingDocJobRunner) New() gocelery.Runner {
123+
mj := &MintNFTForPendingDocJobRunner{
125124
accountsSrv: c.accountsSrv,
126125
pendingDocsSrv: c.pendingDocsSrv,
126+
pendingRepo: c.pendingRepo,
127127
docSrv: c.docSrv,
128128
dispatcher: c.dispatcher,
129129
utilityAPI: c.utilityAPI,
130130
ipfsPinningSrv: c.ipfsPinningSrv,
131131
}
132132

133-
documentPendingTasks := loadCommitAndMintTasks(
134-
c.pendingDocsSrv,
135-
c.docSrv,
136-
c.dispatcher,
137-
c.utilityAPI,
138-
c.ipfsPinningSrv,
133+
commitAndMintNFTTasks := mergeTaskMaps(
134+
loadAnchoringTasksForPendingDocument(c.pendingDocsSrv, c.pendingRepo, c.dispatcher),
135+
loadNFTMintTasks(c.docSrv, c.utilityAPI, c.ipfsPinningSrv),
139136
)
140137

141-
mj.Base = jobs.NewBase(documentPendingTasks)
138+
mj.Base = jobs.NewBase(commitAndMintNFTTasks)
139+
142140
return mj
143141
}
144142

@@ -154,76 +152,7 @@ func mergeTaskMaps[K comparable, V any](taskMaps ...map[K]V) map[K]V {
154152
return res
155153
}
156154

157-
func loadCommitAndMintTasks(
158-
pendingDocsSrv pending.Service,
159-
docSrv documents.Service,
160-
dispatcher jobs.Dispatcher,
161-
utilityAPI utility.API,
162-
ipfsPinningSrv ipfs.PinningServiceClient,
163-
) map[string]jobs.Task {
164-
commitTasks := map[string]jobs.Task{
165-
"commit_pending_document": {
166-
RunnerFunc: func(args []interface{}, overrides map[string]interface{}) (interface{}, error) {
167-
account, _, req, err := convertArgs(args)
168-
169-
if err != nil {
170-
log.Errorf("Couldn't convert args: %s", err)
171-
172-
return nil, err
173-
}
174-
175-
ctx := contextutil.WithAccount(context.Background(), account)
176-
177-
_, jobID, err := pendingDocsSrv.Commit(ctx, req.DocumentID)
178-
179-
if err != nil {
180-
log.Errorf("Couldn't commit pending document: %s", err)
181-
182-
return nil, err
183-
}
184-
185-
overrides["document_commit_job"] = jobID
186-
187-
return nil, nil
188-
},
189-
Next: "wait_for_pending_document_commit",
190-
},
191-
"wait_for_pending_document_commit": {
192-
RunnerFunc: func(args []interface{}, overrides map[string]interface{}) (result interface{}, err error) {
193-
account, ok := args[0].(config.Account)
194-
195-
if !ok {
196-
return nil, errors.New("account not provided in args")
197-
}
198-
199-
jobID := overrides["document_commit_job"].(gocelery.JobID)
200-
201-
log.Infof("Waiting for document to be committed, job ID - %s", jobID.Hex())
202-
203-
job, err := dispatcher.Job(account.GetIdentity(), jobID)
204-
205-
if err != nil {
206-
log.Errorf("Couldn't get dispatcher job with ID %s: %s", jobID.Hex(), err)
207-
208-
return nil, fmt.Errorf("failed to fetch job with ID %s: %w", jobID.Hex(), err)
209-
}
210-
211-
if !job.IsSuccessful() {
212-
log.Infof("Document not committed yet, job ID - %s", jobID.Hex())
213-
214-
return nil, errors.New("document not committed yet")
215-
}
216-
217-
return nil, nil
218-
},
219-
Next: "add_nft_v3_to_document",
220-
},
221-
}
222-
223-
return mergeTaskMaps(commitTasks, loadNFTMintTasks(docSrv, dispatcher, utilityAPI, ipfsPinningSrv))
224-
}
225-
226-
type MintNFTJobRunner struct {
155+
type MintNFTForCommittedDocJobRunner struct {
227156
jobs.Base
228157

229158
accountsSrv config.Service
@@ -233,17 +162,20 @@ type MintNFTJobRunner struct {
233162
ipfsPinningSrv ipfs.PinningServiceClient
234163
}
235164

236-
// New returns a new instance of MintNFTJobRunner
237-
func (m *MintNFTJobRunner) New() gocelery.Runner {
238-
mj := &MintNFTJobRunner{
165+
// New returns a new instance of MintNFTForCommittedDocJobRunner
166+
func (m *MintNFTForCommittedDocJobRunner) New() gocelery.Runner {
167+
mj := &MintNFTForCommittedDocJobRunner{
239168
accountsSrv: m.accountsSrv,
240169
docSrv: m.docSrv,
241170
dispatcher: m.dispatcher,
242171
utilityAPI: m.utilityAPI,
243172
ipfsPinningSrv: m.ipfsPinningSrv,
244173
}
245174

246-
nftMintTasks := loadNFTMintTasks(m.docSrv, m.dispatcher, m.utilityAPI, m.ipfsPinningSrv)
175+
nftMintTasks := mergeTaskMaps(
176+
loadAnchoringTasksForCommittedDocument(m.docSrv, m.dispatcher),
177+
loadNFTMintTasks(m.docSrv, m.utilityAPI, m.ipfsPinningSrv),
178+
)
247179

248180
mj.Base = jobs.NewBase(nftMintTasks)
249181
return mj
@@ -283,11 +215,9 @@ const (
283215
DocumentVersionAttributeKey = "document_version"
284216
)
285217

286-
func loadNFTMintTasks(
218+
func loadAnchoringTasksForCommittedDocument(
287219
docSrv documents.Service,
288220
dispatcher jobs.Dispatcher,
289-
utilityAPI utility.API,
290-
ipfsPinningSrv ipfs.PinningServiceClient,
291221
) map[string]jobs.Task {
292222
return map[string]jobs.Task{
293223
"add_nft_v3_to_document": {
@@ -362,6 +292,102 @@ func loadNFTMintTasks(
362292
},
363293
Next: "store_nft_on_ipfs",
364294
},
295+
}
296+
}
297+
298+
func loadAnchoringTasksForPendingDocument(
299+
pendingDocsSrv pending.Service,
300+
pendingRepo pending.Repository,
301+
dispatcher jobs.Dispatcher,
302+
) map[string]jobs.Task {
303+
return map[string]jobs.Task{
304+
"add_nft_v3_to_pending_document": {
305+
RunnerFunc: func(args []interface{}, overrides map[string]interface{}) (result interface{}, err error) {
306+
account, itemID, req, err := convertArgs(args)
307+
308+
if err != nil {
309+
log.Errorf("Couldn't convert args: %s", err)
310+
311+
return nil, err
312+
}
313+
314+
ctx := contextutil.WithAccount(context.Background(), account)
315+
316+
doc, err := pendingDocsSrv.Get(ctx, req.DocumentID, documents.Pending)
317+
318+
if err != nil {
319+
log.Errorf("Couldn't get pending document: %s", err)
320+
321+
return nil, err
322+
}
323+
324+
err = doc.AddNFT(req.GrantReadAccess, req.CollectionID, itemID)
325+
326+
if err != nil {
327+
log.Errorf("Couldn't add NFT to document: %s", err)
328+
329+
return nil, fmt.Errorf("failed to add nft to document: %w", err)
330+
}
331+
332+
if err = pendingRepo.Update(account.GetIdentity().ToBytes(), req.DocumentID, doc); err != nil {
333+
log.Errorf("Couldn't update document: %s", err)
334+
335+
return nil, fmt.Errorf("couldn't update document: %w", err)
336+
}
337+
338+
_, jobID, err := pendingDocsSrv.Commit(ctx, req.DocumentID)
339+
340+
if err != nil {
341+
log.Errorf("Couldn't commit pending document: %s", err)
342+
343+
return nil, fmt.Errorf("failed to commit document: %w", err)
344+
}
345+
346+
overrides["document_commit_job"] = jobID
347+
348+
return nil, nil
349+
},
350+
Next: "wait_for_pending_document_commit",
351+
},
352+
"wait_for_pending_document_commit": {
353+
RunnerFunc: func(args []interface{}, overrides map[string]interface{}) (result interface{}, err error) {
354+
account, ok := args[0].(config.Account)
355+
356+
if !ok {
357+
return nil, errors.New("account not provided in args")
358+
}
359+
360+
jobID := overrides["document_commit_job"].(gocelery.JobID)
361+
362+
log.Info("Waiting for pending document to be anchored")
363+
364+
job, err := dispatcher.Job(account.GetIdentity(), jobID)
365+
366+
if err != nil {
367+
log.Errorf("Couldn't dispatch job: %s", err)
368+
369+
return nil, fmt.Errorf("failed to fetch job: %w", err)
370+
}
371+
372+
if !job.IsSuccessful() {
373+
log.Info("Document not committed yet")
374+
375+
return nil, errors.New("document not committed yet")
376+
}
377+
378+
return nil, nil
379+
},
380+
Next: "store_nft_on_ipfs",
381+
},
382+
}
383+
}
384+
385+
func loadNFTMintTasks(
386+
docSrv documents.Service,
387+
utilityAPI utility.API,
388+
ipfsPinningSrv ipfs.PinningServiceClient,
389+
) map[string]jobs.Task {
390+
return map[string]jobs.Task{
365391
"store_nft_on_ipfs": {
366392
RunnerFunc: func(args []interface{}, overrides map[string]interface{}) (result interface{}, err error) {
367393
account, _, req, err := convertArgs(args)

0 commit comments

Comments
 (0)