Skip to content

Commit 0a60d18

Browse files
delbonisadiabat
authored andcommitted
Refactoring P2P setup code and set up new DB layer framework (#330)
* Added basic event bus system. * Better sync guarantees for event firing. * Properly handle events being fired when there's no handlers for them. * Basic framework of new storage layer and new p2p framework. * Added "channel state update" event type to HTLC. * Implemented most of connection setup and workflow and added some hooks into qln code to support it. * Removed old P2P connection code replacing it with calls to new system. Hope it all works this time. * Create new PeerManager when initing a LitNode. Also rearranged some things in NewLitNode in qln/init.go to make it clearer what's happening. * Fixed locking problem in eventbus caused by early return. * Implemented peer database for new BoltDB storage layer. * Fixed some issues due to Go having silly semantics regarding pointrs and interfaces. * Implemented a lot of logging messages, fixed some locking problems. * Fixed not starting message processor when initiating connections after handshake. * Removed a couple of unnecssary log messages. * Tweaked some things in the qln event handlers, trying to get it to work. * Fixed mismatched peer numbering schemes confusing some message handling code. * Some tweaks to maybe help with debugging? * Fixed RemotePeer's QCs getting cleared preemptively, causing lots of problems. * Removed some more peer-related DB code that's now redundant. * Added new message handlng infrastructure. * Patched in most of the network message IO code to the new message processor system. * Added peer disconnect handler. Also removed the OmniIn channel that was already unused. * Fixed some inconsistencies and other bugs in marshalling between new and old messaging systems. * Replaced OmniOut with (wrapped) calls to the new messaging system. * Tried to fix "nil elkrem receiver" problem when breaking. * Made some changes to lnio in preparation for walletmgr. * Renamed btcutil/btcec to btcutil/koblitz. * Moved btcutil/koblitz to crypto/koblitz. * Moved DB code into new hierarchy and renamed lnio to lncore which is a more accurate name. * Updated btcec package references to koblitz, as missed in previous commit. * Added back support for UPnP, NAT, and SOCKS5 proxys outbound. * Make sure we're gotten rid of lit and lit-af on `make clean`. * Fixed some naming issues. * Updated references to "log" to new "logging" package. Also fixed some references to btcec that were there after the rebase for some reason. * Fixed some issues with the the argument parsing not working right. * Fix crash(es) involved in Connect RPC call. * Updated code for saving nicknames. * Fixed more nickname stuff in DB code. * Added some serialization/deserialization tests.
1 parent 6a00ae6 commit 0a60d18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2723
-905
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ clean:
4242
rm -f cmd/lit-af/lit-af
4343
rm -f cmd/glit/glit
4444
cd test && ./clean.sh
45+
rm -f lit lit-af
4546

4647
test: lit
4748
build/env.sh ./gotests.sh

btcutil/address.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"bytes"
99
"encoding/hex"
1010
"errors"
11-
1211
"github.com/mit-dci/lit/btcutil/base58"
13-
"github.com/mit-dci/lit/btcutil/btcec"
1412
"github.com/mit-dci/lit/btcutil/chaincfg"
13+
"github.com/mit-dci/lit/crypto/koblitz"
1514
"github.com/mit-dci/lit/crypto/ripemd160"
1615
)
1716

