Skip to content

Commit ce0cd24

Browse files
authored
feat(net): enforce single contract and disallow unused auths field (#7064)
1 parent 0b59aca commit ce0cd24

5 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,4 +880,8 @@ public int getRetCount() {
880880
public int getContractCount() {
881881
return this.getInstance().getRawData().getContractCount();
882882
}
883+
884+
public boolean hasAuths() {
885+
return this.getInstance().getRawData().getAuthsCount() > 0;
886+
}
883887
}

‎framework/src/main/java/org/tron/core/db/Manager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,10 @@ public boolean pushTransaction(final TransactionCapsule trx)
850850
TooBigTransactionException, TransactionExpirationException,
851851
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException {
852852

853+
if (trx.hasAuths()) {
854+
throw new ContractValidateException("Transaction auths are not supported.");
855+
}
856+
853857
if (isShieldedTransaction(trx.getInstance()) && !chainBaseManager.getDynamicPropertiesStore()
854858
.supportShieldedTransaction()) {
855859
throw new ContractValidateException("ShieldedTransferContract is not supported.");

‎framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.tron.common.utils.Sha256Hash;
2222
import org.tron.core.ChainBaseManager;
2323
import org.tron.core.Constant;
24+
import org.tron.core.actuator.ActuatorConstant;
2425
import org.tron.core.capsule.TransactionCapsule;
2526
import org.tron.core.config.args.Args;
2627
import org.tron.core.exception.P2pException;
@@ -107,6 +108,9 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
107108
break;
108109
}
109110
TransactionCapsule capsule = new TransactionCapsule(trx);
111+
if (capsule.hasAuths()) {
112+
continue;
113+
}
110114
capsule.sanitize();
111115
capsule.resetResult();
112116
if (capsule.getSerializedSize() > Constant.TRANSACTION_MAX_BYTE_SIZE) {
@@ -158,9 +162,10 @@ private Set<Sha256Hash> check(PeerConnection peer, TransactionsMessage msg)
158162
throw new P2pException(TypeEnum.BAD_MESSAGE,
159163
"trx: " + id + " without request.");
160164
}
161-
if (trx.getRawData().getContractCount() < 1) {
162-
throw new P2pException(TypeEnum.BAD_TRX,
163-
"tx " + item.getHash() + " contract size should be greater than 0");
165+
int contractCount = trx.getRawData().getContractCount();
166+
if (contractCount != 1) {
167+
throw new P2pException(TypeEnum.BAD_TRX, String.format(
168+
"tx %s " + ActuatorConstant.CONTRACT_SIZE_NOT_EQUAL_TO_ONE, id, contractCount));
164169
}
165170
}
166171
for (Transaction trx : list) {

‎framework/src/test/java/org/tron/core/db/ManagerTest.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,50 @@ private void checkForkRecovery(boolean failRestore, boolean retryOldHead,
21432143
}
21442144
}
21452145

2146+
@Test
2147+
public void authsRejectedFromPendingButAcceptedInBlock() throws Exception {
2148+
String key = PublicMethod.getRandomPrivateKey();
2149+
byte[] privateKey = ByteArray.fromHexString(key);
2150+
ByteString address = ByteString.copyFrom(ECKey.fromPrivate(privateKey).getAddress());
2151+
chainManager.getAccountStore().put(address.toByteArray(),
2152+
new AccountCapsule(Account.newBuilder().setAddress(address).build()));
2153+
chainManager.getWitnessScheduleStore().saveActiveWitnesses(new ArrayList<>());
2154+
chainManager.addWitness(address);
2155+
chainManager.getWitnessStore().put(address.toByteArray(), new WitnessCapsule(address));
2156+
long time = 1533529947843L;
2157+
dbManager.pushBlock(new BlockCapsule(
2158+
blockGenerate.getSignedBlock(address, time, privateKey)));
2159+
Map<ByteString, String> keys = addTestWitnessAndAccount();
2160+
keys.put(address, key);
2161+
2162+
ECKey ownerKey = new ECKey(Utils.getRandom());
2163+
byte[] owner = ownerKey.getAddress();
2164+
chainManager.getAccountStore().put(owner, new AccountCapsule(Account.newBuilder()
2165+
.setAddress(ByteString.copyFrom(owner)).setBalance(1_000_000_000L).build()));
2166+
BlockCapsule parent = createTestBlockCapsule(time + 3000, chainManager.getHeadBlockNum() + 1,
2167+
chainManager.getHeadBlockId().getByteString(), keys);
2168+
dbManager.pushBlock(parent);
2169+
2170+
Transaction base = transfer(owner, address.toByteArray(), 1L, parent, time + 1_000_000L)
2171+
.getInstance();
2172+
Transaction withAuths = base.toBuilder().setRawData(base.getRawData().toBuilder()
2173+
.addAuths(Protocol.authority.getDefaultInstance())).build();
2174+
ContractValidateException pending = assertThrows(ContractValidateException.class,
2175+
() -> dbManager.pushTransaction(new TransactionCapsule(withAuths)));
2176+
Assert.assertEquals(ContractValidateException.class, pending.getClass());
2177+
Assert.assertEquals("Transaction auths are not supported.", pending.getMessage());
2178+
2179+
TransactionCapsule signed = new TransactionCapsule(withAuths);
2180+
signed.sign(ownerKey.getPrivKeyBytes());
2181+
BlockCapsule block = new BlockCapsule(blockWithTransfer(time + 6000, parent.getNum() + 1,
2182+
parent.getBlockId().getByteString(), keys, signed).getData());
2183+
dbManager.pushBlock(block);
2184+
2185+
Assert.assertEquals(block.getBlockId(), chainManager.getHeadBlockId());
2186+
Assert.assertTrue(chainManager.getTransactionStore()
2187+
.has(signed.getTransactionId().getBytes()));
2188+
}
2189+
21462190
private TransactionCapsule transfer(byte[] owner, byte[] to, long amount,
21472191
BlockCapsule refBlock, long expiration) {
21482192
TransferContract contract = TransferContract.newBuilder()

‎framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void testProcessMessage() {
174174
new TransactionsMessage(transactionList2)));
175175
Assert.assertEquals(P2pException.TypeEnum.BAD_TRX, emptyContract.getType());
176176
Assert.assertTrue(emptyContract.getMessage()
177-
.contains("contract size should be greater than 0"));
177+
.contains("contract size should be exactly 1"));
178178
} catch (Exception e) {
179179
Assert.fail(e.getMessage());
180180
} finally {
@@ -970,6 +970,28 @@ public void testOversizedTransactionDroppedBeforeQueueAdmission() throws Excepti
970970
}
971971
}
972972

