-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.js
787 lines (492 loc) · 26.6 KB
/
vm.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import {DefaultStateManager} from '@ethereumjs/statemanager'
import {Address,Account} from '@ethereumjs/util'
import {Transaction} from '@ethereumjs/tx'
import {Common} from '@ethereumjs/common'
import {Block} from '@ethereumjs/block'
import {Trie} from '@ethereumjs/trie'
import {LevelDB} from './LevelDB.js'
import {VM} from '@ethereumjs/vm'
import rlp from '@ethereumjs/rlp'
import {Level} from 'level'
import Web3 from 'web3'
//_________________________________________________________ CONSTANTS POOL _________________________________________________________
// 'KLY_EVM' Contains state of EVM
// 'STATE' Contains metadata for KLY-EVM pseudochain (e.g. blocks, logs and so on)
const {
name,
networkId,
chainId,
coinbase, // this address will be set as a block creator, but all the fees will be automatically redirected to KLY env and distributed among pool stakers
hardfork,
gasLimitForBlock,
creds
} = CONFIG.EVM
const trie = new Trie({
db:new LevelDB(new Level(process.env.CHAINDATA_PATH+'/KLY_EVM')) // use own implementation. See the sources
})
const common = Common.custom({name,networkId,chainId},hardfork)
const stateManager = new DefaultStateManager({trie})
// Create our VM instance
const vm = await VM.create({common,stateManager})
const web3 = new Web3()
let block = Block.fromBlockData({header:{gasLimit:gasLimitForBlock,miner:coinbase}},{common})
/*
Default block template for KLY-EVM
[+] Miner(block creator) value will be mutable
[+] Timestamp will be mutable & deterministic
P.S: BTW everything will be changable
*/
// const block = Block.fromBlockData({header:{miner:'0x0000000000000000000000000000000000000000',timestamp:133713371337}},{common})
//_________________________________________________________ EXPORT SECTION _________________________________________________________
export let KLY_EVM = {
/**
* ### Execute tx in KLY-EVM
*
* @param {string} serializedEVMTxWith0x - EVM signed tx in hexadecimal to be executed in EVM in context of given block
*
* @returns txResult
*
*/
callEVM:async serializedEVMTxWith0x=>{
let serializedEVMTxWithout0x = serializedEVMTxWith0x.slice(2) // delete 0x
let tx = Transaction.fromSerializedTx(Buffer.from(serializedEVMTxWithout0x,'hex'))
let txResult = await vm.runTx({tx,block})
// We'll need full result to store logs and so on
if(!txResult.execResult.exceptionError) return txResult
},
/**
* ### Execute tx in KLY-EVM without state changes
*
* @param {import('@ethereumjs/tx').TxData | string} txDataOrSerializedTxInHexWith0x - EVM signed tx(TxData like(see EVM docs)) or serialized tx to be executed in EVM in context of given block
*
* @returns {string} result of executed contract / default tx
*/
sandboxCall:async(txDataOrSerializedTxInHexWith0x,isJustCall)=>{
// In case it's just KLY-EVM call to read from contract - then ok, otherwise(is isJustCall=false) we assume that it's attempt to add to mempool(so we need to verify signature and other stuff)
let tx = isJustCall ? Transaction.fromTxData(txDataOrSerializedTxInHexWith0x) : Transaction.fromSerializedTx(Buffer.from(txDataOrSerializedTxInHexWith0x.slice(2),'hex'))
if(isJustCall){
let {to,data} = tx
let vmCopy = await vm.copy()
let txResult = await vmCopy.evm.runCall({
to,data,
block
})
return txResult.execResult.exceptionError || web3.utils.toHex(txResult.execResult.returnValue)
}else {
let vmCopy = await vm.copy()
let origin = tx.getSenderAddress()
let {to,data,value,gasLimit} = tx
let caller = origin
if(tx.validate() && tx.verifySignature()){
let account = await vmCopy.stateManager.getAccount(origin)
if(account.nonce === tx.nonce && account.balance >= value){
let txResult = await vmCopy.evm.runCall({
origin,caller,to,data,gasLimit,
block
})
return txResult.execResult.exceptionError || web3.utils.toHex(txResult.execResult.returnValue)
} return {error:{msg:'Wrong nonce value or insufficient balance'}}
} return {error:{msg:'Transaction validation failed. Make sure signature is ok and required amount of gas is set'}}
}
},
/**
*
* ### Add the account to storage
*
* @param {string} address - EVM-compatible 20-bytes address
* @param {number} balanceInEthKly - wished balance
* @param {number} nonce - account nonce
*
*
* @returns {Object} result - The execution status
* @returns {boolean} result.status
*
*/
putAccount:async(address,balanceInEthKly,nonce=0)=>{
let accountData = {
nonce,
balance: BigInt(balanceInEthKly) * (BigInt(10) ** BigInt(18)), // balanceInEthKly * 1 eth. So, if you want to set balance to X KLY on KLY-EVM - set parameter value to X
}
let status = await vm.stateManager.putAccount(Address.fromString(address),Account.fromAccountData(accountData)).then(()=>({status:true})).catch(_=>({status:false}))
return status
},
putContract:async(address,balanceInEthKly,nonce,code,storage)=>{
let accountData = {
nonce,
balance:BigInt(balanceInEthKly) * (BigInt(10) ** BigInt(18)), // balanceInEthKly * 1 eth. So, if you want to set balance to X KLY on KLY-EVM - set parameter value to X
}
address = Address.fromString(address)
await vm.stateManager.putAccount(address,Account.fromAccountData(accountData))
for (const [key,value] of Object.entries(storage)) {
const storageKey = Buffer.from(key,'hex')
const storageValue = Buffer.from(rlp.decode(`0x${value}`))
await vm.stateManager.putContractStorage(address,storageKey,storageValue)
}
const codeBuffer = Buffer.from(code,'hex')
await vm.stateManager.putContractCode(address,codeBuffer)
},
/**
*
* ### Returns the state of account related to address
*
* @param {string} address - EVM-compatible 20-bytes address
*
* @returns {Object} account - The account from state
*
* @returns {BigInt} account.nonce
* @returns {BigInt} account.balance
* @returns {Buffer} account.storageRoot
* @returns {Buffer} account.codeHash
* @returns {boolean} account.virtual
*
*
*/
getAccount:async address => vm.stateManager.getAccount(Address.fromString(address)),
/**
*
* ### Returns the root of VM state
*
* @returns {string} root of state of KLY-EVM in hexadecimal
*
*/
getStateRoot:async()=>{
let stateRoot = await vm.stateManager.getStateRoot()
return stateRoot.toString('hex') //32-bytes hexadecimal form
},
/**
*
* ### Set the root of VM state
*
* @param {string} 32-bytes hexadecimal root of VM's state
*
*/
setStateRoot: stateRootInHex => stateManager.setStateRoot(Buffer.from(stateRootInHex,'hex')),
//____________________________________ Auxiliary functionality ____________________________________
/**
*
* ### Get the gas required for VM execution
*
* @param {import('@ethereumjs/tx').TxData} txData - EVM-like transaction with fields like from,to,value,data,etc.
*
*
* @returns {string} required number of gas to deploy contract or call method
*
*/
estimateGasUsed:async txData => {
let tx = Transaction.fromTxData(txData)
let {to,data} = tx
let origin = Address.fromString(txData.from) || tx.isSigned() && tx.getSenderAddress()
let caller = origin
let vmCopy = await vm.copy()
let txResult = await vmCopy.evm.runCall({origin,caller,to,data,block})
let gasUsed = txResult.execResult.executionGasUsed
// If gas is 0 - then it's default tx, so we need to run it via .runTx after getting signed with our private key and nonce
if(gasUsed === BigInt(0)){
try{
txData.gasLimit = txData.gas || txData.gasLimit
txData.gasLimit = BigInt(txData.gasLimit) === BigInt(0) ? BigInt(CONFIG.EVM.maxAllowedGasAmountForSandboxExecution) : txData.gasLimit
txData.gasPrice = BigInt(txData.gasPrice) === BigInt(0) ? BigInt(CONFIG.EVM.gasPriceInWeiAndHex) : txData.gasPrice
let finalTx = Transaction.fromTxData(txData)
finalTx = finalTx.sign(Buffer.from(creds.privateKey,'hex'))
txResult = await vmCopy.runTx({tx:finalTx,block,skipBalance:true,skipNonce:true})
gasUsed = txResult.totalGasSpent
}catch(e){
return {error:{msg:JSON.stringify(e)}}
}
}
return txResult.execResult.exceptionError || web3.utils.toHex(gasUsed.toString())
},
/**
*
* @returns {Block} the current block that used on VT
*/
getCurrentBlock:()=>block,
setCurrentBlockParams:(nextIndex,timestamp,parentHash)=>{
block = Block.fromBlockData({
header:{
gasLimit:gasLimitForBlock,
miner:coinbase,
timestamp,
parentHash:Buffer.from(parentHash,'hex'),
number:nextIndex
}
},{common})
},
/**
* ### Returns tx and its receipt in appropriate serialized form to store
*
*
* @param {string} transactionInHex
* @param {import('@ethereumjs/vm').RunTxResult} evmResult
* @param {Object} logsMap storage {contractAddress=>logsArray} where logsArray contains all the logs by this contract in current block
*
*
* @returns {Object}
*/
getTransactionWithReceiptToStore:(transactionInHex,evmResult,logsMap)=>{
/*
________________________ WHAT WE NEED TO STORE TO STATE DB ________________________
'TX:'+txHash - {tx,receipt} - tx and receipt by txHash
*/
let tx = Transaction.fromSerializedTx(Buffer.from(transactionInHex.slice(2),'hex'))
if(tx){
let transaction = tx.toJSON()
transaction.blockHash = '0x'+KLY_EVM.getCurrentBlock().hash().toString('hex')
transaction.blockNumber = web3.utils.toHex(KLY_EVM.getCurrentBlock().header.number.toString())
transaction.hash = '0x'+tx.hash().toString('hex')
transaction.from ||= tx.getSenderAddress().toString()
transaction.transactionIndex = 0
//______________ Working with receipt ______________
let receipt = evmResult.receipt
/*
______________________Must return______________________
✅transactionHash : DATA, 32 Bytes - hash of the transaction.
✅transactionIndex: QUANTITY - integer of the transactions index position in the block.
✅blockHash: DATA, 32 Bytes - hash of the block where this transaction was in.
✅blockNumber: QUANTITY - block number where this transaction was in.
✅from: DATA, 20 Bytes - address of the sender.
✅to: DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
✅cumulativeGasUsed : QUANTITY - The total amount of gas used when this transaction was executed in the block.
✅effectiveGasPrice : QUANTITY - The sum of the base fee and tip paid per unit of gas.
✅gasUsed : QUANTITY - The amount of gas used by this specific transaction alone.
✅contractAddress : DATA, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null.
✅logs: Array - Array of log objects, which this transaction generated.
✅logsBloom: DATA, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs.
✅type: DATA - integer of the transaction type, 0x00 for legacy transactions, 0x01 for access list types, 0x02 for dynamic fees. It also returns either :
⌛️root : DATA 32 bytes of post-transaction stateroot (pre Byzantium)
✅status: QUANTITY either 1 (success) or 0 (failure)
_____________________Add manually______________________
transactionHash - '0x'+tx.hash().toString('hex')
transactionIndex - '0x0'
blockHash - '0x'+block.hash().toString('hex')
blockNumber - block.header.number (in hex)
from - tx.getSenderAddress().toString()
to - tx.to
cumulativeGasUsed - convert to hex
effectiveGasPrice - take from tx gasPrice tx.gasPrice
gasUsed - take from tx execution result (result.execResult.executionGasUsed.toString())
type - tx.type (convert to hex)
contractAddress - take from tx (vm.runTx({tx,block}).createdAddress). Otherwise - set as null
logsBloom - '0x'+receipt.bitvector.toString('hex')
*/
let {hash,blockHash,blockNumber,from,to,gasPrice} = transaction
// Put in order logs
let logsForReceipt = receipt.logs.map(singleLog=>{
/*
Each single log is array with 3 objects
[0] - contract address which forced event. Need to hex
[1] - array of topics. Need to hex them
[2] - pure data related to log. Need to hex
*/
let [contractAddressBuffer,topicsBuffers,pureData] = singleLog
// Serialization
let address = '0x'+Buffer.from(contractAddressBuffer).toString('hex')
let topics = topicsBuffers.map(buffer=>'0x'+Buffer.from(buffer).toString('hex'))
let data = '0x'+Buffer.from(pureData).toString('hex')
/*
Now we need to add some extra data to log
address - contract address
topics
data - pureHexLogs
blockNumber(bigint to hex)
txHash
txIndex
blockHash
logIndex - 0
removed - false
id- 'log_00000000'
*/
let finalLogForm = {
address,
topics,
data,
blockNumber,
transactionHash:hash,
transactionIndex:'0x0',
blockHash,
logIndex:'0x0',
removed:false
}
KLY_EVM.storeLog(logsMap,finalLogForm)
return finalLogForm
})
let futureReceipt = {
status:receipt.status,
transactionHash:hash,
transactionIndex:'0x0',
blockHash,
blockNumber,
from,
cumulativeGasUsed:web3.utils.toHex(receipt.cumulativeBlockGasUsed.toString()),
effectiveGasPrice:gasPrice,
gasUsed:web3.utils.toHex(evmResult.execResult.executionGasUsed.toString()),
type:web3.utils.toHex(tx.type),
logsBloom:'0x'+receipt.bitvector.toString('hex'),
logs:logsForReceipt
}
if(to) futureReceipt.to = to
if(evmResult.createdAddress) futureReceipt.contractAddress = evmResult.createdAddress.toString()
else futureReceipt.contractAddress = null
return {tx:transaction,receipt:futureReceipt}
}
},
getBlockToStore: currentHash =>{
/*
Now, we need to store block
______________________Block must have______________________
✅number: QUANTITY - the block number. null when its pending block.
⌛️hash: DATA, 32 Bytes - hash of the block. null when its pending block.
✅parentHash: DATA, 32 Bytes - hash of the parent block.
✅nonce: DATA, 8 Bytes - hash of the generated proof-of-work. null when its pending block.
✅sha3Uncles: DATA, 32 Bytes - SHA3 of the uncles data in the block.
✅transactionsRoot: DATA, 32 Bytes - the root of the transaction trie of the block.
✅stateRoot: DATA, 32 Bytes - the root of the final state trie of the block.
✅receiptsRoot: DATA, 32 Bytes - the root of the receipts trie of the block.
✅miner: DATA, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.
✅difficulty: QUANTITY - integer of the difficulty for this block.
✅totalDifficulty: QUANTITY - integer of the total difficulty of the chain until this block.
✅extraData: DATA - the "extra data" field of this block.
✅logsBloom: DATA, 256 Bytes - the bloom filter for the logs of the block. null when its pending block.
✅gasLimit: QUANTITY - the maximum gas allowed in this block.
✅gasUsed: QUANTITY - the total used gas by all transactions in this block.
✅timestamp: QUANTITY - the unix timestamp for when the block was collated.
✅transactions: Array - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
✅uncles: Array - Array of uncle hashes.
✅size: QUANTITY - integer the size of this block in bytes.
________________________Current________________________
{
header: {
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
uncleHash: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
coinbase: '0x0000000000000000000000000000000000000000',
stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
transactionsTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
difficulty: '0x0',
number: '0x0',
gasLimit: '0xffffffffffffff',
gasUsed: '0x0',
timestamp: '0x1f21f020c9',
extraData: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
nonce: '0x0000000000000000'
},
transactions: [],
uncleHeaders: []
}
_________________________TODO__________________________
✅hash - '0x'+block.hash.toString('hex')
✅uncleHash => sha3Uncles
✅transactionsTrie => transactionsRoot
✅receiptTrie => receiptsRoot
✅coinbase => miner
✅totalDifficulty - '0x0'
✅size - '0x0'
✅transactions - push the hashes of txs runned in this block
✅uncleHeaders => uncles[]
*/
let currentBlock = KLY_EVM.getCurrentBlock()
let {number,parentHash,nonce,uncleHash,transactionsTrie,receiptTrie,coinbase,stateRoot,difficulty,logsBloom,gasLimit,gasUsed,mixHash,extraData,timestamp} = currentBlock.header
let blockTemplate = {
number:Web3.utils.toHex(number.toString()),
hash:'0x'+currentHash.toString('hex'),
parentHash:'0x'+parentHash.toString('hex'),
nonce:'0x'+nonce.toString('hex'),
extraData:'0x'+extraData.toString('hex'),
sha3Uncles:'0x'+uncleHash.toString('hex'),
transactionsRoot:'0x'+transactionsTrie.toString('hex'),
receiptsRoot:'0x'+receiptTrie.toString('hex'),
stateRoot:'0x'+stateRoot.toString('hex'),
miner:coinbase.toString(),
timestamp:Web3.utils.toHex(timestamp.toString()),
size:'0x0',
gasLimit:Web3.utils.toHex(gasLimit.toString()),
gasUsage:Web3.utils.toHex(gasUsed.toString()),
logsBloom:'0x'+logsBloom.toString('hex'),
totalDifficulty:'0x0',
difficulty:Web3.utils.toHex(difficulty.toString()),
mixHash:'0x'+mixHash.toString('hex'),
transactions:[],
uncleHeaders:[]
}
return blockTemplate
},
//
storeLog:(logsMap,logInstance) => {
/*
____________________Filter options are____________________
fromBlock:QUANTITY|TAG - (optional, default: "latest") Integer block number, or "latest" for the last mined block or "pending", "earliest" for not yet mined transactions.
toBlock:QUANTITY|TAG - (optional, default: "latest") Integer block number, or "latest" for the last mined block or "pending", "earliest" for not yet mined transactions.
address:DATA|Array, 20 Bytes - (optional) Contract address or a list of addresses from which logs should originate.
topics:Array of DATA, - (optional) Array of 32 Bytes DATA topics. Topics are order-dependent. Each topic can also be an array of DATA with "or" options.
blockhash:DATA, 32 Bytes - (optional, future) With the addition of EIP-234, blockHash will be a new filter option which restricts the logs returned to the single block with the 32-byte hash blockHash. Using blockHash is equivalent to fromBlock = toBlock = the block number with hash blockHash. If blockHash is present in the filter criteria, then neither fromBlock nor toBlock are allowed.
___________________Example of response____________________
[
{
✅address: '0x15ecf34ECDb72bAfd3DbA990D01E20338681f6dE',
✅blockNumber: 18776,
✅transactionHash: '0x42b4c699f613045f09a7201fe328a9a91843c0fafdb0bd1f5a22d13b964522bb',
✅transactionIndex: 0,
✅blockHash: '0xce26fb2518f4c79228c188132c996dea311c93da73cf934d630dd696e3f70181',
✅logIndex: 0,
✅removed: false,
✅id: 'log_b8492241',
⌛️returnValues: Result {
'0': 'Hello as argument',
'1': '1672832828',
payload: 'Hello as argument',
blocktime: '1672832828'
},
⌛️event:'Checkpoint',
⌛️signature: '0x5d882878f6c50530e63829854e64755332e385dbf9dd9c2798e07d9c88c67e40',
⌛️raw: {
data: '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000063b5673c000000000000000000000000000000000000000000000000000000000000001148656c6c6f20617320617267756d656e74000000000000000000000000000000',
topics: [Array]
}
},
...(next logs)
]
_____________________Add manually______________________
address - '0x'+result.receipt.logs[0][0].toString('hex') (NOTE: The first(0) element in each arrays in <logs> array is the appropriate contract address logs related to)
Example:
logs:[
[<address0>,<topics0>,<logs0>],
[<address1>,<topics1>,<logs1>],
...
]
blockNumber - block.number
transactionHash - '0x'+tx.hash().toString('hex')
transactionIndex - set manually(in hex)
blockHash - '0x'+block.hash().toString('hex')
logIndex - take from logs received from tx.receipt
removed - false(no chain reorganization )
id - 'log_00000000'
returnValues - take from web3.eth.abi.decodeLog(JSON.parse(ABI),logsInHex,topicsArrayInHex)
event - take from query
signature - event signature hash (topics[0])
raw: {
data:'0x'+logsInHex,
topics:topicsArrayInHex
}
______________Function parameters______________
[+] Logsmap - {contractAddress=>[logInstance0,logInstance1,...]}
[+] LogInstance has the following structure
{
address,
topics,
data,
blockNumber,
transactionHash:hash,
transactionIndex:'0x0',
blockHash,
logIndex:'0x0',
removed:false,
id:'log_00000000'
}
*/
if(!Array.isArray(logsMap[logInstance.address])) logsMap[logInstance.address]=[]
logsMap[logInstance.address].push(logInstance)
}
}
global.KLY_EVM = KLY_EVM