public TempStateStore invoke(TransactionHusk tx, TransactionReceipt txReceipt, Store origin) { // Find invoke method and invoke // validation method TempStateStore txState = new TempStateStore(origin); try { // transaction is multiple method for (JsonElement transactionElement: JsonUtil.parseJsonArray(tx.getBody())) { JsonObject txBody = transactionElement.getAsJsonObject(); // check contract Version ContractId txContractId = ContractId.of(txBody.get("contractId").getAsString()); RuntimeContractWrap wrap = contracts.get(txContractId); TempStateStore txElementState = wrap.invokeTransaction(txBody, txReceipt, txState); if(txReceipt.isSuccess()) { txState.putAll(txElementState.changeValues()); } } } catch (Throwable e) { txReceipt.setStatus(ExecuteStatus.ERROR); JsonObject errorLog = new JsonObject(); errorLog.addProperty("error", e.getMessage()); txReceipt.addLog(errorLog); } return txState; }
@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); } }
public TransactionRuntimeResult invoke(TransactionHusk tx) { TransactionReceipt txReceipt = new TransactionReceiptImpl(tx); TransactionRuntimeResult trr = new TransactionRuntimeResult(tx); trr.setTransctionReceipt(txReceipt); TempStateStore store = invoke(tx, txReceipt, stateStore); trr.setChangeValues(store.changeValues()); return trr; }
public TempStateStore invokeTransaction(JsonObject txBody, TransactionReceipt txReceipt, Store origin) throws InvocationTargetException, IllegalAccessException { // set State Store TempStateStore store = new TempStateStore(origin); // set store to contract ContractUtils.updateContractFields(contract, stateField, store); transactionReceiptField.set(contract, txReceipt); // TODO Check Transaction has contractID String methodName = txBody.get("method").getAsString(); ContractMethod method = invokeMethods.get(methodName); // filter method exist if(method == null) { txReceipt.setStatus(ExecuteStatus.ERROR); txReceipt.addLog(errorLog("method is not exist")); return store; } // check exist params if(txBody.has("params") && method.isParams()) { JsonObject params = txBody.getAsJsonObject("params"); method.getMethod().invoke(contract, params); } else if (!method.isParams()) { method.getMethod().invoke(contract); } else { txReceipt.setStatus(ExecuteStatus.ERROR); txReceipt.addLog(errorLog("params is not exist")); return store; } return store; }
@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; } }
TempStateStore blockState = new TempStateStore(stateStore); for(TransactionHusk tx: block.getBody()) { TransactionReceipt txReceipt = new TransactionReceiptImpl(tx); blockState.putAll(txResult.changeValues()); result.setBlockResult(blockState.changeValues());