Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/smc api refactoring #1209

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apl-api2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<supportingFilesToGenerate>ApiException.java,NotFoundException.java</supportingFilesToGenerate>
<!-- IMPORTANT! NO SPACES ALLOWED BETWEEN modelsToGenerate values-->
<modelsToGenerate>
AccountInfo,AccountInfoResp,AccountReq,AccountReqTest,AccountReqSendMoney,BlockchainInfo,BlockInfo,BlockchainState,CountResponse,CreateChildAccountResp,EmptyResponse,HealthResponse,ListResponse,QueryCountResult,QueryObject,QueryResult,TransactionInfo,TransactionInfoArrayResp,TransactionInfoResp,TxReceipt,TxRequest,PublishContractReq,CallContractMethodReq,ContractListResponse,ContractDetails,ContractDetailsResponse,ContractStateResponse,ContractMethod,CallMethodResult,TransactionArrayResp,CallViewMethodReq,ResultValueResponse,MemberSpec,PropertySpec,ArgSpec,ContractSpecResponse,CurrencyBurningTxCreationRequest,TransactionCreationRequest,TransactionCreationResponse,UnconfirmedTransactionInfo,CurrencyBurningTxParams,AddressSpec,AddressSpecResponse,TransactionVerificationResponse,LastTransactionVerificationResponse,TransactionVerification,StringListResponse,ModuleSourceResponse,ModuleListResponse,AsrSpec,AsrSpecResponse,AsrMemberSpec,ContractEventsRequest,ContractEventsResponse,ContractEventDetails
AccountInfo,AccountInfoResp,AccountReq,AccountReqTest,AccountReqSendMoney,BlockchainInfo,BlockInfo,BlockchainState,CountResponse,CreateChildAccountResp,EmptyResponse,HealthResponse,ListResponse,QueryCountResult,QueryObject,QueryResult,TransactionInfo,TransactionInfoArrayResp,TransactionInfoResp,TxReceipt,TxRequest,PublishContractReq,CallContractMethodReq,ContractListResponse,ContractDetails,ContractDetailsResponse,ContractStateResponse,ContractMethod,CallMethodResult,TransactionByteArrayResp,CallViewMethodReq,ResultValueResponse,MemberSpec,PropertySpec,ArgSpec,ContractSpecResponse,CurrencyBurningTxCreationRequest,TransactionCreationRequest,TransactionCreationResponse,UnconfirmedTransactionInfo,CurrencyBurningTxParams,AddressSpec,AddressSpecResponse,TransactionVerificationResponse,LastTransactionVerificationResponse,TransactionVerification,StringListResponse,ModuleSourceResponse,ModuleListResponse,AsrSpec,AsrSpecResponse,AsrMemberSpec,ContractEventsRequest,ContractEventsResponse,ContractEventDetails
</modelsToGenerate>
<configHelp>false</configHelp>
<configOptions>
Expand Down
14 changes: 7 additions & 7 deletions apl-api2/src/main/resources/yaml/apollo-api-v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/TransactionArrayResp'
$ref: '#/components/schemas/TransactionByteArrayResp'
400:
$ref: '#/components/responses/BadRequest'
500:
Expand All @@ -674,7 +674,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/TransactionArrayResp'
$ref: '#/components/schemas/TransactionByteArrayResp'
400:
$ref: '#/components/responses/BadRequest'
500:
Expand Down Expand Up @@ -711,9 +711,9 @@ paths:
post:
tags:
- smc
summary: Returns signed transaction to call the contract method.
summary: Create, sign and return the 'Call smart-contract' transaction.
description: |
Returns signed transaction to call the contract method.
Create, sign and return the 'Call smart-contract' transaction.
operationId: createCallContractMethodTx
requestBody:
description: the contract method and method parameters
Expand All @@ -728,7 +728,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/TransactionArrayResp'
$ref: '#/components/schemas/TransactionByteArrayResp'
400:
$ref: '#/components/responses/BadRequest'
500:
Expand Down Expand Up @@ -860,7 +860,7 @@ paths:
example: 1591696372000
- name: transaction
in: query
description: The transaction that contains contract
description: The contract transaction id
required: false
schema:
type: string
Expand Down Expand Up @@ -2372,7 +2372,7 @@ components:
items:
type: string

