From 6120292d73b5c76b73f2ffa5c096ca1ab357f082 Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 8 Nov 2018 14:47:41 +0100 Subject: [PATCH] Fabo/1500 fixed optimistic updates (#1535) * fixed optimistic updates * linted * cleanup * trigger CI * fixed undeterminisim in test with transactions from the lcdClientMock * fixed more undeterminism --- CHANGELOG.md | 3 +- app/src/renderer/connectors/lcdClientMock.js | 4 +- app/src/renderer/vuex/modules/delegation.js | 45 ++++++------------ .../__snapshots__/transactions.spec.js.snap | 4 +- test/unit/specs/store/delegation.spec.js | 47 +++++++++++++++++++ 5 files changed, 67 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acfd048f98..5c0104fc83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * [\#1449](https://github.com/cosmos/voyager/issues/1449) shortNumber to num scripts for more readable numbers. @jbibla * [\#1464](https://github.com/cosmos/voyager/issues/1464) Added governance transactions to tx history page @fedekunze * [\1401](https://github.com/cosmos/voyager/issues/1401) Display governance proposals index. @fedekunze -* [\#1472](https://github.com/cosmos/voyager/issues/1472) Added mock functionality for redelegation @fedekunze + @faboweb +* [\#1472](https://github.com/cosmos/voyager/issues/1472) Added mock functionality for redelegation @fedekunze + @faboweb ### Changed @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * [\#1480](https://github.com/cosmos/voyager/issues/1480) Fixed false detection of node crash in e2e test start. @faboweb * [\#1451](https://github.com/cosmos/voyager/issues/1451) Provide better sourcemaps to make debugging easier. @faboweb * [\#1409](https://github.com/cosmos/voyager/issues/1409) Fixed disabled unbond and redelegation button when delegation amount was less than 1 @fedekunze +* [\#1500](https://github.com/cosmos/voyager/issues/1500) Fixed wrong optimistic updates to the atom balance after staking @faboweb @fedekunze * [\#1517](https://github.com/cosmos/voyager/issues/1517) Fixed wrong account format used for querying selfBond @faboweb * [\#1503](https://github.com/cosmos/voyager/issues/1503) Added e2e test for balance updates after delegation @faboweb * [\#1440](https://github.com/cosmos/voyager/issues/1440) Fixed an error that prevented disconnecting from the RPC websocket if it wasn't defined @fedekunze diff --git a/app/src/renderer/connectors/lcdClientMock.js b/app/src/renderer/connectors/lcdClientMock.js index b58fdba3da..307e06da23 100644 --- a/app/src/renderer/connectors/lcdClientMock.js +++ b/app/src/renderer/connectors/lcdClientMock.js @@ -58,7 +58,7 @@ let state = { amount: `1234` } ], - address: makeHash() + address: addresses[1] } ], outputs: [ @@ -106,7 +106,7 @@ let state = { amount: `1234` } ], - address: makeHash() + address: addresses[1] } ] } diff --git a/app/src/renderer/vuex/modules/delegation.js b/app/src/renderer/vuex/modules/delegation.js index c1b7357c1d..0e8f7771af 100644 --- a/app/src/renderer/vuex/modules/delegation.js +++ b/app/src/renderer/vuex/modules/delegation.js @@ -1,6 +1,6 @@ "use strict" -import { calculateShares, calculateTokens } from "scripts/common" +import { calculateShares } from "scripts/common" export default ({ node }) => { let emptyState = { loading: false, @@ -194,32 +194,24 @@ export default ({ node }) => { if (mappedDelegations) { // (optimistic update) we update the atoms of the user before we get the new values from chain - let atomsDiff = - stakingTransactions.delegations && - stakingTransactions.delegations - // compare old and new delegations and diff against old atoms - .map( - delegation => - calculateTokens( - delegation.validator, - state.committedDelegates[ - delegation.validator.operator_address - ] - ) - delegation.atoms - ) - .reduce((sum, diff) => sum + diff, 0) - commit(`setAtoms`, user.atoms + atomsDiff) + + let atomsSum = stakingTransactions.delegations.reduce( + (sum, delegation) => sum + delegation.atoms, + 0 + ) + commit(`setAtoms`, user.atoms - atomsSum) + + // optimistically update the committed delegations + stakingTransactions.delegations.forEach(delegation => { + state.committedDelegates[delegation.validator.operator_address] += + delegation.atoms + }) } // we optimistically update the committed delegations // TODO usually I would just query the new state through the LCD and update the state with the result, but at this point we still get the old shares setTimeout(async () => { - dispatch(`updateDelegates`) //.then(() => - // updateCommittedDelegations( - // delegations, - // commit - // ) - // ) + dispatch(`updateDelegates`) }, 5000) } // deprecated @@ -262,12 +254,3 @@ export default ({ node }) => { actions } } -// needed for optimistic updates, uncomment or delete this when that issue is addressed -// function updateCommittedDelegations(delegations, commit) { -// for (let delegation of delegations) { -// commit("setCommittedDelegation", { -// candidateId: delegation.delegate.operator_address, -// value: delegation.atoms -// }) -// } -// } diff --git a/test/unit/specs/store/__snapshots__/transactions.spec.js.snap b/test/unit/specs/store/__snapshots__/transactions.spec.js.snap index b0cb14e88f..c8927d9e7c 100644 --- a/test/unit/specs/store/__snapshots__/transactions.spec.js.snap +++ b/test/unit/specs/store/__snapshots__/transactions.spec.js.snap @@ -359,7 +359,7 @@ Array [ "value": Object { "inputs": Array [ Object { - "address": "tb1cy0q7p", + "address": "cosmos1pxdf0lvq5jvl9uxznklgc5gxuwzpdy5ynem546", "coins": Array [ Object { "amount": "1234", @@ -409,7 +409,7 @@ Array [ ], "outputs": Array [ Object { - "address": "tb1cy0q7p", + "address": "cosmos1pxdf0lvq5jvl9uxznklgc5gxuwzpdy5ynem546", "coins": Array [ Object { "amount": "1234", diff --git a/test/unit/specs/store/delegation.spec.js b/test/unit/specs/store/delegation.spec.js index 6089f3ca4f..c39552c05d 100644 --- a/test/unit/specs/store/delegation.spec.js +++ b/test/unit/specs/store/delegation.spec.js @@ -1,5 +1,6 @@ import setup from "../../helpers/vuex-setup" import lcdClientMock from "renderer/connectors/lcdClientMock.js" +import delegationModule from "renderer/vuex/modules/delegation.js" let instance = setup() @@ -252,6 +253,52 @@ describe(`Module: Delegations`, () => { expect(store.state.delegation.committedDelegates).toBeTruthy() }) + it(`should update the atoms on a delegation optimistically`, async () => { + const commit = jest.fn() + const delegates = store.state.delegates.delegates + let stakingTransactions = {} + stakingTransactions.delegations = [ + { + validator: delegates[0], + atoms: 109 + }, + { + validator: delegates[1], + atoms: 456 + } + ] + let committedDelegates = { + [delegates[0].operator_address]: 10, + [delegates[1].operator_address]: 50 + } + + await delegationModule({}).actions.submitDelegation( + { + rootState: { + config: { + bondingDenom: `atom` + }, + user: { + atoms: 1000 + }, + wallet: {} + }, + state: { + committedDelegates + }, + dispatch: () => {}, + commit + }, + { stakingTransactions } + ) + + expect(commit).toHaveBeenCalledWith(`setAtoms`, 435) + expect(committedDelegates).toEqual({ + [delegates[0].operator_address]: 119, + [delegates[1].operator_address]: 506 + }) + }) + it(`should update updateDelegates after delegation`, async () => { jest.useFakeTimers() let stakingTransactions = {}