Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

fix: create local doc cache loader #2620

Merged
merged 1 commit into from
Mar 9, 2021

Conversation

baha-ai
Copy link
Contributor

@baha-ai baha-ai commented Mar 9, 2021

This change creates a local JSON-LD Document Cache loader using a cache of type *sync.Map
to avoid 'error on cache writes' panics.

This change also introduces the 'benchmark' Make target to support benchmarking code in the framework.

closes #1833 for the fatal error mentioned in the comment
Also related and closes the discussion about the same panic topic in #2487

Signed-off-by: Baha Shaaban [email protected]

@baha-ai baha-ai self-assigned this Mar 9, 2021
@baha-ai baha-ai added bug Something isn't working chore enhancement New feature or request labels Mar 9, 2021
@codecov
Copy link

codecov bot commented Mar 9, 2021

Codecov Report

Merging #2620 (8168d34) into main (ada2929) will decrease coverage by 0.01%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2620      +/-   ##
==========================================
- Coverage   88.41%   88.39%   -0.02%     
==========================================
  Files         264      265       +1     
  Lines       20614    20636      +22     
==========================================
+ Hits        18225    18242      +17     
- Misses       1400     1405       +5     
  Partials      989      989              
Impacted Files Coverage Δ
pkg/doc/jsonld/document_loader.go 72.72% <72.72%> (ø)
pkg/doc/signature/jsonld/processor.go 73.68% <75.00%> (ø)
pkg/doc/did/doc.go 87.79% <100.00%> (ø)
pkg/doc/jsonld/jsonld.go 100.00% <100.00%> (+20.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ada2929...baaee32. Read the comment docs.

@@ -685,6 +711,25 @@ const jsonVCWithProperContexts = `{
]
}`

// nolint
const jsonVCWithProperContexts2 = `{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example picked from @llorllale's comment here

Comment on lines 1781 to 1791
// {
// name: "canonizing sample document with extra io.Reader cached dummy context",
// doc: jsonLDMultipleInvalidRDFs,
// result: canonizedSampleVP_extraContext,
// opts: []ProcessorOpts{
// WithRemoveAllInvalidRDF(), WithExternalContext("http://localhost:8652/dummy.jsonld"),
// WithDocumentLoaderCache(map[string]interface{}{
// "http://localhost:8652/dummy.jsonld": strings.NewReader(extraJSONLDContext),
// }),
// },
// },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it is commented out?

Copy link
Contributor Author

@baha-ai baha-ai Mar 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the buffer in strings.NewReader() is not thread safe.
One would need to use a thread safe Reader implementation for this to work. Example: https://gist.github.com/jmackie/11570bdcd8a4c10d72619a5e1f21c5f8

I can probably add a note in the DocumentLoader constructor about this. But this is an edge case with seldom use. Users of the DocumentLoader need to be aware to avoid using strings.NewReader() as a sync.Map value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test SyncReader implementation and uncommented this case, the benchmark is always passing now.

pkg/doc/signature/jsonld/processor.go Outdated Show resolved Hide resolved
@baha-ai baha-ai force-pushed the 1833 branch 3 times, most recently from 25f6287 to ac82bda Compare March 9, 2021 18:35
This change creates a local JSON-LD Document Cache loader using a cache of type *sync.Map
to avoid 'error on cache writes' panics.

This change also introduces the 'benchmark' Make target to support benchmarking code in the framework.

closes hyperledger-archives#1833 for the fatal error mentioned in the comment
Also related and closes the discussion about the same panic topic in hyperledger-archives#2487

Signed-off-by: Baha Shaaban <[email protected]>
@fqutishat fqutishat merged commit 4af0866 into hyperledger-archives:main Mar 9, 2021
@baha-ai baha-ai deleted the 1833 branch March 9, 2021 19:45

cdl.rwMutex.RUnlock()

docFromLoader, err := cdl.nextLoader.LoadDocument(u)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like nextLoader must also be thread-safe.

This will be an issue when people want to add extra contexts in downstream projects and they understandably forget about this bug, such as when they use the DocumentVerifier directly (like zcaps do).

The better solution is to have the user prepare the contexts themselves in an ld.DocumentLoader and be done with this. No need for locking or any of this extra complexity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working chore enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Proof shouldn't override/hardcode jsonld contexts
6 participants