github.com/decred/dcrlnd@v0.7.6/channeldb/migration_01_to_11/payments_test.go (about)

     1  package migration_01_to_11
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math/rand"
     7  	"time"
     8  
     9  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
    10  	lnwire "github.com/decred/dcrlnd/channeldb/migration/lnwire21"
    11  )
    12  
    13  var (
    14  	priv, _ = secp256k1.GeneratePrivateKey()
    15  	pub     = priv.PubKey()
    16  )
    17  
    18  func makeFakePayment() *outgoingPayment {
    19  	fakeInvoice := &Invoice{
    20  		// Use single second precision to avoid false positive test
    21  		// failures due to the monotonic time component.
    22  		CreationDate:   time.Unix(time.Now().Unix(), 0),
    23  		Memo:           []byte("fake memo"),
    24  		Receipt:        []byte("fake receipt"),
    25  		PaymentRequest: []byte(""),
    26  	}
    27  
    28  	copy(fakeInvoice.Terms.PaymentPreimage[:], rev[:])
    29  	fakeInvoice.Terms.Value = lnwire.NewMAtomsFromAtoms(10000)
    30  
    31  	fakePath := make([][33]byte, 3)
    32  	for i := 0; i < 3; i++ {
    33  		copy(fakePath[i][:], bytes.Repeat([]byte{byte(i)}, 33))
    34  	}
    35  
    36  	fakePayment := &outgoingPayment{
    37  		Invoice:        *fakeInvoice,
    38  		Fee:            101,
    39  		Path:           fakePath,
    40  		TimeLockLength: 1000,
    41  	}
    42  	copy(fakePayment.PaymentPreimage[:], rev[:])
    43  	return fakePayment
    44  }
    45  
    46  // randomBytes creates random []byte with length in range [minLen, maxLen)
    47  func randomBytes(minLen, maxLen int) ([]byte, error) {
    48  	randBuf := make([]byte, minLen+rand.Intn(maxLen-minLen))
    49  
    50  	if _, err := rand.Read(randBuf); err != nil {
    51  		return nil, fmt.Errorf("Internal error. "+
    52  			"Cannot generate random string: %v", err)
    53  	}
    54  
    55  	return randBuf, nil
    56  }
    57  
    58  func makeRandomFakePayment() (*outgoingPayment, error) {
    59  	var err error
    60  	fakeInvoice := &Invoice{
    61  		// Use single second precision to avoid false positive test
    62  		// failures due to the monotonic time component.
    63  		CreationDate: time.Unix(time.Now().Unix(), 0),
    64  	}
    65  
    66  	fakeInvoice.Memo, err = randomBytes(1, 50)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	fakeInvoice.Receipt, err = randomBytes(1, 50)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	fakeInvoice.PaymentRequest, err = randomBytes(1, 50)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	preImg, err := randomBytes(32, 33)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	copy(fakeInvoice.Terms.PaymentPreimage[:], preImg)
    86  
    87  	fakeInvoice.Terms.Value = lnwire.MilliAtom(rand.Intn(10000))
    88  
    89  	fakePathLen := 1 + rand.Intn(5)
    90  	fakePath := make([][33]byte, fakePathLen)
    91  	for i := 0; i < fakePathLen; i++ {
    92  		b, err := randomBytes(33, 34)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		copy(fakePath[i][:], b)
    97  	}
    98  
    99  	fakePayment := &outgoingPayment{
   100  		Invoice:        *fakeInvoice,
   101  		Fee:            lnwire.MilliAtom(rand.Intn(1001)),
   102  		Path:           fakePath,
   103  		TimeLockLength: uint32(rand.Intn(10000)),
   104  	}
   105  	copy(fakePayment.PaymentPreimage[:], fakeInvoice.Terms.PaymentPreimage[:])
   106  
   107  	return fakePayment, nil
   108  }