Skip to content

Commit

Permalink
feat(scale utils): extract some reused SCALE en/decoding logic into u…
Browse files Browse the repository at this point in the history
…tils
  • Loading branch information
David-Petrov committed Nov 23, 2023
1 parent b5b3aef commit 61b00d4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main/java/com/limechain/utils/scale/ScaleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.limechain.utils.scale;

import com.limechain.utils.scale.writers.PairWriter;
import io.emeraldpay.polkaj.scale.ScaleCodecReader;
import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import io.emeraldpay.polkaj.scale.ScaleReader;
import io.emeraldpay.polkaj.scale.reader.ListReader;
import io.emeraldpay.polkaj.scale.writer.ListWriter;
import kotlin.Pair;
import lombok.experimental.UtilityClass;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

// TODO: This class is currently just a helper utility class, planned to grow into a unified scale encode/decode util class with whatever methods are useful
// WIP
// Currently trying out different approaches to spare some of the boilerplate around SCALE en/decoding
@UtilityClass
public class ScaleUtils {

@UtilityClass
public class Decode {
public <T> T decode(byte[] encodedData, ScaleReader<T> reader) {
try {
return new ScaleCodecReader(encodedData).read(reader);
} catch (RuntimeException e) {
throw new RuntimeException("Error while SCALE decoding.", e); // TODO: is this a code smell?
}
}

public <T> List<T> decodeList(byte[] encodedData, ScaleReader<T> listItemReader) {
return decode(encodedData, new ListReader<>(listItemReader));
}
}

@UtilityClass
public class Encode {
public <K, V> byte[] encode(List<Pair<K, V>> pairs, Function<K, byte[]> fstSerializer, Function<V, byte[]> sndSerializer) throws
IOException {
return encode(
pairs.stream()
.map(p -> new Pair<>(fstSerializer.apply(p.getFirst()), sndSerializer.apply(p.getSecond())))
.toList());
}

public byte[] encode(List<Pair<byte[], byte[]>> pairs) throws IOException {
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ScaleCodecWriter writer = new ScaleCodecWriter(buffer)) {
new ListWriter<>(new PairWriter<>(ScaleCodecWriter::writeAsList, ScaleCodecWriter::writeAsList))
.write(writer, pairs);
return buffer.toByteArray();
}
}

public byte[] encode(byte[][] values) throws IOException {
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ScaleCodecWriter writer = new ScaleCodecWriter(buffer)) {
new ListWriter<>(ScaleCodecWriter::writeAsList)
.write(writer, Arrays.asList(values));
return buffer.toByteArray();
}
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/limechain/utils/scale/readers/PairReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.limechain.utils.scale.readers;

import io.emeraldpay.polkaj.scale.ScaleCodecReader;
import io.emeraldpay.polkaj.scale.ScaleReader;
import kotlin.Pair;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class PairReader<K, V> implements ScaleReader<Pair<K, V>> {
protected ScaleReader<K> firstReader;
protected ScaleReader<V> secondReader;

@Override
public Pair<K, V> read(ScaleCodecReader scaleCodecReader) {
K first = scaleCodecReader.read(firstReader);
V second = scaleCodecReader.read(secondReader);
return new Pair<>(first, second);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/limechain/utils/scale/writers/PairWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.limechain.utils.scale.writers;

import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import io.emeraldpay.polkaj.scale.ScaleWriter;
import kotlin.Pair;
import lombok.AllArgsConstructor;

import java.io.IOException;

@AllArgsConstructor
public class PairWriter<K, V> implements ScaleWriter<Pair<K, V>> {
protected ScaleWriter<K> firstWriter;
protected ScaleWriter<V> secondWriter;

@Override
public void write(ScaleCodecWriter scaleCodecWriter, Pair<K, V> pair) throws IOException {
scaleCodecWriter.write(firstWriter, pair.getFirst());
scaleCodecWriter.write(secondWriter, pair.getSecond());
}
}

0 comments on commit 61b00d4

Please sign in to comment.