Skip to content

Commit bdfcf6f

Browse files
Merge branch 'develop' into add_fullnode_api
2 parents d9ea656 + ee97761 commit bdfcf6f

11 files changed

Lines changed: 249 additions & 5 deletions

File tree

‎src/main/java/org/tron/core/WalletSolidity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public AssetIssueList getAssetIssueListByTimestamp(long timestamp) {
6666

6767
public AssetIssueList getAssetIssueByAccount(ByteString address) {
6868
List<AssetIssueContract> assetIssueByOwnerAddress = storeAPI
69-
.getAssetIssueByOwnerAddress(address.toStringUtf8());
69+
.getAssetIssueByOwnerAddress(ByteArray.toHexString(address.toByteArray()));
7070
AssetIssueList assetIssueList =
7171
AssetIssueList.newBuilder().addAllAssetIssue(assetIssueByOwnerAddress).build();
7272
return assetIssueList;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public boolean checkBalance(byte[] address, byte[] to, long amount, long balance
203203
return true;
204204
}
205205

206+
@Deprecated
206207
public void sign(byte[] privateKey) {
207208
ECKey ecKey = ECKey.fromPrivate(privateKey);
208209
ECDSASignature signature = ecKey.sign(getRawHash().getBytes());

‎src/main/java/org/tron/core/net/node/NodeImpl.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ private void onHandleBlockMessage(PeerConnection peer, BlockMessage blkMsg) {
600600
processAdvBlock(peer, blkMsg.getBlockCapsule());
601601
startFetchItem();
602602
} else if (syncBlockRequested.containsKey(blockId)) {
603+
if (!peer.getSyncFlag()){
604+
logger.info("rcv a block {} from no need sync peer {}", blockId.getNum(), peer.getNode());
605+
return;
606+
}
603607
//sync mode
604608
syncBlockRequested.remove(blockId);
605609
//peer.getSyncBlockToFetch().remove(blockId);
@@ -705,12 +709,22 @@ private void processSyncBlock(BlockCapsule block) {
705709
ReasonCode finalReason = reason;
706710
getActivePeer().stream()
707711
.filter(peer -> peer.getBlockInProc().contains(block.getBlockId()))
708-
.forEach(peer -> disconnectPeer(peer, finalReason));
712+
.forEach(peer -> cleanUpSyncPeer(peer, finalReason));
709713
}
710714

711715
isHandleSyncBlockActive = true;
712716
}
713717

718+
private void cleanUpSyncPeer(PeerConnection peer, ReasonCode reasonCode){
719+
peer.setSyncFlag(false);
720+
while (!peer.getSyncBlockToFetch().isEmpty()){
721+
BlockId blockId = peer.getSyncBlockToFetch().pop();
722+
blockWaitToProc.remove(blockId);
723+
blockJustReceived.remove(blockId);
724+
}
725+
disconnectPeer(peer, reasonCode);
726+
}
727+
714728
private void onHandleTransactionMessage(PeerConnection peer, TransactionMessage trxMsg) {
715729
//logger.info("on handle transaction message");
716730
try {
@@ -719,6 +733,7 @@ private void onHandleTransactionMessage(PeerConnection peer, TransactionMessage
719733
} else {
720734
peer.getAdvObjWeRequested().remove(trxMsg.getMessageId());
721735
del.handleTransaction(trxMsg.getTransactionCapsule());
736+
broadcast(trxMsg);
722737
}
723738
} catch (TraitorPeerException e) {
724739
logger.error(e.getMessage());

‎src/main/java/org/tron/core/net/peer/PeerConnection.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public void setConnectTime(long connectTime) {
4444

4545
private long connectTime;
4646

47+
private boolean syncFlag = true;
48+
4749
//broadcast
4850
private Queue<Sha256Hash> invToUs = new LinkedBlockingQueue<>();
4951

@@ -219,6 +221,14 @@ public void setInvWeAdv(Queue<Sha256Hash> invWeAdv) {
219221
this.invWeAdv = invWeAdv;
220222
}
221223

224+
public boolean getSyncFlag(){
225+
return syncFlag;
226+
}
227+
228+
public void setSyncFlag(boolean syncFlag){
229+
this.syncFlag = syncFlag;
230+
}
231+
222232
public String logSyncStats() {
223233
//TODO: return tron sync status here.
224234
// int waitResp = lastReqSentTime > 0 ? (int) (System.currentTimeMillis() - lastReqSentTime) / 1000 : 0;

‎src/main/java/org/tron/program/SolidityNode.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.tron.common.application.Application;
88
import org.tron.common.application.ApplicationFactory;
99
import org.tron.common.overlay.client.DatabaseGrpcClient;
10+
import org.tron.common.overlay.discover.NodeManager;
11+
import org.tron.common.overlay.discover.UDPListener;
12+
import org.tron.common.overlay.server.ChannelManager;
13+
import org.tron.common.overlay.server.PeerServer;
1014
import org.tron.core.Constant;
1115
import org.tron.core.capsule.BlockCapsule;
1216
import org.tron.core.config.DefaultConfig;
@@ -58,9 +62,9 @@ private void syncLoop(Args args) {
5862
}
5963

6064
private void syncSolidityBlock() throws BadBlockException {
65+
DynamicProperties remoteDynamicProperties = databaseGrpcClient.getDynamicProperties();
66+
long remoteLastSolidityBlockNum = remoteDynamicProperties.getLastSolidityBlockNum();
6167
while (true) {
62-
DynamicProperties remoteDynamicProperties = databaseGrpcClient.getDynamicProperties();
63-
long remoteLastSolidityBlockNum = remoteDynamicProperties.getLastSolidityBlockNum();
6468
long lastSolidityBlockNum = dbManager.getDynamicPropertiesStore()
6569
.getLatestSolidifiedBlockNum();
6670
if (lastSolidityBlockNum < remoteLastSolidityBlockNum) {
@@ -119,6 +123,14 @@ public static void main(String[] args) throws InterruptedException {
119123
appT.startServices();
120124
// appT.startup();
121125

126+
//Disable peer discovery for solidity node
127+
UDPListener udpListener = context.getBean(UDPListener.class);
128+
udpListener.close();
129+
ChannelManager channelManager = context.getBean(ChannelManager.class);
130+
channelManager.close();
131+
NodeManager nodeManager = context.getBean(NodeManager.class);
132+
nodeManager.close();
133+
122134
SolidityNode node = new SolidityNode();
123135
node.setDbManager(appT.getDbManager());
124136
node.start(cfgArgs);

‎src/main/protos/api/api.proto‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ service Wallet {
105105
};
106106
}
107107

108+
rpc FreezeBalance (FreezeBalanceContract) returns (Transaction) {
109+
option (google.api.http) = {
110+
post: "/wallet/freezebalance"
111+
body: "*"
112+
};
113+
}
114+
115+
rpc UnfreezeBalance (UnfreezeBalanceContract) returns (Transaction) {
116+
option (google.api.http) = {
117+
post: "/wallet/unfreezebalance"
118+
body: "*"
119+
};
120+
}
121+
122+
rpc WithdrawBalance (WithdrawBalanceContract) returns (Transaction) {
123+
option (google.api.http) = {
124+
post: "/wallet/withdrawbalance"
125+
body: "*"
126+
};
127+
}
128+
108129
rpc ListNodes (EmptyMessage) returns (NodeList) {
109130
option (google.api.http) = {
110131
post: "/wallet/listnodes"

‎src/main/protos/core/Contract.proto‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,16 @@ message DeployContract {
102102
bytes script = 2;
103103
}
104104

105+
message FreezeBalanceContract {
106+
bytes owner_address = 1;
107+
int64 frozen_balance = 2;
108+
int64 frozen_duration = 3;
109+
}
110+
111+
message UnfreezeBalanceContract {
112+
bytes owner_address = 1;
113+
}
114+
115+
message WithdrawBalanceContract {
116+
bytes owner_address = 1;
117+
}

‎src/main/protos/core/Tron.proto‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ message Account {
3131
// the vote num to this super rep.
3232
int64 vote_count = 2;
3333
}
34+
// frozen balance
35+
message Frozen {
36+
// the frozen trx balance
37+
int64 frozen_balance = 1;
38+
// the expire time
39+
int64 expire_time = 2;
40+
}
41+
3442
bytes account_name = 1;
3543
AccountType type = 2;
3644
// the create adress
@@ -41,10 +49,19 @@ message Account {
4149
repeated Vote votes = 5;
4250
// the other asset owned by this account
4351
map<string, int64> asset = 6;
52+
// the frozen balance
53+
repeated Frozen frozen = 7;
54+
// bandwidth, get from frozen
55+
int64 bandwidth = 8;
4456
// this account create time
4557
int64 create_time = 9;
4658
// this last opration time, including transfer, voting and so on.
4759
int64 latest_opration_time = 10;
60+
61+
// witness block producing allowance
62+
int64 allowance = 11;
63+
// last withdraw time
64+
int64 latest_withdraw_time = 12;
4865
// not used so far
4966
bytes code = 13;
5067
}
@@ -114,6 +131,9 @@ message Transaction {
114131
WitnessUpdateContract = 8;
115132
ParticipateAssetIssueContract = 9;
116133
AccountUpdateContract = 10;
134+
FreezeBalanceContract = 11;
135+
UnfreezeBalanceContract = 12;
136+
WithdrawBalanceContract = 13;
117137
CustomContract = 20;
118138
}
119139
ContractType type = 1;

‎src/main/resources/config.conf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ node.discovery = {
1818
node {
1919
# trust node for solidity node
2020
# trustNode = "ip:port"
21+
trustNode = "127.0.0.1:50051"
2122

2223
listen.port = 18888
2324
rpc.port = 50051
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package org.tron.core.net.node;
2+
3+
import com.google.protobuf.ByteString;
4+
import java.io.File;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.AfterClass;
7+
import org.junit.Assert;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.tron.common.application.Application;
12+
import org.tron.common.application.ApplicationFactory;
13+
import org.tron.common.crypto.ECKey;
14+
import org.tron.common.overlay.client.DatabaseGrpcClient;
15+
import org.tron.common.utils.ByteArray;
16+
import org.tron.core.Constant;
17+
import org.tron.core.capsule.BlockCapsule;
18+
import org.tron.core.capsule.utils.BlockUtil;
19+
import org.tron.core.capsule.utils.TransactionUtil;
20+
import org.tron.core.config.DefaultConfig;
21+
import org.tron.core.config.args.Args;
22+
import org.tron.core.db.Manager;
23+
import org.tron.core.net.message.BlockMessage;
24+
import org.tron.core.net.peer.PeerConnection;
25+
import org.tron.core.services.RpcApiService;
26+
import org.tron.protos.Protocol.Block;
27+
import org.tron.protos.Protocol.BlockHeader;
28+
import org.tron.protos.Protocol.DynamicProperties;
29+
import org.tron.protos.Protocol.Transaction;
30+
31+
@Slf4j
32+
public class NodeImplTest {
33+
34+
private static AnnotationConfigApplicationContext context;
35+
36+
private static Application appT;
37+
private static String dbPath = "output_nodeimpl_test";
38+
private static NodeImpl nodeImpl;
39+
private static Manager dbManager;
40+
private static NodeDelegateImpl nodeDelegate;
41+
42+
static {
43+
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
44+
context = new AnnotationConfigApplicationContext(DefaultConfig.class);
45+
Args.getInstance().setSolidityNode(true);
46+
appT = ApplicationFactory.create(context);
47+
}
48+
49+
/**
50+
* init db.
51+
*/
52+
@BeforeClass
53+
public static void init() {
54+
nodeImpl = context.getBean(NodeImpl.class);
55+
dbManager = context.getBean(Manager.class);
56+
nodeDelegate = new NodeDelegateImpl(dbManager);
57+
nodeImpl.setNodeDelegate(nodeDelegate);
58+
}
59+
60+
/**
61+
* remo db when after test.
62+
*/
63+
@AfterClass
64+
public static void removeDb() {
65+
Args.clearParam();
66+
67+
File dbFolder = new File(dbPath);
68+
if (deleteFolder(dbFolder)) {
69+
logger.info("Release resources successful.");
70+
} else {
71+
logger.info("Release resources failure.");
72+
}
73+
context.destroy();
74+
}
75+
76+
private static Boolean deleteFolder(File index) {
77+
if (!index.isDirectory() || index.listFiles().length <= 0) {
78+
return index.delete();
79+
}
80+
for (File file : index.listFiles()) {
81+
if (null != file && !deleteFolder(file)) {
82+
return false;
83+
}
84+
}
85+
return index.delete();
86+
}
87+
88+
@Test
89+
public void testSyncBlockMessage() {
90+
PeerConnection peer = new PeerConnection();
91+
BlockCapsule genesisBlockCapsule = BlockUtil.newGenesisBlockCapsule();
92+
93+
ByteString witnessAddress = ByteString.copyFrom(
94+
ECKey.fromPrivate(
95+
ByteArray.fromHexString(
96+
Args.getInstance().getLocalWitnesses().getPrivateKey()))
97+
.getAddress());
98+
BlockHeader.raw raw = BlockHeader.raw.newBuilder()
99+
.setTimestamp(System.currentTimeMillis())
100+
.setParentHash(genesisBlockCapsule.getParentHash().getByteString())
101+
.setNumber(genesisBlockCapsule.getNum() + 1)
102+
.setWitnessAddress(witnessAddress)
103+
.setWitnessId(1).build();
104+
BlockHeader blockHeader = BlockHeader.newBuilder()
105+
.setRawData(raw)
106+
.build();
107+
108+
Block block = Block.newBuilder().setBlockHeader(blockHeader).build();
109+
110+
BlockCapsule blockCapsule = new BlockCapsule(block);
111+
blockCapsule.sign(
112+
ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()));
113+
blockCapsule.setMerkleRoot();
114+
BlockMessage blockMessage = new BlockMessage(blockCapsule);
115+
peer.getSyncBlockRequested().put(blockMessage.getBlockId(), System.currentTimeMillis());
116+
nodeImpl.onMessage(peer, blockMessage);
117+
Assert.assertEquals(peer.getSyncBlockRequested().size(), 0);
118+
}
119+
120+
@Test
121+
public void testAdvBlockMessage() {
122+
PeerConnection peer = new PeerConnection();
123+
BlockCapsule genesisBlockCapsule = BlockUtil.newGenesisBlockCapsule();
124+
125+
ByteString witnessAddress = ByteString.copyFrom(
126+
ECKey.fromPrivate(
127+
ByteArray.fromHexString(
128+
Args.getInstance().getLocalWitnesses().getPrivateKey()))
129+
.getAddress());
130+
BlockHeader.raw raw = BlockHeader.raw.newBuilder()
131+
.setTimestamp(System.currentTimeMillis())
132+
.setParentHash(genesisBlockCapsule.getParentHash().getByteString())
133+
.setNumber(genesisBlockCapsule.getNum() + 1)
134+
.setWitnessAddress(witnessAddress)
135+
.setWitnessId(1).build();
136+
BlockHeader blockHeader = BlockHeader.newBuilder()
137+
.setRawData(raw)
138+
.build();
139+
140+
Block block = Block.newBuilder().setBlockHeader(blockHeader).build();
141+
142+
BlockCapsule blockCapsule = new BlockCapsule(block);
143+
blockCapsule.sign(
144+
ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()));
145+
blockCapsule.setMerkleRoot();
146+
BlockMessage blockMessage = new BlockMessage(blockCapsule);
147+
peer.getAdvObjWeRequested().put(blockMessage.getBlockId(), System.currentTimeMillis());
148+
nodeImpl.onMessage(peer, blockMessage);
149+
Assert.assertEquals(peer.getAdvObjWeRequested().size(), 0);
150+
}
151+
152+
}

0 commit comments

Comments
 (0)