973+
@Test
974+
public void testMultipleContractsRejectedBeforeAdmission() throws Exception {
975+
Protocol.Transaction base = admissionTransaction(false);
976+
Protocol.Transaction twoContracts = base.toBuilder().setRawData(base.getRawData().toBuilder()
977+
.addContract(base.getRawData().getContract(0))).build();
978+
assertProtocolRejection(Collections.singletonList(twoContracts), 1, TypeEnum.BAD_TRX);
979+
}
980+
981+
@Test
982+
public void testAuthsDropKeepsBatch() throws Exception {
983+
List<Protocol.Transaction> base = buildTransferMessage(2).getTransactions()
984+
.getTransactionsList();
985+
Protocol.Transaction valid = base.get(1);
986+
assertSignatureBatch(Arrays.asList(withAuths(base.get(0)), valid),
987+
Collections.singletonList(valid), Collections.emptyList());
988+
}
989+
990+
private Protocol.Transaction withAuths(Protocol.Transaction transaction) {
991+
return transaction.toBuilder().setRawData(transaction.getRawData().toBuilder()
992+
.addAuths(Protocol.authority.getDefaultInstance())).build();
993+
}
994+
973995
@Test
974996
public void testQueueAdmissionSizeBoundary() throws Exception {
975997
int limit = (int) Constant.TRANSACTION_MAX_BYTE_SIZE;

0 commit comments

Comments
 (0)