Skip to content

Commit

Permalink
Merge pull request #1853 from lavanet/PRT-fix-chain-router-auto-compl…
Browse files Browse the repository at this point in the history
…etion-pt2

feat: PRT - fix chain router auto completion
  • Loading branch information
omerlavanet authored Dec 19, 2024
2 parents 11840ea + 410b585 commit 1d65f38
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 24 deletions.
8 changes: 8 additions & 0 deletions config/provider_examples/strk_addon_example_with_root.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
endpoints:
- api-interface: jsonrpc
chain-id: STRK
network-address:
address: "127.0.0.1:2220"
node-urls:
- url: wss://starknet-mainnet.domain.com/ws/XXX
- url: https://starknet-mainnet.domain.com/XXX
12 changes: 12 additions & 0 deletions config/provider_examples/strk_addon_example_with_ws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
endpoints:
- api-interface: jsonrpc
chain-id: STRK
network-address:
address: "127.0.0.1:2220"
node-urls:
- url: wss://starknet-mainnet.domain.com/ws/XXX/rpc/v0_7
internal-path: /ws/rpc/v0_7
- url: wss://starknet-mainnet.domain.com/ws/XXX/rpc/v0_6
internal-path: /ws/rpc/v0_6
- url: wss://starknet-mainnet.domain.com/ws/XXX
- url: https://starknet-mainnet.domain.com/XXX
1 change: 1 addition & 0 deletions protocol/chainlib/chain_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (cf *ChainFetcher) Verify(ctx context.Context, verification VerificationCon
return utils.LavaFormatWarning("[-] verify failed to parse result", err,
utils.LogAttr("chain_id", chainId),
utils.LogAttr("Api_interface", cf.endpoint.ApiInterface),
utils.LogAttr("function_template", parsing.FunctionTemplate),
)
}

Expand Down
18 changes: 3 additions & 15 deletions protocol/chainlib/chain_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ func (cri *chainRouterImpl) BatchNodeUrlsByServices(rpcProviderEndpoint lavasess
nodeUrl.Addons = append(nodeUrl.Addons, WebSocketExtension)
routerKey.SetExtensions(nodeUrl.Addons)
}

_, isAlreadyAutoGenerated := autoGeneratedInternalPaths[nodeUrl.InternalPath]
cri.setRouterKeyInBatch(nodeUrl, returnedBatch, routerKey, rpcProviderEndpoint, isAlreadyAutoGenerated) // will override existing entries
cri.setRouterKeyInBatch(nodeUrl, returnedBatch, routerKey, rpcProviderEndpoint, true) // will override existing entries

