Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Conflict with MongoDB and 'window.crypto.getRandomValues' #4241

Open
Gredys opened this issue Aug 9, 2021 · 9 comments
Open

Conflict with MongoDB and 'window.crypto.getRandomValues' #4241

Gredys opened this issue Aug 9, 2021 · 9 comments
Assignees

Comments

@Gredys
Copy link

Gredys commented Aug 9, 2021

Issue

When trying to call insert method to MongoDB in migration deploy callback it raises stack overflow exception on calling getRandomValues in crypto bundle.

RangeError: Maximum call stack size exceeded
    at Object.getRandomValues (C:\...\Truffle\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:5:1)
    at Object.getRandomValues (C:\....\Truffle\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\....\Truffle\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
...
    at Object.getRandomValues (C:\....\Truffle\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
Truffle v5.4.3 (core: 5.4.3)
Node v14.17.1

Steps to Reproduce

  1. Initialize fresh truffle project
  2. Install MongoDB
  3. Try to save anything to DB in migration callback method

Expected Behavior

Data will be inserted correctly without exceptions rised by truffle bundles

Actual Results

Exception when trying to save anything to DB.
Truffle bundles getRandomValues method that utilizes crypto bundle. (file 853.bundled.js)
But in same time method getRandomValues === crypto.getRandomValues and this leading to infinite loop.

Environment

  • Operating System: Windows 10
  • Ethereum client: Hyperledger Besu
  • Truffle version (truffle version): Truffle v5.4.3 (core: 5.4.3)
  • node version (node --version): Node v14.17.1
  • npm version (npm --version): 7.20.0

Additional information

Adding code (between double start symbols **) to supplied bundle (853.bundled.js) fixes issue.

function getRandomValues(buf) {
  if (window.crypto && window.crypto.getRandomValues **&& window.crypto !== this**) {
    return window.crypto.getRandomValues(buf);
  }
@fainashalts
Copy link
Contributor

Hi @Gredys thanks for bringing this to our attention! Do you have a specific git repo we can look at or more reproduction steps so we can dig in further?

Just to verify, what environment are you running this in? Are you attempting to run your code in the browser or are you in a node environment?

@Gredys
Copy link
Author

Gredys commented Aug 11, 2021

Hello @fainashalts .
I will create simple repo example and send it to you a bit later.
I was doing everything from phpstorm teminal, so its basically node environment.

@Gredys
Copy link
Author

Gredys commented Aug 22, 2021

Sorry. Was busy few weeks. Tomorrow gonna create this example.

@eggplantzzz
Copy link
Contributor

Hey @Gredys just let us know when you have a simple example that we can test against and we'll try and figure out a solution!

@Gredys
Copy link
Author

Gredys commented Sep 3, 2021

@eggplantzzz I am really sorry for wasting your time. Here is an example project - truffle-issue-4241

I used Ganache and local instance of MongoDB with evertying set on default.

Example output of my migration log

Windows [Version 10.0.19043.1202]
(c) Корпорация Майкрософт (Microsoft Corporation). Все права защищены.

C:\Projects\Hexoid\truffle-issue-4241>truffle migrate --reset

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'ganache'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0x75d3bb2fff3399c84c3da64786fc46c79f15a5fa041c453c8793014464896603
   > Blocks: 0            Seconds: 0
   > contract address:    0xDA020de8E6B75d1Ce7eA3d88Ce2B84090F6919Ca
   > block number:        10
   > block timestamp:     1630668183
   > account:             0xF7812ee8DcefCaA506E1824A3944B0c65CFCb97b
   > balance:             99.9675956
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH


RangeError: Maximum call stack size exceeded
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:5:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
    at Object.getRandomValues (C:\Users\Hexoid\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\get-random-values\index.js:6:1)
Truffle v5.4.1 (core: 5.4.1)
Node v14.17.1

C:\Projects\Hexoid\truffle-issue-4241>

Command used to get this issue - truffle migrate --reset (work same without --reset, it used only to force migration run)

@eggplantzzz
Copy link
Contributor

eggplantzzz commented Sep 9, 2021

No problem at all, thanks for reporting it. I'll try and look into this for you!

So to reproduce the error just run the migrations? Can you give me specific steps to take?

@Gredys
Copy link
Author

Gredys commented Sep 10, 2021

  1. Download and start MongoDB with default settings (make sure port is same as in 1_initial_migration.js file)
  2. Download and start Ganache with default settings
  3. Download repository and try to migrate it

Once migration script will output information about current migration (Contract) - script will try to save that infomation in MongoDB and you will get call stack exception.

P.S. Sorry for my language knowing :D

@eggplantzzz
Copy link
Contributor

Thanks @Gredys! And you are fine :)

@eggplantzzz
Copy link
Contributor

eggplantzzz commented Feb 17, 2022

I think webpack recommends a different fix for the missing crypto lib problem. Check out KorbinianKuhn's answer here. They recommend to use crypto-browserify to shim that library. You can see the errors that webpack gives that prescribe this on this stack overflow issue. Perhaps that will resolve this issue if we can implement that in place of the shim currently in Truffle.

https://github.com/trufflesuite/truffle/blob/develop/packages/core/cli.js#L6-L10
https://github.com/trufflesuite/truffle/blob/develop/packages/core/lib/console-child.js#L7-L11

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants