-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTestNodeServer.cs
1243 lines (1037 loc) · 54 KB
/
TestNodeServer.cs
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Meadow.Core.EthTypes;
using Meadow.JsonRpc;
using Meadow.JsonRpc.Server;
using Meadow.JsonRpc.Types;
using Meadow.EVM.Configuration;
using Meadow.EVM.Data_Types;
using Meadow.EVM.Data_Types.State;
using Meadow.EVM.Data_Types.Transactions;
using Meadow.EVM.EVM.Definitions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Meadow.EVM;
using Meadow.Core.Utils;
using Meadow.JsonRpc.Types.Debugging;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.Versioning;
using Meadow.Core.Cryptography.Ecdsa;
using Meadow.Core.Cryptography;
using Meadow.Core.RlpEncoding;
using System.Net;
using Meadow.Core.AccountDerivation;
using System.Threading;
using System.Collections.Concurrent;
namespace Meadow.TestNode
{
public class TestNodeServer : IRpcController, IDisposable
{
#region Fields
private long _currentSnapshotId;
private long _currentLogFilterId;
#endregion
#region Properties
public JsonRpcHttpServer RpcServer;
public TestNodeChain TestChain { get; }
public List<Address> AccountKeys { get; private set; }
public Dictionary<Address, EthereumEcdsa> AccountDictionary { get; private set; }
public Dictionary<ulong, (StateSnapshot snapshot, TimeSpan timeStampOffset)> Snapshots { get; private set; }
public Dictionary<ulong, FilterOptions> LogFilters { get; private set; }
public ConcurrentDictionary<ulong, (BigInteger Original, BigInteger Last)> NewBlockFilters { get; private set; } = new ConcurrentDictionary<ulong, (BigInteger Original, BigInteger Last)>();
#endregion
static TestNodeServer()
{
EthereumEcdsa.IncludeKeyDataInExceptions = true;
}
#region Constructor
/// <param name="port">If null or unspecified the http server binds to a random port.</param>
/// <param name="accountConfig">Configure number of accounts to generate, ether balance, wallet derivation method.</param>
public TestNodeServer(int? port = null, IPAddress address = null, AccountConfiguration accountConfig = null)
{
// Initialize our basic components.
RpcServer = new JsonRpcHttpServer(this, ConfigureWebHost, port, address);
AccountKeys = new List<Address>();
AccountDictionary = new Dictionary<Address, EthereumEcdsa>();
Snapshots = new Dictionary<ulong, (StateSnapshot snapshot, TimeSpan timeStampOffset)>();
LogFilters = new Dictionary<ulong, FilterOptions>();
// We create our genesis state by giving all of our genesis accounts a balance
State genesisState = new State();
// Set up a few accounts and give them balances in our genesis state.
accountConfig = (accountConfig ?? new AccountConfiguration());
BigInteger initialAccountBalance = new BigInteger(1e18M * accountConfig.DefaultAccountEtherBalance);
var accountDerivation = accountConfig.AccountDerivationMethod ?? HDAccountDerivation.Create();
// Generate a keypairs
foreach (var keypair in EthereumEcdsa.Generate(accountConfig.AccountGenerationCount, accountDerivation))
{
// Get an account from the public key hash.
Meadow.EVM.Data_Types.Addressing.Address account = new Meadow.EVM.Data_Types.Addressing.Address(keypair.GetPublicKeyHash());
Address frontEndAddress = new Address(account.ToByteArray());
// Set it in our lookup.
AccountKeys.Add(frontEndAddress);
AccountDictionary.Add(frontEndAddress, keypair);
// Set our account's balance in our genesis state
genesisState.SetBalance(account, initialAccountBalance);
}
// Commit our changes to the genesis state.
genesisState.CommitChanges();
// Create a configuration where we force the newest implemented version number. (And we provide our genesis state, and it's database so we can resolve items from it).
Configuration configuration = new Configuration(genesisState.Configuration.Database, EthereumRelease.Byzantium, null, genesisState);
// Set our chain ID
configuration.ChainID = (EthereumChainID)77;
// Set our genesis block difficulty (this is a special override case, if difficulty is 1, new difficulties will all be 1).
configuration.GenesisBlock.Header.Difficulty = 1;
// We disable ethash validation so we can process blocks quickly.
configuration.IgnoreEthashVerification = true;
// Set up our test chain
TestChain = new TestNodeChain(configuration);
}
#endregion
#region Functions
IWebHostBuilder ConfigureWebHost(IWebHostBuilder webHostBuilder)
{
return webHostBuilder.ConfigureLogging((hostingContext, logging) =>
{
//logging.AddFilter("System", LogLevel.Debug);
//logging.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Trace);
logging.SetMinimumLevel(LogLevel.Debug);
});
}
// Versioning
public Task<string> Version()
{
// Obtain our version as an integer.
// int version = (int)TestChain.Chain.Configuration.Version;
// TODO: investigate this..
// Other node implementations return chainID for version for clients to query and use for signing raw transactions,
// since eth_chainId is not implemented or enabled on all other implementations for some reason.
int version = (int)TestChain.Chain.Configuration.ChainID;
return Task.FromResult(version.ToString(CultureInfo.InvariantCulture));
}
// Accounts
public Task<Address[]> Accounts()
{
// Obtain our controlled accounts.
return Task.FromResult(AccountKeys.ToArray());
}
public Task<UInt256> GetBalance(Address account, DefaultBlockParameter blockParameter)
{
// Obtain our account address
Meadow.EVM.Data_Types.Addressing.Address accountAddress = new Meadow.EVM.Data_Types.Addressing.Address(account.GetBytes());
// Obtain our balance from our state.
BigInteger accountBalance = TestChain.Chain.State.GetBalance(accountAddress);
// Return the balance.
return Task.FromResult(new UInt256(accountBalance));
}
public Task<byte[]> GetCode(Address address, DefaultBlockParameter blockParameter)
{
// We want the state from this block parameter.
State postBlockState = GetStateFromBlockParameters(blockParameter);
if (postBlockState == null)
{
return Task.FromResult((byte[])null);
}
// Obtain our code for the account
return Task.FromResult(postBlockState.GetCodeSegment(new Meadow.EVM.Data_Types.Addressing.Address(address.GetBytes())));
}
// Chain
public Task<ulong> IncreaseTime(ulong seconds)
{
// We offset our time by the given amount of seconds.
TestChain.Chain.Configuration.CurrentTimestampOffset += TimeSpan.FromSeconds(seconds);
return Task.FromResult(seconds);
}
public Task<UInt256> GasPrice()
{
// Return our minimum gas price.
return Task.FromResult(new UInt256(TestChain.MinimumGasPrice));
}
public Task<Address> Coinbase()
{
// Return our address
return Task.FromResult(new Address(TestChain.Coinbase.ToByteArray()));
}
public Task<ulong> ChainID()
{
return Task.FromResult((ulong)(int)TestChain.Chain.Configuration.ChainID);
}
public Task Mine()
{
// We force the mining of a block.
TestChain.MiningUpdate(true);
return Task.CompletedTask;
}
// Block Information
public Task<ulong> BlockNumber()
{
// Obtain our most recent block number for our current state.
return Task.FromResult((ulong)TestChain.Chain.State.CurrentBlock.Header.BlockNumber);
}
public Task<Block> GetBlockByHash(Hash hash, bool getFullTransactionObjects)
{
// Obtain the block by hash
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(hash.GetBytes());
return Task.FromResult(JsonTypeConverter.CoreBlockToJsonBlock(TestChain, block));
}
public Task<Block> GetBlockByNumber(DefaultBlockParameter blockParameter, bool getFullTransactionObjects)
{
// Obtain the block by hash
ulong blockNumber = (ulong)BlockNumberFromBlockParameters(blockParameter);
// Obtain the block hash for this block number.
byte[] blockHash = TestChain.Chain.GetBlockHashFromBlockNumber(blockNumber);
// Obtain the block using the hash.
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash);
// Return the block
return Task.FromResult(JsonTypeConverter.CoreBlockToJsonBlock(TestChain, block));
}
// Transaction Information
public Task<TransactionObject> GetTransactionByHash(Hash transactionHash)
{
// Obtain our transaction from a hash
var result = TestChain.Chain.GetTransactionPosition(transactionHash.GetBytes());
// TODO: If result is null, meaning the transaction hash is not known to the chain.
// Obtain the block for this transaction
byte[] blockHash = TestChain.Chain.GetBlockHashFromBlockNumber(result.Value.blockNumber);
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash);
// Return our transaction.
return Task.FromResult(JsonTypeConverter.CoreTransactionToJsonTransaction(block, (ulong)result.Value.transactionIndex));
}
public Task<TransactionObject> GetTransactionByBlockHashAndIndex(Hash blockHash, ulong transactionIndex)
{
// Obtain the block for this block hash.
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash.GetBytes());
// TODO: If block is null
// TODO: If transaction index is out of bounds.
// Return our transaction.
return Task.FromResult(JsonTypeConverter.CoreTransactionToJsonTransaction(block, transactionIndex));
}
public Task<ulong> GetBlockTransactionCountByHash(Hash blockHash)
{
// Obtain the block for this block hash.
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash.GetBytes());
// If the block is null we'll say it has zero transactions.
if (block == null)
{
return Task.FromResult((ulong)0);
}
// Return our transaction count.
return Task.FromResult((ulong)block.Transactions.Length);
}
public Task<ulong> GetBlockTransactionCountByNumber(DefaultBlockParameter blockParameter)
{
// Obtain the block
BigInteger blockNumber = BlockNumberFromBlockParameters(blockParameter);
// Obtain our block from block number.
byte[] blockHash = TestChain.Chain.GetBlockHashFromBlockNumber(blockNumber);
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash);
// Return our transaction count.
return Task.FromResult((ulong)block.Transactions.Length);
}
public Task<ulong> GetTransactionCount(Address address, DefaultBlockParameter blockParameter)
{
// Obtain the state for the block parameter
State state = GetStateFromBlockParameters(blockParameter);
if (state == null)
{
return Task.FromResult((ulong)0);
}
// Obtain the account's nonce for that address from that state. (The nonce is incremented for every transaction).
BigInteger nonce = state.GetNonce(new Meadow.EVM.Data_Types.Addressing.Address(address.GetBytes()));
return Task.FromResult((ulong)(nonce - TestChain.Chain.Configuration.InitialAccountNonce));
}
// Calls/Transactions
public Task<Hash> SendRawTransaction(byte[] signedData)
{
// We assume the signed data is RLP encoded.
RLPItem rlpTransaction = RLP.Decode(signedData);
Meadow.EVM.Data_Types.Transactions.Transaction transaction = new Meadow.EVM.Data_Types.Transactions.Transaction(rlpTransaction);
// Obtain our address we can presume the contract will be at once deployed (or if its already deployed)
var sender = transaction.GetSenderAddress();
var targetInformation = GetTransactionTargetInformation(sender, transaction.To);
// Process the transaction and return the result
return ProcessTransactionInternal(transaction, targetInformation.deploying, targetInformation.targetDeployedAddress);
}
public Task<UInt256> EstimateGas(CallParams callParams, DefaultBlockParameter blockParameter)
{
// Verify our block parameters are for the latest block.
if (blockParameter.ParameterType == BlockParameterType.Earliest ||
blockParameter.ParameterType == BlockParameterType.Pending)
{
return Task.FromException<UInt256>(new NotImplementedException("EstimateGas does not support estimations at earlier or pending points in the chain."));
}
if (blockParameter.ParameterType == BlockParameterType.BlockNumber && TestChain.Chain.State.CurrentBlock.Header.BlockNumber != blockParameter.BlockNumber)
{
return Task.FromException<UInt256>(new NotImplementedException("EstimateGas only supports estimations at the current block number in the chain."));
}
// Snapshot our state at this height in block
ulong snapshotID = Snapshot().Result;
// Define our resulting transaction receipt and exception (in case one occurs)
TransactionReceipt transactionReceipt = null;
Exception transactionException = null;
// Try to process our transaction and obtain the receipt.
try
{
// Process the transaction and obtain the transaction hash.
var transactionHash = SendTransaction(new TransactionParams()
{
From = callParams.From,
To = callParams.To,
Data = callParams.Data,
Gas = callParams.Gas,
GasPrice = callParams.GasPrice,
Nonce = null,
Value = callParams.Value
}).Result;
// Obtain our transaction receipt.
transactionReceipt = GetTransactionReceipt(transactionHash).Result;
}
catch (Exception ex)
{
// An exception occurred, so we set our transaction exception.
transactionException = ex;
}
// Revert to our previous state
Revert(snapshotID);
// Remove the snapshot from our lookup
Snapshots.Remove(snapshotID);
// If our exception is not null, return it
if (transactionException != null)
{
return Task.FromException<UInt256>(transactionException);
}
// Return our gas used.
return Task.FromResult((UInt256)transactionReceipt.GasUsed);
}
public Task<Hash> SendTransaction(TransactionParams transactionParams)
{
// Create our default parameters
Meadow.EVM.Data_Types.Addressing.Address to = new Meadow.EVM.Data_Types.Addressing.Address(0);
if (transactionParams.To.HasValue)
{
to = new Meadow.EVM.Data_Types.Addressing.Address(transactionParams.To.Value.GetBytes());
}
// Obtain some information which helps us build our contract and determine deployment address if deploying.
var sender = new Meadow.EVM.Data_Types.Addressing.Address(transactionParams.From.Value.GetBytes());
var targetInformation = GetTransactionTargetInformation(sender, to);
// Create our transaction using the provided parameters and some fallback options.
Meadow.EVM.Data_Types.Transactions.Transaction transaction = new Meadow.EVM.Data_Types.Transactions.Transaction(
transactionParams.Nonce ?? targetInformation.senderNonce,
(BigInteger?)transactionParams.GasPrice ?? TestChain.MinimumGasPrice,
(BigInteger?)transactionParams.Gas ?? GasDefinitions.CalculateGasLimit(TestChain.Chain.GetHeadBlock().Header, TestChain.Chain.Configuration),
to,
(BigInteger?)transactionParams.Value ?? 0,
transactionParams.Data ?? Array.Empty<byte>());
// Obtain the provided accounts keypair
EthereumEcdsa keypair = AccountDictionary[transactionParams.From.Value];
// If we're past the spurious dragon fork, we begin using chain ID.
EthereumChainID? chainID = null;
if (TestChain.Chain.Configuration.Version >= EthereumRelease.SpuriousDragon)
{
chainID = TestChain.Chain.Configuration.ChainID;
}
// Sign the transaction with this account's keypair
transaction.Sign(keypair, chainID);
// Process the transaction and return the result
return ProcessTransactionInternal(transaction, targetInformation.deploying, targetInformation.targetDeployedAddress);
}
public Task<byte[]> Call(CallParams callParams, DefaultBlockParameter blockParameter)
{
// Obtain a state from this block parameter.
State postBlockState = GetStateFromBlockParameters(blockParameter);
if (postBlockState == null)
{
return Task.FromResult((byte[])null);
}
// Execute and obtain our result
Meadow.EVM.EVM.Execution.EVMExecutionResult result = HandleCall(callParams, postBlockState);
// TODO: After a typical/expected revert, end up with an unthrown Exception object
Exception ex = TestChain.Chain.Configuration.DebugConfiguration.Error;
if (ex != null)
{
return Task.FromException<byte[]>(ex);
}
// Return the return data
return Task.FromResult(result.ReturnData.ToArray());
}
public Task<TransactionReceipt> GetTransactionReceipt(Hash transactionHash)
{
// Create a receipt instance
TransactionReceipt receipt = new TransactionReceipt();
// Obtain our block and transaction index from our hash
var result = TestChain.Chain.GetTransactionPosition(transactionHash.GetBytes());
if (!result.HasValue)
{
return Task.FromResult<TransactionReceipt>(null);
}
// Obtain the block for this transaction
byte[] blockHash = TestChain.Chain.GetBlockHashFromBlockNumber(result.Value.blockNumber);
Meadow.EVM.Data_Types.Block.Block block = TestChain.Chain.GetBlock(blockHash);
// Obtain our transaction and receipt.
int transactionIndex = (int)result.Value.transactionIndex;
Meadow.EVM.Data_Types.Transactions.Transaction transaction = block.Transactions[(int)result.Value.transactionIndex];
Meadow.EVM.Data_Types.Transactions.Receipt transactionReceipt = State.GetReceipt(block.Header, transactionIndex, TestChain.Chain.Configuration.Database);
// Obtain our contract address (if we send this transaction to the create contract address)
Meadow.EVM.Data_Types.Addressing.Address contractAddress = GetDeployedAddress(transaction.To, transaction.GetSenderAddress(), blockHash);
// Create our filter log array
FilterLogObject[] jsonLogs = new FilterLogObject[transactionReceipt.Logs.Count];
for (int i = 0; i < jsonLogs.Length; i++)
{
// Grab the current log
Meadow.EVM.Data_Types.Transactions.Log log = transactionReceipt.Logs[i];
// Set it in our array
jsonLogs[i] = JsonTypeConverter.CoreLogToJsonLog(log, i, (ulong)result.Value.blockNumber, (ulong)result.Value.transactionIndex, transactionHash, new Hash(blockHash));
}
// Determine if we succeeded. After byzantium, a single byte "1" is success, a blank array is fail. Otherwise it is just the actual state root hash.
bool transactionSucceeded = ((transactionReceipt.StateRoot.Length == 1 && transactionReceipt.StateRoot[0] == 1) || transactionReceipt.StateRoot.Length == KeccakHash.HASH_SIZE);
// Convert our receipt to our json format
TransactionReceipt jsonReceipt = new TransactionReceipt()
{
BlockHash = new Hash(block.Header.GetHash()),
BlockNumber = (ulong)result.Value.blockNumber,
TransactionIndex = (ulong)result.Value.transactionIndex,
TransactionHash = new Hash(transaction.GetHash()),
CumulativeGasUsed = (ulong)transactionReceipt.GasUsed,
LogsBloom = BigIntegerConverter.GetBytes(transactionReceipt.Bloom, EVMDefinitions.BLOOM_FILTER_SIZE),
GasUsed = (ulong)transactionReceipt.GasUsed,
Status = (ulong)(transactionSucceeded ? 1 : 0),
Logs = jsonLogs,
};
// If we have a contract address, set it
if (contractAddress != null)
{
jsonReceipt.ContractAddress = new Address(contractAddress.ToByteArray());
}
// Return our receipt.
return Task.FromResult(jsonReceipt);
}
public Task<ulong> NewBlockFilter()
{
var filterID = (ulong)Interlocked.Increment(ref _currentLogFilterId);
var curBlock = TestChain.Chain.State.CurrentBlock.Header.BlockNumber; // TestChain.Chain.GetHeadBlock().Header.BlockNumber;
NewBlockFilters[filterID] = (curBlock, curBlock);
return Task.FromResult(filterID);
}
// Filters/Logs
public Task<LogObjectResult> GetFilterChanges(ulong filterID)
{
if (NewBlockFilters.TryGetValue(filterID, out var entry))
{
var result = new LogObjectResult
{
ResultType = LogObjectResultType.Hashes
};
var curBlock = TestChain.Chain.State.CurrentBlock.Header.BlockNumber;
var lastRequestBlock = entry.Last;
// Since we instant-mine blocks on transaction, and truffle & web3
// setup the filter *after* the transaction, we artificially set
// the block number back by one. This is a dirty hack but Ganache
// also does this..
if (lastRequestBlock > 0)
{
lastRequestBlock--;
}
if (curBlock > lastRequestBlock)
{
var newBlockCount = curBlock - lastRequestBlock;
result.Hashes = new Hash[(int)newBlockCount];
for (var i = 0; i < newBlockCount; i++)
{
var blockHashBytes = TestChain.Chain.GetBlockHashFromBlockNumber(lastRequestBlock + i + 1);
result.Hashes[i] = new Hash(blockHashBytes);
}
NewBlockFilters[filterID] = (entry.Original, curBlock);
}
else
{
result.Hashes = Array.Empty<Hash>();
}
return Task.FromResult(result);
}
// Obtain our filter
if (LogFilters.TryGetValue(filterID, out var filterOptions))
{
// TODO: this always returns all changes...
// Need to store a delta (block number?) after each requset to provide deltas since last request.
// Obtain our logs.
return GetLogs(filterOptions);
}
return Task.FromResult(new LogObjectResult());
// TODO: Implement
throw new NotImplementedException();
}
public Task<LogObjectResult> GetFilterLogs(ulong filterID)
{
// Obtain our filter
if (LogFilters.TryGetValue(filterID, out var filterOptions))
{
// Obtain our logs.
return GetLogs(filterOptions);
}
if (NewBlockFilters.TryGetValue(filterID, out var entry))
{
var result = new LogObjectResult
{
ResultType = LogObjectResultType.Hashes
};
var curBlock = TestChain.Chain.State.CurrentBlock.Header.BlockNumber;
if (curBlock > entry.Original)
{
var newBlockCount = curBlock - entry.Original;
result.Hashes = new Hash[(int)newBlockCount];
for (var i = 0; i < newBlockCount; i++)
{
var blockHashBytes = TestChain.Chain.GetBlockHashFromBlockNumber(entry.Original + i + 1);
result.Hashes[i] = new Hash(blockHashBytes);
}
}
else
{
result.Hashes = Array.Empty<Hash>();
}
return Task.FromResult(result);
}
// The filter id doesn't exist
return Task.FromResult<LogObjectResult>(null);
}
private bool CheckAddressFilter(BigInteger bloom, FilterOptions filterOptions)
{
// If we have no filter options, we include everything.
// If we have address filters, we verify them now.
if (filterOptions.Address != null)
{
// For each address to filter.
bool foundInclusion = false;
foreach (var addr in filterOptions.Address)
{
// Obtain the address as an EVM address type.
var filterAddress = new Meadow.EVM.Data_Types.Addressing.Address(addr.GetBytes());
// Generate a bloom filter from the given filter address.
BigInteger addressBloomFilter = BloomFilter.Generate(filterAddress, Meadow.EVM.Data_Types.Addressing.Address.ADDRESS_SIZE);
// Check if we found inclusion in this set
if (BloomFilter.Check(bloom, addressBloomFilter))
{
// Set our inclusion and exit
foundInclusion = true;
break;
}
}
// If we couldn't satisfy the address filter on this bloom filter, we return false.
return foundInclusion;
}
// If we have no address filters, we
return true;
}
public Task<LogObjectResult> GetLogs(FilterOptions filterOptions)
{
// We create our bloom filter for these filter options
BigInteger baseBloomFilter = 0;
if (filterOptions.Topics != null)
{
for (int i = 0; i < filterOptions.Topics.Length; i++)
{
if (filterOptions.Topics[i] != null)
{
// Obtain our topic value.
BigInteger topicValue = BigIntegerConverter.GetBigInteger(filterOptions.Topics[i].Value.GetBytes());
// Add it to our bloom filter
baseBloomFilter |= BloomFilter.Generate(topicValue);
}
}
}
// Obtain our bounds.
BigInteger startBlockNumber = BlockNumberFromBlockParameters(filterOptions.FromBlock);
BigInteger endBlockNumber = BlockNumberFromBlockParameters(filterOptions.ToBlock);
// Create our Log Object Result
LogObjectResult result = new LogObjectResult();
result.ResultType = LogObjectResultType.LogObjects;
// Loop for all blocks
List<FilterLogObject> jsonLogs = new List<FilterLogObject>();
for (BigInteger i = startBlockNumber; i <= endBlockNumber; i++)
{
// Obtain the block at this index.
var blockHash = TestChain.Chain.GetBlockHashFromBlockNumber(i);
var block = TestChain.Chain.GetBlock(blockHash);
// Verify the block's bloom filter with address and topics
if (!BloomFilter.Check(block.Header.Bloom, baseBloomFilter) || !CheckAddressFilter(block.Header.Bloom, filterOptions))
{
continue;
}
// Obtain all the receipts for this block
Meadow.EVM.Data_Types.Transactions.Transaction[] transactions = block.Transactions;
Meadow.EVM.Data_Types.Transactions.Receipt[] receipts = State.GetReceipts(block, TestChain.Chain.Configuration.Database);
// Loop through all the receipts
for (int j = 0; j < receipts.Length; j++)
{
// Obtain our receipt
var receipt = receipts[j];
// Verify the receipt's bloom filter with address and topics
if (!BloomFilter.Check(receipt.Bloom, baseBloomFilter) || !CheckAddressFilter(receipt.Bloom, filterOptions))
{
break;
}
// Obtain our transaction hash
byte[] transactionHash = transactions[j].GetHash();
// Loop through the logs
for (int x = 0; x < receipt.Logs.Count; x++)
{
// If the filter address exists, and our receipt address doesn't match, we skip.
if (filterOptions.Address != null && !filterOptions.Address.Contains(new Address(receipt.Logs[x].Address.ToByteArray())))
{
continue;
}
// Next we'll want to filter the topics.
// TODO: The C# filter topic type might not support multiple-to-one matches, for the "OR" case. Revisit this.
bool skip = false;
for (int t = 0; t < filterOptions.Topics.Length && !skip; t++)
{
// If we have a filter for this argument.
if (filterOptions.Topics[t].HasValue)
{
// Obtain this indexed topic
byte[] topicData = BigIntegerConverter.GetBytes(receipt.Logs[x].Topics[t]);
if (!topicData.ValuesEqual(filterOptions.Topics[t].Value.GetBytes()))
{
skip = true;
}
}
}
// If we should skip it, skip
if (skip)
{
continue;
}
// Otherwise we add this log to our log list.
jsonLogs.Add(JsonTypeConverter.CoreLogToJsonLog(receipt.Logs[x], x, (ulong)block.Header.BlockNumber, (ulong)j, new Hash(transactionHash), new Hash(blockHash)));
}
}
}
// Set our result's log objects
result.LogObjects = jsonLogs.ToArray();
// Return our result
return Task.FromResult(result);
}
public Task<ulong> NewFilter(FilterOptions filterOptions)
{
// Set our filter in our dictionary
ulong filterId = (ulong)Interlocked.Increment(ref _currentLogFilterId);
LogFilters[filterId] = filterOptions;
// Return filter id
return Task.FromResult(filterId);
}
public Task<bool> UninstallFilter(ulong filterId)
{
if (LogFilters.Remove(filterId))
{
return Task.FromResult(true);
}
else if (NewBlockFilters.TryRemove(filterId, out _))
{
return Task.FromResult(true);
}
return Task.FromResult(false);
}
// Snapshot/Revert
public Task<ulong> Snapshot()
{
// Obtain our snapshot id and increment it.
ulong snapshotId = (ulong)Interlocked.Increment(ref _currentSnapshotId);
// Set our snapshot in our snapshot lookup.
var snapshot = TestChain.Chain.State.Snapshot();
var timeStampOffset = TestChain.Chain.Configuration.CurrentTimestampOffset;
Snapshots[snapshotId] = (snapshot, timeStampOffset);
// Return the snapshot id.
return Task.FromResult(snapshotId);
}
public Task<bool> Revert(ulong snapshotID)
{
// Verify our snapshot ID is in our lookup
if (Snapshots.TryGetValue(snapshotID, out var snapshotComponents))
{
// Revert the chain to this snapshot
TestChain.Chain.Revert(snapshotComponents.snapshot);
// Set our time stamp offset
TestChain.Chain.Configuration.CurrentTimestampOffset = snapshotComponents.timeStampOffset;
// Return our success status
return Task.FromResult(true);
}
return Task.FromResult(false);
}
// Cryptography
public Task<byte[]> Sign(Address account, byte[] message)
{
// If our account isn't in our list of accounts, return null.
if (!AccountDictionary.TryGetValue(account, out var keypair))
{
return null;
}
// Note: There are very many implementations of eth_sign across applications.
// We base ours off of Geth, which apparently uses r,s,v.
// (Source: https://github.com/paritytech/parity/issues/5490)
// (More: https://github.com/ethereum/go-ethereum/pull/2940)
// We create our prepended/generated message
byte[] prependedMessage = UTF8Encoding.UTF8.GetBytes("\x19Ethereum Signed Message:\n");
byte[] messageLength = UTF8Encoding.UTF8.GetBytes(message.Length.ToString(CultureInfo.InvariantCulture));
// Calculate our hash from our concatted message
byte[] hash = KeccakHash.ComputeHashBytes(prependedMessage.Concat(messageLength, message));
// Sign the hash
var signature = keypair.SignData(hash);
// We want our result in r,s,v format.
byte[] r = BigIntegerConverter.GetBytes(signature.r);
byte[] s = BigIntegerConverter.GetBytes(signature.s);
byte[] v = new byte[] { EthereumEcdsa.GetVFromRecoveryID(null, signature.RecoveryID) };
// Concat all pieces together and return.
byte[] result = r.Concat(s, v);
return Task.FromResult(result);
}
public Task<Hash> Sha3(byte[] data)
{
// Compute a keccak 256 digest on the data.
return Task.FromResult(new Hash(KeccakHash.ComputeHashBytes(data)));
}
// Unimplemented
public Task<string> ProtocolVersion()
{
// TODO: Implement
throw new NotImplementedException();
}
public Task<string> ClientVersion()
{
var runtime = Assembly.GetEntryAssembly().GetCustomAttribute<TargetFrameworkAttribute>().FrameworkName;
var version = GetType().Assembly.GetName().Version;
var os = RuntimeInformation.OSDescription.Trim().Replace(' ', '_');
var result = $"Meadow-TestServer/v{version}/{os}/{runtime}";
return Task.FromResult(result);
}
public Task<SyncStatus> Syncing()
{
// TODO: Implement
var result = new SyncStatus { IsSyncing = false, CurrentBlock = 999, HighestBlock = 2124, StartingBlock = 2 };
return Task.FromResult(result);
}
public Task<string[]> GetCompilers()
{
// TODO: Implement
throw new NotImplementedException();
}
public Task<ulong> PeerCount()
{
throw new NotImplementedException();
}
public Task<bool> Listening()
{
throw new NotImplementedException();
}
public Task<bool> Mining()
{
throw new NotImplementedException();
}
public Task<UInt256> HashRate()
{
throw new NotImplementedException();
}
public Task SetContractSizeCheckDisabled(bool enabled)
{
if (TestChain?.Chain?.Configuration?.DebugConfiguration != null)
{
TestChain.Chain.Configuration.DebugConfiguration.IsContractSizeCheckDisabled = enabled;
}
return Task.CompletedTask;
}
// Tracing
public Task SetTracingEnabled(bool enabled)
{
// Set our coverage collection enabled status.
if (TestChain?.Chain?.Configuration?.DebugConfiguration != null)
{
TestChain.Chain.Configuration.DebugConfiguration.IsTracing = enabled;
}
return Task.CompletedTask;
}
public Task<byte[]> GetHashPreimage(byte[] hash)
{
// Verify our hash is not null
if (hash == null)
{
return Task.FromException<byte[]>(new ArgumentNullException("Could not obtain pre-image because the given hash was null."));
}
// Try to obtain the pre-image
byte[] preimage = null;
if (TestChain?.Chain?.Configuration?.DebugConfiguration?.TryGetPreimage(hash, out preimage) == true)
{
return Task.FromResult(preimage);
}
else
{
return Task.FromResult<byte[]>(null);
}
}
public Task<ExecutionTrace> GetExecutionTrace()
{
// Verify we have an execution trace.
if (TestChain?.Chain?.Configuration?.DebugConfiguration.IsTracing != true || TestChain?.Chain?.Configuration?.DebugConfiguration.ExecutionTrace == null)
{
return Task.FromResult<ExecutionTrace>(null);
}
// Return our converted execution trace.
ExecutionTrace executionTrace = JsonTypeConverter.CoreExecutionTraceJsonExecutionTrace(TestChain.Chain.Configuration.DebugConfiguration.ExecutionTrace);
return Task.FromResult(executionTrace);
}
// Coverage
public Task SetCoverageEnabled(bool enabled)
{
// Set our coverage collection enabled status.
if (TestChain?.Chain?.Configuration?.CodeCoverage != null)
{
TestChain.Chain.Configuration.CodeCoverage.Enabled = enabled;
}
return Task.CompletedTask;
}
public Task<CompoundCoverageMap> GetCoverageMap(Address contractAddress)
{
// Verify our coverage collection configuration exists.
if (TestChain?.Chain?.Configuration?.CodeCoverage == null)
{
return Task.FromResult((CompoundCoverageMap)null);
}
// We try to obtain the code coverage map for this address.
var coverageMap = TestChain.Chain.Configuration.CodeCoverage.Get(new Meadow.EVM.Data_Types.Addressing.Address(contractAddress.GetBytes()));
// And return the coverage map
CompoundCoverageMap result = JsonTypeConverter.CoreCompoundCoverageMapsToJsonCompoundCoverageMaps(coverageMap);
return Task.FromResult(result);
}
public Task<CompoundCoverageMap[]> GetAllCoverageMaps()
{
// Verify our coverage collection configuration exists.
if (TestChain?.Chain?.Configuration?.CodeCoverage == null)
{
return Task.FromResult(Array.Empty<CompoundCoverageMap>());
}
// We try to obtain all coverage maps.
var coverageMaps = TestChain.Chain.Configuration.CodeCoverage.GetAll();
if (coverageMaps == null)
{
return Task.FromResult(Array.Empty<CompoundCoverageMap>());
}
// Now we obtain internal data from that.
CompoundCoverageMap[] result = new CompoundCoverageMap[coverageMaps.Length];
for (int i = 0; i < result.Length; i++)
{