Skip to content

Commit

Permalink
Fix snapshot bug [#3409]
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Sep 21, 2023
1 parent 1f432a2 commit 948f4b6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions internal/database/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,10 @@ func (db *Database) collectMessages(w *snapshot.Writer, index *badger.DB, opts *
return errors.UnknownError.WithFormat("collect %x: %w", hash, err)
}

// Collect the transaction status (which is the only part of the
// transaction entity that is still used by exec v2)
err = records.Collect(batch.newTransaction(transactionKey{Hash: hash}).newStatus(), copts)
// Collect the transaction's records. Executor v2 only uses the
// transaction status, but transactions and signatures from v1 are still
// stored here, so they should be collected.
err = records.Collect(batch.newTransaction(transactionKey{Hash: hash}), copts)
if err != nil {
return errors.UnknownError.WithFormat("collect %x status: %w", hash, err)
}
Expand Down
34 changes: 34 additions & 0 deletions internal/database/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,37 @@ func TestCollectAndRestore(t *testing.T) {
Txn(st.TxID).Succeeds(),
Txn(st.TxID).Produced().Succeeds())
}

func TestPreservationOfOldTransactions(t *testing.T) {
// Some random transaction
env, err := build.Transaction().For("alice", "tokens").BurnTokens(1, 0).
SignWith("alice", "book", "1").Version(1).Timestamp(1).PrivateKey(make([]byte, 64)).
Done()
require.NoError(t, err)

// Store it in a database
txn := env.Transaction[0]
db := database.OpenInMemory(nil)
db.SetObserver(execute.NewDatabaseObserver())
batch := db.Begin(true)
defer batch.Discard()
require.NoError(t, batch.Transaction(txn.GetHash()).Main().Put(&database.SigOrTxn{Transaction: txn}))
require.NoError(t, batch.Account(txn.Header.Principal).MainChain().Inner().AddHash(txn.GetHash(), false))
require.NoError(t, batch.Commit())

// Collect a snapshot
buf := new(ioutil.Buffer)
require.NoError(t, db.Collect(buf, nil, nil))

// Restore the snapshot
db = database.OpenInMemory(nil)
db.SetObserver(execute.NewDatabaseObserver())
require.NoError(t, db.Restore(buf, nil))

// Verify the transaction still exists
batch = db.Begin(false)
defer batch.Discard()
txn2, err := batch.Transaction(txn.GetHash()).Main().Get()
require.NoError(t, err)
require.True(t, txn.Equal(txn2.Transaction))
}

0 comments on commit 948f4b6

Please sign in to comment.