package loopdb import ( "context" "io/ioutil" "os" "path/filepath" "testing" "github.com/btcsuite/btcd/chaincfg" "github.com/coreos/bbolt" "github.com/stretchr/testify/require" ) // TestMigrationUpdates asserts that the swap updates migration is carried out // correctly. func TestMigrationUpdates(t *testing.T) { var ( legacyDbVersion = Hex("00000003") ) legacyDb := map[string]interface{}{ "metadata": map[string]interface{}{ "dbp": legacyDbVersion, }, "loop-in": map[string]interface{}{ Hex("acae09fec9020b7996042613eede68a9eaf29eb28c21ea9943b19e344365a4bb"): map[string]interface{}{ "contract": Hex("161b25277262bdb5c7c2827b975b2cbc7eb13e222b30cf88ea6daef4bcf22bdac4116c23071472cb000000000000ea6003f2f513a8fd7958b6a229dfb8835f6ab2c9c63cc3e138784d3e8c0e0ebbdd4e61033f26c40666977ed497eea4694d6dd3f07dbcf037089234ff665cd0a07fea329400007b8a00000000000059a600000000000009ca000077a20000000600000000000000000000000000000000000000000000000000000000000000000000"), "updates": map[string]interface{}{ Hex("0000000000000001"): Hex("161b252772cb524508000000000000000000000000000000000000000000000000"), Hex("0000000000000002"): Hex("161b252837115e9b09ffffffffffff1f6a00000000000000000000000000000000"), Hex("0000000000000003"): Hex("161b252ab670360d0200000000000009ca00000000000000000000000000000000"), }, }, }, "uncharge-swaps": map[string]interface{}{ Hex("c3b3d7a145dbd2bab5aa1f505305f31ee432fe23b0801f065fac453dd9b1f923"): map[string]interface{}{ "contract": Hex("161b2526643767387ca76e58c964a8f2b6c0a13392b2dea93bde260226a263fb836954054ed1756b000000000000c350fd11016c6e6263727431333337306e3170303072343775707035366c7671663836753565766135647868686c706c78303733756a70676e3979767977376130766a37746d307678793276683576716471327770657832757270307963717a7279787139377a76757173703570373232733970686a6e6e6e706c3778716e796a78353373706863346c396735306b396e347836703761793577707539306b6673397179397173717a353766676a7a67676838343439377375716b383436787a3333336a713036736c6b38637a323872657466363672796b7876396a746e6a3072683979666a6170777065617265713071396679797a666664676d6874687973617370757565746e6b72306b32376370326173366a750269d66fd2cea620dc06f1f7de7838f0c8b145b82c7033080c398862f3421a23230382cb637badbb07f9926a06ecd88b6150513ea0060dc8d6dc1c1fb623926b0a0f000077d400000000000b458c00000000000005f10000000000000024000077a22c6263727431713271756332666777737971376463617a73666e3332636a7874667671647671366a6c70706574fd0f016c6e626372743530313834306e317030307234377570703563776561306732396d30667434646432726167397870306e726d6a72396c33726b7a717037706a6c34337a6e6d6b64336c79337364713877646d6b7a757163717a7279787139377a767571737035616478717538766168643730743776747165777578366d6d64337977636639767835736476717567753833327230676e373466733971793971737168746773636638386e377664767136716e71307a657775366d7471616e326c7a306e7534737a72376c6b36646d343673336c78726572656e333972616b7a6c777378346c613538733966773630356d6767766b766879716e743339713976737367777879367571707236713273780000000600000000000003f20000000000000000161b25262710ce00"), "outgoing-chan-set": nil, "updates": map[string]interface{}{ Hex("0000000000000001"): Hex("161b252a770e649b01000000000000053900000000000000000000000000000001"), Hex("0000000000000002"): Hex("161b252ab671bdd90200000000000005f10000000000001a9c0000000000000003"), }, }, }, } ctxb := context.Background() // Restore a legacy database. tempDirName, err := ioutil.TempDir("", "clientstore") require.NoError(t, err) defer os.RemoveAll(tempDirName) tempPath := filepath.Join(tempDirName, dbFileName) db, err := bbolt.Open(tempPath, 0600, nil) require.NoError(t, err) err = db.Update(func(tx *bbolt.Tx) error { return RestoreDB(tx, legacyDb) }) // Close database regardless of update result. db.Close() // Assert update was successful. require.NoError(t, err) // Open db and migrate to the latest version. store, err := NewBoltSwapStore(tempDirName, &chaincfg.MainNetParams) require.NoError(t, err) // Fetch the legacy loop out swap and assert that the updates are still // there. outSwaps, err := store.FetchLoopOutSwaps(ctxb) require.NoError(t, err) outSwap := outSwaps[0] require.Len(t, outSwap.Events, 2) require.Equal(t, StateSuccess, outSwap.Events[1].State) // Fetch the legacy loop in swap and assert that the updates are still // there. inSwaps, err := store.FetchLoopInSwaps(ctxb) require.NoError(t, err) inSwap := inSwaps[0] require.Len(t, inSwap.Events, 3) require.Equal(t, StateSuccess, outSwap.Events[1].State) }