Skip to content

Commit

Permalink
chore: fix serialize/deserialize for NodeUpdate and NodeCreate (#…
Browse files Browse the repository at this point in the history
…2092)

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored Nov 22, 2024
1 parent 4637da6 commit d719e27
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public AccountId getAccountId() {
* @return {@code this}
*/
public NodeCreateTransaction setAccountId(AccountId accountId) {
Objects.requireNonNull(accountId);
requireNotFrozen();
this.accountId = accountId;
return this;
Expand All @@ -215,7 +214,6 @@ public String getDescription() {
*/
public NodeCreateTransaction setDescription(String description) {
requireNotFrozen();
Objects.requireNonNull(description);
this.description = description;
return this;
}
Expand Down Expand Up @@ -288,7 +286,7 @@ public NodeCreateTransaction addServiceEndpoint(Endpoint serviceEndpoint) {
*/
@Nullable
public byte[] getGossipCaCertificate() {
return gossipCaCertificate != null ? Arrays.copyOf(gossipCaCertificate, gossipCaCertificate.length) : null;
return gossipCaCertificate;
}

/**
Expand All @@ -299,9 +297,8 @@ public byte[] getGossipCaCertificate() {
* @return {@code this}
*/
public NodeCreateTransaction setGossipCaCertificate(byte[] gossipCaCertificate) {
Objects.requireNonNull(gossipCaCertificate);
requireNotFrozen();
this.gossipCaCertificate = Arrays.copyOf(gossipCaCertificate, gossipCaCertificate.length);
this.gossipCaCertificate = gossipCaCertificate;
return this;
}

Expand All @@ -311,7 +308,7 @@ public NodeCreateTransaction setGossipCaCertificate(byte[] gossipCaCertificate)
*/
@Nullable
public byte[] getGrpcCertificateHash() {
return grpcCertificateHash != null ? Arrays.copyOf(grpcCertificateHash, grpcCertificateHash.length) : null;
return grpcCertificateHash;
}

/**
Expand All @@ -322,9 +319,8 @@ public byte[] getGrpcCertificateHash() {
* @return {@code this}
*/
public NodeCreateTransaction setGrpcCertificateHash(byte[] grpcCertificateHash) {
Objects.requireNonNull(grpcCertificateHash);
requireNotFrozen();
this.grpcCertificateHash = Arrays.copyOf(grpcCertificateHash, grpcCertificateHash.length);
this.grpcCertificateHash = grpcCertificateHash;
return this;
}

Expand All @@ -343,7 +339,6 @@ public Key getAdminKey() {
* @return {@code this}
*/
public NodeCreateTransaction setAdminKey(Key adminKey) {
Objects.requireNonNull(adminKey);
requireNotFrozen();
this.adminKey = adminKey;
return this;
Expand Down Expand Up @@ -406,9 +401,11 @@ void initFromTransactionBody() {
serviceEndpoints.add(Endpoint.fromProtobuf(serviceEndpoint));
}

gossipCaCertificate = body.getGossipCaCertificate().toByteArray();
var protobufGossipCert = body.getGossipCaCertificate();
gossipCaCertificate = protobufGossipCert.equals(ByteString.empty()) ? null : protobufGossipCert.toByteArray();

grpcCertificateHash = body.getGrpcCertificateHash().toByteArray();
var protobufGrpcCert = body.getGrpcCertificateHash();
grpcCertificateHash = protobufGrpcCert.equals(ByteString.empty()) ? null : protobufGrpcCert.toByteArray();

if (body.hasAdminKey()) {
adminKey = Key.fromProtobufKey(body.getAdminKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public AccountId getAccountId() {
* @return {@code this}
*/
public NodeUpdateTransaction setAccountId(AccountId accountId) {
Objects.requireNonNull(accountId);
requireNotFrozen();
this.accountId = accountId;
return this;
Expand All @@ -262,7 +261,6 @@ public String getDescription() {
*/
public NodeUpdateTransaction setDescription(String description) {
requireNotFrozen();
Objects.requireNonNull(description);
this.description = description;
return this;
}
Expand Down Expand Up @@ -345,7 +343,7 @@ public NodeUpdateTransaction addServiceEndpoint(Endpoint serviceEndpoint) {
*/
@Nullable
public byte[] getGossipCaCertificate() {
return gossipCaCertificate != null ? Arrays.copyOf(gossipCaCertificate, gossipCaCertificate.length) : null;
return gossipCaCertificate;
}

/**
Expand All @@ -356,9 +354,8 @@ public byte[] getGossipCaCertificate() {
* @return {@code this}
*/
public NodeUpdateTransaction setGossipCaCertificate(byte[] gossipCaCertificate) {
Objects.requireNonNull(gossipCaCertificate);
requireNotFrozen();
this.gossipCaCertificate = Arrays.copyOf(gossipCaCertificate, gossipCaCertificate.length);
this.gossipCaCertificate = gossipCaCertificate;
return this;
}

Expand All @@ -368,7 +365,7 @@ public NodeUpdateTransaction setGossipCaCertificate(byte[] gossipCaCertificate)
*/
@Nullable
public byte[] getGrpcCertificateHash() {
return grpcCertificateHash != null ? Arrays.copyOf(grpcCertificateHash, grpcCertificateHash.length) : null;
return grpcCertificateHash;
}

/**
Expand All @@ -379,9 +376,8 @@ public byte[] getGrpcCertificateHash() {
* @return {@code this}
*/
public NodeUpdateTransaction setGrpcCertificateHash(byte[] grpcCertificateHash) {
Objects.requireNonNull(grpcCertificateHash);
requireNotFrozen();
this.grpcCertificateHash = Arrays.copyOf(grpcCertificateHash, grpcCertificateHash.length);
this.grpcCertificateHash = grpcCertificateHash;
return this;
}

Expand All @@ -400,7 +396,6 @@ public Key getAdminKey() {
* @return {@code this}
*/
public NodeUpdateTransaction setAdminKey(Key adminKey) {
Objects.requireNonNull(adminKey);
requireNotFrozen();
this.adminKey = adminKey;
return this;
Expand Down Expand Up @@ -469,9 +464,13 @@ void initFromTransactionBody() {
serviceEndpoints.add(Endpoint.fromProtobuf(serviceEndpoint));
}

gossipCaCertificate = body.getGossipCaCertificate().getValue().toByteArray();
if (body.hasGossipCaCertificate()) {
gossipCaCertificate = body.getGossipCaCertificate().getValue().toByteArray();
}

grpcCertificateHash = body.getGrpcCertificateHash().getValue().toByteArray();
if (body.hasGrpcCertificateHash()) {
grpcCertificateHash = body.getGrpcCertificateHash().getValue().toByteArray();
}

if (body.hasAdminKey()) {
adminKey = Key.fromProtobufKey(body.getAdminKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ void fromScheduledTransaction() {
assertThat(tx).isInstanceOf(NodeCreateTransaction.class);
}

@Test
void testSerializeDeserialize() throws Exception {
var tx = new NodeCreateTransaction().setDescription(TEST_DESCRIPTION);
var tx2 = new NodeCreateTransaction().setDescription(TEST_DESCRIPTION);
var tx2Bytes = tx2.toBytes();
NodeCreateTransaction deserializedTx2 = (NodeCreateTransaction) Transaction.fromBytes(tx2Bytes);
assertThat(tx.getGossipCaCertificate()).isEqualTo(deserializedTx2.getGossipCaCertificate());
assertThat(tx.getGrpcCertificateHash()).isEqualTo(deserializedTx2.getGrpcCertificateHash());
}

@Test
void testSetNull() {
new NodeCreateTransaction()
.setDescription(null)
.setAccountId(null)
.setGossipCaCertificate(null)
.setGrpcCertificateHash(null)
.setAdminKey(null);
}


@Test
void constructNodeCreateTransactionFromTransactionBodyProtobuf() {
var transactionBodyBuilder = NodeCreateTransactionBody.newBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private NodeUpdateTransaction spawnTestTransaction() {
.setTransactionId(TransactionId.withValidStart(AccountId.fromString("0.0.5006"), TEST_VALID_START))
.setNodeId(TEST_NODE_ID)
.setAccountId(TEST_ACCOUNT_ID)
.setAccountId(TEST_ACCOUNT_ID)
.setDescription(TEST_DESCRIPTION)
.setGossipEndpoints(TEST_GOSSIP_ENDPOINTS)
.setServiceEndpoints(TEST_SERVICE_ENDPOINTS)
Expand All @@ -118,6 +117,36 @@ void shouldBytes() throws Exception {
assertThat(tx2.toString()).isEqualTo(tx.toString());
}

@Test
void testNullCertificates() throws Exception {
var tx = new NodeUpdateTransaction();
var tx2Bytes = tx.toBytes();
NodeUpdateTransaction deserializedTx = (NodeUpdateTransaction) Transaction.fromBytes(tx2Bytes);
assertThat(deserializedTx.getGossipCaCertificate()).isNull();
assertThat(deserializedTx.getGrpcCertificateHash()).isNull();
}

@Test
void testEmptyCertificates() throws Exception {
var tx = new NodeUpdateTransaction()
.setGossipCaCertificate(new byte[]{})
.setGrpcCertificateHash(new byte[]{});
var tx2Bytes = tx.toBytes();
NodeUpdateTransaction deserializedTx = (NodeUpdateTransaction) Transaction.fromBytes(tx2Bytes);
assertThat(deserializedTx.getGossipCaCertificate()).isEqualTo(new byte[]{});
assertThat(deserializedTx.getGrpcCertificateHash()).isEqualTo(new byte[]{});
}

@Test
void testSetNull() {
new NodeUpdateTransaction()
.setDescription(null)
.setAccountId(null)
.setGossipCaCertificate(null)
.setGrpcCertificateHash(null)
.setAdminKey(null);
}

@Test
void fromScheduledTransaction() {
var transactionBody = SchedulableTransactionBody.newBuilder()
Expand Down

0 comments on commit d719e27

Please sign in to comment.