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

Commit b550ccb

Browse files
committed
init
1 parent f245f91 commit b550ccb

File tree

11 files changed

+288
-17
lines changed

11 files changed

+288
-17
lines changed

packages/core/lib/command-utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
} = require("./commands/commands");
1212
const Web3 = require("web3");
1313
const TruffleError = require("@truffle/error");
14+
const StreamingWeb3HttpProvider = require("@truffle/stream-provider");
1415

1516
const defaultHost = "127.0.0.1";
1617
const managedGanacheDefaultPort = 9545;
@@ -324,9 +325,11 @@ const displayGeneralHelp = options => {
324325
OS.EOL +
325326
"Usage: truffle <command> [options]"
326327
)
327-
.epilog("See more at https://trufflesuite.com/docs/" +
328-
OS.EOL +
329-
"For Ethereum JSON-RPC documentation see https://ganache.dev")
328+
.epilog(
329+
"See more at https://trufflesuite.com/docs/" +
330+
OS.EOL +
331+
"For Ethereum JSON-RPC documentation see https://ganache.dev"
332+
)
330333
// showHelp prints using console.error, this won't log in a
331334
// child process - "log" forces it to use console.log instead
332335
.showHelp("log");
@@ -397,7 +400,7 @@ const deriveConfigEnvironment = function (detectedConfig, network, url) {
397400
configuredNetwork = {
398401
network_id: customConfig.network_id || defaultNetworkId,
399402
provider: function () {
400-
return new Web3.providers.HttpProvider(configuredNetworkUrl, {
403+
return new StreamingWeb3HttpProvider(configuredNetworkUrl, {
401404
keepAlive: false
402405
});
403406
},

packages/debugger/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@truffle/source-map-utils": "^1.3.108",
2828
"bn.js": "^5.1.3",
2929
"debug": "^4.3.1",
30+
"JSONStream": "1.3.5",
3031
"json-pointer": "^0.6.1",
3132
"json-stable-stringify": "^1.0.1",
3233
"lodash": "^4.17.21",

packages/environment/environment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const Web3 = require("web3");
21
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
32
const expect = require("@truffle/expect");
43
const TruffleError = require("@truffle/error");
54
const { Resolver } = require("@truffle/resolver");
65
const Artifactor = require("@truffle/artifactor");
76
const Ganache = require("ganache");
87
const Provider = require("@truffle/provider");
8+
const StreamingWeb3HttpProvider = require("@truffle/stream-provider");
99

1010
const Environment = {
1111
// It's important config is a Config object and not a vanilla object
@@ -77,7 +77,7 @@ const Environment = {
7777
...config.networks[network],
7878
network_id: ganacheOptions.network_id,
7979
provider: function () {
80-
return new Web3.providers.HttpProvider(url, { keepAlive: false });
80+
return new StreamingWeb3HttpProvider(url, { keepAlive: false });
8181
}
8282
};
8383

packages/provider/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Web3 = require("web3");
33
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
44
const wrapper = require("./wrapper");
55
const DEFAULT_NETWORK_CHECK_TIMEOUT = 5000;
6+
const StreamingWeb3HttpProvider = require("@truffle/stream-provider");
67

78
module.exports = {
89
wrap: function (provider, options) {
@@ -25,7 +26,7 @@ module.exports = {
2526
options.url || "ws://" + options.host + ":" + options.port
2627
);
2728
} else {
28-
provider = new Web3.providers.HttpProvider(
29+
provider = new StreamingWeb3HttpProvider(
2930
options.url || `http://${options.host}:${options.port}`,
3031
{ keepAlive: false }
3132
);
@@ -49,17 +50,18 @@ module.exports = {
4950
return new Promise((resolve, reject) => {
5051
const noResponseFromNetworkCall = setTimeout(() => {
5152
let errorMessage =
52-
"There was a timeout while attempting to connect to the network at " + host +
53+
"There was a timeout while attempting to connect to the network at " +
54+
host +
5355
".\n Check to see that your provider is valid." +
5456
"\n If you have a slow internet connection, try configuring a longer " +
5557
"timeout in your Truffle config. Use the " +
5658
"networks[networkName].networkCheckTimeout property to do this.";
5759

58-
if (network === "dashboard") {
59-
errorMessage +=
60-
"\n Also make sure that your Truffle Dashboard browser " +
61-
"tab is open and connected to MetaMask.";
62-
}
60+
if (network === "dashboard") {
61+
errorMessage +=
62+
"\n Also make sure that your Truffle Dashboard browser " +
63+
"tab is open and connected to MetaMask.";
64+
}
6365

6466
throw new Error(errorMessage);
6567
}, networkCheckTimeout);
@@ -75,7 +77,9 @@ module.exports = {
7577
} catch (error) {
7678
console.log(
7779
"> Something went wrong while attempting to connect to the " +
78-
"network at " + host + ". Check your network configuration."
80+
"network at " +
81+
host +
82+
". Check your network configuration."
7983
);
8084
clearTimeout(noResponseFromNetworkCall);
8185
clearTimeout(networkCheck);
@@ -86,5 +90,5 @@ module.exports = {
8690
}, networkCheckDelay);
8791
})();
8892
});
89-
},
93+
}
9094
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

packages/stream-provider/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 ConsenSys, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

packages/stream-provider/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# @truffle/stream-provider
2+
3+
A Web3.providers.HttpProvider that can handle JSON blobs greater than 1 GB.
4+
5+
Large blob handling is currently only used for `debug_traceTransaction`
6+
results.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import axios, { AxiosRequestConfig } from "axios";
2+
import type { JsonRpcResponse } from "web3-core-helpers";
3+
import { errors } from "web3-core-helpers";
4+
import _Web3HttpProvider, { HttpProvider } from "web3-providers-http";
5+
const JSONStream = require("JSONStream");
6+
7+
// they export types, but in the wrong place
8+
const Web3HttpProvider = (_Web3HttpProvider as any) as typeof HttpProvider;
9+
10+
export class StreamingWeb3HttpProvider extends Web3HttpProvider {
11+
/**
12+
* Should be used to make async request
13+
*
14+
* @method send
15+
* @param {Object} payload
16+
* @param {Function} callback triggered on end with (err, result)
17+
*/
18+
send(
19+
payload: any,
20+
callback: (error: Error | null, result: JsonRpcResponse | undefined) => void
21+
) {
22+
if (
23+
typeof payload === "object" &&
24+
payload.method === "debug_traceTransaction"
25+
) {
26+
const requestOptions: AxiosRequestConfig = {
27+
method: "post",
28+
url: this.host,
29+
responseType: "stream",
30+
data: payload,
31+
timeout: this.timeout,
32+
withCredentials: this.withCredentials,
33+
headers: this.headers
34+
? this.headers.reduce((acc, header) => {
35+
acc[header.name] = header.value;
36+
return acc;
37+
}, {} as Record<string, string>)
38+
: undefined
39+
};
40+
// transitional.clarifyTimeoutError is required so we can detect and emit
41+
// a timeout error the way web3 already does
42+
(requestOptions as any).transitional = {
43+
clarifyTimeoutError: true
44+
};
45+
const agents = {
46+
httpsAgent: (this as any).httpsAgent,
47+
httpAgent: (this as any).httpAgent,
48+
baseUrl: (this as any).baseUrl
49+
};
50+
if (this.agent) {
51+
agents.httpsAgent = this.agent.https;
52+
agents.httpAgent = this.agent.http;
53+
agents.baseUrl = this.agent.baseUrl;
54+
}
55+
requestOptions.httpAgent = agents.httpAgent;
56+
requestOptions.httpsAgent = agents.httpsAgent;
57+
requestOptions.baseURL = agents.baseUrl;
58+
59+
axios(requestOptions)
60+
.then(async response => {
61+
let error = null;
62+
let result: any = {};
63+
try {
64+
for await (const { key, value } of response.data.pipe(
65+
JSONStream.parse([true, { emitKey: true }])
66+
)) {
67+
result[key] = value;
68+
}
69+
} catch (e) {
70+
error = errors.InvalidResponse(e);
71+
}
72+
this.connected = true;
73+
// process.nextTick so an exception thrown in the callback doesn't
74+
// bubble back up to here
75+
process.nextTick(callback, error, result);
76+
})
77+
.catch(error => {
78+
this.connected = false;
79+
if (error.code === "ETIMEDOUT") {
80+
// web3 passes timeout as a number to ConnectionTimeout, despite the
81+
// type requiring a string
82+
callback(errors.ConnectionTimeout(this.timeout as any), undefined);
83+
} else {
84+
callback(errors.InvalidConnection(this.host), undefined);
85+
}
86+
});
87+
} else {
88+
return super.send(payload, callback);
89+
}
90+
}
91+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@truffle/stream-provider",
3+
"description": "Enables streaming results into memory from debug_traceTransaction",
4+
"license": "MIT",
5+
"author": "David Murdoch <[email protected]>",
6+
"homepage": "https://github.com/trufflesuite/truffle/tree/master/packages/stream-provider#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/trufflesuite/truffle.git",
10+
"directory": "packages/stream-provider"
11+
},
12+
"bugs": {
13+
"url": "https://github.com/trufflesuite/truffle/issues"
14+
},
15+
"version": "0.0.1",
16+
"main": "dist/index.js",
17+
"files": [
18+
"dist"
19+
],
20+
"directories": {
21+
"lib": "lib"
22+
},
23+
"scripts": {
24+
"build": "tsc",
25+
"prepare": "yarn build"
26+
},
27+
"types": "dist/index.d.ts",
28+
"dependencies": {
29+
"web3-providers-http": "^1.6.0",
30+
"web3-core-helpers": "1.5.3",
31+
"JSONStream": "1.3.5",
32+
"axios": "^0.21.1"
33+
},
34+
"keywords": [
35+
"ethereum",
36+
"etherscan",
37+
"ipfs",
38+
"solidity",
39+
"web3"
40+
],
41+
"publishConfig": {
42+
"access": "public"
43+
}
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"module": "commonjs",
5+
"esModuleInterop": true,
6+
"target": "es2016",
7+
"downlevelIteration": true,
8+
"noImplicitAny": true,
9+
"moduleResolution": "node",
10+
"sourceMap": true,
11+
"outDir": "dist",
12+
"baseUrl": ".",
13+
"lib": ["es2017"],
14+
"paths": {
15+
},
16+
"rootDir": "lib",
17+
"types": ["debug", "node"]
18+
},
19+
"include": [
20+
"./lib/**/*.ts"
21+
]
22+
23+
}

0 commit comments

Comments
 (0)