11package org .tron .core .jsonrpc ;
22
3+ import com .alibaba .fastjson .JSON ;
4+ import com .google .gson .JsonArray ;
5+ import com .google .gson .JsonObject ;
36import com .google .protobuf .ByteString ;
7+ import io .prometheus .client .CollectorRegistry ;
48import javax .annotation .Resource ;
59import lombok .extern .slf4j .Slf4j ;
10+ import org .apache .http .client .methods .CloseableHttpResponse ;
11+ import org .apache .http .client .methods .HttpPost ;
12+ import org .apache .http .entity .StringEntity ;
13+ import org .apache .http .impl .client .CloseableHttpClient ;
14+ import org .apache .http .impl .client .HttpClients ;
15+ import org .apache .http .util .EntityUtils ;
616import org .bouncycastle .util .encoders .Hex ;
717import org .junit .Assert ;
818import org .junit .Before ;
919import org .junit .Test ;
1020import org .tron .common .BaseTest ;
21+ import org .tron .common .parameter .CommonParameter ;
22+ import org .tron .common .prometheus .Metrics ;
1123import org .tron .common .utils .ByteArray ;
1224import org .tron .common .utils .Sha256Hash ;
1325import org .tron .core .Constant ;
1729import org .tron .core .capsule .TransactionCapsule ;
1830import org .tron .core .config .args .Args ;
1931import org .tron .core .services .NodeInfoService ;
32+ import org .tron .core .services .jsonrpc .FullNodeJsonRpcHttpService ;
2033import org .tron .core .services .jsonrpc .TronJsonRpcImpl ;
2134import org .tron .core .services .jsonrpc .types .BlockResult ;
2235import org .tron .core .services .jsonrpc .types .TransactionResult ;
@@ -39,9 +52,15 @@ public class JsonrpcServiceTest extends BaseTest {
3952 @ Resource
4053 private Wallet wallet ;
4154
55+ @ Resource
56+ private FullNodeJsonRpcHttpService fullNodeJsonRpcHttpService ;
57+
4258 static {
4359 dbPath = "output_jsonrpc_service_test" ;
4460 Args .setParam (new String []{"--output-directory" , dbPath }, Constant .TEST_CONF );
61+ CommonParameter .getInstance ().setJsonRpcHttpFullNodeEnable (true );
62+ CommonParameter .getInstance ().setMetricsPrometheusEnable (true );
63+ Metrics .init ();
4564
4665 OWNER_ADDRESS =
4766 Wallet .getAddressPreFixString () + "abd4b9367799eaa3197fecb144eb71de1e049abc" ;
@@ -235,4 +254,40 @@ public void testGetTransactionByHash() {
235254 transactionResult .getBlockNumber ());
236255 }
237256
238- }
257+ @ Test
258+ public void testGetBlockByNumber2 () {
259+ fullNodeJsonRpcHttpService .start ();
260+ JsonArray params = new JsonArray ();
261+ params .add (ByteArray .toJsonHex (blockCapsule .getNum ()));
262+ params .add (false );
263+ JsonObject requestBody = new JsonObject ();
264+ requestBody .addProperty ("jsonrpc" , "2.0" );
265+ requestBody .addProperty ("method" , "eth_getBlockByNumber" );
266+ requestBody .add ("params" , params );
267+ requestBody .addProperty ("id" , 1 );
268+ CloseableHttpResponse response ;
269+ try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
270+ HttpPost httpPost = new HttpPost ("http://127.0.0.1:8545/jsonrpc" );
271+ httpPost .addHeader ("Content-Type" , "application/json" );
272+ httpPost .setEntity (new StringEntity (requestBody .toString ()));
273+ response = httpClient .execute (httpPost );
274+ String resp = EntityUtils .toString (response .getEntity ());
275+ BlockResult blockResult = JSON .parseObject (resp ).getObject ("result" , BlockResult .class );
276+ Assert .assertEquals (ByteArray .toJsonHex (blockCapsule .getNum ()),
277+ blockResult .getNumber ());
278+ Assert .assertEquals (blockCapsule .getTransactions ().size (),
279+ blockResult .getTransactions ().length );
280+ Assert .assertEquals ("0x0000000000000000" ,
281+ blockResult .getNonce ());
282+ response .close ();
283+ Assert .assertEquals (1 , CollectorRegistry .defaultRegistry .getSampleValue (
284+ "tron:jsonrpc_service_latency_seconds_count" ,
285+ new String [] {"method" }, new String [] {"eth_getBlockByNumber" }).intValue ());
286+ } catch (Exception e ) {
287+ Assert .fail (e .getMessage ());
288+ } finally {
289+ fullNodeJsonRpcHttpService .stop ();
290+ }
291+ }
292+
293+ }
0 commit comments