CasperService.java

  1. package com.syntifi.casper.sdk.service;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;

  7. import com.googlecode.jsonrpc4j.ExceptionResolver;
  8. import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
  9. import com.googlecode.jsonrpc4j.JsonRpcMethod;
  10. import com.googlecode.jsonrpc4j.JsonRpcParam;
  11. import com.googlecode.jsonrpc4j.JsonRpcParamsPassMode;
  12. import com.googlecode.jsonrpc4j.ProxyUtil;
  13. import com.syntifi.casper.sdk.exception.CasperClientExceptionResolver;
  14. import com.syntifi.casper.sdk.identifier.block.BlockIdentifier;
  15. import com.syntifi.casper.sdk.identifier.dictionary.DictionaryIdentifier;
  16. import com.syntifi.casper.sdk.model.account.AccountData;
  17. import com.syntifi.casper.sdk.model.auction.AuctionData;
  18. import com.syntifi.casper.sdk.model.balance.BalanceData;
  19. import com.syntifi.casper.sdk.model.block.JsonBlockData;
  20. import com.syntifi.casper.sdk.model.deploy.Deploy;
  21. import com.syntifi.casper.sdk.model.deploy.DeployData;
  22. import com.syntifi.casper.sdk.model.deploy.DeployResult;
  23. import com.syntifi.casper.sdk.model.dictionary.DictionaryData;
  24. import com.syntifi.casper.sdk.model.era.EraInfoData;
  25. import com.syntifi.casper.sdk.model.peer.PeerData;
  26. import com.syntifi.casper.sdk.model.stateroothash.StateRootHashData;
  27. import com.syntifi.casper.sdk.model.status.StatusData;
  28. import com.syntifi.casper.sdk.model.storedvalue.StoredValueData;
  29. import com.syntifi.casper.sdk.model.transfer.TransferData;

  30. /**
  31.  * Interface to be used as Dynamic Proxy for RPC method operation
  32.  *
  33.  * @author Alexandre Carvalho
  34.  * @author Andre Bertolace
  35.  * @since 0.0.1
  36.  */
  37. public interface CasperService {
  38.         /**
  39.          * Get network peers data
  40.          *
  41.          * @return Object holding the api version and peer list
  42.          */
  43.         @JsonRpcMethod("info_get_peers")
  44.         public PeerData getPeerData();

  45.         /**
  46.          * Get latest block info
  47.          *
  48.          * @return Object holding the api version and block
  49.          */
  50.         @JsonRpcMethod("chain_get_block")
  51.         public JsonBlockData getBlock();

  52.         /**
  53.          * Retrieve block info by its {@link BlockIdentifier}
  54.          *
  55.          * @param blockIdentifier BlockIdentifier data
  56.          * @return Object holding the api version and block
  57.          */
  58.         @JsonRpcMethod("chain_get_block")
  59.         public JsonBlockData getBlock(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  60.         /**
  61.          * Retrieve last block's transfers
  62.          *
  63.          * @return Object holding the api version and transfer data
  64.          */
  65.         @JsonRpcMethod("chain_get_block_transfers")
  66.         public TransferData getBlockTransfers();

  67.         /**
  68.          * Retrieve block transfers by its {@link BlockIdentifier}
  69.          *
  70.          * @param blockIdentifier BlockIdentifier data
  71.          * @return Object holding the api version and transfer data
  72.          */
  73.         @JsonRpcMethod("chain_get_block_transfers")
  74.         public TransferData getBlockTransfers(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  75.         /**
  76.          * Returns a state root hash at the last Block
  77.          *
  78.          * @return Object holding the api version and state root hash data
  79.          */
  80.         @JsonRpcMethod("chain_get_state_root_hash")
  81.         public StateRootHashData getStateRootHash();

  82.         /**
  83.          * Returns a state root hash at a given a {@link BlockIdentifier}
  84.          *
  85.          * @param blockIdentifier BlockIdentifier data
  86.          * @return Object holding the api version and state root hash data
  87.          */
  88.         @JsonRpcMethod("chain_get_state_root_hash")
  89.         public StateRootHashData getStateRootHash(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  90.         /**
  91.          * Returns a stored value from the network
  92.          *
  93.          * @param stateRootHash Hash of the state root
  94.          * @param key           `casper_types::Key` as formatted string
  95.          * @param path          The path components starting from the key as base
  96.          * @return Object holding the api version, the merkle proof and the stored value
  97.          *         data
  98.          */
  99.         @JsonRpcMethod("state_get_item")
  100.         public StoredValueData getStateItem(@JsonRpcParam("state_root_hash") String stateRootHash,
  101.                         @JsonRpcParam("key") String key, @JsonRpcParam("path") List<String> path);

  102.         /**
  103.          * Returns an EraInfo from the network
  104.          *
  105.          * @param blockIdentifier BlockIdentifier data
  106.          * @return Object holding api version and EraInfo
  107.          */
  108.         @JsonRpcMethod("chain_get_era_info_by_switch_block")
  109.         public EraInfoData getEraInfoBySwitchBlock(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  110.         /**
  111.          * Returns a Deploy from the network
  112.          *
  113.          * @param deployHash The deploy hash
  114.          * @return Object holding the api version, the deploy and the map of block hash
  115.          *         to execution result
  116.          */
  117.         @JsonRpcMethod("info_get_deploy")
  118.         public DeployData getDeploy(@JsonRpcParam("deploy_hash") String deployHash);

  119.         /**
  120.          * Returns the current status of the node
  121.          *
  122.          * @return Object holding the apiversion, minimal block information, build
  123.          *         version and other properties
  124.          */
  125.         @JsonRpcMethod("info_get_status")
  126.         public StatusData getStatus();

  127.         /**
  128.          * Returns an Account from the network
  129.          *
  130.          * @param publicKey       the account's public key
  131.          * @param blockIdentifier BlockIdentifier data
  132.          * @return Oject holding the api version, the account data and the merkle proof
  133.          */
  134.         @JsonRpcMethod("state_get_account_info")
  135.         public AccountData getStateAccountInfo(@JsonRpcParam("public_key") String publicKey,
  136.                         @JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  137.         /**
  138.          * Returns the Auction info for a given block
  139.          *
  140.          * @param blockIdentifier BlockIdentifier data
  141.          * @return Object holding the api version and auction state data
  142.          */
  143.         @JsonRpcMethod("state_get_auction_info")
  144.         public AuctionData getStateAuctionInfo(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier);

  145.         /**
  146.          * Lookup a dictionary item via an Contract's named keys. Returns an item from a
  147.          * Dictionary given the AccountNamedKey/ContractNamedKey/Dictionary/Uref
  148.          *
  149.          * @param stateRootHash        the hash of state root
  150.          * @param dictionaryIdentifier any concrete DictionaryIdentifier
  151.          * @return Object holding the api version, the dictionary key, the merkle proof
  152.          *         and the stored value
  153.          */
  154.         @JsonRpcMethod("state_get_dictionary_item")
  155.         public DictionaryData getStateDictionaryItem(@JsonRpcParam("state_root_hash") String stateRootHash,
  156.                         @JsonRpcParam("dictionary_identifier") DictionaryIdentifier dictionaryIdentifier);

  157.         /**
  158.          * Fetches balance value
  159.          *
  160.          * @param stateRootHash the hash of state root
  161.          * @param purseUref     formatted URef
  162.          * @return Result for "state_get_balance" RPC response
  163.          */
  164.         @JsonRpcMethod("state_get_balance")
  165.         public BalanceData getBalance(@JsonRpcParam("state_root_hash") String stateRootHash,
  166.                         @JsonRpcParam("purse_uref") String purseUref);

  167.         /**
  168.          * Sends a deploy to be received by the network
  169.          *
  170.          * @param deploy the deploy object to send to the network
  171.          * @return Object holding the api version and the deploy hash
  172.          */
  173.         @JsonRpcMethod(value = "account_put_deploy", paramsPassMode = JsonRpcParamsPassMode.ARRAY)
  174.         public DeployResult putDeploy(Deploy deploy);

  175.         /**
  176.          * Builds a CasperService for the node ip/port pair
  177.          *
  178.          * @param ip   the peer ip to connect to
  179.          * @param port the service port of the peer
  180.          * @return A Dynamic Proxy to CasperService
  181.          * @throws MalformedURLException is thrown if ip/port are not compliant
  182.          */
  183.         public static CasperService usingPeer(String ip, int port) throws MalformedURLException {
  184.                 CasperObjectMapper objectMapper = new CasperObjectMapper();
  185.                 Map<String, String> newHeaders = new HashMap<>();
  186.                 newHeaders.put("Content-Type", "application/json");
  187.                 JsonRpcHttpClient client = new JsonRpcHttpClient(objectMapper, new URL("http", ip, port, "/rpc"),
  188.                                 newHeaders);

  189.                 ExceptionResolver exceptionResolver = new CasperClientExceptionResolver();
  190.                 client.setExceptionResolver(exceptionResolver);

  191.                 return ProxyUtil.createClientProxy(CasperService.class.getClassLoader(), CasperService.class, client);
  192.         }
  193. }