Skip to content
Snippets Groups Projects
Commit a8ab35cf authored by Jake Taylor's avatar Jake Taylor
Browse files

god grant us all merciful deaths

parent bca02b91
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!23god grant us all merciful deaths
......@@ -11,6 +11,7 @@ package indexedDb
import (
"github.com/pkg/errors"
"gitlab.com/elixxir/crypto/broadcast"
cryptoChannel "gitlab.com/elixxir/crypto/channel"
"gitlab.com/elixxir/xxdk-wasm/storage"
"syscall/js"
......@@ -94,27 +95,7 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher,
return nil, err
}
// FIXME: The below is a hack that for some reason prevents moving on with
// uninitialized database despite the previous call to Await.
// It would be idea to find a different solution.
// Close and open again to ensure the state is finalized
err = db.Close()
if err != nil {
return nil, err
}
openRequest, err = idb.Global().Open(ctx, databaseName, currentVersion,
func(db *idb.Database, oldVersion, newVersion uint) error {
return nil
})
if err != nil {
return nil, err
}
// Wait for database open to finish
db, err = openRequest.Await(ctx)
if err != nil {
return nil, err
}
// Save the encryption status to storage
encryptionStatus := encryption != nil
loadedEncryptionStatus, err := storage.StoreIndexedDbEncryptionStatus(
databaseName, encryptionStatus)
......@@ -122,13 +103,17 @@ func newWASMModel(databaseName string, encryption cryptoChannel.Cipher,
return nil, err
}
// Verify encryption status does not change
if encryptionStatus != loadedEncryptionStatus {
return nil, errors.New(
"Cannot load database with different encryption status.")
} else if !encryptionStatus {
jww.WARN.Printf("IndexedDb encryption disabled!")
}
return &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}, err
// Attempt to ensure the database has been properly initialized
wrapper := &wasmModel{db: db, receivedMessageCB: cb, cipher: encryption}
return wrapper, wrapper.hackTestDb()
}
// v1Upgrade performs the v0 -> v1 database upgrade.
......@@ -195,3 +180,26 @@ func v1Upgrade(db *idb.Database) error {
return nil
}
// hackTestDb is a horrible function that exists as the result of an extremely
// long discussion about why initializing the IndexedDb sometimes silently
// fails. It ultimately tries to prevent an unrecoverable situation by actually
// inserting some nonsense data and then checking to see if it persists.
// If this function still exists in 2023, god help us all. Amen.
func (w *wasmModel) hackTestDb() error {
testId := &id.DummyUser
testChannel := &broadcast.Channel{
ReceptionID: testId,
Name: "test",
Description: "test",
}
w.JoinChannel(testChannel)
result, err := w.get(channelsStoreName, js.ValueOf(testId.String()))
if err != nil {
return err
}
if len(result) == 0 {
return errors.Errorf("Failed to test db, record not present")
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment