Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabo/492 move data calls to lcd client #559

Merged
merged 4 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions app/src/renderer/lcdClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,12 @@ Object.assign(Client.prototype, {
}
},

// Tendermint RPC
status: req('GET', '/tendermint/status'),

// staking
candidate: argReq('GET', '/query/stake/candidates'),
candidate: argReq('GET', '/query/stake/candidate'),
candidates: req('GET', '/query/stake/candidates'),
buildDelegate: req('POST', '/build/stake/delegate'),
buildUnbond: req('POST', '/build/stake/unbond'),
bondingsByDelegator: argReq('GET', '/tx/bondings/delegator'),
bondingsByValidator: argReq('GET', '/tx/bondings/validator')

// TODO: separate API registration for different modules
bondingsByDelegator: argReq('GET', '/query/stake/delegator')
})

module.exports = Client
5 changes: 1 addition & 4 deletions app/src/renderer/vuex/modules/delegates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios'
import indicateValidators from 'scripts/indicateValidators'

export default ({ dispatch, node }) => {
Expand Down Expand Up @@ -41,9 +40,7 @@ export default ({ dispatch, node }) => {
return state.delegates
},
async getDelegate ({ commit }, pubkey) {
let delegate = (await axios.get(`http://localhost:${node.relayPort}/query/stake/candidate/${pubkey.data}`)).data.data
// TODO move into cosmos-sdk
// let delegate = (await node.candidate(pubkeyToString(pubkey))).data
let delegate = (await node.candidate(pubkey.data)).data
delegate.isValidator = false
commit('addDelegate', delegate)
return delegate
Expand Down
5 changes: 1 addition & 4 deletions app/src/renderer/vuex/modules/delegation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import axios from 'axios'

export default ({ commit, node }) => {
let state = {
loading: false,
Expand Down Expand Up @@ -63,8 +61,7 @@ export default ({ commit, node }) => {
},
// load committed delegation from LCD
async getBondedDelegate ({ commit }, { address, pubkey }) {
// TODO move into cosmos-sdk
let bond = (await axios.get(`http://localhost:${node.relayPort}/query/stake/delegator/${address}/${pubkey}`)).data.data
let bond = (await node.bondingsByDelegator([address, pubkey])).data
commit('setCommittedDelegation', { candidateId: bond.PubKey.data, value: bond.Shares })
},
walletDelegate ({ dispatch }, args) {
Expand Down
18 changes: 9 additions & 9 deletions tasks/testnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ async function main () {
// save to tmp dir and pass to app dev runner
console.log(`fetching genesis for network "${network}"`)
let genesisJson = await get(`https://github.com/tendermint/testnets/raw/master/${network}/gaia/genesis.json`)
.catch(e => {
throw new Error(`Can't load genesis.json: ${e.message}`)
})
.catch(e => {
throw new Error(`Can't load genesis.json: ${e.message}`)
})
let configToml = await get(`https://github.com/tendermint/testnets/raw/master/${network}/gaia/config.toml`)
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
let gaiaVersionTxt = await get(`https://github.com/tendermint/testnets/raw/master/${network}/gaia/gaiaversion.txt`)
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
.catch(e => {
throw new Error(`Can't load config.toml: ${e.message}`)
})
let path = join(tmpdir(), Math.random().toString(36).slice(2))
mkdirp(path)
write(join(path, 'genesis.json'), genesisJson)
Expand Down
12 changes: 12 additions & 0 deletions test/unit/helpers/node_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ module.exports = {
deliver_tx: { code: 0 }
}),
sign: () => Promise.resolve(null),
candidate: () => Promise.resolve({
data: {
pub_key: { data: '' },
description: { name: 'test' }
}
}),
bondingsByDelegator: () => Promise.resolve({
data: {
PubKey: { data: '' },
Shares: 0
}
}),

// RPC
rpc: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ exports[`PageDelegates should filter the delegates 1`] = `
</div>
</header>
<main
class="ni-page-main"
class="ni-page-main ps"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ps?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the perfect-scrollbar plugin :/ We need to probably mock it out in tests at some point.

>
<div
class="ni-modal-search"
Expand Down Expand Up @@ -452,6 +452,26 @@ exports[`PageDelegates should filter the delegates 1`] = `
</span>
</a>
</div>
<div
class="ps__rail-x"
style="left: 0px; top: 0px;"
>
<div
class="ps__thumb-x"
style="left: 0px; width: 0px;"
tabindex="0"
/>
</div>
<div
class="ps__rail-y"
style="top: 0px; left: 0px;"
>
<div
class="ps__thumb-y"
style="top: 0px; height: 0px;"
tabindex="0"
/>
</div>
</main>
</div>
`;
10 changes: 5 additions & 5 deletions test/unit/specs/lcdClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe('LCD Client', () => {
axios.get = jest.fn()
.mockReturnValueOnce(Promise.resolve({ data: { foo: 'bar' } }))

let res = await client.status()
let res = await client.listKeys()
expect(res).toEqual({ foo: 'bar' })
expect(axios.get.mock.calls[0]).toEqual([
'http://localhost:8998/tendermint/status',
'http://localhost:8998/keys',
undefined
])
})
Expand All @@ -35,7 +35,7 @@ describe('LCD Client', () => {
let res = await client.bondingsByDelegator([ 'foo', 'bar' ])
expect(res).toEqual({ foo: 'bar' })
expect(axios.get.mock.calls[0]).toEqual([
'http://localhost:8998/tx/bondings/delegator/foo/bar',
'http://localhost:8998/query/stake/delegator/foo/bar',
undefined
])
})
Expand Down Expand Up @@ -76,13 +76,13 @@ describe('LCD Client', () => {
}))

try {
await client.status()
await await client.listKeys()
} catch (err) {
expect(err.message).toBe('foo')
expect(err.code).toBe(123)
}
expect(axios.get.mock.calls[0]).toEqual([
'http://localhost:8998/tendermint/status',
'http://localhost:8998/keys',
undefined
])
})
Expand Down
28 changes: 8 additions & 20 deletions test/unit/specs/store/delegates.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import setup from '../../helpers/vuex-setup'

let axios = require('axios')

let instance = setup()

describe('Module: Delegates', () => {
Expand Down Expand Up @@ -36,37 +34,29 @@ describe('Module: Delegates', () => {
})

it('fetches a candidate', async () => {
axios.get = jest.fn()
node.candidate = jest.fn()
.mockReturnValueOnce(Promise.resolve({
data: {
data: {
pub_key: { data: 'foo' },
test: 123
}
pub_key: { data: 'foo' },
test: 123
}
}))

await store.dispatch('getDelegate', { data: 'foo' })
expect(axios.get.mock.calls[0][0]).toBe('http://localhost:9060/query/stake/candidate/foo')
expect(store.state.delegates.delegates[0].test).toBe(123)
})

it('fetches all candidates', async () => {
axios.get = jest.fn()
node.candidate = jest.fn()
.mockReturnValueOnce(Promise.resolve({
data: {
data: {
pub_key: { data: 'foo' },
test: 123
}
pub_key: { data: 'foo' },
test: 123
}
}))
.mockReturnValueOnce(Promise.resolve({
data: {
data: {
pub_key: { data: 'bar' },
test: 456
}
pub_key: { data: 'bar' },
test: 456
}
}))

Expand All @@ -79,8 +69,6 @@ describe('Module: Delegates', () => {
})

await store.dispatch('getDelegates')
expect(axios.get.mock.calls[0][0]).toBe('http://localhost:9060/query/stake/candidate/foo')
expect(axios.get.mock.calls[1][0]).toBe('http://localhost:9060/query/stake/candidate/bar')
expect(store.state.delegates.delegates[0].test).toBe(123)
expect(store.state.delegates.delegates[1].test).toBe(456)
})
Expand Down
20 changes: 7 additions & 13 deletions test/unit/specs/store/delegation.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import setup from '../../helpers/vuex-setup'

let axios = require('axios')

let instance = setup()

describe('Module: Delegations', () => {
Expand Down Expand Up @@ -65,30 +63,26 @@ describe('Module: Delegations', () => {
})

it('fetches bonded delegates', async () => {
axios.get = jest.fn()
node.bondingsByDelegator = jest.fn()
.mockReturnValueOnce({
data: {
data: {
PubKey: { data: 'foo' },
Shares: 123
}
PubKey: { data: 'foo' },
Shares: 123
}
})
.mockReturnValueOnce({
data: {
data: {
PubKey: { data: 'bar' },
Shares: 456
}
PubKey: { data: 'bar' },
Shares: 456
}
})

await store.dispatch('getBondedDelegates', [
{ pub_key: { data: 'foo' } },
{ pub_key: { data: 'bar' } }
])
expect(axios.get.mock.calls[0][0]).toEqual('http://localhost:9060/query/stake/delegator/someaddress/foo')
expect(axios.get.mock.calls[1][0]).toEqual('http://localhost:9060/query/stake/delegator/someaddress/bar')
expect(node.bondingsByDelegator.mock.calls[0][0]).toEqual(['someaddress', 'foo'])
expect(node.bondingsByDelegator.mock.calls[1][0]).toEqual(['someaddress', 'bar'])

expect(store.state.delegation.committedDelegates).toEqual({
foo: 123,
Expand Down