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

     1  package migration_01_to_11
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  
     9  	"github.com/decred/dcrlnd/keychain"
    10  
    11  	"github.com/decred/dcrd/chaincfg/chainhash"
    12  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
    13  	"github.com/decred/dcrd/dcrutil/v4"
    14  	"github.com/decred/dcrd/wire"
    15  	lnwire "github.com/decred/dcrlnd/channeldb/migration/lnwire21"
    16  	"github.com/decred/dcrlnd/shachain"
    17  )
    18  
    19  func privKeyFromBytes(b []byte) (*secp256k1.PrivateKey, *secp256k1.PublicKey) {
    20  	k := secp256k1.PrivKeyFromBytes(b)
    21  	return k, k.PubKey()
    22  }
    23  
    24  var (
    25  	key = [chainhash.HashSize]byte{
    26  		0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
    27  		0x68, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
    28  		0xd, 0xe7, 0x93, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
    29  		0x1e, 0xb, 0x4c, 0xf9, 0x9e, 0xc5, 0x8c, 0xe9,
    30  	}
    31  	rev = [chainhash.HashSize]byte{
    32  		0x51, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
    33  		0x48, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
    34  		0x2d, 0xe7, 0x93, 0xe4,
    35  	}
    36  	testTx = &wire.MsgTx{
    37  		Version: 1,
    38  		TxIn: []*wire.TxIn{
    39  			{
    40  				PreviousOutPoint: wire.OutPoint{
    41  					Hash:  chainhash.Hash{},
    42  					Index: 0xffffffff,
    43  				},
    44  				SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
    45  				Sequence:        0xffffffff,
    46  			},
    47  		},
    48  		TxOut: []*wire.TxOut{
    49  			{
    50  				Value: 5000000000,
    51  				PkScript: []byte{
    52  					0x41, // OP_DATA_65
    53  					0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
    54  					0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
    55  					0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
    56  					0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
    57  					0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
    58  					0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
    59  					0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
    60  					0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
    61  					0xa6, // 65-byte signature
    62  					0xac, // OP_CHECKSIG
    63  				},
    64  			},
    65  		},
    66  		LockTime: 5,
    67  	}
    68  	privKey, pubKey = privKeyFromBytes(key[:])
    69  )
    70  
    71  // makeTestDB creates a new instance of the ChannelDB for testing purposes. A
    72  // callback which cleans up the created temporary directories is also returned
    73  // and intended to be executed after the test completes.
    74  func makeTestDB() (*DB, func(), error) {
    75  	// First, create a temporary directory to be used for the duration of
    76  	// this test.
    77  	tempDirName, err := ioutil.TempDir("", "channeldb")
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  
    82  	// Next, create channeldb for the first time.
    83  	cdb, err := Open(tempDirName)
    84  	if err != nil {
    85  		return nil, nil, err
    86  	}
    87  
    88  	cleanUp := func() {
    89  		cdb.Close()
    90  		os.RemoveAll(tempDirName)
    91  	}
    92  
    93  	return cdb, cleanUp, nil
    94  }
    95  
    96  func createTestChannelState(cdb *DB) (*OpenChannel, error) {
    97  	// Simulate 1000 channel updates.
    98  	producer, err := shachain.NewRevocationProducerFromBytes(key[:])
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	store := shachain.NewRevocationStore()
   103  	for i := 0; i < 1; i++ {
   104  		preImage, err := producer.AtIndex(uint64(i))
   105  		if err != nil {
   106  			return nil, err
   107  		}
   108  
   109  		if err := store.AddNextEntry(preImage); err != nil {
   110  			return nil, err
   111  		}
   112  	}
   113  
   114  	localCfg := ChannelConfig{
   115  		ChannelConstraints: ChannelConstraints{
   116  			DustLimit:        dcrutil.Amount(rand.Int63()),
   117  			MaxPendingAmount: lnwire.MilliAtom(rand.Int63()),
   118  			ChanReserve:      dcrutil.Amount(rand.Int63()),
   119  			MinHTLC:          lnwire.MilliAtom(rand.Int63()),
   120  			MaxAcceptedHtlcs: uint16(rand.Int31()),
   121  			CsvDelay:         uint16(rand.Int31()),
   122  		},
   123  		MultiSigKey: keychain.KeyDescriptor{
   124  			PubKey: privKey.PubKey(),
   125  		},
   126  		RevocationBasePoint: keychain.KeyDescriptor{
   127  			PubKey: privKey.PubKey(),
   128  		},
   129  		PaymentBasePoint: keychain.KeyDescriptor{
   130  			PubKey: privKey.PubKey(),
   131  		},
   132  		DelayBasePoint: keychain.KeyDescriptor{
   133  			PubKey: privKey.PubKey(),
   134  		},
   135  		HtlcBasePoint: keychain.KeyDescriptor{
   136  			PubKey: privKey.PubKey(),
   137  		},
   138  	}
   139  	remoteCfg := ChannelConfig{
   140  		ChannelConstraints: ChannelConstraints{
   141  			DustLimit:        dcrutil.Amount(rand.Int63()),
   142  			MaxPendingAmount: lnwire.MilliAtom(rand.Int63()),
   143  			ChanReserve:      dcrutil.Amount(rand.Int63()),
   144  			MinHTLC:          lnwire.MilliAtom(rand.Int63()),
   145  			MaxAcceptedHtlcs: uint16(rand.Int31()),
   146  			CsvDelay:         uint16(rand.Int31()),
   147  		},
   148  		MultiSigKey: keychain.KeyDescriptor{
   149  			PubKey: privKey.PubKey(),
   150  			KeyLocator: keychain.KeyLocator{
   151  				Family: keychain.KeyFamilyMultiSig,
   152  				Index:  9,
   153  			},
   154  		},
   155  		RevocationBasePoint: keychain.KeyDescriptor{
   156  			PubKey: privKey.PubKey(),
   157  			KeyLocator: keychain.KeyLocator{
   158  				Family: keychain.KeyFamilyRevocationBase,
   159  				Index:  8,
   160  			},
   161  		},
   162  		PaymentBasePoint: keychain.KeyDescriptor{
   163  			PubKey: privKey.PubKey(),
   164  			KeyLocator: keychain.KeyLocator{
   165  				Family: keychain.KeyFamilyPaymentBase,
   166  				Index:  7,
   167  			},
   168  		},
   169  		DelayBasePoint: keychain.KeyDescriptor{
   170  			PubKey: privKey.PubKey(),
   171  			KeyLocator: keychain.KeyLocator{
   172  				Family: keychain.KeyFamilyDelayBase,
   173  				Index:  6,
   174  			},
   175  		},
   176  		HtlcBasePoint: keychain.KeyDescriptor{
   177  			PubKey: privKey.PubKey(),
   178  			KeyLocator: keychain.KeyLocator{
   179  				Family: keychain.KeyFamilyHtlcBase,
   180  				Index:  5,
   181  			},
   182  		},
   183  	}
   184  
   185  	chanID := lnwire.NewShortChanIDFromInt(uint64(rand.Int63()))
   186  
   187  	return &OpenChannel{
   188  		ChanType:            SingleFunderBit,
   189  		ChainHash:           key,
   190  		FundingOutpoint:     wire.OutPoint{Hash: key, Index: rand.Uint32()},
   191  		ShortChannelID:      chanID,
   192  		IsInitiator:         true,
   193  		IsPending:           true,
   194  		IdentityPub:         pubKey,
   195  		Capacity:            dcrutil.Amount(10000),
   196  		LocalChanCfg:        localCfg,
   197  		RemoteChanCfg:       remoteCfg,
   198  		TotalMAtomsSent:     8,
   199  		TotalMAtomsReceived: 2,
   200  		LocalCommitment: ChannelCommitment{
   201  			CommitHeight:  0,
   202  			LocalBalance:  lnwire.MilliAtom(9000),
   203  			RemoteBalance: lnwire.MilliAtom(3000),
   204  			CommitFee:     dcrutil.Amount(rand.Int63()),
   205  			FeePerKB:      dcrutil.Amount(5000),
   206  			CommitTx:      testTx,
   207  			CommitSig:     bytes.Repeat([]byte{1}, 71),
   208  		},
   209  		RemoteCommitment: ChannelCommitment{
   210  			CommitHeight:  0,
   211  			LocalBalance:  lnwire.MilliAtom(3000),
   212  			RemoteBalance: lnwire.MilliAtom(9000),
   213  			CommitFee:     dcrutil.Amount(rand.Int63()),
   214  			FeePerKB:      dcrutil.Amount(5000),
   215  			CommitTx:      testTx,
   216  			CommitSig:     bytes.Repeat([]byte{1}, 71),
   217  		},
   218  		NumConfsRequired:        4,
   219  		RemoteCurrentRevocation: privKey.PubKey(),
   220  		RemoteNextRevocation:    privKey.PubKey(),
   221  		RevocationProducer:      producer,
   222  		RevocationStore:         store,
   223  		Db:                      cdb,
   224  		FundingTxn:              testTx,
   225  	}, nil
   226  }