11
11
12
12
using Akka . Actor ;
13
13
using Akka . Util . Internal ;
14
+ using FluentAssertions ;
14
15
using Microsoft . VisualStudio . TestTools . UnitTesting ;
16
+ using Neo . Cryptography . ECC ;
15
17
using Neo . IO ;
16
18
using Neo . Json ;
17
19
using Neo . Ledger ;
23
25
using Neo . UnitTests . Extensions ;
24
26
using System ;
25
27
using System . Linq ;
28
+ using System . Security . Policy ;
29
+ using static Neo . SmartContract . Native . NeoToken ;
26
30
27
31
namespace Neo . Plugins . RpcServer . Tests
28
32
{
@@ -61,6 +65,11 @@ public void TestGetBlockByHash()
61
65
{
62
66
Assert . AreEqual ( VerifyResult . Succeed , tx . VerifyStateIndependent ( UnitTests . TestProtocolSettings . Default ) ) ;
63
67
} ) ;
68
+
69
+ result = _rpcServer . GetBlock ( new BlockHashOrIndex ( block . Hash ) , true ) ;
70
+ var block3 = block . ToJson ( UnitTests . TestProtocolSettings . Default ) ;
71
+ block3 [ "confirmations" ] = NativeContract . Ledger . CurrentIndex ( snapshot ) - block . Index + 1 ;
72
+ result . ToString ( ) . Should ( ) . Be ( block3 . ToString ( ) ) ;
64
73
}
65
74
66
75
[ TestMethod ]
@@ -78,6 +87,11 @@ public void TestGetBlockByIndex()
78
87
{
79
88
Assert . AreEqual ( VerifyResult . Succeed , tx . VerifyStateIndependent ( UnitTests . TestProtocolSettings . Default ) ) ;
80
89
} ) ;
90
+
91
+ result = _rpcServer . GetBlock ( new BlockHashOrIndex ( block . Index ) , true ) ;
92
+ var block3 = block . ToJson ( UnitTests . TestProtocolSettings . Default ) ;
93
+ block3 [ "confirmations" ] = NativeContract . Ledger . CurrentIndex ( snapshot ) - block . Index + 1 ;
94
+ result . ToString ( ) . Should ( ) . Be ( block3 . ToString ( ) ) ;
81
95
}
82
96
83
97
[ TestMethod ]
@@ -120,6 +134,11 @@ public void TestGetBlockHeader()
120
134
var header = block . Header . ToJson ( _neoSystem . Settings ) ;
121
135
header [ "confirmations" ] = NativeContract . Ledger . CurrentIndex ( snapshot ) - block . Index + 1 ;
122
136
Assert . AreEqual ( header . ToString ( ) , result . ToString ( ) ) ;
137
+
138
+ result = _rpcServer . GetBlockHeader ( new BlockHashOrIndex ( block . Hash ) , false ) ;
139
+ var headerArr = Convert . FromBase64String ( result . AsString ( ) ) ;
140
+ var header2 = headerArr . AsSerializable < Header > ( ) ;
141
+ header2 . ToJson ( _neoSystem . Settings ) . ToString ( ) . Should ( ) . Be ( block . Header . ToJson ( _neoSystem . Settings ) . ToString ( ) ) ;
123
142
}
124
143
125
144
[ TestMethod ]
@@ -129,9 +148,23 @@ public void TestGetContractState()
129
148
var contractState = TestUtils . GetContract ( ) ;
130
149
snapshot . AddContract ( contractState . Hash , contractState ) ;
131
150
snapshot . Commit ( ) ;
151
+
132
152
var result = _rpcServer . GetContractState ( new ContractNameOrHashOrId ( contractState . Hash ) ) ;
153
+ Assert . AreEqual ( contractState . ToJson ( ) . ToString ( ) , result . ToString ( ) ) ;
133
154
155
+ result = _rpcServer . GetContractState ( new ContractNameOrHashOrId ( contractState . Id ) ) ;
134
156
Assert . AreEqual ( contractState . ToJson ( ) . ToString ( ) , result . ToString ( ) ) ;
157
+
158
+ var byId = _rpcServer . GetContractState ( new ContractNameOrHashOrId ( - 1 ) ) ;
159
+ var byName = _rpcServer . GetContractState ( new ContractNameOrHashOrId ( "ContractManagement" ) ) ;
160
+ byId . ToString ( ) . Should ( ) . Be ( byName . ToString ( ) ) ;
161
+
162
+ snapshot . DeleteContract ( contractState . Hash ) ;
163
+ snapshot . Commit ( ) ;
164
+ Action act = ( ) => _rpcServer . GetContractState ( new ContractNameOrHashOrId ( contractState . Hash ) ) ;
165
+ act . Should ( ) . Throw < RpcException > ( ) . WithMessage ( RpcError . UnknownContract . Message ) ;
166
+ act = ( ) => _rpcServer . GetContractState ( new ContractNameOrHashOrId ( contractState . Id ) ) ;
167
+ act . Should ( ) . Throw < RpcException > ( ) . WithMessage ( RpcError . UnknownContract . Message ) ;
135
168
}
136
169
137
170
[ TestMethod ]
@@ -143,8 +176,10 @@ public void TestGetRawMemPool()
143
176
_neoSystem . MemPool . TryAdd ( tx , snapshot ) ;
144
177
145
178
var result = _rpcServer . GetRawMemPool ( ) ;
146
-
147
179
Assert . IsTrue ( ( ( JArray ) result ) . Any ( p => p . AsString ( ) == tx . Hash . ToString ( ) ) ) ;
180
+
181
+ result = _rpcServer . GetRawMemPool ( true ) ;
182
+ Assert . IsTrue ( ( ( JArray ) result [ "verified" ] ) . Any ( p => p . AsString ( ) == tx . Hash . ToString ( ) ) ) ;
148
183
}
149
184
150
185
[ TestMethod ]
@@ -154,10 +189,14 @@ public void TestGetRawTransaction()
154
189
var tx = TestUtils . CreateValidTx ( snapshot , _wallet , _walletAccount ) ;
155
190
_neoSystem . MemPool . TryAdd ( tx , snapshot ) ;
156
191
snapshot . Commit ( ) ;
157
- var result = _rpcServer . GetRawTransaction ( tx . Hash , true ) ;
158
192
193
+ var result = _rpcServer . GetRawTransaction ( tx . Hash , true ) ;
159
194
var json = Utility . TransactionToJson ( tx , _neoSystem . Settings ) ;
160
195
Assert . AreEqual ( json . ToString ( ) , result . ToString ( ) ) ;
196
+
197
+ result = _rpcServer . GetRawTransaction ( tx . Hash , false ) ;
198
+ var tx2 = Convert . FromBase64String ( result . AsString ( ) ) . AsSerializable < Transaction > ( ) ;
199
+ tx2 . ToJson ( _neoSystem . Settings ) . ToString ( ) . Should ( ) . Be ( tx . ToJson ( _neoSystem . Settings ) . ToString ( ) ) ;
161
200
}
162
201
163
202
[ TestMethod ]
@@ -197,6 +236,15 @@ public void TestFindStorage()
197
236
json [ "next" ] = 1 ;
198
237
json [ "results" ] = jarr ;
199
238
Assert . AreEqual ( json . ToString ( ) , result . ToString ( ) ) ;
239
+
240
+ var result2 = _rpcServer . FindStorage ( new ContractNameOrHashOrId ( contractState . Hash ) , Convert . ToBase64String ( key ) ) ;
241
+ result2 . ToString ( ) . Should ( ) . Be ( result . ToString ( ) ) ;
242
+
243
+ Enumerable . Range ( 0 , 51 ) . ToList ( ) . ForEach ( i => TestUtils . StorageItemAdd ( snapshot , contractState . Id , new byte [ ] { 0x01 , ( byte ) i } , new byte [ ] { 0x02 } ) ) ;
244
+ snapshot . Commit ( ) ;
245
+ var result4 = _rpcServer . FindStorage ( new ContractNameOrHashOrId ( contractState . Hash ) , Convert . ToBase64String ( new byte [ ] { 0x01 } ) , 0 ) ;
246
+ result4 [ "next" ] . Should ( ) . Be ( RpcServerSettings . Default . FindStoragePageSize ) ;
247
+ ( result4 [ "truncated" ] ) . AsBoolean ( ) . Should ( ) . Be ( true ) ;
200
248
}
201
249
202
250
[ TestMethod ]
@@ -232,12 +280,16 @@ public void TestGetNextBlockValidators()
232
280
public void TestGetCandidates ( )
233
281
{
234
282
var snapshot = _neoSystem . GetSnapshotCache ( ) ;
283
+
235
284
var result = _rpcServer . GetCandidates ( ) ;
236
285
var json = new JArray ( ) ;
237
286
var validators = NativeContract . NEO . GetNextBlockValidators ( snapshot , _neoSystem . Settings . ValidatorsCount ) ;
287
+
288
+ var key = new KeyBuilder ( NativeContract . NEO . Id , 33 ) . Add ( ECPoint . Parse ( "02237309a0633ff930d51856db01d17c829a5b2e5cc2638e9c03b4cfa8e9c9f971" , ECCurve . Secp256r1 ) ) ;
289
+ snapshot . Add ( key , new StorageItem ( new CandidateState ( ) { Registered = true , Votes = 10000 } ) ) ;
238
290
snapshot . Commit ( ) ;
239
291
var candidates = NativeContract . NEO . GetCandidates ( _neoSystem . GetSnapshotCache ( ) ) ;
240
-
292
+ result = _rpcServer . GetCandidates ( ) ;
241
293
foreach ( var candidate in candidates )
242
294
{
243
295
var item = new JObject ( ) ;
@@ -350,7 +402,8 @@ public void TestGetBlockHashInvalidIndex()
350
402
var block = TestUtils . CreateBlockWithValidTransactions ( snapshot , _wallet , _walletAccount , 3 ) ;
351
403
TestUtils . BlocksAdd ( snapshot , block . Hash , block ) ;
352
404
snapshot . Commit ( ) ;
353
- Assert . ThrowsException < RpcException > ( ( ) => _rpcServer . GetBlockHash ( block . Index + 1 ) ) ;
405
+ Action act = ( ) => _rpcServer . GetBlockHash ( block . Index + 1 ) ;
406
+ act . Should ( ) . Throw < RpcException > ( ) ;
354
407
}
355
408
356
409
[ TestMethod ]
0 commit comments