Skip to content

Commit 1696dbb

Browse files
committed
add checkPermissionOprations
2 parents 1d4f511 + 4abe342 commit 1696dbb

7 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎src/main/java/org/tron/core/capsule/AccountCapsule.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ public ByteString getAccountId() {
176176
return this.account.getAccountId();
177177
}
178178

179+
public byte[] getWitnessPermissionAddress() {
180+
if (this.account.getWitnessPermission().getKeysCount() == 0) {
181+
return getAddress().toByteArray();
182+
} else {
183+
return this.account.getWitnessPermission().getKeys(0).getAddress().toByteArray();
184+
}
185+
}
186+
179187
public long getBalance() {
180188
return this.account.getBalance();
181189
}

‎src/main/java/org/tron/core/capsule/BlockCapsule.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.tron.common.utils.Time;
3434
import org.tron.core.capsule.utils.MerkleTree;
3535
import org.tron.core.config.Parameter.ChainConstant;
36+
import org.tron.core.db.Manager;
3637
import org.tron.core.exception.BadItemException;
3738
import org.tron.core.exception.ValidateSignatureException;
3839
import org.tron.protos.Protocol.Block;
@@ -209,13 +210,22 @@ private Sha256Hash getRawHash() {
209210
return Sha256Hash.of(this.block.getBlockHeader().getRawData().toByteArray());
210211
}
211212

212-
public boolean validateSignature() throws ValidateSignatureException {
213+
public boolean validateSignature(Manager dbManager) throws ValidateSignatureException {
213214
try {
214-
return Arrays
215-
.equals(ECKey.signatureToAddress(getRawHash().getBytes(),
216-
TransactionCapsule
217-
.getBase64FromByteString(block.getBlockHeader().getWitnessSignature())),
218-
block.getBlockHeader().getRawData().getWitnessAddress().toByteArray());
215+
byte[] toAddress = ECKey.signatureToAddress(getRawHash().getBytes(),
216+
TransactionCapsule.getBase64FromByteString(block.getBlockHeader().getWitnessSignature()));
217+
218+
byte[] witnessAddress = block.getBlockHeader().getRawData().getWitnessAddress().toByteArray();
219+
220+
if (dbManager.getDynamicPropertiesStore().getAllowMultiSign() != 1) {
221+
return Arrays
222+
.equals(toAddress, witnessAddress);
223+
} else {
224+
return Arrays
225+
.equals(toAddress,
226+
dbManager.getAccountStore().get(witnessAddress).getWitnessPermissionAddress());
227+
}
228+
219229
} catch (SignatureException e) {
220230
throw new ValidateSignatureException(e.getMessage());
221231
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ public synchronized void pushBlock(final BlockCapsule block)
914914
try (PendingManager pm = new PendingManager(this)) {
915915

916916
if (!block.generatedByMyself) {
917-
if (!block.validateSignature()) {
917+
if (!block.validateSignature(this)) {
918918
logger.warn("The signature is not validated.");
919919
throw new BadBlockException("The signature is not validated");
920920
}
@@ -996,6 +996,7 @@ public synchronized void pushBlock(final BlockCapsule block)
996996
return;
997997
}
998998
try (ISession tmpSession = revokingStore.buildSession()) {
999+
9991000
applyBlock(newBlock);
10001001
tmpSession.commit();
10011002
// if event subscribe is enabled, post block trigger to queue

‎src/main/resources/config.conf‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ genesis.block = {
380380
parentHash = "0xe58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f"
381381
}
382382

383+
// Optional.The default is empty.
384+
// It is used when the witness account has set the witnessPermission.
385+
// When it is not empty, the localWitnessAccountAddress represents the address of the witness account,
386+
// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account.
387+
// When it is empty,the localwitness is configured with the private key of the witness account.
388+
389+
//localWitnessAccountAddress =
390+
383391
localwitness = [
384392
]
385393

‎src/test/java/org/tron/core/capsule/AccountCapsuleTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.protobuf.ByteString;
44
import java.io.File;
5+
import java.util.Arrays;
56
import java.util.List;
67
import java.util.Map;
78
import java.util.Random;
@@ -20,6 +21,8 @@
2021
import org.tron.core.db.Manager;
2122
import org.tron.protos.Contract;
2223
import org.tron.protos.Protocol.AccountType;
24+
import org.tron.protos.Protocol.Key;
25+
import org.tron.protos.Protocol.Permission;
2326
import org.tron.protos.Protocol.Vote;
2427

2528
@Ignore
@@ -307,4 +310,30 @@ public void sameTokenNameOpenAssertAmountV2test() {
307310
Assert.assertEquals(accountCapsule.getAssetMapV2().get(String.valueOf(id+1)).longValue(), 500L);
308311
}
309312

313+
@Test
314+
public void witnessPermissionTest() {
315+
AccountCapsule accountCapsule =
316+
new AccountCapsule(
317+
ByteString.copyFromUtf8("owner"),
318+
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
319+
AccountType.Normal,
320+
10000);
321+
322+
Assert.assertTrue(
323+
Arrays.equals(ByteArray.fromHexString(OWNER_ADDRESS),
324+
accountCapsule.getWitnessPermissionAddress()));
325+
326+
String witnessPermissionAddress =
327+
Wallet.getAddressPreFixString() + "cc6a17a49648a8ad32055c06f60fa14ae46df912cc";
328+
accountCapsule = new AccountCapsule(accountCapsule.getInstance().toBuilder().
329+
setWitnessPermission(Permission.newBuilder().addKeys(
330+
Key.newBuilder()
331+
.setAddress(ByteString.copyFrom(ByteArray.fromHexString(witnessPermissionAddress)))
332+
.build()).
333+
build()).build());
334+
335+
Assert.assertTrue(
336+
Arrays.equals(ByteArray.fromHexString(witnessPermissionAddress),
337+
accountCapsule.getWitnessPermissionAddress()));
338+
}
310339
}

‎src/test/java/org/tron/core/config/args/ArgsTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ public void get() {
7777
Assert.assertEquals(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, args.getMaxHeaderListSize());
7878
Assert.assertEquals(1L, args.getAllowCreationOfContracts());
7979

80-
Assert.assertEquals("f4df789d3210ac881cb900464dd30409453044d2777060a0c391cbdf4c6a4f57",
80+
Assert.assertEquals("f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62",
8181
args.getLocalWitnesses().getPrivateKey());
82-
Assert.assertEquals("",
82+
Assert.assertEquals("a0299f3db80a24b20a254b89ce639d59132f157f13",
8383
args.getLocalWitnesses().getWitnessAccountAddress());
8484

85+
8586
}
8687
}

‎src/test/resources/config-test.conf‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ seed.node = {
247247
parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000"
248248
}
249249

250+
251+
// Optional.The default is empty.
252+
// It is used when the witness account has set the witnessPermission.
253+
// When it is not empty, the localWitnessAccountAddress represents the address of the witness account,
254+
// and the localwitness is configured with the private key of the witnessPermissionAddress in the witness account.
255+
// When it is empty,the localwitness is configured with the private key of the witness account.
256+
257+
//localWitnessAccountAddress =
258+
250259
localwitness = [
251260
f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62
252261
]

0 commit comments

Comments
 (0)