Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Polkadot v0.8.28 #50

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public class AccountInfo {

private Long nonce;
private Long refcount;
private Long consumers;
private Long providers;
private AccountData data;

public Long getNonce() {
Expand All @@ -16,12 +17,20 @@ public void setNonce(Long nonce) {
this.nonce = nonce;
}

public Long getRefcount() {
return refcount;
public Long getConsumers() {
return consumers;
}

public void setRefcount(Long refcount) {
this.refcount = refcount;
public void setConsumers(Long consumers) {
this.consumers = consumers;
}

public Long getProviders() {
return providers;
}

public void setProviders(Long providers) {
this.providers = providers;
}

public AccountData getData() {
Expand All @@ -38,12 +47,13 @@ public boolean equals(Object o) {
if (!(o instanceof AccountInfo)) return false;
AccountInfo that = (AccountInfo) o;
return Objects.equals(nonce, that.nonce) &&
Objects.equals(refcount, that.refcount) &&
Objects.equals(consumers, that.consumers) &&
Objects.equals(providers, that.providers) &&
Objects.equals(data, that.data);
}

@Override
public int hashCode() {
return Objects.hash(nonce, refcount, data);
return Objects.hash(nonce, consumers, providers, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class AccountInfoReader implements ScaleReader<AccountInfo> {
public AccountInfo read(ScaleCodecReader rdr) {
AccountInfo result = new AccountInfo();
result.setNonce(rdr.readUint32());
result.setRefcount(rdr.readUint32());
result.setConsumers(rdr.readUint32());
result.setProviders(rdr.readUint32());
result.setData(rdr.read(new AccountDataReader()));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.emeraldpay.polkaj.scaletypes;

import io.emeraldpay.polkaj.scale.UnionValue;
import io.emeraldpay.polkaj.types.Address;
import io.emeraldpay.polkaj.types.DotAmount;

Expand All @@ -13,7 +14,7 @@ public class BalanceTransfer extends ExtrinsicCall {
/**
* Destination address
*/
private Address destination;
private UnionValue<MultiAddress> destination;
/**
* Balance to transfer
*/
Expand Down Expand Up @@ -43,14 +44,18 @@ public void init(Metadata metadata) {
init(call);
}

public Address getDestination() {
public UnionValue<MultiAddress> getDestination() {
return destination;
}

public void setDestination(Address destination) {
public void setDestination(UnionValue<MultiAddress> destination) {
this.destination = destination;
}

public void setDestination(Address destination) {
this.destination = MultiAddress.AccountID.from(destination);
}

public DotAmount getBalance() {
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

public class BalanceTransferReader implements ScaleReader<BalanceTransfer> {

private final SS58Type.Network network;
private final MultiAddressReader destinationReader;

public BalanceTransferReader(SS58Type.Network network) {
this.network = network;
this.destinationReader = new MultiAddressReader(network);
}

@Override
public BalanceTransfer read(ScaleCodecReader rdr) {
BalanceTransfer result = new BalanceTransfer();
result.setModuleIndex(rdr.readUByte());
result.setCallIndex(rdr.readUByte());
result.setDestination(new Address(network, rdr.readUint256()));
result.setDestination(rdr.read(destinationReader));
result.setBalance(new DotAmount(rdr.read(ScaleCodecReader.COMPACT_BIGINT)));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.io.IOException;

public class BalanceTransferWriter implements ScaleWriter<BalanceTransfer> {
private static final MultiAddressWriter DESTINATION_WRITER = new MultiAddressWriter();

@Override
public void write(ScaleCodecWriter wrt, BalanceTransfer value) throws IOException {
wrt.writeByte(value.getModuleIndex());
wrt.writeByte(value.getCallIndex());
wrt.writeUint256(value.getDestination().getPubkey());
wrt.write(DESTINATION_WRITER, value.getDestination());
wrt.write(ScaleCodecWriter.COMPACT_BIGINT, value.getBalance().getValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.emeraldpay.polkaj.scaletypes;

import io.emeraldpay.polkaj.scale.UnionValue;
import io.emeraldpay.polkaj.types.Address;
import io.emeraldpay.polkaj.types.DotAmount;
import io.emeraldpay.polkaj.types.Hash512;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static class TransactionInfo {
/**
* Sender
*/
private Address sender;
private UnionValue<MultiAddress> sender;
/**
* Signature
*/
Expand All @@ -106,14 +107,18 @@ public static class TransactionInfo {
*/
private DotAmount tip = DotAmount.ZERO;

public Address getSender() {
public UnionValue<MultiAddress> getSender() {
return sender;
}

public void setSender(Address sender) {
public void setSender(UnionValue<MultiAddress> sender) {
this.sender = sender;
}

public void setSender(Address value) {
this.sender = MultiAddress.AccountID.from(value);
}

public SR25519Signature getSignature() {
return signature;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.emeraldpay.polkaj.scale.reader.UnionReader;
import io.emeraldpay.polkaj.scale.reader.UnsupportedReader;
import io.emeraldpay.polkaj.ss58.SS58Type;
import io.emeraldpay.polkaj.types.Address;
import io.emeraldpay.polkaj.types.DotAmount;
import io.emeraldpay.polkaj.types.Hash512;

Expand Down Expand Up @@ -52,16 +51,16 @@ static class TransactionInfoReader implements ScaleReader<Extrinsic.TransactionI
);
private static final EraReader ERA_READER = new EraReader();

private final SS58Type.Network network;
private final MultiAddressReader senderReader;

public TransactionInfoReader(SS58Type.Network network) {
this.network = network;
this.senderReader = new MultiAddressReader(network);
}

@Override
public Extrinsic.TransactionInfo read(ScaleCodecReader rdr) {
Extrinsic.TransactionInfo result = new Extrinsic.TransactionInfo();
result.setSender(new Address(network, rdr.readUint256()));
result.setSender(rdr.read(senderReader));
result.setSignature(rdr.read(SIGNATURE_READER).getValue());
result.setEra(rdr.read(ERA_READER));
result.setNonce(rdr.read(ScaleCodecReader.COMPACT_BIGINT).longValueExact());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public void write(ScaleCodecWriter wrt, Extrinsic<CALL> value) throws IOExceptio

static class TransactionInfoWriter implements ScaleWriter<Extrinsic.TransactionInfo> {

private static final MultiAddressWriter SENDER_WRITER = new MultiAddressWriter();
private static final EraWriter ERA_WRITER = new EraWriter();

@Override
public void write(ScaleCodecWriter wrt, Extrinsic.TransactionInfo value) throws IOException {
wrt.writeUint256(value.getSender().getPubkey());
wrt.write(SENDER_WRITER, value.getSender());
wrt.writeByte(Extrinsic.SignatureType.SR25519.getCode());
wrt.writeByteArray(value.getSignature().getValue().getBytes());
wrt.write(ERA_WRITER, value.getEra());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.emeraldpay.polkaj.scaletypes;

import io.emeraldpay.polkaj.scale.UnionValue;
import io.emeraldpay.polkaj.types.Address;

import java.util.Objects;

/**
* Represents the MultiAddress enum defined in Substrate
*
* See: https://github.com/paritytech/substrate/blob/master/primitives/runtime/src/multiaddress.rs
*/
public abstract class MultiAddress {

public enum Type {
ID(0),
INDEX(1),
RAW(2),
ADDRESS32(3),
ADDRESS20(4);

private final int code;

Type(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}

/**
* Helper to create a UnionValue from a concrete MultiAddress instance
*
* @param type a valid MultiAddress.Type constant
* @param value concrete MultiAddress instance
* @return
*/
public static UnionValue<MultiAddress> from(int type, MultiAddress value) {
return new UnionValue<>(type, value);
}

/**
* First MultiAddress type, used as a wrapper of a standard Address object
*/
public static class AccountID extends MultiAddress {

private final Address address;

public AccountID(Address address) {
this.address = address;
}

public Address getAddress() {
return address;
}

/**
* Transforms a standard Address object into a fully wrapped MultiAddress UnionValue
*/
public static UnionValue<MultiAddress> from(Address address) {
return MultiAddress.from(Type.ID.getCode(), new AccountID(address));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AccountID)) return false;
if (!((AccountID)o).canEquals(this)) return false;
AccountID that = (AccountID) o;
return Objects.equals(address, that.address);
}

@Override
public int hashCode() {
return Objects.hash(address);
}

public boolean canEquals(Object o) {
return (o instanceof AccountID);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.emeraldpay.polkaj.scaletypes;

import io.emeraldpay.polkaj.scale.ScaleCodecReader;
import io.emeraldpay.polkaj.scale.ScaleReader;
import io.emeraldpay.polkaj.scale.UnionValue;
import io.emeraldpay.polkaj.scale.reader.UnionReader;
import io.emeraldpay.polkaj.scale.reader.UnsupportedReader;
import io.emeraldpay.polkaj.ss58.SS58Type;
import io.emeraldpay.polkaj.types.Address;

import java.util.Arrays;

public class MultiAddressReader implements ScaleReader<UnionValue<MultiAddress>> {
private final UnionReader<MultiAddress> reader;

public MultiAddressReader(SS58Type.Network network) {
this.reader = new UnionReader<>(
Arrays.asList(
new AccountIDReader(network),
new UnsupportedReader<>("AccountIndex addresses are not supported"),
new UnsupportedReader<>("Raw addresses are not supported"),
new UnsupportedReader<>("Address32 addresses are not supported"),
new UnsupportedReader<>("Address20 addresses are not supported")
)
);
}

@Override
public UnionValue<MultiAddress> read(ScaleCodecReader rdr) {
return rdr.read(reader);
}

static class AccountIDReader implements ScaleReader<MultiAddress> {
private final SS58Type.Network network;

public AccountIDReader(SS58Type.Network network) {
this.network = network;
}

@Override
public MultiAddress read(ScaleCodecReader rdr) {
return new MultiAddress.AccountID(new Address(network, rdr.readUint256()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.emeraldpay.polkaj.scaletypes;

import io.emeraldpay.polkaj.scale.ScaleCodecWriter;
import io.emeraldpay.polkaj.scale.ScaleWriter;
import io.emeraldpay.polkaj.scale.UnionValue;
import io.emeraldpay.polkaj.scale.writer.UnionWriter;

import java.io.IOException;
import java.util.Arrays;

public class MultiAddressWriter implements ScaleWriter<UnionValue<MultiAddress>> {
private static final UnionWriter<MultiAddress> WRITER = new UnionWriter<>(
Arrays.asList(
new AccountIDWriter()
)
);

@Override
public void write(ScaleCodecWriter wrt, UnionValue<MultiAddress> value) throws IOException {
wrt.write(WRITER, value);
}

static class AccountIDWriter implements ScaleWriter<MultiAddress> {
@Override
public void write(ScaleCodecWriter wrt, MultiAddress value) throws IOException {
MultiAddress.AccountID accountID = (MultiAddress.AccountID) value;
wrt.writeUint256(accountID.getAddress().getPubkey());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class AccountInfoReaderSpec extends Specification {

def "Read value"() {
setup:
def value = Hex.decodeHex("1100000003000000f70af5f6f3c843050000000000000000000000000000000000000000000000000000c52ebca2b10000000000000000000000c52ebca2b1000000000000000000")
def value = Hex.decodeHex("110000000300000004000000f70af5f6f3c843050000000000000000000000000000000000000000000000000000c52ebca2b10000000000000000000000c52ebca2b1000000000000000000")
when:
def act = new ScaleCodecReader(value).read(reader)
then:
act != null
act.nonce == 17
act.refcount == 3
act.consumers == 3
act.providers == 4
with(act.data) {
free == DotAmount.fromPlancks(379367743775116023)
reserved == DotAmount.ZERO
Expand Down
Loading