lotus/lotuspond/front/src/FullNode.js
2019-08-02 16:15:14 +02:00

188 lines
5.2 KiB
JavaScript

import React from 'react';
import { Client } from 'rpc-websockets'
import Cristal from 'react-cristal'
import { BlockLinks } from "./BlockLink";
import StorageNodeInit from "./StorageNodeInit";
const stateConnected = 'connected'
const stateConnecting = 'connecting'
const stateGettingToken = 'getting-token'
async function awaitListReducer(prev, c) {
return [...await prev, await c]
}
function truncAddr(addr) {
if (addr.length > 41) {
return <abbr title={addr}>{addr.substr(0, 38) + '...'}</abbr>
}
return addr
}
class FullNode extends React.Component {
constructor(props) {
super(props)
this.state = {
state: stateGettingToken,
id: "~",
mining: false,
}
this.loadInfo = this.loadInfo.bind(this)
this.startMining = this.startMining.bind(this)
this.newScepAddr = this.newScepAddr.bind(this)
this.startStorageMiner = this.startStorageMiner.bind(this)
this.connect()
}
async connect() {
console.log("gettok")
const token = await this.props.pondClient.call('Pond.TokenFor', [this.props.node.ID])
this.setState(() => ({
state: stateConnecting,
token: token,
}))
const client = new Client(`ws://127.0.0.1:${this.props.node.ApiPort}/rpc/v0?token=${token}`)
client.on('open', async () => {
this.setState(() => ({
state: stateConnected,
client: client,
version: {Version: "~version~"},
id: "~peerid~",
peers: -1,
balances: []
}))
const id = await this.state.client.call("Filecoin.ID", [])
this.setState(() => ({id: id}))
this.props.onConnect(client, id)
this.loadInfo()
setInterval(this.loadInfo, 2050)
})
console.log(token) // todo: use
}
async loadInfo() {
const version = await this.state.client.call("Filecoin.Version", [])
this.setState(() => ({version: version}))
const peers = await this.state.client.call("Filecoin.NetPeers", [])
this.setState(() => ({peers: peers.length}))
const tipset = await this.state.client.call("Filecoin.ChainHead", [])
this.setState(() => ({tipset: tipset}))
const addrss = await this.state.client.call('Filecoin.WalletList', [])
let defaultAddr = ""
if (addrss.length > 0) {
defaultAddr = await this.state.client.call('Filecoin.WalletDefaultAddress', [])
}
const balances = await addrss.map(async addr => {
let balance = 0
try {
balance = await this.state.client.call('Filecoin.WalletBalance', [addr])
} catch {
balance = -1
}
return [addr, balance]
}).reduce(awaitListReducer, Promise.resolve([]))
this.setState(() => ({balances: balances, defaultAddr: defaultAddr}))
}
async startMining() {
// TODO: Use actual miner address
// see cli/miner.go
this.setState({mining: true})
let addr = "t0523423423" // in case we have no wallets
if (this.state.defaultAddr) {
addr = this.state.defaultAddr
}
this.setState({mining: true})
await this.state.client.call("Filecoin.MinerStart", [addr])
}
async newScepAddr() {
const t = "secp256k1"
await this.state.client.call("Filecoin.WalletNew", [t])
this.loadInfo()
}
async startStorageMiner() {
this.props.mountWindow((onClose) => <StorageNodeInit fullRepo={this.props.node.Repo} fullConn={this.props.conn} pondClient={this.props.pondClient} onClose={onClose} mountWindow={this.props.mountWindow}/>)
}
render() {
let runtime = <div></div>
if (this.state.state === stateConnected) {
let chainInfo = <div></div>
if (this.state.tipset !== undefined) {
chainInfo = (
<div>
Head: {
<BlockLinks cids={this.state.tipset.Cids} conn={this.state.client} mountWindow={this.props.mountWindow} />
} H:{this.state.tipset.Height}
</div>
)
}
let mine = <a href="#" disabled={this.state.mining} onClick={this.startMining}>[Mine]</a>
if (this.state.mining) {
mine = "[Mining]"
}
let storageMine = <a href="#" onClick={this.startStorageMiner}>[Spawn Storage Miner]</a>
let balances = this.state.balances.map(([addr, balance]) => {
let line = <span>{truncAddr(addr)}:&nbsp;{balance}&nbsp;(ActTyp)</span>
if (this.state.defaultAddr === addr) {
line = <b>{line}</b>
}
return <div>{line}</div>
})
runtime = (
<div>
<div>v{this.state.version.Version}, <abbr title={this.state.id}>{this.state.id.substr(-8)}</abbr>, {this.state.peers} peers</div>
<div>Repo: LOTUS_PATH={this.props.node.Repo}</div>
{chainInfo}
<div>
{mine} {storageMine}
</div>
<div>
<div>Balances: [New <a href="#" onClick={this.newScepAddr}>[Secp256k1]</a>]</div>
<div>{balances}</div>
</div>
</div>
)
}
return (
<Cristal
title={"Node " + this.props.node.ID}
initialPosition={{x: this.props.node.ID*30, y: this.props.node.ID * 30}} >
<div className="CristalScroll">
<div className="FullNode">
<div>{this.props.node.ID} - {this.state.state}</div>
{runtime}
</div>
</div>
</Cristal>
)
}
}
export default FullNode