Skip to content
Snippets Groups Projects
Commit abefd9d7 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

Merge branch 'hotfix/hackTestdb' into 'release'

god grant us all merciful deaths

See merge request !23
parents 98fcacbf 4d21d48d
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
......@@ -10,14 +10,13 @@
package indexedDb
import (
"github.com/hack-pad/go-indexeddb/idb"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
cryptoChannel "gitlab.com/elixxir/crypto/channel"
"gitlab.com/elixxir/xxdk-wasm/storage"
"syscall/js"
"github.com/hack-pad/go-indexeddb/idb"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/channels"
"gitlab.com/xx_network/primitives/id"
)
......@@ -94,27 +93,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 +101,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 +178,28 @@ 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 {
testMessage := &Message{
ID: 0,
Nickname: "test",
MessageID: id.DummyUser.Marshal(),
}
msgId, helper := w.receiveHelper(testMessage, false)
if helper != nil {
return helper
}
result, err := w.get(messageStoreName, js.ValueOf(msgId))
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