Skip to content

Commit

Permalink
Fixed node public key for harvesting
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriaCarrera committed Feb 19, 2021
1 parent 9f04435 commit fed65b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
33 changes: 25 additions & 8 deletions src/screens/Harvest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { showPasscode } from '@src/utils/passcode';
import translate from '@src/locales/i18n';
import Trunc from '@src/components/organisms/Trunc';
import {Router} from "@src/Router";
import {showMessage} from "react-native-flash-message";

const styles = StyleSheet.create({
showButton: {
Expand Down Expand Up @@ -69,6 +70,7 @@ type State = {};
class Harvest extends Component<Props, State> {
state = {
selectedNode: null,
selectedNodeUrl: null,
isLoading: false,
};

Expand All @@ -78,7 +80,7 @@ class Harvest extends Component<Props, State> {
for (let node of nodes) {
if (node.url === selectedAccount.harvestingNode) {
this.setState({
selectedNode: node.publicKey,
selectedNodeUrl: node.url,
});
}
}
Expand All @@ -87,20 +89,35 @@ class Harvest extends Component<Props, State> {

getSelectedUrl = () => {
const { nodes } = this.props;
const { selectedNode } = this.state;
const nodeObj = nodes.find(node => node.publicKey === selectedNode);
const { selectedNodeUrl } = this.state;
const nodeObj = nodes.find(node => node.url === selectedNodeUrl);
return nodeObj ? nodeObj.url : null;
};

getHarvestingNodesDropDown = () => {
const { nodes } = this.props;
return nodes.map(node => ({
value: node.publicKey,
value: node.url,
label: node.url,
}));
};

onSelectHarvestingNode = node => this.setState({ selectedNode: node });
onSelectHarvestingNode = node => {
const url = 'http://' + node + ':3000';
HarvestingService.getNodePublicKeyFromNode(url)
.then(publicKey => {
this.setState({ selectedNode: publicKey, selectedNodeUrl: node });
})
.catch(e => {
this.setState({ selectedNodeUrl: null });
Router.showFlashMessageOverlay().then(() => {
showMessage({
message: translate('Settings.nisNode.errorBadNodeDescription'),
type: 'danger',
});
});
});
};

startHarvesting = async _ => {
const callBack = async () => {
Expand Down Expand Up @@ -143,7 +160,7 @@ class Harvest extends Component<Props, State> {

render() {
const { status, totalBlockCount, totalFeesEarned, onOpenMenu, onOpenSettings, balance, minRequiredBalance, nativeMosaicNamespace, harvestingModel, selectedAccount } = this.props;
const { selectedNode, isLoading } = this.state;
const { selectedNodeUrl, isLoading } = this.state;
const notEnoughBalance = balance < minRequiredBalance;
let statusStyle;
switch (status) {
Expand Down Expand Up @@ -233,12 +250,12 @@ class Harvest extends Component<Props, State> {
theme="light"
list={this.getHarvestingNodesDropDown()}
title={translate('harvest.selectNode')}
value={selectedNode}
value={selectedNodeUrl}
onChange={this.onSelectHarvestingNode}
/>
<Button
isLoading={isLoading}
isDisabled={!selectedNode || notEnoughBalance}
isDisabled={!selectedNodeUrl || notEnoughBalance}
text={translate('harvest.startHarvesting')}
theme="light"
onPress={() => this.startHarvesting()}
Expand Down
9 changes: 8 additions & 1 deletion src/services/HarvestingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class HarvestingService {

const peerNodes = await nodeRepository.getNodePeers().toPromise();
return [
...this.getHarvestingNodeList(),
// ...this.getHarvestingNodeList(),
...peerNodes
.sort((a, b) => a.host.localeCompare(b.host))
.map(node => {
Expand All @@ -179,6 +179,13 @@ export default class HarvestingService {
];
}

static async getNodePublicKeyFromNode(node: string): Promise<string> {
const repositoryFactory = new RepositoryFactoryHttp(node);
const nodeRepository = repositoryFactory.createNodeRepository();
const nodeInfo = await nodeRepository.getNodeInfo().toPromise();
return nodeInfo.nodePublicKey;
}

/**
* Static list for the time being - until the dynamic solution
*/
Expand Down

0 comments on commit fed65b9

Please sign in to comment.