TransactionArrayResp:
TransactionByteArrayResp:
required:
- tx
allOf:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.apollocurrency.aplwallet.apl.core.dao.blockchain;

import com.apollocurrency.aplwallet.api.v2.model.TxReceipt;
import com.apollocurrency.aplwallet.apl.core.model.Sort;
import com.apollocurrency.aplwallet.apl.util.api.Sort;
import com.apollocurrency.aplwallet.apl.util.db.TransactionalDataSource;
import com.apollocurrency.aplwallet.apl.core.entity.appdata.ChatInfo;
import com.apollocurrency.aplwallet.apl.core.entity.blockchain.TransactionEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.apollocurrency.aplwallet.apl.core.db.DatabaseManager;
import com.apollocurrency.aplwallet.apl.core.entity.appdata.ChatInfo;
import com.apollocurrency.aplwallet.apl.core.entity.blockchain.TransactionEntity;
import com.apollocurrency.aplwallet.apl.core.model.Sort;
import com.apollocurrency.aplwallet.apl.util.api.Sort;
import com.apollocurrency.aplwallet.apl.core.model.TransactionDbInfo;
import com.apollocurrency.aplwallet.apl.core.transaction.PrunableTransaction;
import com.apollocurrency.aplwallet.apl.crypto.Convert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
/**
* @author andrew.zinchenko@gmail.com
*/
public final class ComplexKey implements DbKey {
public final class ThreeValuesKey implements DbKey {

private final long idA;
private final String idB;
private final byte[] idC;

public ComplexKey(long idA, String idB, byte[] idC) {
public ThreeValuesKey(long idA, String idB, byte[] idC) {
this.idA = idA;
this.idB = idB;
this.idC = idC;
Expand All @@ -42,7 +42,7 @@ public int setPK(PreparedStatement pstmt, int index) throws SQLException {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComplexKey that = (ComplexKey) o;
ThreeValuesKey that = (ThreeValuesKey) o;
return idA == that.idA && idB.equals(that.idB) && Arrays.equals(idC, that.idC);
}

Expand All @@ -56,7 +56,7 @@ public int hashCode() {

@Override
public String toString() {
return "ComplexKey{" + "idA=" + idA + ", idB=" + idB + ", idC=" + Convert.toHexString(idC) + '}';
return "ThreeValuesKey{" + "idA=" + idA + ", idB=" + idB + ", idC=" + Convert.toHexString(idC) + '}';

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
/**
* @author andrew.zinchenko@gmail.com
*/
public abstract class ComplexKeyFactory<T> extends KeyFactory<T> {
public abstract class ThreeValuesKeyFactory<T> extends KeyFactory<T> {

private final String idColumnA;
private final String idColumnB;
private final String idColumnC;

protected ComplexKeyFactory(String idColumnA, String idColumnB, String idColumnC) {
protected ThreeValuesKeyFactory(String idColumnA, String idColumnB, String idColumnC) {
super(" WHERE" +
" " + idColumnA + " = ? " +
"AND " + idColumnB + " = ? " +
Expand All @@ -32,10 +32,10 @@ protected ComplexKeyFactory(String idColumnA, String idColumnB, String idColumnC

@Override
public DbKey newKey(ResultSet rs) throws SQLException {
return new ComplexKey(rs.getLong(idColumnA), rs.getString(idColumnB), rs.getBytes(idColumnC));
return new ThreeValuesKey(rs.getLong(idColumnA), rs.getString(idColumnB), rs.getBytes(idColumnC));
}

public DbKey newKey(long idA, String idB, byte[] idC) {
return new ComplexKey(idA, idB, idC);
return new ThreeValuesKey(idA, idB, idC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import com.apollocurrency.aplwallet.api.v2.model.ContractEventDetails;
import com.apollocurrency.aplwallet.apl.core.converter.db.smc.SmcContractEventLogDetailsRowMapper;
import com.apollocurrency.aplwallet.apl.core.converter.db.smc.SmcContractEventLogRowMapper;
import com.apollocurrency.aplwallet.apl.core.dao.JdbcQueryExecutionHelper;
import com.apollocurrency.aplwallet.apl.core.dao.state.derived.DerivedDbTable;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.DbKey;
import com.apollocurrency.aplwallet.apl.core.db.DatabaseManager;
import com.apollocurrency.aplwallet.apl.core.entity.state.smc.SmcContractEventLogEntry;
import com.apollocurrency.aplwallet.apl.core.service.fulltext.FullTextOperationData;
import com.apollocurrency.aplwallet.apl.util.annotation.DatabaseSpecificDml;
import com.apollocurrency.aplwallet.apl.util.annotation.DmlMarker;
import com.apollocurrency.aplwallet.apl.util.api.PositiveRange;
import com.apollocurrency.aplwallet.apl.util.api.Sort;
import com.apollocurrency.aplwallet.apl.util.db.DbUtils;
import com.apollocurrency.aplwallet.apl.util.db.TransactionalDataSource;
import com.apollocurrency.aplwallet.apl.util.injectable.PropertiesHolder;
Expand Down Expand Up @@ -45,6 +48,7 @@ public class SmcContractEventLogTable extends DerivedDbTable<SmcContractEventLog
private static final SmcContractEventLogDetailsRowMapper detailsRowMapper = new SmcContractEventLogDetailsRowMapper();

private final PropertiesHolder propertiesHolder;
private final JdbcQueryExecutionHelper<ContractEventDetails> txQueryExecutionHelper;

/**
* Number of blocks to keep when trimming
Expand All @@ -64,6 +68,7 @@ public SmcContractEventLogTable(PropertiesHolder propertiesHolder,
this.propertiesHolder = propertiesHolder;
this.batchCommitSize = propertiesHolder.BATCH_COMMIT_SIZE();
trimKeep = propertiesHolder.getIntProperty("apl.smcEventLogTrimKeep", -1);
this.txQueryExecutionHelper = new JdbcQueryExecutionHelper<>(databaseManager.getDataSource(), (rs) -> detailsRowMapper.map(rs, null));
}

/**
Expand Down Expand Up @@ -202,7 +207,7 @@ public List<SmcContractEventLogEntry> getEntries(long eventId, byte[] signature,
return entryList;
}

public List<ContractEventDetails> getEventsByFilter(Long contract, String name, int heightFrom, int heightTo, int from, int to, String order) {
public List<ContractEventDetails> getEventsByFilter(Long contract, String name, PositiveRange blockRange, PositiveRange paging, Sort order) {
StringBuilder sql = new StringBuilder(
"SELECT el.*, " +
"e.contract, e.name, e.spec " +
Expand All @@ -214,39 +219,29 @@ public List<ContractEventDetails> getEventsByFilter(Long contract, String name,
sql.append(" AND e.name = ? ");
}

if (heightTo > 0) {
if (blockRange.isTopBoundarySet()) {
sql.append(" AND el.height <= ? ");
}

sql.append("ORDER BY el.db_id ").append(order);
sql.append(DbUtils.limitsClause(from, to));
sql.append(DbUtils.limitsClause(paging));
log.trace("Sql.query={}", sql);

try (Connection con = databaseManager.getDataSource().getConnection();
PreparedStatement pstm = con.prepareStatement(sql.toString())) {
return txQueryExecutionHelper.executeListQuery(con -> {
PreparedStatement pstm = con.prepareStatement(sql.toString());
int i = 0;
pstm.setLong(++i, contract);
pstm.setInt(++i, heightFrom);

pstm.setInt(++i, blockRange.from());
if (name != null) {
pstm.setString(++i, name);
}

if (heightTo > 0) {
pstm.setInt(++i, heightTo);
if (blockRange.to() > 0) {
pstm.setInt(++i, blockRange.to());
}
DbUtils.setLimits(++i, pstm, from, to);
DbUtils.setLimits(++i, pstm, paging);
pstm.setFetchSize(50);
try (ResultSet rs = pstm.executeQuery()) {
List<ContractEventDetails> list = new ArrayList<>();
while (rs.next()) {
list.add(detailsRowMapper.map(rs, null));
}
return list;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return pstm;
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.apollocurrency.aplwallet.apl.core.converter.db.smc.SmcContractMappingRowMapper;
import com.apollocurrency.aplwallet.apl.core.dao.state.derived.VersionedDeletableEntityDbTable;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.ComplexKeyFactory;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.ThreeValuesKeyFactory;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.DbKey;
import com.apollocurrency.aplwallet.apl.core.db.DatabaseManager;
import com.apollocurrency.aplwallet.apl.core.entity.state.smc.SmcContractMappingEntity;
Expand All @@ -28,7 +28,7 @@
* @author andrew.zinchenko@gmail.com
*/
public class SmcContractMappingTable extends VersionedDeletableEntityDbTable<SmcContractMappingEntity> {
public static final ComplexKeyFactory<SmcContractMappingEntity> KEY_FACTORY = new ComplexKeyFactory<>("address", "name", "entry_key") {
public static final ThreeValuesKeyFactory<SmcContractMappingEntity> KEY_FACTORY = new ThreeValuesKeyFactory<>("address", "name", "entry_key") {
@Override
public DbKey newKey(SmcContractMappingEntity mapping) {
if (mapping.getDbKey() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import com.apollocurrency.aplwallet.api.v2.model.ContractDetails;
import com.apollocurrency.aplwallet.apl.core.converter.db.smc.SmcContractDetailsRowMapper;
import com.apollocurrency.aplwallet.apl.core.converter.db.smc.SmcContractRowMapper;
import com.apollocurrency.aplwallet.apl.core.dao.JdbcQueryExecutionHelper;
import com.apollocurrency.aplwallet.apl.core.dao.state.derived.EntityDbTable;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.DbKey;
import com.apollocurrency.aplwallet.apl.core.dao.state.keyfactory.LongKeyFactory;
import com.apollocurrency.aplwallet.apl.core.db.DatabaseManager;
import com.apollocurrency.aplwallet.apl.core.entity.state.smc.SmcContractEntity;
import com.apollocurrency.aplwallet.apl.core.service.fulltext.FullTextOperationData;
import com.apollocurrency.aplwallet.apl.core.service.state.smc.ContractQuery;
import com.apollocurrency.aplwallet.apl.util.db.DbUtils;
import com.apollocurrency.aplwallet.apl.util.db.TransactionalDataSource;

Expand All @@ -23,7 +25,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -45,13 +46,12 @@ public DbKey newKey(SmcContractEntity contract) {
private static final String TABLE_NAME = "smc_contract";

private static final SmcContractRowMapper MAPPER = new SmcContractRowMapper(KEY_FACTORY);

private final SmcContractDetailsRowMapper smcContractDetailsRowMapper;
private final JdbcQueryExecutionHelper<ContractDetails> txQueryExecutionHelper;

@Inject
public SmcContractTable(DatabaseManager databaseManager, Event<FullTextOperationData> fullTextOperationDataEvent, SmcContractDetailsRowMapper smcContractDetailsRowMapper) {
super(TABLE_NAME, KEY_FACTORY, false, null, databaseManager, fullTextOperationDataEvent);
this.smcContractDetailsRowMapper = smcContractDetailsRowMapper;
this.txQueryExecutionHelper = new JdbcQueryExecutionHelper<>(databaseManager.getDataSource(), (rs) -> smcContractDetailsRowMapper.map(rs, null));
}

@Override
Expand Down Expand Up @@ -110,80 +110,21 @@ public void save(Connection con, SmcContractEntity entity) throws SQLException {
}
}

//TODO use a special wrapping object for query filter params with default settings and statement adjustments
public List<ContractDetails> getContractsByFilter(Long address, Long txId, Long owner, String name, String baseContract, Integer blockTimestamp, String status, int height, int from, int to) {
String namePrefix = null;
String baseContractPrefix = null;
public List<ContractDetails> getContractsByFilter(ContractQuery query) {
StringBuilder sql = new StringBuilder(
"SELECT sc.*, " +
"ss.status as smc_status " +
"FROM smc_contract sc " +
"LEFT JOIN smc_state ss on sc.address = ss.address " +
"WHERE sc.latest = true AND sc.height <= ? AND ss.latest = true ");

if (address != null) {
sql.append(" AND sc.address = ? ");
}
if (txId != null) {
sql.append(" AND sc.transaction_id = ? ");
}
if (owner != null) {
sql.append(" AND sc.owner = ? ");
}
if (name != null && !name.isEmpty()) {
sql.append(" AND sc.name LIKE ? ");
namePrefix = name.replace("%", "\\%").replace("_", "\\_") + "%";
}
if (baseContract != null && !baseContract.isEmpty()) {
sql.append(" AND sc.base_contract LIKE ? ");
baseContractPrefix = baseContract.replace("%", "\\%").replace("_", "\\_") + "%";
}
if (blockTimestamp != null) {
sql.append(" AND sc.block_timestamp >= ? ");
}
if (status != null) {
sql.append(" AND ss.status = ? ");
}
"LEFT JOIN smc_state ss on sc.address = ss.address ");
sql.append(query.toWhereClause("WHERE sc.latest = true AND ss.latest = true "));
sql.append("ORDER BY sc.block_timestamp DESC, sc.db_id DESC ");
sql.append(DbUtils.limitsClause(from, to));
sql.append(DbUtils.limitsClause(query.getPaging()));

//TODO try to use JdbcQueryExecutionHelper
try (Connection con = databaseManager.getDataSource().getConnection();
PreparedStatement pstm = con.prepareStatement(sql.toString())) {
int i = 0;
pstm.setInt(++i, height);
if (address != null) {
pstm.setLong(++i, address);
}
if (txId != null) {
pstm.setLong(++i, txId);
}
if (owner != null) {
pstm.setLong(++i, owner);
}
if (namePrefix != null) {
pstm.setString(++i, namePrefix);
}
if (baseContractPrefix != null) {
pstm.setString(++i, baseContractPrefix);
}
if (blockTimestamp != null) {
pstm.setInt(++i, blockTimestamp);
}
if (status != null) {
pstm.setString(++i, status);
}
DbUtils.setLimits(++i, pstm, from, to);
return txQueryExecutionHelper.executeListQuery(con -> {
PreparedStatement pstm = con.prepareStatement(sql.toString());
query.setPreparedStatementParameters(pstm);
pstm.setFetchSize(50);
try (ResultSet rs = pstm.executeQuery()) {
List<ContractDetails> list = new ArrayList<>();
while (rs.next()) {
list.add(smcContractDetailsRowMapper.map(rs, null));
}
return list;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return pstm;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package com.apollocurrency.aplwallet.apl.core.http;

import com.apollocurrency.aplwallet.apl.core.model.Block;
import com.apollocurrency.aplwallet.apl.core.model.Sort;
import com.apollocurrency.aplwallet.apl.util.api.Sort;
import com.apollocurrency.aplwallet.apl.core.model.Transaction;
import com.apollocurrency.aplwallet.apl.core.entity.state.account.Account;
import com.apollocurrency.aplwallet.apl.core.entity.state.account.AccountAsset;
Expand Down
Loading