-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,016 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/limechain/network/protocol/warp/dto/Block.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.limechain.network.protocol.warp.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class Block { | ||
private BlockHeader header; | ||
private BlockBody body; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/limechain/network/protocol/warp/dto/BlockBody.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.limechain.network.protocol.warp.dto; | ||
|
||
import com.limechain.utils.HashUtils; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecReader; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class BlockBody { | ||
|
||
private List<Extrinsic> extrinsics; | ||
|
||
public byte[][] getExtrinsicsAsByteArray() { | ||
return extrinsics.stream() | ||
.map(Extrinsic::getExtrinsic) | ||
.toArray(byte[][]::new); | ||
} | ||
|
||
public byte[] getEncoded(){ | ||
ByteArrayOutputStream buf = new ByteArrayOutputStream(); | ||
try (ScaleCodecWriter writer = new ScaleCodecWriter(buf)) { | ||
|
||
writer.writeCompact(extrinsics.size()); | ||
for (Extrinsic extrinsic : extrinsics) { | ||
writer.writeAsList(extrinsic.getExtrinsic()); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return HashUtils.hashWithBlake2b(buf.toByteArray()); | ||
} | ||
|
||
public static BlockBody fromEncoded(byte[] encoded) { | ||
ScaleCodecReader reader = new ScaleCodecReader(encoded); | ||
|
||
ArrayList<Extrinsic> extrinsics = new ArrayList<>(); | ||
|
||
int extrinsicsCount = reader.readCompactInt(); | ||
for (int i = 0; i < extrinsicsCount; i++) { | ||
byte[] extrinsic = reader.readByteArray(); | ||
extrinsics.add(new Extrinsic(extrinsic)); | ||
} | ||
|
||
return new BlockBody(extrinsics); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/limechain/network/protocol/warp/dto/Extrinsic.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.limechain.network.protocol.warp.dto; | ||
|
||
import com.limechain.utils.HashUtils; | ||
import io.emeraldpay.polkaj.scale.ScaleCodecWriter; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import org.apache.tomcat.util.buf.HexUtils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class Extrinsic { | ||
private byte[] extrinsic; | ||
|
||
@Override | ||
public String toString(){ | ||
return HexUtils.toHexString(extrinsic); | ||
} | ||
|
||
public byte[] getHash(){ | ||
ByteArrayOutputStream buf = new ByteArrayOutputStream(); | ||
try (ScaleCodecWriter writer = new ScaleCodecWriter(buf)) { | ||
writer.writeAsList(extrinsic); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return HashUtils.hashWithBlake2b(buf.toByteArray()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.