Skip to content

Commit

Permalink
fix: pass tests using adminwalelt and multiple admin addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpy committed Mar 28, 2020
1 parent 305c963 commit e039785
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .env.dev
Expand Up @@ -3,7 +3,8 @@ GUNDB_PASS=password
GUNDB_SERVERMODE=false
GUNDB_PEERS="https://goodgun-dev.herokuapp.com/gun"
JWT_PASS=G00DDAPP
MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
#MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
MNEMONIC="they bus duty candy uncover assault bullet kitchen kit acid spin physical"
INFURA_API=
NETWORK=develop
LOG_LEVEL=debug
Expand All @@ -29,6 +30,6 @@ FUSE_API=https://explorer.fuse.io
TWILIO_AUTH_ID=
TWILIO_AUTH_TOKEN=
TWILIO_PHONE_NUMBER=
NUMBER_OF_ADMIN_WALLET_ACCOUNTS=1
NUMBER_OF_ADMIN_WALLET_ACCOUNTS=5
ADMIN_MIN_BALANCE=100000
MARKET_PASSWORD=markettokenmarkettokenmarkettoke
5 changes: 3 additions & 2 deletions .env.test
Expand Up @@ -3,7 +3,8 @@ PORT=3004
NODE_ENV=test
GUNDB_PASS=password
GUNDB_SERVERMODE=true
MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
#MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
MNEMONIC="they bus duty candy uncover assault bullet kitchen kit acid spin physical"
INFURA_API=a2eaa99cd7224a6db590ea38babff108
NETWORK=develop
LOG_LEVEL=silent
Expand Down Expand Up @@ -41,4 +42,4 @@ MARKET_PASSWORD=markettokenmarkettokenmarkettoke
SECURE_KEY=d41d8cd98f00b204e9800998ecf8427e
ENABLE_MONGO_LOCK=false
MONGO_DB_URI=mongodb+srv://gooddollar:nN0oCoatunBLWSxw@cluster0-h5vxm.mongodb.net/gooddollar-test4?retryWrites=true&w=majority
NUMBER_OF_ADMIN_WALLET_ACCOUNTS=1
NUMBER_OF_ADMIN_WALLET_ACCOUNTS=5
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ env:
- GUNDB_PASS=test
- JWT_PASS=test
- MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
- ADMIN_MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
- ADMIN_MNEMONIC="they bus duty candy uncover assault bullet kitchen kit acid spin physical"

- NETWORK=develop
- CI=false
Expand All @@ -26,6 +26,7 @@ before_script:
- popd

script:
- export MNEMONIC=$ADMIN_MNEMONIC
- npm link @gooddollar/goodcontracts
- npm run coverage -- --silent
- npm run coveralls
Expand Down
5 changes: 3 additions & 2 deletions src/server/blockchain/AdminWallet.js
Expand Up @@ -389,7 +389,7 @@ export class Wallet {
let userBalance = await this.web3.eth.getBalance(address)
let maxTopWei = parseInt(web3Utils.toWei('1000000', 'gwei'))
let toTop = maxTopWei - userBalance
log.debug('TopWallet:', { userBalance, toTop })
log.debug('TopWallet:', { address, userBalance, toTop })
if (toTop > 0 && (force || toTop / maxTopWei >= 0.75)) {
let res = await this.sendTransaction(this.proxyContract.methods.topWallet(address))
log.debug('Topwallet result:', { res })
Expand Down Expand Up @@ -462,9 +462,10 @@ export class Wallet {
})
.on('confirmation', c => onConfirmation && onConfirmation(c))
.on('error', async e => {
log.error('sendTransaction error:', e.message, e)
if (isNonceError(e)) {
let netNonce = parseInt(await this.web3.eth.getTransactionCount(address))
log.error('sendTransaciton nonce failure retry', {
log.warn('sendTransaciton nonce failure retry', {
errMessage: e.message,
nonce,
gas,
Expand Down
16 changes: 13 additions & 3 deletions src/server/blockchain/__tests__/adminWallet.test.js
@@ -1,6 +1,7 @@
import AdminWallet from '../AdminWallet'
import txManager from '../../utils/tx-manager'
import Web3 from 'web3'
import delay from 'delay'

const web3 = new Web3()
const generateWalletAddress = () => web3.eth.accounts.create().address
Expand All @@ -14,6 +15,8 @@ describe('adminwallet', () => {
test(`adminWallet top wallet shouldn't throws an error when user is not whitelisted/verified`, async () => {
const unverifiedAddress = generateWalletAddress()
const tx = await AdminWallet.topWallet(unverifiedAddress, null).catch(e => false)
const balance = await AdminWallet.web3.eth.getBalance(unverifiedAddress)
expect(balance).toEqual('1000000000000000')
expect(tx).toBeTruthy()
})

Expand Down Expand Up @@ -47,11 +50,18 @@ describe('adminwallet', () => {
test('adminWallet receive queue nonce', async () => {
const promises = []
for (let i = 0; i < 5; i++) {
await delay(100) //hack otherwise txes fail, looks like a web3 issue, sending txes out of order
const unverifiedAddress = generateWalletAddress()
// console.log('unverifiedAddress', unverifiedAddress)
promises.push(AdminWallet.topWallet(unverifiedAddress, null, true))
promises.push(
AdminWallet.topWallet(unverifiedAddress)
.then(tx => tx.blockNumber)
.catch(e => e)
)
}
const res = await Promise.all(promises).catch(_ => false)
const res = await Promise.all(promises)
const uniqueBlocks = new Set(res)
res.forEach(n => expect(n).toEqual(expect.any(Number))) //check it was excuted on one or two block
expect(uniqueBlocks.size).toBeLessThanOrEqual(2)
expect(res).toBeTruthy()
})

Expand Down
20 changes: 11 additions & 9 deletions src/server/utils/tx-manager/queueMutex.js
Expand Up @@ -18,8 +18,8 @@ export default class queueMutex {
async createListIfNotExists(addresses) {
for (let address of addresses) {
if (!this.getWallet(address)) {
await this.createWallet(address)
log.info('created mutex for address:', { address })
const data = await this.createWallet(address)
log.info('created mutex for address:', { address, nonce: data.nonce })
}
}
}
Expand Down Expand Up @@ -118,15 +118,17 @@ export default class queueMutex {
async getFirstFreeAddress(addresses) {
return new Promise(resolve => {
const interval = setInterval(() => {
for (let address of addresses) {
if (this.isLocked(address) === false) {
log.debug('getFirstFreeAddress: address not locked', { address })
//use random to simulate real conditions, otherwise same address will be taken on single host
const address = addresses[Math.floor(Math.random() * addresses.length)]
// for (let address of addresses) {
if (this.isLocked(address) === false) {
log.debug('getFirstFreeAddress: address not locked', { address })

clearInterval(interval)
return resolve(address)
}
log.debug('getFirstFreeAddress: address locked', { address })
clearInterval(interval)
return resolve(address)
}
log.debug('getFirstFreeAddress: address locked', { address })
// }
}, 100)
})
}
Expand Down

0 comments on commit e039785

Please sign in to comment.