Skip to content
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 @@ -55,7 +55,7 @@ public <T> T decode(int id, String content, JavaType clazz) {
}
if (response.getError() != null) {
throw new CompletionException(
new RpcException(response.getError().getCode(), response.getError().getMessage())
new RpcException(response.getError().getCode(), response.getError().getMessage(), response.getError().getData())
);
}
return response.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ public class RpcException extends RuntimeException {

private final int code;
private final String rpcMessage;
private final String rpcData;

public RpcException(int code, String rpcMessage) {
this(code, rpcMessage, null);
this(code, rpcMessage, null, null);
}

public RpcException(int code, String rpcMessage, String rpcData) {
this(code, rpcMessage, rpcData, null);
}

public RpcException(int code, String rpcMessage, Throwable e) {
super("RPC Exception " + code + ": " + rpcMessage, e);
this(code, rpcMessage, null, e);
}

public RpcException(int code, String rpcMessage, String rpcData, Throwable e) {
super("RPC Exception " + code + ": " + rpcMessage + (rpcData == null ? "" : " (" + rpcData + ")"), e);
this.code = code;
this.rpcMessage = rpcMessage;
this.rpcData = rpcData;
}

public int getCode() {
Expand All @@ -22,4 +32,8 @@ public int getCode() {
public String getRpcMessage() {
return rpcMessage;
}

public String getRpcData() {
return rpcData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
public class RpcResponseError {
private int code;
private String message;
private String data;

public RpcResponseError() {
}

public RpcResponseError(int code, String message) {
public RpcResponseError(int code, String message, String data) {
this.code = code;
this.message = message;
this.data = data;
}

public int getCode() {
Expand All @@ -27,17 +29,22 @@ public String getMessage() {
return message;
}

public String getData() {
return data;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RpcResponseError)) return false;
RpcResponseError that = (RpcResponseError) o;
return code == that.code &&
Objects.equals(message, that.message);
Objects.equals(message, that.message) &&
Objects.equals(data, that.data);
}

@Override
public int hashCode() {
return Objects.hash(code, message);
return Objects.hash(code, message, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ class AbstractPolkadotApiSpec extends Specification {
setup:
def response = '{\n' +
' "jsonrpc": "2.0",\n' +
' "error": {"code": 100, "message": "Test error", "details": 1},\n' +
' "error": {"code": 100, "message": "Test error", "data": "Test data"},\n' +
' "id": 0\n' +
'}'
when:
client.decode(0, response, client.responseType(BlockResponseJson))
then:
def t = thrown(CompletionException)
t.cause instanceof RpcException
t.message == "io.emeraldpay.polkaj.api.RpcException: RPC Exception 100: Test error (Test data)"
with((RpcException)t.cause) {
code == 100
rpcMessage == "Test error"
rpcData == "Test data"
}
}

Expand All @@ -110,6 +112,12 @@ class AbstractPolkadotApiSpec extends Specification {
then:
def t = thrown(CompletionException)
t.cause instanceof RpcException
t.message == "io.emeraldpay.polkaj.api.RpcException: RPC Exception -32603: Server returned invalid id: 1 != 0"
with((RpcException)t.cause) {
code == -32603
rpcMessage == "Server returned invalid id: 1 != 0"
rpcData == null
}
}

def "Fail to decode if invalid json"() {
Expand All @@ -120,6 +128,12 @@ class AbstractPolkadotApiSpec extends Specification {
then:
def t = thrown(CompletionException)
t.cause instanceof RpcException
t.message == "io.emeraldpay.polkaj.api.RpcException: RPC Exception -32603: Server returned invalid JSON"
with((RpcException)t.cause) {
code == -32603
rpcMessage == "Server returned invalid JSON"
rpcData == null
}
}

class TestingPolkadotApi extends AbstractPolkadotApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public class PolkadotHttpApi extends AbstractPolkadotApi implements AutoCloseabl
private final HttpRequest.Builder request;
private boolean closed = false;

private PolkadotHttpApi(URI target, HttpClient httpClient, String basicAuth, Runnable onClose, ObjectMapper objectMapper) {
private PolkadotHttpApi(URI target, HttpClient httpClient, String basicAuth, Runnable onClose, ObjectMapper objectMapper, Duration timeout) {
super(objectMapper);
this.httpClient = httpClient;
this.onClose = onClose;

HttpRequest.Builder request = HttpRequest.newBuilder()
.uri(target)
.timeout(Duration.ofMinutes(1))
.timeout(timeout)
.header("User-Agent", "Polkaj/0.3") //TODO generate version during compilation
.header("Content-Type", APPLICATION_JSON);

Expand Down Expand Up @@ -148,6 +148,7 @@ public static class Builder {
private HttpClient httpClient;
private Runnable onClose;
private ObjectMapper objectMapper;
private Duration timeout;


/**
Expand Down Expand Up @@ -228,6 +229,17 @@ public Builder objectMapper(ObjectMapper objectMapper) {
return this;
}

/**
* Override the default timeout with a custom duration.
*
* @param timeout Duration
* @return builder
*/
public Builder timeout(Duration timeout) {
this.timeout = timeout;
return this;
}

protected void initDefaults() {
if (httpClient == null && target == null) {
try {
Expand All @@ -243,6 +255,9 @@ protected void initDefaults() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new PolkadotModule());
}
if (timeout == null) {
timeout = Duration.ofMinutes(1);
}
}

/**
Expand All @@ -261,7 +276,7 @@ public PolkadotHttpApi build() {
.build();
}

return new PolkadotHttpApi(target, httpClient, basicAuth, onClose, objectMapper);
return new PolkadotHttpApi(target, httpClient, basicAuth, onClose, objectMapper, timeout);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import io.emeraldpay.polkaj.types.Hash256
import io.emeraldpay.polkaj.api.RpcException
import io.emeraldpay.polkaj.json.BlockResponseJson
import org.mockserver.integration.ClientAndServer
import org.mockserver.model.Delay
import org.mockserver.model.HttpRequest
import org.mockserver.model.HttpResponse
import org.mockserver.model.MediaType
import spock.lang.Shared
import spock.lang.Specification

import java.net.http.HttpTimeoutException
import java.nio.charset.Charset
import java.time.Duration
import java.util.concurrent.ExecutionException

class PolkadotHttpClientSpec extends Specification {
Expand All @@ -24,6 +27,7 @@ class PolkadotHttpClientSpec extends Specification {
def setup() {
client = PolkadotHttpApi.newBuilder()
.connectTo("http://localhost:18080")
.timeout(Duration.ofSeconds(1))
.build()
mockServer = ClientAndServer.startClientAndServer(18080)
}
Expand Down Expand Up @@ -80,6 +84,28 @@ class PolkadotHttpClientSpec extends Specification {

}

def "Timeouts in one second"() {
setup:
def response = '{\n' +
' "jsonrpc": "2.0",\n' +
' "result": "0x5d83f66b61701da4cbd7a60137db89c69469a4f798b62aba9176ab253b423828",\n' +
' "id": 0\n' +
'}'
mockServer.when(
HttpRequest.request()
).respond(
HttpResponse.response(response).withContentType(MediaType.APPLICATION_JSON).withDelay(Delay.seconds(2))
)

when:
client.execute(RpcCall.create(String, "chain_getFinalisedHead")).get()

then:
def t = thrown(ExecutionException)
t.cause instanceof HttpTimeoutException

}

def "Make request for complex object"() {
setup:
def response = '{' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public <T> WsResponse decode(String json) throws IOException {
protected RpcResponseError decodeError(JsonParser parser) throws IOException {
int code = 0;
String message = null;
String data = null;
while (parser.nextToken() != JsonToken.END_OBJECT) {
if (parser.currentToken() == null) {
break;
Expand All @@ -101,9 +102,11 @@ protected RpcResponseError decodeError(JsonParser parser) throws IOException {
code = decodeNumber(parser);
} else if ("message".equals(field)) {
message = parser.getValueAsString();
} else if ("data".equals(field)) {
data = parser.getValueAsString();
}
}
return new RpcResponseError(code, message);
return new RpcResponseError(code, message, data);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public <T> void accept(RpcResponse<T> response) {
try {
if (response.getError() != null) {
f.getHandler().completeExceptionally(new CompletionException(
new RpcException(response.getError().getCode(), response.getError().getMessage())
new RpcException(response.getError().getCode(), response.getError().getMessage(), response.getError().getData())
));
} else {
f.getHandler().complete(response.getResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PolkadotWsClientSpec extends Specification {

def "Fail to subscribe with invalid command"() {
when:
server.onNextReply('{"jsonrpc":"2.0","error":{"code": -1, "message": "Test"},"id":0}')
server.onNextReply('{"jsonrpc":"2.0","error":{"code": -1, "message": "Test", "data": "Test data"},"id":0}')
def f = client.subscribe(SubscribeCall.create(Hash256.class, "test_subscribeNone", "test_unsubscribeNone"))
f.get(TIMEOUT, TimeUnit.SECONDS)
then:
Expand All @@ -145,6 +145,7 @@ class PolkadotWsClientSpec extends Specification {
with((RpcException)t.cause) {
code == -1
rpcMessage == "Test"
rpcData == "Test data"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class AccountInfo {

private Long nonce;
private Integer refcount;
private Long refcount;
private AccountData data;

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

public Integer getRefcount() {
public Long getRefcount() {
return refcount;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AccountInfoReader implements ScaleReader<AccountInfo> {
public AccountInfo read(ScaleCodecReader rdr) {
AccountInfo result = new AccountInfo();
result.setNonce(rdr.readUint32());
result.setRefcount(rdr.readUByte());
result.setRefcount(rdr.readUint32());
result.setData(rdr.read(new AccountDataReader()));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Extrinsic.TransactionInfo read(ScaleCodecReader rdr) {
Extrinsic.TransactionInfo result = new Extrinsic.TransactionInfo();
result.setSender(new Address(network, rdr.readUint256()));
result.setSignature(rdr.read(SIGNATURE_READER).getValue());
result.setEra(rdr.readCompactInt());
result.setEra(rdr.readEra());
result.setNonce(rdr.read(ScaleCodecReader.COMPACT_BIGINT).longValueExact());
result.setTip(new DotAmount(rdr.read(ScaleCodecReader.COMPACT_BIGINT)));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void write(ScaleCodecWriter wrt, Extrinsic.TransactionInfo value) throws
wrt.writeUint256(value.getSender().getPubkey());
wrt.writeByte(Extrinsic.SignatureType.SR25519.getCode());
wrt.writeByteArray(value.getSignature().getValue().getBytes());
wrt.writeCompact(value.getEra());
wrt.writeEra(value.getEra());
wrt.write(ScaleCodecWriter.COMPACT_BIGINT, BigInteger.valueOf(value.getNonce()));
wrt.write(ScaleCodecWriter.COMPACT_BIGINT, value.getTip().getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static class Module {
private List<Event> events;
private List<Constant> constants;
private List<Error> errors;
private Integer index;

public String getName() {
return name;
Expand Down Expand Up @@ -126,6 +127,14 @@ public void setErrors(List<Error> errors) {
this.errors = errors;
}

public Integer getIndex() {
return index;
}

public void setIndex(Integer index) {
this.index = index;
}

public Optional<Call> findCall(String name) {
if (calls == null) {
return Optional.empty();
Expand All @@ -145,12 +154,13 @@ public final boolean equals(Object o) {
Objects.equals(calls, module.calls) &&
Objects.equals(events, module.events) &&
Objects.equals(constants, module.constants) &&
Objects.equals(errors, module.errors);
Objects.equals(errors, module.errors) &&
Objects.equals(index, module.index);
}

@Override
public final int hashCode() {
return Objects.hash(name, storage, calls, events, constants, errors);
return Objects.hash(name, storage, calls, events, constants, errors, index);
}
}

Expand Down
Loading