forked from LimeChain/Fruzhin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement fallback when block announce fails. (#18)
# Description Implement rpc fallback when block announce is not triggered for some time interval. Closes LimeChain#494
- Loading branch information
Showing
10 changed files
with
165 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
src/main/java/com/limechain/network/protocol/blockannounce/teavm/BlockAnnounceHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package com.limechain.network.protocol.blockannounce.teavm; | ||
|
||
import com.limechain.network.protocol.blockannounce.BlockAnnounceEngine; | ||
import com.limechain.utils.Stopwatch; | ||
import com.limechain.utils.StringUtils; | ||
|
||
|
||
public class BlockAnnounceHandler implements BlockAnnounceExport { | ||
|
||
private final Stopwatch stopwatch; | ||
|
||
public BlockAnnounceHandler(Stopwatch stopwatch) { | ||
this.stopwatch = stopwatch; | ||
} | ||
|
||
public void blockAnnounce(String announce, String peerId) { | ||
BlockAnnounceEngine.handleBlockAnnounce(StringUtils.fromHex(announce), peerId); | ||
stopwatch.reset(); | ||
} | ||
|
||
public String getHandshake() { | ||
return BlockAnnounceEngine.getHandshake(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.limechain.teavm; | ||
|
||
import org.teavm.jso.JSBody; | ||
import org.teavm.jso.JSFunctor; | ||
import org.teavm.jso.JSObject; | ||
|
||
public class TeaVMScheduler { | ||
|
||
public static void schedule(TeaVMRunnable task, int intervalMillis) { | ||
scheduleNative(task, intervalMillis); | ||
} | ||
|
||
@JSBody(params = {"callback", "interval"}, script = "setInterval(callback, interval);") | ||
private static native void scheduleNative(TeaVMRunnable callback, int intervalMillis); | ||
|
||
@JSFunctor | ||
public interface TeaVMRunnable extends JSObject { | ||
void run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.limechain.utils; | ||
|
||
import com.limechain.network.protocol.warp.dto.BlockHeader; | ||
import com.limechain.network.protocol.warp.dto.HeaderDigest; | ||
import com.limechain.network.protocol.warp.scale.reader.HeaderDigestReader; | ||
import com.limechain.polkaj.Hash256; | ||
import com.limechain.polkaj.reader.ScaleCodecReader; | ||
import com.limechain.rpc.dto.ChainGetHeaderResult; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class RpcUtils { | ||
|
||
public static BlockHeader toBlockHeader(ChainGetHeaderResult result) { | ||
BlockHeader header = new BlockHeader(); | ||
|
||
header.setBlockNumber(new BigInteger( | ||
StringUtils.remove0xPrefix(result.getNumber()), 16)); | ||
header.setParentHash(Hash256.from(result.getParentHash())); | ||
header.setStateRoot(Hash256.from(result.getStateRoot())); | ||
header.setExtrinsicsRoot(Hash256.from(result.getExtrinsicsRoot())); | ||
|
||
List<String> digestHexes = result.getDigest().getLogs(); | ||
HeaderDigest[] digests = new HeaderDigest[digestHexes.size()]; | ||
for (int i = 0; i < digestHexes.size(); i++) { | ||
digests[i] = new HeaderDigestReader().read( | ||
new ScaleCodecReader(StringUtils.hexToBytes(digestHexes.get(i)))); | ||
} | ||
header.setDigest(digests); | ||
|
||
return header; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.limechain.utils; | ||
|
||
public class Stopwatch { | ||
|
||
private long startTime; | ||
|
||
public Stopwatch() { | ||
reset(); | ||
} | ||
|
||
public void reset() { | ||
this.startTime = System.currentTimeMillis(); | ||
} | ||
|
||
public long getElapsedTime() { | ||
return System.currentTimeMillis() - startTime; | ||
} | ||
} |