From a8ab35cf6bca183c09819becadc2e7ab9e5f19ce Mon Sep 17 00:00:00 2001 From: Jake Taylor <jake@elixxir.io> Date: Wed, 26 Oct 2022 19:28:49 -0500 Subject: [PATCH] god grant us all merciful deaths --- indexedDb/init.go | 52 +++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/indexedDb/init.go b/indexedDb/init.go index 271300b4..908a13be 100644 --- a/indexedDb/init.go +++ b/indexedDb/init.go @@ -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 +} -- GitLab