2017-05-05 18:43:11 +02:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2022-02-07 13:58:21 +01:00
|
|
|
"testing"
|
2017-05-05 18:43:11 +02:00
|
|
|
|
2020-01-28 02:25:36 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-05-05 18:43:11 +02:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2022-05-05 22:11:50 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-05 18:43:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestWaitingProofStore tests add/get/remove functions of the waiting proof
|
|
|
|
// storage.
|
|
|
|
func TestWaitingProofStore(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-27 09:04:55 +02:00
|
|
|
db, err := MakeTestDB(t)
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "failed to make test database")
|
2017-05-05 18:43:11 +02:00
|
|
|
|
2024-08-21 08:34:57 +02:00
|
|
|
proof1 := NewWaitingProof(true, &lnwire.AnnounceSignatures1{
|
2018-01-31 05:19:40 +01:00
|
|
|
NodeSignature: wireSig,
|
|
|
|
BitcoinSignature: wireSig,
|
2020-01-28 02:25:36 +01:00
|
|
|
ExtraOpaqueData: make([]byte, 0),
|
2017-05-05 18:43:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
store, err := NewWaitingProofStore(db)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create the waiting proofs storage: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Add(proof1); err != nil {
|
|
|
|
t.Fatalf("unable add proof to storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proof2, err := store.Get(proof1.Key())
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "unable retrieve proof from storage")
|
2017-05-05 18:43:11 +02:00
|
|
|
if !reflect.DeepEqual(proof1, proof2) {
|
2020-01-28 02:25:36 +01:00
|
|
|
t.Fatalf("wrong proof retrieved: expected %v, got %v",
|
|
|
|
spew.Sdump(proof1), spew.Sdump(proof2))
|
2017-05-05 18:43:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := store.Get(proof1.OppositeKey()); err != ErrWaitingProofNotFound {
|
|
|
|
t.Fatalf("proof shouldn't be found: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Remove(proof1.Key()); err != nil {
|
|
|
|
t.Fatalf("unable remove proof from storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.ForAll(func(proof *WaitingProof) error {
|
|
|
|
return errors.New("storage should be empty")
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {}); err != nil && err != ErrWaitingProofNotFound {
|
2017-05-05 18:43:11 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|