From 8c9ad51135ed5675eb0668ae22c29881f7fb2a12 Mon Sep 17 00:00:00 2001 From: Denis Ermolin Date: Thu, 20 Jan 2022 22:39:38 +0700 Subject: [PATCH] stuff --- .../staking/stakeManager/StakeManager.sol | 13 +++--- package-lock.json | 25 ++++------- .../stakeManager/StakeManager.Staking.js | 43 +++++++++++++++++++ 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/contracts/staking/stakeManager/StakeManager.sol b/contracts/staking/stakeManager/StakeManager.sol index 09532754f..910700f48 100644 --- a/contracts/staking/stakeManager/StakeManager.sol +++ b/contracts/staking/stakeManager/StakeManager.sol @@ -567,11 +567,17 @@ contract StakeManager is address currentSigner = validators[validatorId].signer; // update signer event logger.logSignerChange(validatorId, currentSigner, signer, signerPubkey); + + if (validators[validatorId].deactivationEpoch == 0) { + // didn't unstake, safe to remove the signer from the list + _removeSigner(currentSigner); + } + + _insertSigner(signer); signerToValidator[currentSigner] = INCORRECT_VALIDATOR_ID; signerToValidator[signer] = validatorId; validators[validatorId].signer = signer; - _updateSigner(currentSigner, signer); // reset update time to current time latestSignerUpdateEpoch[validatorId] = _currentEpoch; @@ -1202,11 +1208,6 @@ contract StakeManager is } } - function _updateSigner(address prevSigner, address newSigner) internal { - _removeSigner(prevSigner); - _insertSigner(newSigner); - } - function _removeSigner(address signerToDelete) internal { uint256 totalSigners = signers.length; address swapSigner = signers[totalSigners - 1]; diff --git a/package-lock.json b/package-lock.json index 7d7b3eced..e6e96d4cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1218,7 +1218,7 @@ "requires": { "underscore": "1.9.1", "web3-core-helpers": "1.2.1", - "websocket": "github:web3-js/WebSocket-Node#polyfill/globalThis" + "websocket": "github:web3-js/WebSocket-Node#ef5ea2f41daf4a2113b80c9223df884b4d56c400" } }, "web3-shh": { @@ -1704,7 +1704,7 @@ "requires": { "underscore": "1.9.1", "web3-core-helpers": "1.2.1", - "websocket": "github:web3-js/WebSocket-Node#polyfill/globalThis" + "websocket": "github:web3-js/WebSocket-Node#ef5ea2f41daf4a2113b80c9223df884b4d56c400" } }, "web3-shh": { @@ -2190,7 +2190,7 @@ "requires": { "underscore": "1.9.1", "web3-core-helpers": "1.2.1", - "websocket": "github:web3-js/WebSocket-Node#polyfill/globalThis" + "websocket": "github:web3-js/WebSocket-Node#ef5ea2f41daf4a2113b80c9223df884b4d56c400" } }, "web3-shh": { @@ -2401,7 +2401,7 @@ "eth-lib": "0.2.7", "ethereumjs-common": "^1.3.2", "ethereumjs-tx": "^2.1.1", - "scrypt-shim": "github:web3-js/scrypt-shim", + "scrypt-shim": "github:web3-js/scrypt-shim#aafdadda13e660e25e1c525d1f5b2443f5eb1ebb", "underscore": "1.9.1", "uuid": "3.3.2", "web3-core": "1.2.2", @@ -2532,7 +2532,7 @@ "requires": { "underscore": "1.9.1", "web3-core-helpers": "1.2.2", - "websocket": "github:web3-js/WebSocket-Node#polyfill/globalThis" + "websocket": "github:web3-js/WebSocket-Node#ef5ea2f41daf4a2113b80c9223df884b4d56c400" }, "dependencies": { "websocket": { @@ -3340,8 +3340,7 @@ "version": "0.3.2", "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.3.2.tgz", "integrity": "sha1-qJS3XUvE9s1nnvMkSp/Y9Gri1Cg=", - "dev": true, - "optional": true + "dev": true }, "binary-extensions": { "version": "1.13.1", @@ -3637,15 +3636,13 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-1.0.0.tgz", "integrity": "sha1-rEaBd8SUNAWgkvyPKXYMb/xiBsA=", - "dev": true, - "optional": true + "dev": true }, "is-glob": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-2.0.1.tgz", "integrity": "sha1-0Jb5JqPe1WAPP9/ZEZjLCIjC2GM=", "dev": true, - "optional": true, "requires": { "is-extglob": "^1.0.0" } @@ -3655,7 +3652,6 @@ "resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz", "integrity": "sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU=", "dev": true, - "optional": true, "requires": { "kind-of": "^3.0.2" }, @@ -3665,7 +3661,6 @@ "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", "dev": true, - "optional": true, "requires": { "is-buffer": "^1.1.5" } @@ -3690,8 +3685,7 @@ "version": "6.0.3", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", "integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==", - "dev": true, - "optional": true + "dev": true }, "normalize-path": { "version": "2.1.1", @@ -10707,8 +10701,7 @@ "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "optional": true + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" }, "normalize-url": { "version": "4.5.0", diff --git a/test/units/staking/stakeManager/StakeManager.Staking.js b/test/units/staking/stakeManager/StakeManager.Staking.js index a753b86b0..fa8dd3135 100644 --- a/test/units/staking/stakeManager/StakeManager.Staking.js +++ b/test/units/staking/stakeManager/StakeManager.Staking.js @@ -6,6 +6,7 @@ import { } from '../../../helpers/utils.js' import { expectEvent, expectRevert, BN } from '@openzeppelin/test-helpers' import { wallets, walletAmounts, freshDeploy, approveAndStake } from '../deployment' +import { assert } from 'chai' module.exports = function(accounts) { let owner = accounts[0] @@ -317,6 +318,48 @@ module.exports = function(accounts) { }) describe('unstake', function() { + describe('when Alice unstakes and update the signer', function() { + const AliceWallet = wallets[1] + const BobWallet = wallets[3] + const AliceNewWallet = wallets[2] + + before(freshDeploy) + before(doStake(AliceWallet)) + before(doStake(BobWallet)) + before('Change signer', async function() { + const signerUpdateLimit = await this.stakeManager.signerUpdateLimit() + await this.stakeManager.advanceEpoch(signerUpdateLimit) + + this.validatorId = await this.stakeManager.getValidatorId(AliceWallet.getAddressString()) + + }) + + it('Alice should unstake', async function() { + this.receipt = await this.stakeManager.unstake(this.validatorId, { + from: AliceWallet.getAddressString() + }) + }) + + it('Signers list should have only Bob\'s signer', async function() { + assert((await this.stakeManager.signers(0)) == BobWallet.getChecksumAddressString(), 'no Bob signer!') + await expectRevert.unspecified(this.stakeManager.signers(1)) + }) + + it('Alice should update signer', async function() { + await this.stakeManager.updateSigner(this.validatorId, AliceNewWallet.getPublicKeyString(), { + from: AliceWallet.getAddressString() + }) + }) + + it('Signers list should have new Alice\'s signer and Bob\'s signer', async function() { + const signers = [await this.stakeManager.signers(0), await this.stakeManager.signers(1)] + + + assert(signers.findIndex(x => x == AliceNewWallet.getChecksumAddressString()) !== -1, 'no Alice signer!') + assert(signers.findIndex(x => x == BobWallet.getChecksumAddressString()) !== -1, 'no Bob signer!') + }) + }) + describe('when user unstakes right after stake', async function() { const user = wallets[2].getChecksumAddressString() const amounts = walletAmounts[wallets[2].getAddressString()]