Skip to content

Commit

Permalink
feat: BlockState
Browse files Browse the repository at this point in the history
  • Loading branch information
ablax committed Nov 9, 2023
1 parent 00d07ab commit 00d07ab
Show file tree
Hide file tree
Showing 7 changed files with 1,016 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/limechain/network/protocol/warp/dto/Block.java
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;
}
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);
}

}
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());
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/limechain/storage/DBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public class DBConstants {
*/
public static final String STATE_TRIE_ROOT_HASH = "stateTrieRootState";

/**
* Key under which the hash of the latest finalised block header is stored.
*/
public static final String FINALIZED_BLOCK_KEY = "finalised_head";

/**
* Key under which the highest round and set id is stored.
*/
public static final String HIGHEST_ROUND_AND_SET_ID_KEY = "hrs";
}
Loading

0 comments on commit 00d07ab

Please sign in to comment.