Skip to content

Commit

Permalink
Merge branch 'main' into 03607-add-contract-initcode
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Li <xin.li@hedera.com>
  • Loading branch information
xin-hedera committed Apr 22, 2022
2 parents 800c9e9 + a6793ad commit 22159f6
Show file tree
Hide file tree
Showing 22 changed files with 426 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@
@Entity
@NoArgsConstructor
@SuperBuilder
@ToString(exclude = {"evmAddress", "initcode"})
public class Contract extends AbstractEntity implements Aliasable {

@Column(updatable = false)
@ToString.Exclude
private byte[] evmAddress;

@Column(updatable = false)
@Convert(converter = FileIdConverter.class)
private EntityId fileId;

@Column(updatable = false)
@ToString.Exclude
private byte[] initcode;

@Convert(converter = UnknownIdConverter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* ‍
*/

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
Expand All @@ -41,22 +42,33 @@
@NoArgsConstructor
@Upsertable
public class Schedule {

@Column(updatable = false)
private Long consensusTimestamp;

@Column(updatable = false)
@Convert(converter = AccountIdConverter.class)
private EntityId creatorAccountId;

private Long executedTimestamp;

@Column(updatable = false)
private Long expirationTime;

@Column(updatable = false)
@Convert(converter = AccountIdConverter.class)
private EntityId payerAccountId;

@Id
private Long scheduleId;

@Column(updatable = false)
@ToString.Exclude
private byte[] transactionBody;

@Column(updatable = false)
private boolean waitForExpiry;

public void setScheduleId(EntityId scheduleId) {
this.scheduleId = scheduleId != null ? scheduleId.getId() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

import com.google.common.collect.Range;
import com.google.protobuf.ByteString;
import com.hederahashgraph.api.proto.java.Key;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
import com.hederahashgraph.api.proto.java.SignaturePair;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.SecureRandom;
Expand All @@ -48,9 +51,6 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionOperations;

import com.hederahashgraph.api.proto.java.Key;
import com.hederahashgraph.api.proto.java.ResponseCodeEnum;
import com.hederahashgraph.api.proto.java.SignaturePair;
import com.hedera.mirror.common.domain.addressbook.AddressBook;
import com.hedera.mirror.common.domain.addressbook.AddressBookEntry;
import com.hedera.mirror.common.domain.addressbook.AddressBookServiceEndpoint;
Expand Down Expand Up @@ -363,9 +363,11 @@ public DomainWrapper<Schedule, Schedule.ScheduleBuilder> schedule() {
var builder = Schedule.builder()
.consensusTimestamp(timestamp())
.creatorAccountId(entityId(ACCOUNT))
.expirationTime(timestamp())
.payerAccountId(entityId(ACCOUNT))
.scheduleId(entityId(SCHEDULE).getId())
.transactionBody(bytes(64));
.transactionBody(bytes(64))
.waitForExpiry(true);
return new DomainWrapperImpl<>(builder, builder::build);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected void persistItems(Collection<?> items, Connection connection) {
log.debug("Inserted {} and updated {} from a total of {} rows to {}",
insertCount, updateCount, items.size(), finalTableName);
} catch (Exception e) {
throw new ParserException(String.format("Error copying %d items to table %s", items.size(), tableName), e);
throw new ParserException(String.format("Error copying %d items to table %s", items.size(), finalTableName), e);
}
}

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

import com.google.protobuf.ByteString;
import com.google.protobuf.UnknownFieldSet;

import com.hederahashgraph.api.proto.java.AccountAmount;
import com.hederahashgraph.api.proto.java.ConsensusMessageChunkInfo;
import com.hederahashgraph.api.proto.java.ConsensusSubmitMessageTransactionBody;
Expand All @@ -34,7 +33,6 @@
import com.hederahashgraph.api.proto.java.FractionalFee;
import com.hederahashgraph.api.proto.java.NftTransfer;
import com.hederahashgraph.api.proto.java.RoyaltyFee;
import com.hederahashgraph.api.proto.java.ScheduleCreateTransactionBody;
import com.hederahashgraph.api.proto.java.SignaturePair;
import com.hederahashgraph.api.proto.java.TokenAssociateTransactionBody;
import com.hederahashgraph.api.proto.java.TokenAssociation;
Expand Down Expand Up @@ -233,8 +231,6 @@ public void onItem(RecordItem recordItem) throws ImporterException {
insertTokenUpdate(recordItem);
} else if (body.hasTokenWipe()) {
insertTokenAccountWipe(recordItem);
} else if (body.hasScheduleCreate()) {
insertScheduleCreate(recordItem);
}

// Record token transfers can be populated for multiple transaction types
Expand Down Expand Up @@ -953,28 +949,6 @@ private void updateTokenSupply(EntityId tokenId, long newTotalSupply, long modif
updateToken(token, modifiedTimestamp);
}

private void insertScheduleCreate(RecordItem recordItem) {
if (entityProperties.getPersist().isSchedules()) {
ScheduleCreateTransactionBody scheduleCreateTransactionBody = recordItem.getTransactionBody()
.getScheduleCreate();
long consensusTimestamp = recordItem.getConsensusTimestamp();
var scheduleId = EntityId.of(recordItem.getRecord().getReceipt().getScheduleID());
var creatorAccount = recordItem.getPayerAccountId();
var payerAccount = creatorAccount;
if (scheduleCreateTransactionBody.hasPayerAccountID()) {
payerAccount = EntityId.of(scheduleCreateTransactionBody.getPayerAccountID());
}

Schedule schedule = new Schedule();
schedule.setConsensusTimestamp(consensusTimestamp);
schedule.setCreatorAccountId(creatorAccount);
schedule.setPayerAccountId(payerAccount);
schedule.setScheduleId(scheduleId);
schedule.setTransactionBody(scheduleCreateTransactionBody.getScheduledTransactionBody().toByteArray());
entityListener.onSchedule(schedule);
}
}

private void insertTransactionSignatures(EntityId entityId, long consensusTimestamp,
List<SignaturePair> signaturePairList) {
Set<ByteString> publicKeyPrefixes = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,15 +24,22 @@

import com.hedera.mirror.common.domain.entity.Entity;
import com.hedera.mirror.common.domain.entity.EntityId;
import com.hedera.mirror.common.domain.transaction.TransactionType;
import com.hedera.mirror.common.domain.schedule.Schedule;
import com.hedera.mirror.common.domain.transaction.RecordItem;
import com.hedera.mirror.common.domain.transaction.Transaction;
import com.hedera.mirror.common.domain.transaction.TransactionType;
import com.hedera.mirror.common.util.DomainUtils;
import com.hedera.mirror.importer.parser.record.entity.EntityListener;
import com.hedera.mirror.importer.parser.record.entity.EntityProperties;

@Named
class ScheduleCreateTransactionHandler extends AbstractEntityCrudTransactionHandler<Entity> {

ScheduleCreateTransactionHandler(EntityListener entityListener) {
private final EntityProperties entityProperties;

ScheduleCreateTransactionHandler(EntityListener entityListener, EntityProperties entityProperties) {
super(entityListener, TransactionType.SCHEDULECREATE);
this.entityProperties = entityProperties;
}

@Override
Expand All @@ -51,4 +58,28 @@ protected void doUpdateEntity(Entity entity, RecordItem recordItem) {
entity.setMemo(transactionBody.getMemo());
entityListener.onEntity(entity);
}

@Override
protected void doUpdateTransaction(Transaction transaction, RecordItem recordItem) {
if (!recordItem.isSuccessful() || !entityProperties.getPersist().isSchedules()) {
return;
}

var body = recordItem.getTransactionBody().getScheduleCreate();
long consensusTimestamp = recordItem.getConsensusTimestamp();
var creatorAccount = recordItem.getPayerAccountId();
var expirationTime = body.hasExpirationTime() ? DomainUtils.timestampInNanosMax(body.getExpirationTime()) : null;
var payerAccount = body.hasPayerAccountID() ? EntityId.of(body.getPayerAccountID()) : creatorAccount;
var scheduleId = EntityId.of(recordItem.getRecord().getReceipt().getScheduleID());

Schedule schedule = new Schedule();
schedule.setConsensusTimestamp(consensusTimestamp);
schedule.setCreatorAccountId(creatorAccount);
schedule.setExpirationTime(expirationTime);
schedule.setPayerAccountId(payerAccount);
schedule.setScheduleId(scheduleId);
schedule.setTransactionBody(body.getScheduledTransactionBody().toByteArray());
schedule.setWaitForExpiry(body.getWaitForExpiry());
entityListener.onSchedule(schedule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -34,6 +34,10 @@
@RequiredArgsConstructor
public class GenericUpsertQueryGenerator implements UpsertQueryGenerator {

private static final String UPSERT_TEMPLATE = "/db/template/upsert.vm";

private static final String UPSERT_HISTORY_TEMPLATE = "/db/template/upsert_history.vm";

private final UpsertEntity upsertEntity;

@Override
Expand Down Expand Up @@ -67,7 +71,8 @@ public String getInsertQuery() {
velocityEngine.setProperty("resource.loader.class.class", ClasspathResourceLoader.class.getName());
velocityEngine.init();

Template template = velocityEngine.getTemplate("/db/template/upsert.vm");
String templatePath = upsertEntity.getUpsertable().history() ? UPSERT_HISTORY_TEMPLATE : UPSERT_TEMPLATE;
Template template = velocityEngine.getTemplate(templatePath);

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("finalTable", getFinalTableName());
Expand All @@ -81,6 +86,8 @@ public String getInsertQuery() {
velocityContext.put("idColumns", upsertEntity.columns(UpsertColumn::isId, "e.{0}"));
velocityContext.put("idJoin", upsertEntity.columns(UpsertColumn::isId, "e.{0} = t.{0}", " and "));
velocityContext.put("insertColumns", upsertEntity.columns("{0}"));
velocityContext.put("notNullableColumn",
upsertEntity.column(c -> !c.isNullable() && !c.isId(), "coalesce(t.{0}, e.{0}) is not null"));
velocityContext.put("updateColumns", upsertEntity.columns(UpsertColumn::isUpdatable, "{0} = excluded.{0}"));

StringWriter writer = new StringWriter();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -30,6 +30,7 @@ class UpsertColumn implements Comparable<UpsertColumn> {
private final boolean history;
private final boolean id;
private final String name;
private final boolean nullable;
private final boolean updatable;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -38,6 +38,14 @@ class UpsertEntity {
private final Upsertable upsertable;
private final Set<UpsertColumn> columns;

public String column(Predicate<UpsertColumn> filter, String pattern) {
return columns.stream()
.filter(filter)
.findFirst()
.map(c -> MessageFormat.format(pattern, c.getName(), c.getDefaultValue()))
.orElse("");
}

public String columns(String pattern) {
return columns(c -> true, pattern, ",");
}
Expand Down

0 comments on commit 22159f6

Please sign in to comment.