mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-04-28 15:12:16 +00:00
feat: update SelfAdaptiveShardingExecutor
use merkle to manage transactions in blocks
This commit is contained in:
parent
837cf8ba70
commit
8c86d4e767
@ -13,10 +13,7 @@ import org.bdware.sc.util.JsonUtil;
|
|||||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
@ -97,7 +94,9 @@ public class SelfAdaptiveShardingExecutor implements ContractExecutor {
|
|||||||
block.prevHash,
|
block.prevHash,
|
||||||
block.requests.length,
|
block.requests.length,
|
||||||
block.timestamp));
|
block.timestamp));
|
||||||
if (!executedBlocks.contains(block.hash)) {
|
if (!toExecuted.containsKey(block.prevHash) &&
|
||||||
|
!executedBlocks.contains(block.hash) &&
|
||||||
|
block.isValid()) {
|
||||||
toExecuted.put(block.prevHash, block);
|
toExecuted.put(block.prevHash, block);
|
||||||
synchronized (flag) {
|
synchronized (flag) {
|
||||||
flag.notify();
|
flag.notify();
|
||||||
@ -149,7 +148,7 @@ public class SelfAdaptiveShardingExecutor implements ContractExecutor {
|
|||||||
for (int i = 0; i < requests.length; ++i) {
|
for (int i = 0; i < requests.length; ++i) {
|
||||||
requests[i] = reqQueue.poll();
|
requests[i] = reqQueue.poll();
|
||||||
}
|
}
|
||||||
this.b.fillBlock(null, requests);
|
this.b.fillBlock(requests);
|
||||||
return this.b;
|
return this.b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +156,7 @@ public class SelfAdaptiveShardingExecutor implements ContractExecutor {
|
|||||||
String prevHash = "0";
|
String prevHash = "0";
|
||||||
String hash;
|
String hash;
|
||||||
String checkPoint;
|
String checkPoint;
|
||||||
|
String body;
|
||||||
ContractRequest[] requests;
|
ContractRequest[] requests;
|
||||||
long timestamp;
|
long timestamp;
|
||||||
|
|
||||||
@ -167,14 +167,46 @@ public class SelfAdaptiveShardingExecutor implements ContractExecutor {
|
|||||||
this.prevHash = prev;
|
this.prevHash = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fillBlock(String cp, ContractRequest[] requests) {
|
public void fillBlock(ContractRequest[] requests) {
|
||||||
this.checkPoint = cp;
|
|
||||||
this.requests = requests;
|
this.requests = requests;
|
||||||
this.timestamp = System.currentTimeMillis();
|
this.timestamp = System.currentTimeMillis();
|
||||||
hash = HashUtil.sha3(
|
this.body = merkle(requests);
|
||||||
prevHash,
|
this.hash = computeHash();
|
||||||
cp,
|
}
|
||||||
Arrays.stream(requests).map(ContractRequest::getRequestID).collect(Collectors.joining()));
|
|
||||||
}
|
public boolean isValid() {
|
||||||
|
return computeHash().equals(hash) && body.equals(merkle(this.requests));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String computeHash() {
|
||||||
|
return HashUtil.sha3(
|
||||||
|
this.prevHash,
|
||||||
|
this.checkPoint,
|
||||||
|
this.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String merkle(ContractRequest[] requests) {
|
||||||
|
if (requests.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (requests.length == 1) {
|
||||||
|
return HashUtil.sha3(requests[0].getRequestID());
|
||||||
|
}
|
||||||
|
Queue<String> reqQueue =
|
||||||
|
Arrays.stream(requests).map(ContractRequest::getRequestID)
|
||||||
|
.collect(Collectors.toCollection(ArrayDeque::new));
|
||||||
|
do {
|
||||||
|
int size;
|
||||||
|
for (size = reqQueue.size(); size > 1; size -= 2) {
|
||||||
|
reqQueue.add(HashUtil.sha3(reqQueue.poll(), reqQueue.poll()));
|
||||||
|
}
|
||||||
|
if (size == 1) {
|
||||||
|
reqQueue.add(reqQueue.poll());
|
||||||
|
}
|
||||||
|
} while (1 != reqQueue.size());
|
||||||
|
return reqQueue.poll();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user