public StateStore buildStateStore(BranchId branchId) { return new StateStore(getDbSource(branchId + "/state")); }
@After public void close() { stateStore.close(); }
@Test public void getStateSize() throws Exception { // first is null assertTrue(this.stateStore.getStateSize() == 0L); // add some state this.stateStore.put("STATE", new JsonObject()); // state size is 1L assertTrue(this.stateStore.getStateSize() == 1L); }
@Test public void getState() throws Exception { String stateKey = "State"; JsonObject obj = new JsonParser().parse("{\"value\":\"value\"}").getAsJsonObject(); stateStore.put(stateKey, obj); JsonObject obj2 = stateStore.get(stateKey); assertTrue(obj.equals(obj2)); log.debug(obj.getClass().toString()); }
public void commitBlockResult(BlockRuntimeResult result) { // TODO store transaction bybatch Map<String, JsonObject> changes = result.getBlockResult(); result.getTxReceipts().stream().forEach(txr -> { txReceiptStore.put(txr); }); if (!changes.isEmpty()) { changes.entrySet().stream().forEach(r -> { stateStore.put(r.getKey(), r.getValue()); }); } // TODO make transaction Receipt Event }
@Test public void createTest() { String description = "ETH TO YEED"; JsonObject branch = getEthToYeedBranch(description); String branchId = BranchId.of(branch).toString(); JsonObject params = new JsonObject(); params.add(branchId, branch); TransactionReceipt receipt = new TransactionReceiptImpl(); receipt.setIssuer(stateValue.getOwner().toString()); try { txReceiptField.set(stemContract, receipt); receipt = stemContract.create(params); } catch (IllegalAccessException e) { e.printStackTrace(); } assertThat(receipt.isSuccess()).isTrue(); JsonObject saved = stateStore.get(branchId); assertThat(saved).isNotNull(); assertThat(saved.get("description").getAsString()).isEqualTo(description); }
@Test public void init() { JsonObject originObj = new JsonObject(); originObj.addProperty("test", "origin"); stateStore.put("TEST", originObj); TempStateStore store = new TempStateStore(stateStore); assert store.get("TEST").equals(originObj); JsonObject testObj = new JsonObject(); testObj.addProperty("test", "yes"); store.put("TEST", testObj); assert store.get("TEST").equals(testObj); store.close(); assert store.get("TEST").equals(originObj); } }
@Before public void setUp() throws IllegalAccessException { StateStore<JsonObject> coinContractStateStore = new StateStore<>(new HashMapDbSource()); List<Field> txReceipt = ContractUtils.txReceiptFields(coinContract); if (txReceipt.size() == 1) { txReceiptField = txReceipt.get(0); } for (Field f : ContractUtils.contractFields(coinContract, ContractStateStore.class)) { f.setAccessible(true); f.set(coinContract, coinContractStateStore); } genesis(); }
@Test public void putState() throws Exception { String stateKey = "State"; JsonObject obj = new JsonParser().parse("{\"value\":\"value\"}").getAsJsonObject(); stateStore.put(stateKey, obj); assertTrue(stateStore.getStateSize() == 1L); }
@After public void close() { stateStore.close(); }
@Test public void querySomeContract() throws Exception { CoinContract contract = new CoinContract(); Store<String,JsonObject> tempStore = new StateStore<>(new HashMapDbSource()); tempStore.put("TOTAL_SUPPLY", new JsonParser().parse("{\"balance\":10000}").getAsJsonObject()); RuntimeQuery query = new RuntimeQuery(contract); query.setStore(tempStore); Object result = query.query("totalSupply", null); assert BigDecimal.valueOf(10000).equals(result); } }
public void close() { this.blockStore.close(); this.transactionStore.close(); this.metaStore.close(); // TODO refactoring this.stateStore.close(); this.transactionReceiptStore.close(); }
@Before public void setUp() { stateStore = new StateStore(new HashMapDbSource()); }
@Before public void setUp() { stateStore = new StateStore(new HashMapDbSource()); }
Runtime<JsonObject> runtime = new Runtime<>( new StateStore<>(new HashMapDbSource()), new TransactionReceiptStore(new HashMapDbSource())); runtime.addContract(stemContract, contract);
@Before public void setUp() throws IllegalAccessException { stateStore = new StateStore<>(new HashMapDbSource()); stemContract = new StemContract(); JsonObject json = ContractTestUtils.createSampleBranchJson(); stateValue = StemContractStateValue.of(json); JsonObject params = createParams(stateValue.getJson()); TransactionReceipt receipt = new TransactionReceiptImpl(); receipt.setIssuer(stateValue.getOwner().toString()); List<Field> txReceipt = ContractUtils.txReceiptFields(stemContract); if (txReceipt.size() == 1) { txReceiptField = txReceipt.get(0); } for (Field f : ContractUtils.contractFields(stemContract, ContractStateStore.class)) { f.setAccessible(true); f.set(stemContract, stateStore); } try { txReceiptField.set(stemContract, receipt); stemContract.genesis(params); } catch (IllegalAccessException e) { e.printStackTrace(); } }
@Test public void yeedRuntimeTest() { CoinContract contract = new CoinContract(); ContractId coinContract = Constants.YEED_CONTRACT_ID; Runtime runtime = new Runtime<>( new StateStore<>(new HashMapDbSource()), new TransactionReceiptStore(new HashMapDbSource()) ); String genesisStr = "{\"alloc\": {\"c91e9d46dd4b7584f0b6348ee18277c10fd7cb94\":" + " {\"balance\": \"1000000000\"},\"1a0cdead3d1d1dbeef848fef9053b4f0ae06db9e\":" + " {\"balance\": \"1000000000\"},\"cee3d4755e47055b530deeba062c5bd0c17eb00f\":" + " {\"balance\": \"998000000000\"}}}"; JsonObject genesisParams = JsonUtil.parseJsonObject(genesisStr); runtime.addContract(coinContract, contract); BranchId branchId = TestConstants.YEED; TransactionBuilder builder = new TransactionBuilder(); TransactionHusk testTx = builder.setBranchId(branchId) .addTxBody(coinContract, "genesis", genesisParams) .setWallet(TestConstants.wallet()) .build(); TransactionRuntimeResult result = runtime.invoke(testTx); assertThat(result.getReceipt().isSuccess()).isTrue(); assertThat(result.getChangeValues() .get("c91e9d46dd4b7584f0b6348ee18277c10fd7cb94") .get("balance").getAsBigDecimal() ).isEqualTo(BigDecimal.valueOf(1000000000)); }
@Test public void initTest() throws InvocationTargetException, IllegalAccessException { StemContract contract = new StemContract(); RuntimeInvoke invoke = new RuntimeInvoke(contract); Store tempStore = new StateStore<>(new HashMapDbSource()); JsonObject json = ContractTestUtils.createSampleBranchJson(); BranchId branchId = BranchId.of(json); TransactionHusk createTx = BlockChainTestUtils.createBranchTxHusk(branchId, "create", json); TransactionReceipt receipt = new TransactionReceiptImpl(createTx); for (JsonElement txEl: JsonUtil.parseJsonArray(createTx.getBody())) { TempStateStore store = invoke.invokeTransaction( txEl.getAsJsonObject(), receipt, tempStore); assert receipt.isSuccess(); assert store.changeValues().size() > 0; } }
@Test public void executorTest() { CoinContract contract = new CoinContract(); Runtime runtime = new Runtime<>( new StateStore<>(new HashMapDbSource()), new TransactionReceiptStore(new HashMapDbSource()) ); runtime.addContract(ContractId.of("c10e873655becf550c4aece75a091f4553d6202d"), contract); // Block Store // Blockchain Runtime StoreBuilder builder = new StoreBuilder(new DefaultConfig(false)); BlockStore store = builder.buildBlockStore(BRANCH_ID); MetaStore meta = builder.buildMetaStore(BRANCH_ID); BlockExecutor ex = new BlockExecutor(store, meta, runtime); // BlockStore add genesis block and other ex.runExecuteBlocks(); } }