@@ -292,21 +291,21 @@ const (
292291
// AddressPubKey is an Address for a pay-to-pubkey transaction.
293292
type AddressPubKey struct {
294293
pubKeyFormat PubKeyFormat
295-
pubKey *btcec.PublicKey
294+
pubKey *koblitz.PublicKey
296295
pubKeyHashID byte
297296
}
298297

299298
// NewAddressPubKey returns a new AddressPubKey which represents a pay-to-pubkey
300299
// address. The serializedPubKey parameter must be a valid pubkey and can be
301300
// uncompressed, compressed, or hybrid.
302301
func NewAddressPubKey(serializedPubKey []byte, net *chaincfg.Params) (*AddressPubKey, error) {
303-
pubKey, err := btcec.ParsePubKey(serializedPubKey, btcec.S256())
302+
pubKey, err := koblitz.ParsePubKey(serializedPubKey, koblitz.S256())
304303
if err != nil {
305304
return nil, err
306305
}
307306

308307
// Set the format of the pubkey. This probably should be returned
309-
// from btcec, but do it here to avoid API churn. We already know the
308+
// from koblitz, but do it here to avoid API churn. We already know the
310309
// pubkey is valid since it parsed above, so it's safe to simply examine
311310
// the leading byte to get the format.
312311
pkFormat := PKFUncompressed
@@ -397,6 +396,6 @@ func (a *AddressPubKey) AddressPubKeyHash() *AddressPubKeyHash {
397396
}
398397

399398
// PubKey returns the underlying public key for the address.
400-
func (a *AddressPubKey) PubKey() *btcec.PublicKey {
399+
func (a *AddressPubKey) PubKey() *koblitz.PublicKey {
401400
return a.pubKey
402401
}

btcutil/blockchain/compress.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package blockchain
66

77
import (
8-
"github.com/mit-dci/lit/btcutil/btcec"
8+
"github.com/mit-dci/lit/crypto/koblitz"
99
"github.com/mit-dci/lit/btcutil/txscript"
1010
)
1111

@@ -218,7 +218,7 @@ func isPubKey(script []byte) (bool, []byte) {
218218

219219
// Ensure the public key is valid.
220220
serializedPubKey := script[1:34]
221-
_, err := btcec.ParsePubKey(serializedPubKey, btcec.S256())
221+
_, err := koblitz.ParsePubKey(serializedPubKey, koblitz.S256())
222222
if err == nil {
223223
return true, serializedPubKey
224224
}
@@ -230,7 +230,7 @@ func isPubKey(script []byte) (bool, []byte) {
230230

231231
// Ensure the public key is valid.
232232
serializedPubKey := script[1:66]
233-
_, err := btcec.ParsePubKey(serializedPubKey, btcec.S256())
233+
_, err := koblitz.ParsePubKey(serializedPubKey, koblitz.S256())
234234
if err == nil {
235235
return true, serializedPubKey
236236
}
@@ -399,7 +399,7 @@ func decompressScript(compressedPkScript []byte, version int32) []byte {
399399
compressedKey := make([]byte, 33)
400400
compressedKey[0] = byte(encodedScriptSize - 2)
401401
copy(compressedKey[1:], compressedPkScript[1:])
402-
key, err := btcec.ParsePubKey(compressedKey, btcec.S256())
402+
key, err := koblitz.ParsePubKey(compressedKey, koblitz.S256())
403403
if err != nil {
404404
return nil
405405
}

btcutil/hdkeychain/extendedkey.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ import (
1616
"encoding/binary"
1717
"errors"
1818
"fmt"
19-
"math/big"
20-
2119
"github.com/mit-dci/lit/btcutil"
2220
"github.com/mit-dci/lit/btcutil/base58"
23-
"github.com/mit-dci/lit/btcutil/btcec"
2421
"github.com/mit-dci/lit/btcutil/chaincfg"
2522
"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
2623
"github.com/mit-dci/lit/coinparam"
24+
"github.com/mit-dci/lit/crypto/koblitz"
25+
"math/big"
2726
)
2827

2928
const (
@@ -145,8 +144,8 @@ func (k *ExtendedKey) pubKeyBytes() []byte {
145144
// This is a private extended key, so calculate and memoize the public
146145
// key if needed.
147146
if len(k.pubKey) == 0 {
148-
pkx, pky := btcec.S256().ScalarBaseMult(k.key)
149-
pubKey := btcec.PublicKey{Curve: btcec.S256(), X: pkx, Y: pky}
147+
pkx, pky := koblitz.S256().ScalarBaseMult(k.key)
148+
pubKey := koblitz.PublicKey{Curve: koblitz.S256(), X: pkx, Y: pky}
150149
k.pubKey = pubKey.SerializeCompressed()
151150
}
152151

@@ -249,7 +248,7 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {
249248
// a child extended key can't be created for this index and the caller
250249
// should simply increment to the next index.
251250
ilNum := new(big.Int).SetBytes(il)
252-
if ilNum.Cmp(btcec.S256().N) >= 0 || ilNum.Sign() == 0 {
251+
if ilNum.Cmp(koblitz.S256().N) >= 0 || ilNum.Sign() == 0 {
253252
return nil, ErrInvalidChild
254253
}
255254

@@ -271,22 +270,22 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {
271270
// childKey = parse256(Il) + parenKey
272271
keyNum := new(big.Int).SetBytes(k.key)
273272
ilNum.Add(ilNum, keyNum)
274-
ilNum.Mod(ilNum, btcec.S256().N)
273+
ilNum.Mod(ilNum, koblitz.S256().N)
275274
childKey = ilNum.Bytes()
276275
isPrivate = true
277276
} else {
278277
// Case #3.
279278
// Calculate the corresponding intermediate public key for
280279
// intermediate private key.
281-
ilx, ily := btcec.S256().ScalarBaseMult(il)
280+
ilx, ily := koblitz.S256().ScalarBaseMult(il)
282281
if ilx.Sign() == 0 || ily.Sign() == 0 {
283282
return nil, ErrInvalidChild
284283
}
285284

286285
// Convert the serialized compressed parent public key into X
287286
// and Y coordinates so it can be added to the intermediate
288287
// public key.
289-
pubKey, err := btcec.ParsePubKey(k.key, btcec.S256())
288+
pubKey, err := koblitz.ParsePubKey(k.key, koblitz.S256())
290289
if err != nil {
291290
return nil, err
292291
}
@@ -295,8 +294,8 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {
295294
// derive the final child key.
296295
//
297296
// childKey = serP(point(parse256(Il)) + parentKey)
298-
childX, childY := btcec.S256().Add(ilx, ily, pubKey.X, pubKey.Y)
299-
pk := btcec.PublicKey{Curve: btcec.S256(), X: childX, Y: childY}
297+
childX, childY := koblitz.S256().Add(ilx, ily, pubKey.X, pubKey.Y)
298+
pk := koblitz.PublicKey{Curve: koblitz.S256(), X: childX, Y: childY}
300299
childKey = pk.SerializeCompressed()
301300
}
302301

@@ -335,21 +334,21 @@ func (k *ExtendedKey) Neuter() (*ExtendedKey, error) {
335334
k.depth, k.childNum, false), nil
336335
}
337336

338-
// ECPubKey converts the extended key to a btcec public key and returns it.
339-
func (k *ExtendedKey) ECPubKey() (*btcec.PublicKey, error) {
340-
return btcec.ParsePubKey(k.pubKeyBytes(), btcec.S256())
337+
// ECPubKey converts the extended key to a koblitz public key and returns it.
338+
func (k *ExtendedKey) ECPubKey() (*koblitz.PublicKey, error) {
339+
return koblitz.ParsePubKey(k.pubKeyBytes(), koblitz.S256())
341340
}
342341

343-
// ECPrivKey converts the extended key to a btcec private key and returns it.
342+
// ECPrivKey converts the extended key to a koblitz private key and returns it.
344343
// As you might imagine this is only possible if the extended key is a private
345344
// extended key (as determined by the IsPrivate function). The ErrNotPrivExtKey
346345
// error will be returned if this function is called on a public extended key.
347-
func (k *ExtendedKey) ECPrivKey() (*btcec.PrivateKey, error) {
346+
func (k *ExtendedKey) ECPrivKey() (*koblitz.PrivateKey, error) {
348347
if !k.isPrivate {
349348
return nil, ErrNotPrivExtKey
350349
}
351350

352-
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), k.key)
351+
privKey, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), k.key)
353352
return privKey, nil
354353
}
355354

@@ -471,7 +470,7 @@ func NewMaster(seed []byte, net *coinparam.Params) (*ExtendedKey, error) {
471470

472471
// Ensure the key in usable.
473472
secretKeyNum := new(big.Int).SetBytes(secretKey)
474-
if secretKeyNum.Cmp(btcec.S256().N) >= 0 || secretKeyNum.Sign() == 0 {
473+
if secretKeyNum.Cmp(koblitz.S256().N) >= 0 || secretKeyNum.Sign() == 0 {
475474
return nil, ErrUnusableSeed
476475
}
477476

@@ -518,13 +517,13 @@ func NewKeyFromString(key string) (*ExtendedKey, error) {
518517
// of the order of the secp256k1 curve and not be 0.
519518
keyData = keyData[1:]
520519
keyNum := new(big.Int).SetBytes(keyData)
521-
if keyNum.Cmp(btcec.S256().N) >= 0 || keyNum.Sign() == 0 {
520+
if keyNum.Cmp(koblitz.S256().N) >= 0 || keyNum.Sign() == 0 {
522521
return nil, ErrUnusableSeed
523522
}
524523
} else {
525524
// Ensure the public key parses correctly and is actually on the
526525
// secp256k1 curve.
527-
_, err := btcec.ParsePubKey(keyData, btcec.S256())
526+
_, err := koblitz.ParsePubKey(keyData, koblitz.S256())
528527
if err != nil {
529528
return nil, err
530529
}

btcutil/txscript/engine.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"fmt"
1010
"math/big"
1111

12-
"github.com/mit-dci/lit/btcutil/btcec"
12+
"github.com/mit-dci/lit/crypto/koblitz"
1313
"github.com/mit-dci/lit/crypto/fastsha256"
1414
"github.com/mit-dci/lit/wire"
1515
)
@@ -95,7 +95,7 @@ const (
9595
)
9696

9797
// halforder is used to tame ECDSA malleability (see BIP0062).
98-
var halfOrder = new(big.Int).Rsh(btcec.S256().N, 1)
98+
var halfOrder = new(big.Int).Rsh(koblitz.S256().N, 1)
9999

100100
// Engine is the virtual machine that executes scripts.
101101
type Engine struct {

btcutil/txscript/opcode.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"fmt"
1313
"hash"
1414

15-
"github.com/mit-dci/lit/btcutil/btcec"
15+
"github.com/mit-dci/lit/crypto/koblitz"
1616
"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
1717
"github.com/mit-dci/lit/crypto/fastsha256"
1818
"github.com/mit-dci/lit/crypto/ripemd160"
@@ -1932,19 +1932,19 @@ func opcodeCheckSig(op *parsedOpcode, vm *Engine) error {
19321932
hash = calcSignatureHash(subScript, hashType, &vm.tx, vm.txIdx)
19331933
}
19341934

1935-
pubKey, err := btcec.ParsePubKey(pkBytes, btcec.S256())
1935+
pubKey, err := koblitz.ParsePubKey(pkBytes, koblitz.S256())
19361936
if err != nil {
19371937
vm.dstack.PushBool(false)
19381938
return nil
19391939
}
19401940

1941-
var signature *btcec.Signature
1941+
var signature *koblitz.Signature
19421942
if vm.hasFlag(ScriptVerifyStrictEncoding) ||
19431943
vm.hasFlag(ScriptVerifyDERSignatures) {
19441944

1945-
signature, err = btcec.ParseDERSignature(sigBytes, btcec.S256())
1945+
signature, err = koblitz.ParseDERSignature(sigBytes, koblitz.S256())
19461946
} else {
1947-
signature, err = btcec.ParseSignature(sigBytes, btcec.S256())
1947+
signature, err = koblitz.ParseSignature(sigBytes, koblitz.S256())
19481948
}
19491949
if err != nil {
19501950
vm.dstack.PushBool(false)
@@ -1987,7 +1987,7 @@ func opcodeCheckSigVerify(op *parsedOpcode, vm *Engine) error {
19871987
// the same signature multiple times when verifying a multisig.
19881988
type parsedSigInfo struct {
19891989
signature []byte
1990-
parsedSignature *btcec.Signature
1990+
parsedSignature *koblitz.Signature
19911991
parsed bool
19921992
}
19931993

@@ -2122,7 +2122,7 @@ func opcodeCheckMultiSig(op *parsedOpcode, vm *Engine) error {
21222122
signature := rawSig[:len(rawSig)-1]
21232123

21242124
// Only parse and check the signature encoding once.
2125-
var parsedSig *btcec.Signature
2125+
var parsedSig *koblitz.Signature
21262126
if !sigInfo.parsed {
21272127
if err := vm.checkHashTypeEncoding(hashType); err != nil {
21282128
return err
@@ -2136,11 +2136,11 @@ func opcodeCheckMultiSig(op *parsedOpcode, vm *Engine) error {
21362136
if vm.hasFlag(ScriptVerifyStrictEncoding) ||
21372137
vm.hasFlag(ScriptVerifyDERSignatures) {
21382138

2139-
parsedSig, err = btcec.ParseDERSignature(signature,
2140-
btcec.S256())
2139+
parsedSig, err = koblitz.ParseDERSignature(signature,
2140+
koblitz.S256())
21412141
} else {
2142-
parsedSig, err = btcec.ParseSignature(signature,
2143-
btcec.S256())
2142+
parsedSig, err = koblitz.ParseSignature(signature,
2143+
koblitz.S256())
21442144
}
21452145
sigInfo.parsed = true
21462146
if err != nil {
@@ -2162,7 +2162,7 @@ func opcodeCheckMultiSig(op *parsedOpcode, vm *Engine) error {
21622162
}
21632163

21642164
// Parse the pubkey.
2165-
parsedPubKey, err := btcec.ParsePubKey(pubKey, btcec.S256())
2165+
parsedPubKey, err := koblitz.ParsePubKey(pubKey, koblitz.S256())
21662166
if err != nil {
21672167
continue
21682168
}

btcutil/txscript/sigcache.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package txscript
77
import (
88
"sync"
99

10-
"github.com/mit-dci/lit/btcutil/btcec"
10+
"github.com/mit-dci/lit/crypto/koblitz"
1111
"github.com/mit-dci/lit/btcutil/chaincfg/chainhash"
1212
)
1313

@@ -18,8 +18,8 @@ import (
1818
// match. In the occasion that two sigHashes collide, the newer sigHash will
1919
// simply overwrite the existing entry.
2020
type sigCacheEntry struct {
21-
sig *btcec.Signature
22-
pubKey *btcec.PublicKey
21+
sig *koblitz.Signature
22+
pubKey *koblitz.PublicKey
2323
}
2424

2525
// SigCache implements an ECDSA signature verification cache with a randomized
@@ -55,7 +55,7 @@ func NewSigCache(maxEntries uint) *SigCache {
5555
//
5656
// NOTE: This function is safe for concurrent access. Readers won't be blocked
5757
// unless there exists a writer, adding an entry to the SigCache.
58-
func (s *SigCache) Exists(sigHash chainhash.Hash, sig *btcec.Signature, pubKey *btcec.PublicKey) bool {
58+
func (s *SigCache) Exists(sigHash chainhash.Hash, sig *koblitz.Signature, pubKey *koblitz.PublicKey) bool {
5959
s.RLock()
6060
defer s.RUnlock()
6161

@@ -73,7 +73,7 @@ func (s *SigCache) Exists(sigHash chainhash.Hash, sig *btcec.Signature, pubKey *
7373
//
7474
// NOTE: This function is safe for concurrent access. Writers will block
7575
// simultaneous readers until function execution has concluded.
76-
func (s *SigCache) Add(sigHash chainhash.Hash, sig *btcec.Signature, pubKey *btcec.PublicKey) {
76+
func (s *SigCache) Add(sigHash chainhash.Hash, sig *koblitz.Signature, pubKey *koblitz.PublicKey) {
7777
s.Lock()
7878
defer s.Unlock()
7979

0 commit comments

Comments
 (0)