2018-01-17 04:58:58 +01:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2019-02-20 02:05:30 +01:00
|
|
|
"bytes"
|
2018-01-17 04:58:58 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-02-20 02:05:30 +01:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-01-17 04:58:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestWitnessCacheRetrieval tests that we're able to add and lookup new
|
|
|
|
// witnesses to the witness cache.
|
|
|
|
func TestWitnessCacheRetrieval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
2019-02-20 02:05:17 +01:00
|
|
|
// We'll be attempting to add then lookup two simple hash witnesses
|
2018-01-17 04:58:58 +01:00
|
|
|
// within this test.
|
2019-02-20 02:05:17 +01:00
|
|
|
witness1 := rev[:]
|
|
|
|
witness1Key := sha256.Sum256(witness1)
|
|
|
|
|
|
|
|
witness2 := key[:]
|
|
|
|
witness2Key := sha256.Sum256(witness2)
|
|
|
|
|
|
|
|
witnesses := [][]byte{witness1, witness2}
|
|
|
|
keys := [][]byte{witness1Key[:], witness2Key[:]}
|
2018-01-17 04:58:58 +01:00
|
|
|
|
2019-02-20 02:05:17 +01:00
|
|
|
// First, we'll attempt to add the witnesses to the database.
|
|
|
|
err = wCache.AddWitnesses(Sha256HashWitness, witnesses...)
|
2018-01-17 04:58:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:05:17 +01:00
|
|
|
// With the witnesses stored, we'll now attempt to look them up.
|
|
|
|
for i, key := range keys {
|
|
|
|
witness := witnesses[i]
|
|
|
|
|
|
|
|
// We should get back the *exact* same witness as we originally
|
|
|
|
// stored.
|
|
|
|
dbWitness, err := wCache.LookupWitness(Sha256HashWitness, key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to look up witness: %v", err)
|
|
|
|
}
|
2018-01-17 04:58:58 +01:00
|
|
|
|
2019-02-20 02:05:17 +01:00
|
|
|
if !reflect.DeepEqual(witness, dbWitness[:]) {
|
|
|
|
t.Fatalf("witnesses don't match: expected %x, got %x",
|
|
|
|
witness[:], dbWitness[:])
|
|
|
|
}
|
2018-01-17 04:58:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWitnessCacheDeletion tests that we're able to delete a single witness,
|
|
|
|
// and also a class of witnesses from the cache.
|
|
|
|
func TestWitnessCacheDeletion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to make test database: %v", err)
|
|
|
|
}
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
wCache := cdb.NewWitnessCache()
|
|
|
|
|
|
|
|
// We'll start by adding two witnesses to the cache.
|
|
|
|
witness1 := rev[:]
|
|
|
|
witness1Key := sha256.Sum256(witness1)
|
2019-02-20 02:05:17 +01:00
|
|
|
|
2019-02-20 02:05:04 +01:00
|
|
|
if err := wCache.AddWitnesses(Sha256HashWitness, witness1); err != nil {
|
2018-01-17 04:58:58 +01:00
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
witness2 := key[:]
|
|
|
|
witness2Key := sha256.Sum256(witness2)
|
2019-02-20 02:05:17 +01:00
|
|
|
|
2019-02-20 02:05:04 +01:00
|
|
|
if err := wCache.AddWitnesses(Sha256HashWitness, witness2); err != nil {
|
2018-01-17 04:58:58 +01:00
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now delete the first witness. If we attempt to look it up, we
|
|
|
|
// should get ErrNoWitnesses.
|
|
|
|
err = wCache.DeleteWitness(Sha256HashWitness, witness1Key[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to delete witness: %v", err)
|
|
|
|
}
|
|
|
|
_, err = wCache.LookupWitness(Sha256HashWitness, witness1Key[:])
|
|
|
|
if err != ErrNoWitnesses {
|
|
|
|
t.Fatalf("expected ErrNoWitnesses instead got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll attempt to delete the entire witness class itself. When
|
|
|
|
// we try to lookup the second witness, we should again get
|
|
|
|
// ErrNoWitnesses.
|
|
|
|
if err := wCache.DeleteWitnessClass(Sha256HashWitness); err != nil {
|
|
|
|
t.Fatalf("unable to delete witness class: %v", err)
|
|
|
|
}
|
|
|
|
_, err = wCache.LookupWitness(Sha256HashWitness, witness2Key[:])
|
|
|
|
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()
|
|
|
|
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
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 02:05:04 +01:00
|
|
|
err = wCache.AddWitnesses(234, key[:])
|
2018-01-17 04:58:58 +01:00
|
|
|
if err != ErrUnknownWitnessType {
|
|
|
|
t.Fatalf("expected ErrUnknownWitnessType, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 02:05:30 +01:00
|
|
|
|
|
|
|
// TestAddSha256Witnesses tests that insertion using AddSha256Witnesses behaves
|
|
|
|
// identically to the insertion via the generalized interface.
|
|
|
|
func TestAddSha256Witnesses(t *testing.T) {
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
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[:]
|
|
|
|
witness1Key := sha256.Sum256(witness1)
|
|
|
|
|
|
|
|
witness2 := key[:]
|
|
|
|
witness2Key := sha256.Sum256(witness2)
|
|
|
|
|
|
|
|
var (
|
|
|
|
preimages = []lntypes.Preimage{rev, key}
|
|
|
|
witnesses = [][]byte{witness1, witness2}
|
|
|
|
keys = [][]byte{witness1Key[:], witness2Key[:]}
|
|
|
|
)
|
|
|
|
|
|
|
|
err = wCache.AddWitnesses(Sha256HashWitness, witnesses...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, key := range keys {
|
|
|
|
witness := witnesses[i]
|
|
|
|
|
|
|
|
dbWitness, err := wCache.LookupWitness(
|
|
|
|
Sha256HashWitness, key,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the retrieved witness matches the original.
|
|
|
|
if bytes.Compare(dbWitness, witness) != 0 {
|
|
|
|
t.Fatalf("retrieved witness mismatch, want: %x, "+
|
|
|
|
"got: %x", witness, dbWitness)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now delete the witness, as we'll be reinserting it
|
|
|
|
// using the specialized AddSha256Witnesses method.
|
|
|
|
err = wCache.DeleteWitness(Sha256HashWitness, key)
|
|
|
|
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.
|
|
|
|
for i, key := range keys {
|
|
|
|
witness := witnesses[i]
|
|
|
|
|
|
|
|
dbWitness, err := wCache.LookupWitness(
|
|
|
|
Sha256HashWitness, key,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup witness: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that the retrieved witness matches the original.
|
|
|
|
if bytes.Compare(dbWitness, witness) != 0 {
|
|
|
|
t.Fatalf("retrieved witness mismatch, want: %x, "+
|
|
|
|
"got: %x", witness, dbWitness)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|