Merge pull request #80 from status-im/feat/log-inout

feat: open/close db connection and connect to web3 when calling a login()/logout() proc
This commit is contained in:
Iuri Matias 2021-01-05 10:33:17 -05:00 committed by GitHub
commit cd5d47239c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 49 deletions

View File

@ -69,10 +69,10 @@ proc buildAndRunTest(name: string,
task tests, "Run all tests":
buildAndRunTest "shims"
buildAndRunTest "login"
buildAndRunTest "settings"
buildAndRunTest "login_and_logout"
buildAndRunTest "db_smoke"
buildAndRunTest "waku_smoke"
buildAndRunTest "settings"
buildAndRunTest "callrpc"
buildAndRunTest "migrations"
buildAndRunTest "mailservers"

View File

@ -0,0 +1,51 @@
import os, json
import sqlcipher, web3, chronos, json_serialization
import settings, database, conversions, callrpc
var db_conn*: DbConn
var web3_conn*: Web3
proc login*(accountData, password: string) =
# TODO: get account, validate password, etc
# TODO: should this be async?
# TODO: db should have been initialized somewhere, not here
# TODO: determine where will the DB connection live. In the meantime I'm storing it into a global variable
# TODO: determine where the web3 conn will live
let path = getCurrentDir() / accountData & ".db"
db_conn = initializeDB(path, password)
# TODO: these settings should have been set when calling saveAccountAndLogin
let settingsStr = """{
"address": "0x1122334455667788990011223344556677889900",
"chaos-mode": true,
"networks/current-network": "mainnet_rpc",
"dapps-address": "0x1122334455667788990011223344556677889900",
"eip1581-address": "0x1122334455667788990011223344556677889900",
"installation-id": "ABC-DEF-GHI",
"key-uid": "XYZ",
"latest-derived-path": 0,
"networks/networks": [{"id":"mainnet_rpc","etherscan-link":"https://etherscan.io/address/","name":"Mainnet with upstream RPC","config":{"NetworkId":1,"DataDir":"/ethereum/mainnet_rpc","UpstreamConfig":{"Enabled":true,"URL":"wss://mainnet.infura.io/ws/v3/7230123556ec4a8aac8d89ccd0dd74d7"}}}],
"name": "test",
"photo-path": "ABXYZC",
"preview-privacy?": false,
"public-key": "0x123",
"signing-phrase": "ABC DEF GHI",
"wallet-root-address": "0x1122334455667788990011223344556677889900"
}"""
let settingsObj = JSON.decode(settingsStr, Settings, allowUnknownFields = true)
let nodeConfig = %* {"config": 1}
db_conn.createSettings(settingsObj, nodeConfig)
web3_conn = newWeb3(getSettings(db_conn))
proc logout*() =
waitFor web3_conn.close()
db_conn.close()
web3_conn = nil
proc test_removeDB*(accountData: string) = # TODO: remove this once proper db initialization is available
let path = getCurrentDir() / accountData & ".db"
removeFile(path)

View File

@ -53,10 +53,10 @@ type RemoteMethod* {.pure.} = enum
proc newWeb3*(settings: Settings): Web3 =
let network = settings.getNetwork()
if network.isNone:
raise (ref Web3Error)(msg: "Config not found for network " & settings.currentNetwork)
raise (ref Web3Error)(msg: "config not found for network " & settings.currentNetwork)
if not network.get().config.upstreamConfig.enabled:
raise (ref Web3Error)(msg: "Network " & settings.currentNetwork & " is not enabled")
raise (ref Web3Error)(msg: "network " & settings.currentNetwork & " is not enabled")
result = waitFor newWeb3(network.get().config.upstreamConfig.url)
@ -68,7 +68,10 @@ proc callRPC*(web3Conn: Web3, rpcMethod: RemoteMethod, params: JsonNode): Respon
proc callRPC*(web3Conn: Web3, rpcMethod: string, params: JsonNode): Response =
try:
if web3Conn == nil:
raise (ref Web3Error)(msg: "web3 connection is not available")
try:
var m = parseEnum[RemoteMethod](rpcMethod)
except:
return (true, %* {"code": -32601, "message": "the method " & rpcMethod & " does not exist/is not available"})

View File

@ -1,4 +1,5 @@
import re
import web3/ethtypes
let reHex = re("^[0-9a-f]+$", {reIgnoreCase})
@ -12,3 +13,6 @@ proc isPubKey*(str: string): bool =
str.len == 132 and
str[0..3] == "0x04" and
match(str[2..^1], reHex)
proc parseAddress*(strAddress: string): Address =
fromHex(Address, strAddress)

View File

@ -1,44 +0,0 @@
import ../../nim_status
import test_helpers
import utils
import chronos
import os
import unittest
var success = false
proc checkMessage(message: string): void =
echo "SIGNAL RECEIVED:\n" & message
if message == """{"type":"node.login","event":{}}""":
success = true
procSuite "nim_status":
asyncTest "login":
var onSignal = proc(message: cstring) {.cdecl.} =
setupForeignThreadGC()
checkMessage($message)
tearDownForeignThreadGc()
setSignalEventCallback(onSignal)
resetDirectories() # Recreates the data and nobackup dir
init()
# Call either `createAccountAndLogin("somePassword")` to create a new random account
# Or: `restoreAccountAndLogin("cattle act enable unable own music material canvas either shoe must junior", "somePassword")`
# If no password is specified, it will use "qwerty"
# There's a `login("somePassword")` function that will login the first
# account. It assumes the directories have been already set.
discard createAccountAndLogin("somePassword")
var seconds = 0
while seconds < 300:
if success:
break
echo "..."
sleep 1000
seconds += 1
check:
success == true

View File

@ -0,0 +1,42 @@
import sqlcipher
import os, json, json_serialization
import options
import ../../nim_status/lib/settings
import ../../nim_status/lib/database
import ../../nim_status/lib/callrpc
import ../../nim_status/lib/accounts
import web3/conversions
let accountData = "someAccount"
let passwd = "qwerty"
test_removeDB(accountData)
try:
assert web3_conn == nil
discard callRPC(web3_conn, "eth_gasPrice", %[])
assert "Should fail if reaches this point" == ""
except:
assert getCurrentExceptionMsg() == "web3 connection is not available"
login(accountData, passwd)
# Using an ugly global var :(
let rGasPrice = callRPC(web3_conn, "eth_gasPrice", %[])
assert rGasPrice.error == false
assert rGasPrice.result.getStr()[0..1] == "0x"
logout()
try:
assert web3_conn == nil
discard callRPC(web3_conn, "eth_gasPrice", %[])
assert "Should fail if reaches this point" == ""
except:
assert getCurrentExceptionMsg() == "web3 connection is not available"
# Removing DB to be able to run the test again
test_removeDB(accountData)