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

Enabling connections with websocket client #222

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .github/workflows/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
if: startsWith(matrix.os, 'Ubuntu')
- name: Run Geth
run: |
geth --dev --http --ipcpath /tmp/geth.ipc &
geth --dev --http --ws --ipcpath /tmp/geth.ipc &
disown &
- name: Gem Dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Run `git submodule update --init --recursive` to fetch it.

If your tests are failing make sure you pulled the ethereum/tests
submodule and run a local geth node in background with
`geth --dev --http --ipcpath /tmp/geth.ipc` as we are running some tests
`geth --dev --http --ws --ipcpath /tmp/geth.ipc` as we are running some tests
against a local live node.

Other static test data is available in `fixtures/`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The goal is to have 100% API documentation available.
The test suite expects working local HTTP and IPC endpoints with a prefunded developer account, e.g.:

```shell
geth --dev --http --ipcpath /tmp/geth.ipc &
geth --dev --http --ws --ipcpath /tmp/geth.ipc &
```

It also expects an `$INFURA_TOKEN` in environment to test some ENS queries on mainnet.
Expand Down
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ rufo .
yard doc
rspec

echo "Tests fail? Run \`geth --dev --http --ipcpath /tmp/geth.ipc\` in background and try again."
echo "Tests fail? Run \`geth --dev --http --ws --ipcpath /tmp/geth.ipc\` in background and try again."
3 changes: 3 additions & 0 deletions eth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ Gem::Specification.new do |spec|

# scrypt for encrypted key derivation
spec.add_dependency "scrypt", "~> 3.0"

# websocket for websocket client
spec.add_dependency "websocket-client-simple", "~> 0.6.0"
end
3 changes: 3 additions & 0 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ class ContractExecutionError < StandardError; end
# @param host [String] either an HTTP/S host or an IPC path.
# @return [Eth::Client::Ipc] an IPC client.
# @return [Eth::Client::Http] an HTTP client.
# @return [Eth::Client::Ws] an WebSocket client.
# @raise [ArgumentError] in case it cannot determine the client type.
def self.create(host)
return Client::Ipc.new host if host.end_with? ".ipc"
return Client::Http.new host if host.start_with? "http"
return Client::Ws.new host if host.start_with? "ws"
raise ArgumentError, "Unable to detect client type!"
end

Expand Down Expand Up @@ -525,3 +527,4 @@ def marshal(params)
# Load the client/* libraries
require "eth/client/http"
require "eth/client/ipc"
require "eth/client/ws"
64 changes: 64 additions & 0 deletions lib/eth/client/ws.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2016-2023 The Ruby-Eth Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "websocket-client-simple"

# Provides the {Eth} module.
module Eth

# Provides an WebSocket client.
class Client::Ws < Client

# The host of the HTTP endpoint.
kurotaky marked this conversation as resolved.
Show resolved Hide resolved
attr_reader :host

# Constructor for the WebSocket Client. Should not be used; use
# {Client.create} intead.
#
# @param host [String] an URI pointing to an HTTP RPC-API.
def initialize(host)
super
@host = host
kurotaky marked this conversation as resolved.
Show resolved Hide resolved
setup_websocket
end

# Sends an RPC request to the connected WebSocket client.
#
# @param payload [Hash] the RPC request parameters.
# @return [String] a JSON-encoded response.
def send(payload)
Copy link
Owner

Choose a reason for hiding this comment

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

We recently renamed send to send_request #201

Suggested change
def send(payload)
def send_request(payload)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the info, helpful. I fixed this here: fcaf5db

@ws.send(payload.to_json)
end

private

def setup_websocket
@ws = WebSocket::Client::Simple.connect @host

@ws.on :message do |msg|
puts ">> #{msg.data}"
Copy link
Owner

Choose a reason for hiding this comment

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

let's not have stray puts in the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

puts method has been removed.

end
@ws.on :open do
puts "-- websocket open (#{@host})"
end
@ws.on :close do |e|
puts "-- websocket close (#{e.inspect})"
exit 1
Copy link
Owner

Choose a reason for hiding this comment

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

we should not exit here as we are only providing a library and not an application

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

exit 1 has been deleted.

end
@ws.on :error do |e|
puts "-- error (#{e.inspect})"
end
end
end
end
18 changes: 17 additions & 1 deletion spec/eth/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

describe Client do

# run `geth --dev --http --ipcpath /tmp/geth.ipc`
# run `geth --dev --http --ws --ipcpath /tmp/geth.ipc`
# to provide both http and ipc to pass these tests.
let(:geth_ipc_path) { "/tmp/geth.ipc" }
let(:geth_http_path) { "http://127.0.0.1:8545" }
let(:geth_http_authed_path) { "http://username:[email protected]:8545" }
let(:geth_dev_ws_path) { "ws://127.0.0.1:8546" }
subject(:geth_ipc) { Client.create geth_ipc_path }
subject(:geth_http) { Client.create geth_http_path }
subject(:geth_http_authed) { Client.create geth_http_authed_path }

# it expects an $INFURA_TOKEN in environment
let(:infura_api) { "https://mainnet.infura.io/v3/#{ENV["INFURA_TOKEN"]}" }
subject(:infura_mainnet) { Client.create infura_api }
subject(:geth_dev_ipc) { Client.create geth_dev_ipc_path }
subject(:geth_dev_http) { Client.create geth_dev_http_path }
Copy link
Owner

Choose a reason for hiding this comment

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

where do these two lines come from?

subject(:geth_dev_ws) {
Client.create geth_dev_ws_path
}

describe ".create .initialize" do
it "creates an ipc client" do
Expand All @@ -31,6 +37,12 @@
expect(geth_http.ssl).to be_falsy
end

it "creates an ws client" do
expect(geth_dev_ws).to be
expect(geth_dev_ws).to be_instance_of Client::Ws
expect(geth_dev_ws.host).to eq geth_dev_ws_path
end

it "connects to an infura api" do
expect(infura_mainnet).to be
expect(infura_mainnet).to be_instance_of Client::Http
Expand Down Expand Up @@ -425,4 +437,8 @@
expect(geth_ipc.is_valid_signature(contract, hashed, signature)).to be true
end
end

describe ".send" do
pending("using the mock websocket server")
Copy link
Owner

Choose a reason for hiding this comment

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

looking good so far. let me know when you are ready so I can review it.

end
end