/** * Try do serialize some object. Throws an exception if serialized form is tha same as not serialzied, as it means * that there is no suitable adapter to process. Be caeful because it will throw an exception also for the types * should not be serialized and this way are left intact. * * @param obj to try to serialize * @param <T> * * @return */ public <T> T serializeOrThrow(Object obj) { T result = mapper.serialize(obj, this); if (result == obj) throw new BiSerializationException("can't serialize " + obj); return result; } }
@Test public void calculateProcessingCostSimpleBreakWhileUnpacking() throws Exception { // Should create contract, sign and seal it. Then while calculating cost should break while unpacking contract (signs verifying). // should repeat contract processing procedure on the Node // (Contract.fromPackedTransaction() -> Contract(byte[], TransactionPack) -> Contract.check()) Contract contract = createCoin100apiv3(); contract.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(contract, true); // Check 4096 bits signature (8) + // Register a version (20) int costShouldBe = 28; boolean exceptionThrown = false; try { processContractAsItWillBeOnTheNode(contract, 1); } catch (Quantiser.QuantiserException e) { System.out.println("Thrown correct exception: " + e.getMessage()); exceptionThrown = true; } catch (BiSerializationException e) { System.out.println("Thrown correct exception: " + e.getMessage()); exceptionThrown = true; } assertEquals(true, exceptionThrown); }
exceptionThrown = true; } catch (BiSerializationException e) { System.out.println("Thrown correct exception: " + e.getMessage()); exceptionThrown = true;
@Override public Object deserialize(Binder binder, BiDeserializer deserializer) { try { Constructor c= objectClass.getDeclaredConstructor(); c.setAccessible(true); BiSerializable bs = (BiSerializable) c.newInstance(); bs.deserialize(binder, deserializer); return bs; } catch (Exception e) { throw new BiSerializationException("failed to deserialize " + typeName() + ": " + objectClass.getCanonicalName(), e); } } }