Skip to content

Commit

Permalink
Merge pull request #22 from beetapp/nodes
Browse files Browse the repository at this point in the history
refactor node selection
  • Loading branch information
grctest authored Jan 22, 2025
2 parents 65a9951 + b0bd7c4 commit d523f82
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 121 deletions.
10 changes: 8 additions & 2 deletions src/components/main-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@
url: "/settings"
},
{
text: t("common.actionBar.Logout"),
text: t("common.actionBar.changeNodes"),
index: 10,
icon: "lan",
url: "/nodes"
},
{
text: t("common.actionBar.Logout"),
index: 11,
icon: "logout",
url: "/"
}
Expand All @@ -93,7 +99,7 @@
}
lastIndex.value = newIndex;
if (data.index === 10) {
if (data.index === 11) {
console.log('User logged out.');
store.dispatch("WalletStore/logout");
router.replace("/");
Expand Down
87 changes: 87 additions & 0 deletions src/components/nodes.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import AccountSelect from "./account-select";
import store from '../store/index.js';
import router from '../router/index.js';
const { t } = useI18n({ useScope: 'global' });
let selectedAccount = computed(() => {
if (!store.state.WalletStore.isUnlocked) {
return;
}
return store.getters["AccountStore/getCurrentSafeAccount"]()
})
let storedNodes = ref([]);
watch(selectedAccount, (newVal) => {
if (newVal && newVal.chain) {
storedNodes.value = store.getters["SettingsStore/getNodes"](newVal.chain);
}
}, { immediate: true });
function handleClick(node) {
store.dispatch("SettingsStore/setNode", {
chain: selectedAccount.value.chain,
node: node
}).then(() => {
storedNodes.value = [...store.getters["SettingsStore/getNodes"](selectedAccount.value.chain)];
});
}
onMounted(() => {
if (!store.state.WalletStore.isUnlocked) {
console.log("logging user out...");
store.dispatch("WalletStore/logout");
router.replace("/");
return;
}
});
</script>

<template>
<div
class="dapp-list mt-2"
style="text-align: center; margin-top: auto; margin-bottom: auto;"
>
<AccountSelect />
<ui-grid class="row px-4">
<ui-grid-cell
class="largeHeader"
columns="12"
>
<p class="small text-justify">
{{ t('common.nodes.prompt') }}
</p>
</ui-grid-cell>
<ui-grid-cell columns="6">
<div style="max-height: 200px; overflow-y: auto;">
<ui-list>
<ui-item v-for="(node, index) in storedNodes" :key="index" @click="handleClick(index)">
<ui-item-text-content>{{ index === 0 ? "✔️" : "" }} {{ storedNodes[index].url }}</ui-item-text-content>
</ui-item>
</ui-list>
</div>
</ui-grid-cell>
<ui-grid-cell columns="6">
<router-link
:to="'/dashboard'"
style="text-decoration: none;"
replace
>
<ui-button
outlined
class="step_btn"
>
{{ t('common.nodes.exit') }}
</ui-button>
</router-link>
</ui-grid-cell>
<ui-grid-cell columns="3" />
</ui-grid>
</div>
</template>
2 changes: 1 addition & 1 deletion src/components/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<input
id="inputPassword"
v-model="walletpass"
style="width:97%; margin-top: 5px;"
style="width:97%;"
type="password"
class="form-control mb-4 px-3"
:placeholder=" t('common.password_placeholder')"
Expand Down
70 changes: 15 additions & 55 deletions src/lib/blockchains/BitShares.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as Socket from "simple-websocket";

import * as Actions from "../Actions.js";

import store from "../../store/index.js";

import beautify from "./bitshares/beautify.js";
import { humanReadableFloat } from "../assetUtils.js";

Expand Down Expand Up @@ -669,64 +671,22 @@ export default class BitShares extends BlockchainAPI {
);
}

let diff;
if (this._nodeCheckTime) {
let now = new Date();
let nowTS = now.getTime();
diff = Math.abs(
Math.round((nowTS - this._nodeCheckTime) / 1000)
);
}

if (
!nodeToConnect &&
(!this._nodeLatencies || (diff && diff > 360))
) {
// initializing the blockchain
return this._testNodes()
.then((res) => {
this._node = res.node;
this._nodeLatencies = res.latencies;
this._nodeCheckTime = res.timestamp;
console.log(`Establishing connection to ${res.node}`);
return this._establishConnection(
res.node,
resolve,
reject
);
})
.catch((error) => {
console.log(error);
return this._connectionFailed(
reject,
"",
"Node test fail"
);
});
} else if (!nodeToConnect && this._nodeLatencies) {
// blockchain has previously been initialized
let filteredNodes = this._nodeLatencies.filter((item) => {
if (!this._tempBanned.includes(item.url)) {
return true;
}
});

this._nodeLatencies = filteredNodes;
if (!filteredNodes || !filteredNodes.length) {
return this._connectionFailed(
reject,
"",
"No working nodes"
);
}
const userConfiguredNodes = store.getters['SettingsStore/getNodes'](this._config.coreSymbol);

this._node = filteredNodes[0].url;
return this._establishConnection(
filteredNodes[0].url,
resolve,
reject
if (!userConfiguredNodes || !userConfiguredNodes.length) {
return this._connectionFailed(
reject,
"",
"No user-configured nodes"
);
}

this._node = userConfiguredNodes[0].url;
return this._establishConnection(
this._node,
resolve,
reject
);
});
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib/blockchains/BlockchainAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as Actions from '../Actions.js';
export default class BlockchainAPI {

constructor(config, initNode) {
this._tempBanned = [];
this._config = config;
this._node = initNode;
this._isConnected = false;
Expand Down Expand Up @@ -86,7 +85,6 @@ export default class BlockchainAPI {
*/
_connectionFailed(resolveCallback, node, error) {
console.log(this._config.name + " Failed to connect to " + node, error);
this._tempBanned.push(node);
this._isConnected = false;
this._isConnectingInProgress = false;
if (resolveCallback != null) {
Expand Down
68 changes: 13 additions & 55 deletions src/lib/blockchains/EOSmainnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,64 +149,22 @@ export default class EOS extends BlockchainAPI {
);
}

let diff;
if (this._nodeCheckTime) {
let now = new Date();
let nowTS = now.getTime();
diff = Math.abs(
Math.round((nowTS - this._nodeCheckTime) / 1000)
const userConfiguredNodes = store.getters['SettingsStore/getNodes'](this._config.coreSymbol);

if (!userConfiguredNodes || !userConfiguredNodes.length) {
return this._connectionFailed(
reject,
"",
"No working nodes"
);
}

if (
!nodeToConnect &&
(!this._nodeLatencies || (diff && diff > 360))
) {
// initializing the blockchain
return this._testNodes()
.then((res) => {
this._node = res.node;
this._nodeLatencies = res.latencies;
this._nodeCheckTime = res.timestamp;
console.log(`Establishing connection to ${res.node}`);
return this._establishConnection(
res.node,
resolve,
reject
);
})
.catch((error) => {
console.log(error);
return this._connectionFailed(
reject,
"",
"Node test fail"
);
});
} else if (!nodeToConnect && this._nodeLatencies) {
// blockchain has previously been initialized
let filteredNodes = this._nodeLatencies.filter((item) => {
if (!this._tempBanned.includes(item.url)) {
return true;
}
});

this._nodeLatencies = filteredNodes;
if (!filteredNodes || !filteredNodes.length) {
return this._connectionFailed(
reject,
"",
"No working nodes"
);
}

this._node = filteredNodes[0].url;
return this._establishConnection(
filteredNodes[0].url,
resolve,
reject
);
}
this._node = userConfiguredNodes[0].url;
return this._establishConnection(
this._node,
resolve,
reject
);
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Popups from "../components/popups.vue";
import Receipt from "../components/receipt.vue";
import Local from "../components/local.vue";
import Settings from "../components/settings.vue";
import Nodes from "../components/nodes.vue";

const router = createRouter({
routes: [{
Expand Down Expand Up @@ -109,6 +110,13 @@ const router = createRouter({
header: HeaderSmall
}
},
{
path: '/nodes',
components: {
default: Nodes,
header: HeaderSmall
}
},
{
path: '/modal',
components: {
Expand Down
35 changes: 29 additions & 6 deletions src/store/modules/SettingsStore.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
defaultLocale
} from '../../config/i18n.js'
import { defaultLocale } from '../../config/i18n.js'
import {blockchains} from '../../config/config.js';
import BeetDB from '../../lib/BeetDB.js';

const LOAD_SETTINGS = 'LOAD_SETTINGS';
Expand Down Expand Up @@ -55,6 +54,19 @@ const actions = {
} catch (error) {
console.log(`setNode: ${error}`)
}

let chainNodeList = settings.chainNodes[payload.chain];
if (chainNodeList && chainNodeList.length > payload.node) {
let node = chainNodeList.splice(payload.node, 1)[0];
chainNodeList.unshift(node);
}

try {
settings.chainNodes[payload.chain] = chainNodeList;
} catch (error) {
console.log(`setNodeList: ${error}`)
}

BeetDB.settings.put({id: 'settings', value: JSON.stringify(settings)}).then(() => {
commit(LOAD_SETTINGS, settings);
resolve();
Expand Down Expand Up @@ -135,6 +147,12 @@ const getters = {
return [];
}
return state.settings.chainPermissions[chain];
},
getNodes: (state) => (chain) => {
if (!state.settings.hasOwnProperty('chainNodes')) {
return initialState.settings.chainNodes[chain];
}
return state.settings.chainNodes[chain];
}
};

Expand All @@ -147,9 +165,14 @@ const initialState = {
BTS_TEST: [],
EOS: [],
BEOS: [],
TLOS: [],
BTC: [],
BTC_TEST: []
TLOS: []
},
chainNodes: {
BTS: blockchains.BTS.nodeList,
BTS_TEST: blockchains.BTS_TEST.nodeList,
EOS: blockchains.EOS.nodeList,
BEOS: blockchains.BEOS.nodeList,
TLOS: blockchains.TLOS.nodeList
}
}
};
Expand Down
Loading

0 comments on commit d523f82

Please sign in to comment.