ssbc / ssb-db2

A new database for secure-scuttlebutt
47 stars 8 forks source link

wait for encryption formats to load before encrypting #371

Closed staltz closed 2 years ago

staltz commented 2 years ago

Problem

Our ./encryption-formats/box2.js setup() was actually not asynchronous, it was sync. Because of this, you could always call any of its APIs at any time, and it would work. Add a simple setTimeout(1000 and then it doesn't work anymore.

The deeper problem is that in indexes/private.js we load the index files and if there is an error (and there often is, because the index files are non-existent) then the other computation (such as loading encryption formats) is cancelled. This is a normal behavior of errors in JS, and multicb does it too: if there is an error anywhere, stop everything.

But what we really want is: if there is an error when loading private indexes, that's okay, please proceed async loading the encryption formats too.

Solution

In indexes/private, treat loadFile errors as "callback results", not as errors, and treat encryptionFormats.setup errors as critical (which they are). Simulate async in encryption-formats/box2.js, and then also make sure that create() waits for private indexes (and thus encryption formats) to load before proceeding to encrypt.

(NOTE: tests are not running here because this is a PR of a PR. But once they're merged in formats-split, tests will run. I ran them locally two times, they pass)