-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-test.js
148 lines (120 loc) · 4.46 KB
/
load-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const axios = require('axios').default;
const SHA256 = require('@aws-crypto/sha256-js').Sha256
const defaultProvider = require('@aws-sdk/credential-provider-node').defaultProvider
const HttpRequest = require('@aws-sdk/protocol-http').HttpRequest
const SignatureV4 = require('@aws-sdk/signature-v4').SignatureV4
const Bitcoin = require("bitcoinjs-lib");
const dotenv = require("dotenv");
dotenv.config()
const queryRequest = async (path, data) => {
const signer = new SignatureV4({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET
},
service: 'managedblockchain-query',
region: process.env.AWS_REGION,
sha256: SHA256,
});
//query endpoint
let queryEndpoint = `https://managedblockchain-query.${process.env.AWS_REGION}.amazonaws.com/${path}`;
// parse the URL into its component parts (e.g. host, path)
const url = new URL(queryEndpoint);
// create an HTTP Request object
const req = new HttpRequest({
hostname: url.hostname.toString(),
path: url.pathname.toString(),
body: JSON.stringify(data),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
host: url.hostname,
}
});
// use AWS SignatureV4 utility to sign the request, extract headers and body
const signedRequest = await signer.sign(req, { signingDate: new Date() });
try {
//make the request using axios
const response = await axios({...signedRequest, url: queryEndpoint, data: data})
return response.data
} catch (error) {
console.error('Something went wrong: ', error)
throw error
}
}
const rpcRequest = async ( data) => {
const signer = new SignatureV4({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET
},
service: 'managedblockchain-query',
region: process.env.AWS_REGION,
sha256: SHA256,
});
//query endpoint
let queryEndpoint = `https://testnet.bitcoin.managedblockchain.${process.env.AWS_REGION}.amazonaws.com`;
// parse the URL into its component parts (e.g. host, path)
const url = new URL(queryEndpoint);
// create an HTTP Request object
const req = new HttpRequest({
hostname: url.hostname.toString(),
path: url.pathname.toString(),
body: JSON.stringify(data),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
host: url.hostname,
}
});
// use AWS SignatureV4 utility to sign the request, extract headers and body
const signedRequest = await signer.sign(req, { signingDate: new Date() });
try {
//make the request using axios
const response = await axios({...signedRequest, url: queryEndpoint, data: data})
return response.data
} catch (error) {
console.error('Something went wrong: ')
throw error
}
}
const listTransactions = async() => {
const start = new Date().getTime();
console.log('start', start)
let methodArg = 'list-filtered-transaction-events';
let dataArg = {
"addressIdentifierFilter": {
"transactionEventToAddress" : ["tb1pyl9jyn04gx34zuzw0qu3k7pxlxc0xxgkhjspqa2rucc2mvx2zaqse4mnl6"],
},
"voutFilter": {
"voutSpent": false
},
"network":"BITCOIN_TESTNET",
}
const data = await queryRequest(methodArg, dataArg);
let total = 0
data.events.forEach((event) => {
total += parseFloat(event.value)
})
console.log('transaction count', data.events.length)
console.log('transaction sum', total)
await Promise.all(data.transactions.map((transaction) =>
getTransaction(transaction.transactionId)
))
const end = new Date().getTime();
console.log('end', end)
console.log('diff', end-start)
}
const getTransaction = async(txid) => {
const hexstr = await rpcRequest({"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": [txid]})
const txBytes = Buffer.from(hexstr.result, "hex");
// Parse the transaction byte array
const tx = Bitcoin.Transaction.fromBuffer(txBytes);
console.log(tx.ins[0].script)
const data = await rpcRequest({"jsonrpc": "1.0", "id": "curltest", "method": "decoderawtransaction", "params": [hexstr.result]})
console.log(data.result.vin[0].txinwitness)
}
listTransactions()
//Run the query request.