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 9 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.Range;
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, Range blockRange, Range 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.to().intValue() > 0) {
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().intValue());
if (name != null) {
pstm.setString(++i, name);
}

if (heightTo > 0) {
pstm.setInt(++i, heightTo);
if (blockRange.to().intValue() > 0) {
AndrewBoyarsky marked this conversation as resolved.
Show resolved Hide resolved
pstm.setInt(++i, blockRange.to().intValue());
}
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
Loading