OpBlockchainRules { public boolean validateBlock(OpBlockChain blockChain, OpBlock block, OpBlock prevBlockHeader, boolean validateSignature) { String blockHash = block.getFullHash(); int blockId = block.getBlockId(); int pid = -1; if (prevBlockHeader != null) { if (!OUtils.equals(prevBlockHeader.getFullHash(), block.getStringValue(OpBlock.F_PREV_BLOCK_HASH))) { return error(block, ErrorType.BLOCK_PREV_HASH, prevBlockHeader.getFullHash(), block.getStringValue(OpBlock.F_PREV_BLOCK_HASH), blockHash); } pid = prevBlockHeader.getBlockId(); } if (pid + 1 != blockId) { return error(block, ErrorType.BLOCK_PREV_ID, pid, block.getBlockId(), blockHash); } int dupBl = blockChain.getBlockDepth(block); if (dupBl != -1) { return error(block, ErrorType.BLOCK_HASH_IS_DUPLICATED, blockHash, block.getBlockId(), dupBl); } if (block.getOperations().size() == 0) { return error(block, ErrorType.BLOCK_EMPTY, blockHash); } if (!OUtils.equals(calculateMerkleTreeHash(block), block.getStringValue(OpBlock.F_MERKLE_TREE_HASH))) { return error(block, ErrorType.BLOCK_MERKLE_TREE_FAILED, blockHash, calculateMerkleTreeHash(block), block.getStringValue(OpBlock.F_MERKLE_TREE_HASH)); } if (!OUtils.equals(calculateSigMerkleTreeHash(block), block.getStringValue(OpBlock.F_SIG_MERKLE_TREE_HASH))) { return error(block, ErrorType.BLOCK_SIG_MERKLE_TREE_FAILED, blockHash, calculateSigMerkleTreeHash(block), block.getStringValue(OpBlock.F_SIG_MERKLE_TREE_HASH)); } if (!OUtils.equals(calculateHash(block), block.getFullHash())) { return error(block, ErrorType.BLOCK_HASH_FAILED, block.getFullHash(), calculateHash(block)); } if(!validateSignature) { return true; } OpObject keyObj = getLoginKeyObj(blockChain, block.getStringValue(OpBlock.F_SIGNED_BY)); boolean validateSig = true; Exception ex = null; try { KeyPair pk = getKeyPairFromObj(keyObj, null); byte[] blHash = SecUtils.getHashBytes(block.getFullHash()); if (pk != null && SecUtils.validateSignature(pk, blHash, block.getSignature())) { validateSig = true; } else { validateSig = false; } } catch (FailedVerificationException e) { validateSig = false; ex = e; } catch (RuntimeException e) { validateSig = false; ex = e; } if (!validateSig) { return error(block, ErrorType.BLOCK_SIGNATURE_FAILED, blockHash, block.getStringValue(OpBlock.F_SIGNED_BY), ex); } return true; } OpBlockchainRules(JsonFormatter formatter, ValidationListener logValidation); static boolean validateNickname(String name); static String getRawHash(String hs); static String getSiteFromUser(String name); static String getNicknameFromUser(String name); static String getUserFromNicknameAndSite(String nickname, String site); String calculateMerkleTreeHash(OpBlock op); String calculateSigMerkleTreeHash(OpBlock op); String calculateOperationHash(OpOperation ob, boolean set); int calculateBlockSize(OpBlock cp); JsonFormatter getFormatter(); OpOperation generateHashAndSign(OpOperation op, KeyPair... keyPair); boolean validateRules(OpBlockChain blockchain, OpOperation o, LocalValidationCtx ctx); @SuppressWarnings("unchecked") Map<String, Set<String>> getRoles(OpBlockChain blockchain); boolean validateBlock(OpBlockChain blockChain, OpBlock block, OpBlock prevBlockHeader, boolean validateSignature); boolean validateSignatures(OpBlockChain ctx, OpOperation ob); boolean validateOp(OpBlockChain opBlockChain, OpOperation u, LocalValidationCtx ctx); KeyPair getLoginKeyPair(OpBlockChain ctx, String signedByName, String privateKey); OpObject getLoginKeyObj(OpBlockChain ctx, String signedByName); KeyPair getSignUpKeyPairFromPwd(OpBlockChain blc,
			String name, String pwd); OpBlock createAndSignBlock(Collection<OpOperation> ops, OpBlock prevOpBlockHeader, String serverUser, KeyPair serverKeyPair); static String calculateSuperblockHash(int size, String lastBlockHash); static String calculateHash(OpBlock block); boolean error(OpObject o, ErrorType e, Object... args); boolean error(OpObject o, Exception cause, ErrorType e, Object... args); static final int MAX_BLOCK_SIZE_OPS; static final int MAX_BLOCKHEADER_SIZE; static final int MAX_ALL_OP_SIZE_MB; static final int MAX_BLOCK_SIZE_MB; static final int MAX_AMOUNT_CREATED_OBJ_FOR_OP; static final int MAX_OP_SIZE_MB; static final int BLOCK_VERSION; static final String OP_TYPE_SYS; static final String OP_LOGIN; static final String OP_SIGNUP; static final String OP_ROLE; static final String OP_GRANT; static final String OP_VALIDATE; static final String OP_VOTE; static final String OP_LIMIT; static final String OP_TABLE; static final String OP_OPERATION; static final String OP_BOT; static final String F_DIGEST; static final String F_ALGO; static final String F_PUBKEY; static final String F_NAME; static final String F_SALT; static final String F_AUTH_METHOD; static final String F_OAUTH_PROVIDER; static final String F_OAUTHID_HASH; static final String F_KEYGEN_METHOD; static final String F_DETAILS; static final String F_TYPE; static final String F_SUPER_ROLES; static final String F_ROLES; static final String F_ERROR_MESSAGE; static final String F_PRIVATEKEY; static final String F_UID; static final String F_VALIDATE; static final String F_IF; static final String METHOD_OAUTH; static final String METHOD_PWD; static final String METHOD_PROVIDED; static final String JSON_MSG_TYPE; static final char USER_LOGIN_CHAR; }
@Test public void testValidateBlock() throws FailedVerificationException { assertNotNull(blc.createBlock(serverName, serverKeyPair)); }