import React from 'react'; import { Client } from 'rpc-websockets' import Cristal from 'react-cristal' import { BlockLinks } from "./BlockLink"; import StorageNodeInit from "./StorageNodeInit"; async function awaitListReducer(prev, c) { return [...await prev, await c] } function truncAddr(addr) { if (addr.length > 41) { return {addr.substr(0, 38) + '...'} } return addr } class FullNode extends React.Component { constructor(props) { super(props) this.state = { 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.add1k = this.add1k.bind(this) this.loadInfo() setInterval(this.loadInfo, 2050) } async loadInfo() { const id = await this.props.client.call("Filecoin.ID", []) this.setState(() => ({id: id})) const version = await this.props.client.call("Filecoin.Version", []) this.setState(() => ({version: version})) const peers = await this.props.client.call("Filecoin.NetPeers", []) this.setState(() => ({peers: peers.length})) const tipset = await this.props.client.call("Filecoin.ChainHead", []) this.setState(() => ({tipset: tipset})) const addrss = await this.props.client.call('Filecoin.WalletList', []) let defaultAddr = "" if (addrss.length > 0) { defaultAddr = await this.props.client.call('Filecoin.WalletDefaultAddress', []) } const balances = await addrss.map(async addr => { let balance = 0 try { balance = await this.props.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.props.client.call("Filecoin.MinerStart", [addr]) } async newScepAddr() { const t = "secp256k1" await this.props.client.call("Filecoin.WalletNew", [t]) this.loadInfo() } async startStorageMiner() { this.props.mountWindow((onClose) => ) } async add1k(to) { await this.props.give1k(to) } render() { let runtime =
if (this.state.defaultAddr) { let chainInfo =
if (this.state.tipset !== undefined) { chainInfo = (
Head: { } H:{this.state.tipset.Height}
) } let mine = [Mine] if (this.state.mining) { mine = "[Mining]" } let storageMine = [Spawn Storage Miner] let balances = this.state.balances.map(([addr, balance]) => { let add1k = this.add1k(addr)}>[+1k] let line = {truncAddr(addr)}: {balance} (ActTyp) {add1k} if (this.state.defaultAddr === addr) { line = {line} } return
{line}
}) runtime = (
v{this.state.version.Version}, {this.state.id.substr(-8)}, {this.state.peers} peers
Repo: LOTUS_PATH={this.props.node.Repo}
{chainInfo}
{mine} {storageMine}
Balances: [New [Secp256k1]]
{balances}
) } return (
{this.props.node.ID} - {this.state.state}
{runtime}
) } } export default FullNode