cartesi / machine-emulator

The off-chain implementation of the Cartesi Machine
GNU Lesser General Public License v3.0
58 stars 32 forks source link

Jsonrpc remote machine keeps listening forever when advancing with multiple cli commands #202

Closed edubart closed 3 months ago

edubart commented 4 months ago

Context

Test case:

# start remote cartesi machine
killall jsonrpc-remote-cartesi-machine
jsonrpc-remote-cartesi-machine --server-address=127.0.0.1:8000 --log-level=info &

# create inputs
echo '{"payload":"hello"}' | rollup-memory-range encode input > epoch-0-input-0.bin
echo '{"payload":"hello"}' | rollup-memory-range encode input > epoch-0-input-1.bin
echo '{"msg_sender":"0x7D2c4B415d9F917d6effC7761EAB3849B5EcbF39", "epoch_index": 0, "input_index": 0, "block_number":0, "time_stamp": 0}' | rollup-memory-range encode input-metadata > epoch-0-input-metadata-0.bin
echo '{"msg_sender":"0x7D2c4B415d9F917d6effC7761EAB3849B5EcbF39", "epoch_index": 0, "input_index": 1, "block_number":0, "time_stamp": 0}' | rollup-memory-range encode input-metadata > epoch-0-input-metadata-1.bin

# boot machine
cartesi-machine \
    --remote-address=127.0.0.1:8000 \
    --no-remote-destroy \
    -- rollup-init /usr/bin/echo-dapp --reports=1

# perform first advance state
cartesi-machine \
    --remote-address=127.0.0.1:8000 \
    --no-remote-create \
    --rollup-advance-state=epoch_index:0,input_index_begin:0,input_index_end:1

# perform second advance state
cartesi-machine \
    --remote-address=127.0.0.1:8000 \
    --no-remote-create \
    --rollup-advance-state=epoch_index:0,input_index_begin:1,input_index_end:2

# check listening ports
netstat -antp | grep jsonrpc

The final output is:

tcp        0      0 127.0.0.1:43599         0.0.0.0:*               LISTEN      1415841/jsonrpc-rem 
tcp        0      0 127.0.0.1:39455         0.0.0.0:*               LISTEN      1415843/jsonrpc-rem 
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1415830/jsonrpc-rem

Notice that we have 3 port listening, where just 1 was expected. If we keep doing advances like this, eventually it will exhaust all available ports on the host (about 65535).

Expected behavior

After every advance, jsonrpc-remote-cartesi-machine should keep listening only in one port, instead of multiple ports. Preferably in the same initial port 8000. If it's not the same port, the cli must return the next port the user can connect to, this could be optional and saved to a file, although I dislike this port changing behavior (at least in the cli).

Use case

The idea here is to be able to implement a rudimentary "node" using just the cli to perform advance/inspect states to a single jsonrpc-remote-cartesi-machine instance. For that we need to be able to keep rolling the state using just cli commands without issues.