Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sensei #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/gencluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func run() error {
sort.SliceStable(nodes, func(i, j int) bool { return nodes[i] < nodes[j] })

target := os.Getenv("TARGET")
if target != "cln" && target != "lnd" && target != "lnd-managej" {
if target != "cln" && target != "lnd" && target != "lnd-managej" && target != "sensei" {
return fmt.Errorf("unknown target %v", target)
}

Expand Down Expand Up @@ -113,6 +113,16 @@ func run() error {
Command: "--network=regtest",
}

case alias == "start" && target == "sensei":
serv = service{
Build: "sensei",
DependsOn: []string{"bitcoind"},
Volumes: []string{
"lnd:/cfg",
},
Command: fmt.Sprintf("./senseid --network=regtest --bitcoind-rpc-host=bitcoind --bitcoind-rpc-port=8332 --bitcoind-rpc-username=test --bitcoind-rpc-password=test --database-url=sensei.db"),
}

default:
serv = service{
Image: "pathfinding-benchmark-lnd",
Expand Down
13 changes: 5 additions & 8 deletions cmd/testrunner/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,23 @@ func (l *clightningConnection) NewAddress() (string, error) {
}

func (l *clightningConnection) OpenChannel(peerKey string, amtSat int64,
pushAmtSat int64, private bool) (*lnrpc.ChannelPoint, error) {
pushAmtSat int64, private bool) error {

sat := glightning.NewSat64(uint64(amtSat))
pushMsat := glightning.NewMsat(uint64(pushAmtSat) * 1e3)
minConf := uint16(0)

resp, err := l.client.FundChannelExt(peerKey, sat, nil, private, &minConf, pushMsat)
if err != nil {
return nil, err
return err
}

txID, err := chainhash.NewHashFromStr(resp.FundingTxId)
_, err = chainhash.NewHashFromStr(resp.FundingTxId)
if err != nil {
return nil, err
return err
}

return &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{FundingTxidBytes: txID[:]},
// Index unavailable for cln?
}, err
return nil
}

func (l *clightningConnection) ActiveChannels() (int, error) {
Expand Down
6 changes: 4 additions & 2 deletions cmd/testrunner/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getBitcoindConnection() (*rpcclient.Client, error) {

func run(_ *cli.Context) error {
target := os.Getenv("TARGET")
if target != "cln" && target != "lnd" && target != "lnd-managej" {
if target != "cln" && target != "lnd" && target != "lnd-managej" && target != "sensei" {
return fmt.Errorf("unknown target %v", target)
}

Expand Down Expand Up @@ -96,6 +96,8 @@ func run(_ *cli.Context) error {
client, err = getClightningConnection(node)
case node == "start" && target == "lnd-managej":
client, err = getLndManageJConnection(node)
case node == "start" && target == "sensei":
client, err = getSenseiConnection(node)
default:
client, err = getLndConnection(node)
}
Expand Down Expand Up @@ -160,7 +162,7 @@ func run(_ *cli.Context) error {
nodeLog.Infow("Open channel", "peer", peer)

for _, channel := range channels {
_, err := client.OpenChannel(
err := client.OpenChannel(
peerKey, int64(channel.Capacity),
int64(channel.RemoteBalance),
channel.Private,
Expand Down
14 changes: 4 additions & 10 deletions cmd/testrunner/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,20 @@ func (l *lndConnection) NewAddress() (string, error) {
}

func (l *lndConnection) OpenChannel(peerKey string, amtSat int64,
pushAmtSat int64, private bool) (*lnrpc.ChannelPoint, error) {
pushAmtSat int64, private bool) error {

resp, err := l.lightningClient.OpenChannelSync(context.Background(), &lnrpc.OpenChannelRequest{
_, err := l.lightningClient.OpenChannelSync(context.Background(), &lnrpc.OpenChannelRequest{
LocalFundingAmount: amtSat,
NodePubkeyString: peerKey,
SpendUnconfirmed: true,
PushSat: pushAmtSat,
Private: private,
})
if err != nil {
return nil, err
return err
}

chanPoint := lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: resp.GetFundingTxidBytes(),
},
OutputIndex: resp.OutputIndex,
}
return &chanPoint, nil
return nil
}

func (l *lndConnection) SetPolicy(chanPoint *lnrpc.ChannelPoint,
Expand Down
3 changes: 1 addition & 2 deletions cmd/testrunner/node_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
)

type nodeInterface interface {
GetInfo() (*info, error)
Connect(key, host string) error
NewAddress() (string, error)
OpenChannel(peerKey string, amtSat int64, pushAmtSat int64, private bool) (*lnrpc.ChannelPoint, error)
OpenChannel(peerKey string, amtSat int64, pushAmtSat int64, private bool) error
ActiveChannels() (int, error)
AddInvoice(amtMsat int64) (string, error)
SendPayment(invoice string, aliasMap map[string]string) error
Expand Down
269 changes: 269 additions & 0 deletions cmd/testrunner/sensei.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
package main
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johncantrell97 I might now be getting into a different class of hurdles:

node-start_1         | thread 'main' panicked at 'invalid bitcoind rpc config: Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }', src/main.rs:200:14
node-start_1         | note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
node-start_1         | manage your sensei node at http://127.0.0.1:5401/admin/nodes
node-start_1         | starting 02482907cbfcfa4ec558978bf336203f4af7c010383134b705fa60ab1c687461c9@127.0.0.1:9735
node-start_1         | thread 'sensei' panicked at 'assertion failed: `(left == right)`
node-start_1         |   left: `0c84467d65d0150756208a428f6a0cb281a7c1c9f594c8d7f556e8875a7f0fb7`,
node-start_1         |  right: `6080f1f6eea2bf23c045f549de8ec109fa8cb6e3407fcb6f32ddc884d925a27e`: Blocks must be connected in chain-order - the connected header must build on the last connected header', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/lightning-0.0.108/src/ln/channelmanager.rs:5478:13

This should be reproducible by running ./run.sh sensei

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well the first panic indicates incorrect bitcoind rpc configuration.

the second one is saying that it tried to connect a block where the header.prev_blockhash did not match the best known blockhash. so for some reason it missed blocks.

it's odd that it even got that far or are these separate runs where you fixed rpc info?

I'd try blowing up your sensei db and try a fresh run

Copy link
Contributor Author

@joostjager joostjager Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the first one I think is when it starts a bit early and fails because bitcoind isn't running yet. Docker-compose will then restart, so yes, this could be considered separate runs. I believe it's unrelated to the second error.

The sensei db will be created new every time I run run.sh. But I did find out that the error isn't always happening. Appears to be timing-dependent. Perhaps because a whole bunch of blocks are generated in quick succession, something bad happens occasionally?


import (
"context"
"fmt"
"time"

"github.com/bottlepay/lightning-benchmark/graphreader"
"github.com/bottlepay/lightning-benchmark/sensei"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/routing/route"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type senseiConnection struct {
conn *grpc.ClientConn
pubKey route.Vertex
alias string
client sensei.NodeClient
}

func getSenseiConnection(alias string) (*senseiConnection, error) {
logger := log.With("node", alias)

rpcHost := fmt.Sprintf("node-%v:5401", alias)

opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}

logger.Infow("Attempting to connect to sensei")

var adminConn *grpc.ClientConn
err := tryFunc(
func() error {
var err error
adminConn, err = grpc.Dial(rpcHost, opts...)
if err != nil {
return err
}
return err
}, 60)
if err != nil {
return nil, err
}
defer adminConn.Close()

logger.Infow("Creating node")

adminClient := sensei.NewAdminClient(adminConn)

createResp, err := adminClient.CreateAdmin(context.Background(), &sensei.CreateAdminRequest{
Username: "admin",
Alias: alias,
Passphrase: "secret",
Start: true,
})
if err != nil {
return nil, err
}

pubkey, err := route.NewVertexFromStr(createResp.Pubkey)
if err != nil {
return nil, err
}

time.Sleep(time.Second)

mac := createResp.Macaroon

cred, err := NewMacaroonCredential(mac)
if err != nil {
return nil, err
}

opts = append(opts,
grpc.WithPerRPCCredentials(cred),
)

logger.Infow("Connecting with macaroon")

var conn *grpc.ClientConn
err = tryFunc(
func() error {
var err error
conn, err = grpc.Dial(rpcHost, opts...)
if err != nil {
return err
}
return err
}, 60)
if err != nil {
return nil, err
}

return &senseiConnection{
alias: alias,
conn: conn,
pubKey: pubkey,
client: sensei.NewNodeClient(conn),
}, nil
}

func (l *senseiConnection) PubKey() string {
return l.pubKey.String()
}

func (l *senseiConnection) Close() {
l.conn.Close()
}

func (l *senseiConnection) Connect(key, host string) error {
_, err := l.client.ConnectPeer(context.Background(), &sensei.ConnectPeerRequest{
NodeConnectionString: fmt.Sprintf("%v@%v", key, host),
})

return err
}

func (l *senseiConnection) NewAddress() (string, error) {
resp, err := l.client.GetUnusedAddress(
context.Background(),
&sensei.GetUnusedAddressRequest{},
)
if err != nil {
return "", err
}
return resp.Address, nil
}

func (l *senseiConnection) OpenChannel(peerKey string, amtSat int64,
pushAmtSat int64, private bool) error {

var pushAmt uint64 = uint64(pushAmtSat)

_, err := l.client.OpenChannels(context.Background(), &sensei.OpenChannelsRequest{
Requests: []*sensei.OpenChannelRequest{
{
AmountSats: uint64(amtSat),
Public: !private,
CounterpartyPubkey: peerKey,
PushAmountMsats: &pushAmt,
},
},
})
if err != nil {
return err
}

return nil
}

func (l *senseiConnection) SetPolicy(chanPoint *lnrpc.ChannelPoint,
policy *graphreader.PolicyData) error {

// Not necessary for sender node.
panic("not implemented")
}

func (l *senseiConnection) ActiveChannels() (int, error) {
resp, err := l.client.ListChannels(context.Background(), &sensei.ListChannelsRequest{})
if err != nil {
return 0, err
}

var active = 0
for _, ch := range resp.Channels {
if ch.IsUsable {
active++
}
}

return active, nil
}

func (l *senseiConnection) IsSynced(totalEdges, localChannels int) (bool, error) {
resp, err := l.client.NetworkGraphInfo(context.Background(), &sensei.NetworkGraphInfoRequest{})
if err != nil {
return false, err
}

log.Debugw("Syncing",
"edges", resp.NumKnownEdgePolicies,
"totalEdges", totalEdges,
"localChannels", localChannels)

synced := resp.NumKnownEdgePolicies == uint64(totalEdges)

return synced, nil
}

func (l *senseiConnection) AddInvoice(amtMsat int64) (string, error) {
// Not necessary for sender node.
panic("not implemented")
}

func (l *senseiConnection) SendPayment(invoice string, aliasMap map[string]string) error {
Copy link
Contributor Author

@joostjager joostjager Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is still a bit flakey, but I did get to the point where the test payments are started. Only it seems that they all execute in parallel?

Is it possible to do a synchronous payment?

node-start_1                   | 2022-06-14T07:04:36.291228969Z EVENT: initiated sending 25000000 msats to 02d191e330745d099741f73a1eccf9d1f8de4e97888f06f283f98dfe787bbb937b
node-start_1                   | 2022-06-14T07:04:36.346061844Z EVENT: initiated sending 25000000 msats to 036ecb98c2fc0fa66086943e0d547cf996e9970503fc86001f1cf4c2ce0eda9039
node-start_1                   | 2022-06-14T07:04:36.358378344Z EVENT: initiated sending 25000000 msats to 02d191e330745d099741f73a1eccf9d1f8de4e97888f06f283f98dfe787bbb937b
node-start_1                   | 2022-06-14T07:04:36.386639761Z EVENT: initiated sending 25000000 msats to 036ecb98c2fc0fa66086943e0d547cf996e9970503fc86001f1cf4c2ce0eda9039
node-start_1                   | 2022-06-14T07:04:36.506249761Z EVENT: initiated sending 80000000 msats to 030b59aec6963bf7ae3519e6ec88e7309ab03da11c0aae9776cb135634687f98d8
node-start_1                   | 2022-06-14T07:04:36.528653969Z EVENT: initiated sending 20000000 msats to 034206515ac01a2dac35d14f31b4531f82e8ac154afd627c4290345222a98854ba
node-start_1                   | 2022-06-14T07:04:36.550613428Z EVENT: initiated sending 20000000 msats to 0371b2ba5c50629bfd435d79d81071d0aa556e2d51222c7cd0b8b453c29f024e07
node-start_1                   | 2022-06-14T07:04:36.645271011Z
node-start_1                   | 2022-06-14T07:04:37.109017053Z EVENT: Failed to send payment to payment hash "fdd8ff6c01dce16021653a291bb82e59252671051982b0d7697953c5fb67e656": exhausted payment retry attempts
node-start_1                   | 2022-06-14T07:04:37.499747386Z EVENT: Failed to send payment to payment hash "4ebcf0252ca8165191152e6dab2ad12409199365359bd0f61f8b3d398251bea5": exhausted payment retry attempts
node-start_1                   | 2022-06-14T07:04:37.499757803Z EVENT: successfully sent payment of Some(20000000) millisatoshis (fee 0 msat) from payment hash "d2aa565120294b270d72816a40a2b789c257f4230bc575ac9db160a022fb35fe" with preimage "79899d82a7f080e88e4def0a2ec4e61671a2d5073ea685d973f3dd727617aa7b"
node-start_1                   | 2022-06-14T07:04:38.217352012Z
node-start_1                   | 2022-06-14T07:04:38.311052220Z EVENT: Failed to send payment to payment hash "2a92de7a41f1643ae3de4b1852fb70d0cea9524d9a5fd08c948265a122a30541": exhausted payment retry attempts
node-start_1                   | 2022-06-14T07:04:38.331752678Z EVENT: Failed to send payment to payment hash "f3bd8383f12c96cd2d1ae5a0fa77c2b37f6e61deb33521c5b79c56d4a9b395f1": exhausted payment retry attempts
node-start_1                   | 2022-06-14T07:04:38.348938595Z EVENT: Failed to send payment to payment hash "f9dff00f0f2cfe5b9193c44fd488634c8fd14756406f8a7d3524d2043f5703b6": exhausted payment retry attempts

_, err := l.client.PayInvoice(context.Background(), &sensei.PayInvoiceRequest{
Invoice: invoice,
})
if err != nil {
return err
}

return nil
}

func (l *senseiConnection) HasFunds() error {
for {
resp, err := l.client.GetBalance(context.Background(),
&sensei.GetBalanceRequest{})
if err != nil {
return err
}

if resp.OnchainBalanceSats > 0 {
return nil
}

time.Sleep(time.Second)
}
}

func (l *senseiConnection) Restart() (nodeInterface, error) {
// Not needed for sensei to get synced correctly - hopefully.
panic("not implemented")
}

func (l *senseiConnection) GetChannelBalance() (int, error) {
resp, err := l.client.GetBalance(context.Background(),
&sensei.GetBalanceRequest{})
if err != nil {
return 0, err
}

return int(resp.ChannelBalanceMsats) / 1000, nil
}

// MacaroonCredential wraps a macaroon to implement the
// credentials.PerRPCCredentials interface.
type MacaroonCredential struct {
mac string
}

// RequireTransportSecurity implements the PerRPCCredentials interface.
func (m MacaroonCredential) RequireTransportSecurity() bool {
return false
}

// GetRequestMetadata implements the PerRPCCredentials interface. This method
// is required in order to pass the wrapped macaroon into the gRPC context.
// With this, the macaroon will be available within the request handling scope
// of the ultimate gRPC server implementation.
func (m MacaroonCredential) GetRequestMetadata(ctx context.Context,
uri ...string) (map[string]string, error) {

md := make(map[string]string)
md["macaroon"] = m.mac
return md, nil
}

// NewMacaroonCredential returns a copy of the passed macaroon wrapped in a
// MacaroonCredential struct which implements PerRPCCredentials.
func NewMacaroonCredential(mac string) (MacaroonCredential, error) {
ms := MacaroonCredential{mac: mac}

return ms, nil
}
Loading