Skip to content

Commit

Permalink
feat: Simplified ipv4 address (#1970)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov authored Sep 25, 2024
1 parent 4e7d3aa commit d3f2483
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.Endpoint;
import com.hedera.hashgraph.sdk.IPv4Address;
import com.hedera.hashgraph.sdk.IPv4AddressPart;
import com.hedera.hashgraph.sdk.NodeCreateTransaction;
import com.hedera.hashgraph.sdk.NodeDeleteTransaction;
import com.hedera.hashgraph.sdk.NodeUpdateTransaction;
Expand Down Expand Up @@ -42,15 +40,14 @@ public static void main(String[] args)
String newDescription = "Hedera™ cryptocurrency - updated";

// Set up IPv4 address
IPv4Address ipv4Address = new IPv4Address();
ipv4Address.setHost(new IPv4AddressPart());
ipv4Address.setNetwork(new IPv4AddressPart());
Endpoint gossipEndpoint = new Endpoint();
gossipEndpoint.setAddress(ipv4Address);
gossipEndpoint
.setAddress(new byte[] {0x00, 0x01, 0x02, 0x03});

// Set up service endpoint
Endpoint serviceEndpoint = new Endpoint();
serviceEndpoint.setAddress(ipv4Address);
serviceEndpoint
.setAddress(new byte[] {0x00, 0x01, 0x02, 0x03});

// Generate admin key
PrivateKey adminKey = PrivateKey.generateED25519();
Expand Down
16 changes: 10 additions & 6 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package com.hedera.hashgraph.sdk;

import com.google.errorprone.annotations.Var;
import com.google.protobuf.ByteString;
import com.hedera.hashgraph.sdk.proto.ServiceEndpoint;

import java.util.Arrays;
import javax.annotation.Nullable;
import java.util.Objects;

Expand All @@ -31,7 +33,7 @@
public class Endpoint implements Cloneable {

@Nullable
IPv4Address address = null;
byte[] address = null;

int port;

Expand All @@ -57,7 +59,7 @@ static Endpoint fromProtobuf(ServiceEndpoint serviceEndpoint) {
}

return new Endpoint()
.setAddress(IPv4Address.fromProtobuf(serviceEndpoint.getIpAddressV4()))
.setAddress(serviceEndpoint.getIpAddressV4().toByteArray())
.setPort(port)
.setDomainName(serviceEndpoint.getDomainName());
}
Expand All @@ -68,7 +70,7 @@ static Endpoint fromProtobuf(ServiceEndpoint serviceEndpoint) {
* @return the ipv4 address
*/
@Nullable
public IPv4Address getAddress() {
public byte[] getAddress() {
return address;
}

Expand All @@ -78,7 +80,7 @@ public IPv4Address getAddress() {
* @param address the desired ipv4 address
* @return {@code this}
*/
public Endpoint setAddress(IPv4Address address) {
public Endpoint setAddress(byte[] address) {
this.address = address;
return this;
}
Expand Down Expand Up @@ -132,7 +134,7 @@ ServiceEndpoint toProtobuf() {
var builder = ServiceEndpoint.newBuilder();

if (address != null) {
builder.setIpAddressV4(address.toProtobuf());
builder.setIpAddressV4(ByteString.copyFrom(address));
}

builder.setDomainName(domainName);
Expand All @@ -145,7 +147,9 @@ public String toString() {
if (this.domainName != null && !this.domainName.isEmpty()) {
return domainName + ":" + port;
} else {
return Objects.requireNonNull(address) + ":" + port;
return ((int) address[0] & 0x000000FF) + "." + ((int) address[1] & 0x000000FF) + "." +
((int) address[2] & 0x000000FF) + "." + ((int) address[3] & 0x000000FF) +
":" + port;
}
}

Expand Down
138 changes: 0 additions & 138 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/IPv4Address.java

This file was deleted.

94 changes: 0 additions & 94 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/IPv4AddressPart.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,7 @@ void addressBookQueryWorks(String executeVersion) throws Throwable {

Endpoint spawnEndpoint() {
return new Endpoint()
.setAddress(
new IPv4Address()
.setNetwork(
new IPv4AddressPart()
.setLeft((byte) 0x00)
.setRight((byte) 0x01)
).setHost(
new IPv4AddressPart()
.setLeft((byte) 0x02)
.setRight((byte) 0x03)
)
)
.setAddress(new byte[] {0x00, 0x01, 0x02, 0x03})
.setDomainName("unit.test.com")
.setPort(PORT_NODE_PLAIN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,7 @@ void shouldSerialize() {

private static Endpoint spawnTestEndpoint(byte offset) {
return new Endpoint()
.setAddress(
new IPv4Address()
.setNetwork(
new IPv4AddressPart()
.setLeft((byte) (0x00 + offset))
.setRight((byte) (0x01 + offset))
).setHost(
new IPv4AddressPart()
.setLeft((byte) (0x02 + offset))
.setRight((byte) (0x03 + offset))
)
)
.setAddress(new byte[] {0x00, 0x01, 0x02, 0x03})
.setDomainName(offset + "unit.test.com")
.setPort(42 + offset);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
com.hedera.hashgraph.sdk.NodeCreateTransactionTest.shouldSerialize=[
"# com.hedera.hashgraph.sdk.proto.TransactionBody\nnode_account_i_d {\n account_num: 5005\n realm_num: 0\n shard_num: 0\n}\nnode_create {\n account_id {\n account_num: 9\n realm_num: 6\n shard_num: 0\n }\n admin_key {\n ed25519: \"\\030\\214\\252`\\231\\024#O\\b\\370\\342\\232\\321g\\274\\273\\346\\221}\\211m\\244R\\306\\017\\230\\017j,\\246\\206\\001\"\n }\n description: \"Test description\"\n gossip_ca_certificate: \"\\000\\001\\002\\003\\004\"\n gossip_endpoint {\n domain_name: \"0unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 42\n }\n gossip_endpoint {\n domain_name: \"1unit.test.com\"\n ip_address_v4: \"\\001\\002\\003\\004\"\n port: 43\n }\n gossip_endpoint {\n domain_name: \"2unit.test.com\"\n ip_address_v4: \"\\002\\003\\004\\005\"\n port: 44\n }\n grpc_certificate_hash: \"\\005\\006\\a\\b\\t\"\n service_endpoint {\n domain_name: \"3unit.test.com\"\n ip_address_v4: \"\\003\\004\\005\\006\"\n port: 45\n }\n service_endpoint {\n domain_name: \"4unit.test.com\"\n ip_address_v4: \"\\004\\005\\006\\a\"\n port: 46\n }\n service_endpoint {\n domain_name: \"5unit.test.com\"\n ip_address_v4: \"\\005\\006\\a\\b\"\n port: 47\n }\n service_endpoint {\n domain_name: \"6unit.test.com\"\n ip_address_v4: \"\\006\\a\\b\\t\"\n port: 48\n }\n}\ntransaction_fee: 100000000\ntransaction_i_d {\n account_i_d {\n account_num: 5006\n realm_num: 0\n shard_num: 0\n }\n transaction_valid_start {\n seconds: 1554158542\n }\n}\ntransaction_valid_duration {\n seconds: 120\n}"
"# com.hedera.hashgraph.sdk.proto.TransactionBody\nnode_account_i_d {\n account_num: 5005\n realm_num: 0\n shard_num: 0\n}\nnode_create {\n account_id {\n account_num: 9\n realm_num: 6\n shard_num: 0\n }\n admin_key {\n ed25519: \"\\030\\214\\252`\\231\\024#O\\b\\370\\342\\232\\321g\\274\\273\\346\\221}\\211m\\244R\\306\\017\\230\\017j,\\246\\206\\001\"\n }\n description: \"Test description\"\n gossip_ca_certificate: \"\\000\\001\\002\\003\\004\"\n gossip_endpoint {\n domain_name: \"0unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 42\n }\n gossip_endpoint {\n domain_name: \"1unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 43\n }\n gossip_endpoint {\n domain_name: \"2unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 44\n }\n grpc_certificate_hash: \"\\005\\006\\a\\b\\t\"\n service_endpoint {\n domain_name: \"3unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 45\n }\n service_endpoint {\n domain_name: \"4unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 46\n }\n service_endpoint {\n domain_name: \"5unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 47\n }\n service_endpoint {\n domain_name: \"6unit.test.com\"\n ip_address_v4: \"\\000\\001\\002\\003\"\n port: 48\n }\n}\ntransaction_fee: 100000000\ntransaction_i_d {\n account_i_d {\n account_num: 5006\n realm_num: 0\n shard_num: 0\n }\n transaction_valid_start {\n seconds: 1554158542\n }\n}\ntransaction_valid_duration {\n seconds: 120\n}"
]
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,7 @@ void shouldSerialize() {

private static Endpoint spawnTestEndpoint(byte offset) {
return new Endpoint()
.setAddress(
new IPv4Address()
.setNetwork(
new IPv4AddressPart()
.setLeft((byte) (0x00 + offset))
.setRight((byte) (0x01 + offset))
).setHost(
new IPv4AddressPart()
.setLeft((byte) (0x02 + offset))
.setRight((byte) (0x03 + offset))
)
)
.setAddress(new byte[] {0x00, 0x01, 0x02, 0x03})
.setDomainName(offset + "unit.test.com")
.setPort(42 + offset);
}
Expand Down
Loading

0 comments on commit d3f2483

Please sign in to comment.