2018-01-17 06:58:58 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
2019-02-20 04:05:30 +03:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-01-17 06:58:58 +03:00
|
|
|
)
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// TestWitnessCacheSha256Retrieval tests that we're able to add and lookup new
|
|
|
|
// sha256 preimages to the witness cache.
|
|
|
|
func TestWitnessCacheSha256Retrieval(t *testing.T) {
|
2018-01-17 06:58:58 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// We'll be attempting to add then lookup two simple sha256 preimages
|
2018-01-17 06:58:58 +03:00
|
|
|
// within this test.
|
2019-02-20 04:06:42 +03:00
|
|
|
preimage1 := lntypes.Preimage(rev)
|
|
|
|
preimage2 := lntypes.Preimage(key)
|
2019-02-20 04:05:17 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
preimages := []lntypes.Preimage{preimage1, preimage2}
|
|
|
|
hashes := []lntypes.Hash{preimage1.Hash(), preimage2.Hash()}
|
2018-01-17 06:58:58 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// First, we'll attempt to add the preimages to the database.
|
|
|
|
err = wCache.AddSha256Witnesses(preimages...)
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// With the preimages stored, we'll now attempt to look them up.
|
|
|
|
for i, hash := range hashes {
|
|
|
|
preimage := preimages[i]
|
2019-02-20 04:05:17 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// We should get back the *exact* same preimage as we originally
|
2019-02-20 04:05:17 +03:00
|
|
|
// stored.
|
2019-02-20 04:06:42 +03:00
|
|
|
dbPreimage, err := wCache.LookupSha256Witness(hash)
|
2019-02-20 04:05:17 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to look up witness: %v", err)
|
|
|
|
}
|
2018-01-17 06:58:58 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
if preimage != dbPreimage {
|
2019-02-20 04:05:17 +03:00
|
|
|
t.Fatalf("witnesses don't match: expected %x, got %x",
|
2019-02-20 04:06:42 +03:00
|
|
|
preimage[:], dbPreimage[:])
|
2019-02-20 04:05:17 +03:00
|
|
|
}
|
2018-01-17 06:58:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// TestWitnessCacheSha256Deletion tests that we're able to delete a single
|
|
|
|
// sha256 preimage, and also a class of witnesses from the cache.
|
|
|
|
func TestWitnessCacheSha256Deletion(t *testing.T) {
|
2018-01-17 06:58:58 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// We'll start by adding two preimages to the cache.
|
|
|
|
preimage1 := lntypes.Preimage(key)
|
|
|
|
hash1 := preimage1.Hash()
|
|
|
|
|
|
|
|
preimage2 := lntypes.Preimage(rev)
|
|
|
|
hash2 := preimage2.Hash()
|
2019-02-20 04:05:17 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
if err := wCache.AddSha256Witnesses(preimage1); err != nil {
|
2018-01-17 06:58:58 +03:00
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
if err := wCache.AddSha256Witnesses(preimage2); err != nil {
|
2018-01-17 06:58:58 +03:00
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
// We'll now delete the first preimage. If we attempt to look it up, we
|
2018-01-17 06:58:58 +03:00
|
|
|
// should get ErrNoWitnesses.
|
2019-02-20 04:06:42 +03:00
|
|
|
err = wCache.DeleteSha256Witness(hash1)
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to delete witness: %v", err)
|
|
|
|
}
|
2019-02-20 04:06:42 +03:00
|
|
|
_, err = wCache.LookupSha256Witness(hash1)
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != ErrNoWitnesses {
|
|
|
|
t.Fatalf("expected ErrNoWitnesses instead got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll attempt to delete the entire witness class itself. When
|
2019-02-20 04:06:42 +03:00
|
|
|
// we try to lookup the second preimage, we should again get
|
2018-01-17 06:58:58 +03:00
|
|
|
// ErrNoWitnesses.
|
|
|
|
if err := wCache.DeleteWitnessClass(Sha256HashWitness); err != nil {
|
|
|
|
t.Fatalf("unable to delete witness class: %v", err)
|
|
|
|
}
|
2019-02-20 04:06:42 +03:00
|
|
|
_, err = wCache.LookupSha256Witness(hash2)
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != ErrNoWitnesses {
|
|
|
|
t.Fatalf("expected ErrNoWitnesses instead got: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWitnessCacheUnknownWitness tests that we get an error if we attempt to
|
|
|
|
// query/add/delete an unknown witness.
|
|
|
|
func TestWitnessCacheUnknownWitness(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
|
|
|
// We'll attempt to add a new, undefined witness type to the database.
|
|
|
|
// We should get an error.
|
2019-02-20 04:06:42 +03:00
|
|
|
err = wCache.legacyAddWitnesses(234, key[:])
|
2018-01-17 06:58:58 +03:00
|
|
|
if err != ErrUnknownWitnessType {
|
|
|
|
t.Fatalf("expected ErrUnknownWitnessType, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 04:05:30 +03:00
|
|
|
|
|
|
|
// TestAddSha256Witnesses tests that insertion using AddSha256Witnesses behaves
|
|
|
|
// identically to the insertion via the generalized interface.
|
|
|
|
func TestAddSha256Witnesses(t *testing.T) {
|
2020-06-24 13:50:11 +03:00
|
|
|
cdb, cleanUp, err := MakeTestDB()
|
2019-02-20 04:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
|
|
|
// We'll start by adding a witnesses to the cache using the generic
|
|
|
|
// AddWitnesses method.
|
|
|
|
witness1 := rev[:]
|
2019-02-20 04:06:42 +03:00
|
|
|
preimage1 := lntypes.Preimage(rev)
|
|
|
|
hash1 := preimage1.Hash()
|
2019-02-20 04:05:30 +03:00
|
|
|
|
|
|
|
witness2 := key[:]
|
2019-02-20 04:06:42 +03:00
|
|
|
preimage2 := lntypes.Preimage(key)
|
|
|
|
hash2 := preimage2.Hash()
|
2019-02-20 04:05:30 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
witnesses = [][]byte{witness1, witness2}
|
2019-02-20 04:06:42 +03:00
|
|
|
preimages = []lntypes.Preimage{preimage1, preimage2}
|
|
|
|
hashes = []lntypes.Hash{hash1, hash2}
|
2019-02-20 04:05:30 +03:00
|
|
|
)
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
err = wCache.legacyAddWitnesses(Sha256HashWitness, witnesses...)
|
2019-02-20 04:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
for i, hash := range hashes {
|
|
|
|
preimage := preimages[i]
|
2019-02-20 04:05:30 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
dbPreimage, err := wCache.LookupSha256Witness(hash)
|
2019-02-20 04:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the retrieved witness matches the original.
|
2019-02-20 04:06:42 +03:00
|
|
|
if dbPreimage != preimage {
|
2019-02-20 04:05:30 +03:00
|
|
|
t.Fatalf("retrieved witness mismatch, want: %x, "+
|
2019-02-20 04:06:42 +03:00
|
|
|
"got: %x", preimage, dbPreimage)
|
2019-02-20 04:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now delete the witness, as we'll be reinserting it
|
|
|
|
// using the specialized AddSha256Witnesses method.
|
2019-02-20 04:06:42 +03:00
|
|
|
err = wCache.DeleteSha256Witness(hash)
|
2019-02-20 04:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to delete witness: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, add the same witnesses using the type-safe interface for
|
|
|
|
// lntypes.Preimages..
|
|
|
|
err = wCache.AddSha256Witnesses(preimages...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add sha256 preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, iterate over the keys and assert that the returned witnesses
|
|
|
|
// match the original witnesses. This asserts that the specialized
|
|
|
|
// insertion method behaves identically to the generalized interface.
|
2019-02-20 04:06:42 +03:00
|
|
|
for i, hash := range hashes {
|
|
|
|
preimage := preimages[i]
|
2019-02-20 04:05:30 +03:00
|
|
|
|
2019-02-20 04:06:42 +03:00
|
|
|
dbPreimage, err := wCache.LookupSha256Witness(hash)
|
2019-02-20 04:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the retrieved witness matches the original.
|
2019-02-20 04:06:42 +03:00
|
|
|
if dbPreimage != preimage {
|
2019-02-20 04:05:30 +03:00
|
|
|
t.Fatalf("retrieved witness mismatch, want: %x, "+
|
2019-02-20 04:06:42 +03:00
|
|
|
"got: %x", preimage, dbPreimage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacyAddWitnesses adds a batch of new witnesses of wType to the witness
|
|
|
|
// cache. The type of the witness will be used to map each witness to the key
|
|
|
|
// that will be used to look it up. All witnesses should be of the same
|
|
|
|
// WitnessType.
|
|
|
|
//
|
|
|
|
// NOTE: Previously this method exposed a generic interface for adding
|
|
|
|
// witnesses, which has since been deprecated in favor of a strongly typed
|
|
|
|
// interface for each witness class. We keep this method around to assert the
|
|
|
|
// correctness of specialized witness adding methods.
|
|
|
|
func (w *WitnessCache) legacyAddWitnesses(wType WitnessType,
|
|
|
|
witnesses ...[]byte) error {
|
|
|
|
|
|
|
|
// Optimistically compute the witness keys before attempting to start
|
|
|
|
// the db transaction.
|
|
|
|
entries := make([]witnessEntry, 0, len(witnesses))
|
|
|
|
for _, witness := range witnesses {
|
|
|
|
// Map each witness to its key by applying the appropriate
|
|
|
|
// transformation for the given witness type.
|
|
|
|
switch wType {
|
|
|
|
case Sha256HashWitness:
|
|
|
|
key := sha256.Sum256(witness)
|
|
|
|
entries = append(entries, witnessEntry{
|
|
|
|
key: key[:],
|
|
|
|
witness: witness,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return ErrUnknownWitnessType
|
2019-02-20 04:05:30 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 04:06:42 +03:00
|
|
|
|
|
|
|
return w.addWitnessEntries(wType, entries)
|
2019-02-20 04:05:30 +03:00
|
|
|
}
|