Skip to content

Commit

Permalink
feat(mechanism): add stake info for log trigger (#5419)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlChaoCarl authored Sep 4, 2023
1 parent 068764b commit d6ff733
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.tron.common.logsfilter.trigger;

import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -91,6 +93,10 @@ public class TransactionLogTrigger extends Trigger {
@Setter
private long energyUnitPrice;

@Getter
@Setter
private Map<String, Long> extMap;

public TransactionLogTrigger() {
setTriggerName(Trigger.TRANSACTION_TRIGGER_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tron.common.logsfilter.capsule;

import static org.tron.protos.Protocol.Transaction.Contract.ContractType.CreateSmartContract;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
Expand All @@ -26,7 +27,14 @@
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.TransactionInfo;
import org.tron.protos.contract.AssetIssueContractOuterClass.TransferAssetContract;
import org.tron.protos.contract.BalanceContract.CancelAllUnfreezeV2Contract;
import org.tron.protos.contract.BalanceContract.DelegateResourceContract;
import org.tron.protos.contract.BalanceContract.FreezeBalanceV2Contract;
import org.tron.protos.contract.BalanceContract.TransferContract;
import org.tron.protos.contract.BalanceContract.UnDelegateResourceContract;
import org.tron.protos.contract.BalanceContract.UnfreezeBalanceContract;
import org.tron.protos.contract.BalanceContract.UnfreezeBalanceV2Contract;
import org.tron.protos.contract.BalanceContract.WithdrawExpireUnfreezeContract;
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract;

Expand Down Expand Up @@ -156,6 +164,82 @@ public TransactionLogTriggerCapsule(TransactionCapsule trxCapsule, BlockCapsule
StringUtil.encode58Check(createSmartContract.getOwnerAddress().toByteArray()));
}
break;
case UnfreezeBalanceContract:
UnfreezeBalanceContract unfreezeBalanceContract = contractParameter
.unpack(UnfreezeBalanceContract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(unfreezeBalanceContract.getOwnerAddress().toByteArray()));
if (!ByteString.EMPTY.equals(unfreezeBalanceContract.getReceiverAddress())) {
transactionLogTrigger.setToAddress(StringUtil
.encode58Check(unfreezeBalanceContract.getReceiverAddress().toByteArray()));
}
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(
transactionInfo.getUnfreezeAmount());
break;
case FreezeBalanceV2Contract:
FreezeBalanceV2Contract freezeBalanceV2Contract = contractParameter
.unpack(FreezeBalanceV2Contract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(freezeBalanceV2Contract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(freezeBalanceV2Contract.getFrozenBalance());
break;
case UnfreezeBalanceV2Contract:
UnfreezeBalanceV2Contract unfreezeBalanceV2Contract = contractParameter
.unpack(UnfreezeBalanceV2Contract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(unfreezeBalanceV2Contract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(
unfreezeBalanceV2Contract.getUnfreezeBalance());
break;
case WithdrawExpireUnfreezeContract:
WithdrawExpireUnfreezeContract withdrawExpireUnfreezeContract = contractParameter
.unpack(WithdrawExpireUnfreezeContract.class);

transactionLogTrigger.setFromAddress(StringUtil.encode58Check(
withdrawExpireUnfreezeContract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(transactionInfo.getWithdrawExpireAmount());
break;
case DelegateResourceContract:
DelegateResourceContract delegateResourceContract = contractParameter
.unpack(DelegateResourceContract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(delegateResourceContract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setToAddress(StringUtil
.encode58Check(delegateResourceContract.getReceiverAddress().toByteArray()));
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(
delegateResourceContract.getBalance());
break;
case UnDelegateResourceContract:
UnDelegateResourceContract unDelegateResourceContract = contractParameter
.unpack(UnDelegateResourceContract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(unDelegateResourceContract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setToAddress(StringUtil.encode58Check(
unDelegateResourceContract.getReceiverAddress().toByteArray()));

transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setAssetAmount(
unDelegateResourceContract.getBalance());
break;
case CancelAllUnfreezeV2Contract:
CancelAllUnfreezeV2Contract cancelAllUnfreezeV2Contract = contractParameter
.unpack(CancelAllUnfreezeV2Contract.class);

transactionLogTrigger.setFromAddress(StringUtil
.encode58Check(cancelAllUnfreezeV2Contract.getOwnerAddress().toByteArray()));
transactionLogTrigger.setAssetName("trx");
transactionLogTrigger.setExtMap(transactionInfo.getCancelUnfreezeV2AmountMap());
break;
default:
break;
}
Expand Down Expand Up @@ -269,4 +353,5 @@ private List<InternalTransactionPojo> getInternalTransactionList(
public void processTrigger() {
EventPluginLoader.getInstance().postTransactionTrigger(transactionLogTrigger);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.tron.common.logsfilter;

import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;

import com.google.protobuf.ByteString;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.p2p.utils.ByteArray;
import org.tron.protos.Protocol;
import org.tron.protos.contract.BalanceContract;
import org.tron.protos.contract.Common;

public class TransactionLogTriggerCapsuleTest {

private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";

public TransactionCapsule transactionCapsule;
public BlockCapsule blockCapsule;

@Before
public void setup() {
blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH,
System.currentTimeMillis(), Sha256Hash.ZERO_HASH.getByteString());
}

@Test
public void testConstructorWithUnfreezeBalanceTrxCapsule() {
BalanceContract.UnfreezeBalanceContract.Builder builder2 =
BalanceContract.UnfreezeBalanceContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setReceiverAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.UnfreezeBalanceContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
}


@Test
public void testConstructorWithFreezeBalanceV2TrxCapsule() {
BalanceContract.FreezeBalanceV2Contract.Builder builder2 =
BalanceContract.FreezeBalanceV2Contract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setFrozenBalance(TRX_PRECISION + 100000)
.setResource(Common.ResourceCode.BANDWIDTH);
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.FreezeBalanceV2Contract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName());
Assert.assertEquals(TRX_PRECISION + 100000,
triggerCapsule.getTransactionLogTrigger().getAssetAmount());
}

@Test
public void testConstructorWithUnfreezeBalanceV2TrxCapsule() {
BalanceContract.UnfreezeBalanceV2Contract.Builder builder2 =
BalanceContract.UnfreezeBalanceV2Contract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setUnfreezeBalance(TRX_PRECISION + 4000)
.setResource(Common.ResourceCode.BANDWIDTH);
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.UnfreezeBalanceV2Contract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName());
Assert.assertEquals(TRX_PRECISION + 4000,
triggerCapsule.getTransactionLogTrigger().getAssetAmount());
}


@Test
public void testConstructorWithWithdrawExpireTrxCapsule() {
BalanceContract.WithdrawExpireUnfreezeContract.Builder builder2 =
BalanceContract.WithdrawExpireUnfreezeContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.WithdrawExpireUnfreezeContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName());
Assert.assertEquals(0L, triggerCapsule.getTransactionLogTrigger().getAssetAmount());
}


@Test
public void testConstructorWithDelegateResourceTrxCapsule() {
BalanceContract.DelegateResourceContract.Builder builder2 =
BalanceContract.DelegateResourceContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setReceiverAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)))
.setBalance(TRX_PRECISION + 2000);
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.DelegateResourceContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName());
Assert.assertEquals(TRX_PRECISION + 2000,
triggerCapsule.getTransactionLogTrigger().getAssetAmount());
}

@Test
public void testConstructorWithUnDelegateResourceTrxCapsule() {
BalanceContract.UnDelegateResourceContract.Builder builder2 =
BalanceContract.UnDelegateResourceContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setReceiverAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)))
.setBalance(TRX_PRECISION + 10000);
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.UnDelegateResourceContract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
Assert.assertEquals("trx", triggerCapsule.getTransactionLogTrigger().getAssetName());
Assert.assertEquals(TRX_PRECISION + 10000,
triggerCapsule.getTransactionLogTrigger().getAssetAmount());
}

@Test
public void testConstructorWithCancelAllUnfreezeTrxCapsule() {
BalanceContract.CancelAllUnfreezeV2Contract.Builder builder2 =
BalanceContract.CancelAllUnfreezeV2Contract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
transactionCapsule = new TransactionCapsule(builder2.build(),
Protocol.Transaction.Contract.ContractType.CancelAllUnfreezeV2Contract);

TransactionLogTriggerCapsule triggerCapsule =
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);

Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
}

}

0 comments on commit d6ff733

Please sign in to comment.