if nodeUrl.InternalPath == "" { // root path
if !isWs {
Expand Down Expand Up @@ -222,20 +220,10 @@ func (cri *chainRouterImpl) BatchNodeUrlsByServices(rpcProviderEndpoint lavasess
}

func (*chainRouterImpl) setRouterKeyInBatch(nodeUrl common.NodeUrl, returnedBatch map[string]lavasession.RPCProviderEndpoint, routerKey lavasession.RouterKey, rpcProviderEndpoint lavasession.RPCProviderEndpoint, overrideExistingEntry bool) {
// if the router key does not exit, create it anyway
// if we need to override, override
// if the router key does not exist, create it anyway
// if it exists and we should not override, add the node url to the existing list
routerKeyString := routerKey.String()
if existingEndpoint, ok := returnedBatch[routerKeyString]; ok && !overrideExistingEntry {
// setting the incoming url first as it might be http while existing is websocket. (we prioritize http over ws when possible)
returnedBatch[routerKeyString] = lavasession.RPCProviderEndpoint{
NetworkAddress: rpcProviderEndpoint.NetworkAddress,
ChainID: rpcProviderEndpoint.ChainID,
ApiInterface: rpcProviderEndpoint.ApiInterface,
Geolocation: rpcProviderEndpoint.Geolocation,
NodeUrls: append([]common.NodeUrl{nodeUrl}, existingEndpoint.NodeUrls...),
}

if _, ok := returnedBatch[routerKeyString]; ok && !overrideExistingEntry {
return
}

Expand Down
4 changes: 0 additions & 4 deletions protocol/chainlib/chain_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chainlib

import (
"context"
"fmt"
"log"
"net"
"os"
Expand Down Expand Up @@ -2205,9 +2204,6 @@ func TestChainRouterWithInternalPaths(t *testing.T) {
actualNodeUrlsCount += len(actualEndpoint.NodeUrls)

expectedNodeUrls := play.expectedServicesToNodeUrls[routerKey]
require.Len(t, actualEndpoint.NodeUrls, len(expectedNodeUrls),
fmt.Sprintf("RouterKey: %v, NodeUrls: %v", routerKey, actualEndpoint.NodeUrls))

for _, actualNodeUrl := range actualEndpoint.NodeUrls {
found := false
for _, expectedNodeUrls := range expectedNodeUrls {
Expand Down
3 changes: 2 additions & 1 deletion protocol/chainlib/chainproxy/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -66,7 +67,7 @@ func NewConnector(ctx context.Context, nConns uint, nodeUrl common.NodeUrl) (*Co

rpcClient, err := connector.createConnection(ctx, nodeUrl, connector.numberOfFreeClients())
if err != nil {
return nil, utils.LavaFormatError("Failed to create the first connection", err, utils.Attribute{Key: "address", Value: nodeUrl.UrlStr()})
return nil, fmt.Errorf("failed to create the first connection: %w, url: %s", err, nodeUrl.UrlStr())
}

connector.addClient(rpcClient)
Expand Down
14 changes: 14 additions & 0 deletions protocol/chainlib/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -563,6 +564,19 @@ func NewJrpcChainProxy(ctx context.Context, nConns uint, rpcProviderEndpoint lav
func (cp *JrpcChainProxy) start(ctx context.Context, nConns uint, nodeUrl common.NodeUrl) error {
conn, err := chainproxy.NewConnector(ctx, nConns, nodeUrl)
if err != nil {
isWs, parseErr := IsUrlWebSocket(nodeUrl.Url)
if parseErr == nil && isWs {
newUrl := strings.TrimSuffix(nodeUrl.Url, "/") + "/ws"
originalUrl := nodeUrl.Url
utils.LavaFormatWarning("Failed creating connector for ws, trying to create with /ws path", err, utils.LogAttr("url", nodeUrl.UrlStr()), utils.LogAttr("newUrl", newUrl))
nodeUrl.Url = newUrl
conn, newConnErr := chainproxy.NewConnector(ctx, nConns, nodeUrl)
if newConnErr == nil {
cp.conn = conn
return nil
}
nodeUrl.Url = originalUrl
}
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@ rm $LOGS_DIR/*.log

killall screen
screen -wipe

echo "[Test Setup] installing all binaries"
make install-all

echo "[Test Setup] setting up a new lava node"
screen -d -m -S node bash -c "./scripts/start_env_dev.sh"
screen -ls
echo "[Test Setup] waiting lava node to start"
sleep 3
wait_for_lava_node_to_start

GASPRICE="0.00002ulava"
specs=$(get_all_specs)
lavad tx gov submit-legacy-proposal spec-add $specs --lava-dev-test -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE &
wait_next_block
wait_next_block
lavad tx gov vote 1 yes -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
sleep 4

lavad tx gov submit-legacy-proposal plans-add ./cookbook/plans/test_plans/default.json -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
# Plans proposal
lavad tx gov submit-legacy-proposal plans-add ./cookbook/plans/test_plans/default.json,./cookbook/plans/test_plans/temporary-add.json -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
wait_next_block
wait_next_block
lavad tx gov vote 2 yes -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE

sleep 4
Expand All @@ -25,13 +42,14 @@ PROVIDERSTAKE="500000000000ulava"
PROVIDER1_LISTENER="127.0.0.1:2221"

lavad tx subscription buy DefaultPlan $(lavad keys show user1 -a) -y --from user1 --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE

wait_next_block
lavad tx pairing stake-provider "AVAX" $PROVIDERSTAKE "$PROVIDER1_LISTENER,1" 1 $(operator_address) -y --from servicer1 --provider-moniker "dummyMoniker" --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE

lavad tx project set-policy $(lavad keys show user1 -a)-admin ./cookbook/projects/policy_all_chains_with_addon.yml -y --from user1 --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
sleep_until_next_epoch

screen -d -m -S provider1 bash -c "source ~/.bashrc; lavap rpcprovider \
$PROVIDER1_LISTENER AVAX jsonrpc $AVALANCH_PJRPC \
./config/provider_examples/avalanch_internal_paths_example.yml \
$EXTRA_PROVIDER_FLAGS --geolocation 1 --log_level debug --from servicer1 2>&1 | tee $LOGS_DIR/PROVIDER1.log" && sleep 0.25

screen -d -m -S consumers bash -c "source ~/.bashrc; lavap rpcconsumer \
Expand Down
2 changes: 1 addition & 1 deletion scripts/pre_setups/init_starknet_addons_only_with_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PROVIDER1_LISTENER="127.0.0.1:2220"

lavad tx subscription buy DefaultPlan $(lavad keys show user1 -a) -y --from user1 --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
wait_next_block
lavad tx pairing stake-provider "STRK" $PROVIDERSTAKE "$PROVIDER1_LISTENER,1 $PROVIDER1_LISTENER,1,trace" 1 $(operator_address) -y --from servicer1 --provider-moniker "dummyMoniker" --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
lavad tx pairing stake-provider "STRK" $PROVIDERSTAKE "$PROVIDER1_LISTENER,1" 1 $(operator_address) -y --from servicer1 --provider-moniker "dummyMoniker" --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE

lavad tx project set-policy $(lavad keys show user1 -a)-admin ./cookbook/projects/policy_all_chains_with_addon.yml -y --from user1 --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
sleep_until_next_epoch
Expand Down
34 changes: 34 additions & 0 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"fmt"
"net"
"testing"
"time"

Expand Down Expand Up @@ -47,11 +48,42 @@ func New(t *testing.T, configs ...network.Config) *network.Network {
return net
}

// findFreePort returns an available port number
func findFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()

addr, ok := l.Addr().(*net.TCPAddr)
if !ok {
return 0, fmt.Errorf("failed to get TCP address")
}
return addr.Port, nil
}

// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig
func DefaultConfig() network.Config {
encoding := app.MakeEncodingConfig()
chainID := "chain-" + tmrand.NewRand().Str(6)

// Find available ports for API and gRPC
apiPort, err := findFreePort()
if err != nil {
panic(err)
}
grpcPort, err := findFreePort()
if err != nil {
panic(err)
}

return network.Config{
Codec: encoding.Marshaler,
TxConfig: encoding.TxConfig,
Expand Down Expand Up @@ -81,5 +113,7 @@ func DefaultConfig() network.Config {
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
KeyringOptions: []keyring.Option{},
APIAddress: fmt.Sprintf("tcp://0.0.0.0:%d", apiPort),
GRPCAddress: fmt.Sprintf("0.0.0.0:%d", grpcPort),
}
}

0 comments on commit 1d65f38

Please sign in to comment.