-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from m-Peter/add-net-and-web3-namespaces
Add `net` and `web3` namespaces
- Loading branch information
Showing
5 changed files
with
124 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
) | ||
|
||
// NetAPI offers network related RPC methods | ||
type NetAPI struct{} | ||
|
||
// Listening returns an indication if the node is | ||
// listening for network connections. | ||
func (s *NetAPI) Listening() bool { | ||
return true // always listening | ||
} | ||
|
||
// PeerCount returns the number of connected peers | ||
func (s *NetAPI) PeerCount() hexutil.Uint { | ||
return 1 | ||
} | ||
|
||
// Version returns the current ethereum protocol version. | ||
func (s *NetAPI) Version() string { | ||
return fmt.Sprintf("%d", 666) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ import ( | |
"github.com/onflow/flow-evm-gateway/storage" | ||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/access/grpc" | ||
sdkCrypto "github.com/onflow/flow-go-sdk/crypto" | ||
"github.com/onflow/flow-go-sdk/crypto" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
@@ -52,7 +52,7 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) { | |
panic(err) | ||
} | ||
|
||
url := "http://" + srv.ListenAddr() + "/rpc" | ||
url := "http://" + srv.ListenAddr() | ||
|
||
expectedResponses := strings.Split(responses, "\n") | ||
for i, request := range strings.Split(requests, "\n") { | ||
|
@@ -181,13 +181,13 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) { | |
} | ||
mockFlowClient.On("GetLatestBlock", mock.Anything, mock.Anything).Return(block, nil) | ||
|
||
privateKey, err := sdkCrypto.DecodePrivateKeyHex(sdkCrypto.ECDSA_P256, strings.Replace("2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21", "0x", "", 1)) | ||
privateKey, err := crypto.DecodePrivateKeyHex(crypto.ECDSA_P256, strings.Replace("2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21", "0x", "", 1)) | ||
require.NoError(t, err) | ||
key := &flow.AccountKey{ | ||
Index: 0, | ||
PublicKey: privateKey.PublicKey(), | ||
SigAlgo: privateKey.Algorithm(), | ||
HashAlgo: sdkCrypto.SHA3_256, | ||
HashAlgo: crypto.SHA3_256, | ||
Weight: 1000, | ||
SequenceNumber: uint64(0), | ||
Revoked: false, | ||
|
@@ -486,6 +486,58 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) { | |
|
||
assert.Equal(t, expectedResponse, strings.TrimSuffix(string(content), "\n")) | ||
}) | ||
|
||
t.Run("net endpoints", func(t *testing.T) { | ||
requests := ` | ||
[ | ||
{"jsonrpc":"2.0","id":1,"method":"net_listening","params":[]}, | ||
{"jsonrpc":"2.0","id":2,"method":"net_peerCount","params":[]}, | ||
{"jsonrpc":"2.0","id":3,"method":"net_version","params":[]} | ||
] | ||
` | ||
expectedResponse := ` | ||
[ | ||
{"jsonrpc":"2.0","id":1,"result":true}, | ||
{"jsonrpc":"2.0","id":2,"result":"0x1"}, | ||
{"jsonrpc":"2.0","id":3,"result":"666"} | ||
] | ||
` | ||
|
||
resp := rpcRequest(url, requests, "origin", "test.com") | ||
defer resp.Body.Close() | ||
|
||
content, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
assert.JSONEq(t, expectedResponse, string(content)) | ||
}) | ||
|
||
t.Run("web3 endpoints", func(t *testing.T) { | ||
requests := ` | ||
[ | ||
{"jsonrpc":"2.0","id":1,"method":"web3_clientVersion","params":[]}, | ||
{"jsonrpc":"2.0","id":2,"method":"web3_sha3","params":["0x68656c6c6f20776f726c64"]} | ||
] | ||
` | ||
expectedResponse := ` | ||
[ | ||
{"jsonrpc":"2.0","id":1,"result":"[email protected]"}, | ||
{"jsonrpc":"2.0","id":2,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"} | ||
] | ||
` | ||
|
||
resp := rpcRequest(url, requests, "origin", "test.com") | ||
defer resp.Body.Close() | ||
|
||
content, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
assert.JSONEq(t, expectedResponse, string(content)) | ||
}) | ||
} | ||
|
||
func TestServerJSONRPCOveWebSocketHandler(t *testing.T) { | ||
|
@@ -508,7 +560,7 @@ func TestServerJSONRPCOveWebSocketHandler(t *testing.T) { | |
panic(err) | ||
} | ||
|
||
url := "ws://" + srv.ListenAddr() + "/ws" | ||
url := "ws://" + srv.ListenAddr() | ||
|
||
extraHeaders := []string{"Origin", "*"} | ||
headers := make(http.Header) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
// Web3API offers helper utils | ||
type Web3API struct{} | ||
|
||
// ClientVersion returns the node name | ||
func (s *Web3API) ClientVersion() string { | ||
return "[email protected]" | ||
} | ||
|
||
// Sha3 applies the ethereum sha3 implementation on the input. | ||
// It assumes the input is hex encoded. | ||
func (s *Web3API) Sha3(input hexutil.Bytes) hexutil.Bytes { | ||
return crypto.Keccak256(input) | ||
} |