Skip to content

Commit

Permalink
chore: fix serialize/deserialize
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov committed Nov 20, 2024
1 parent 554c71c commit a5faab7
Show file tree
Hide file tree
Showing 4 changed files with 58 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();
var protobufGossipCert = body.getGossipCaCertificate();
gossipCaCertificate = protobufGossipCert.equals(BytesValue.getDefaultInstance()) ?
null : protobufGossipCert.getValue().toByteArray();

grpcCertificateHash = body.getGrpcCertificateHash().getValue().toByteArray();
var protobufGrpcCert = body.getGrpcCertificateHash();
grpcCertificateHash = protobufGrpcCert.equals(BytesValue.getDefaultInstance()) ?
null : protobufGrpcCert.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 @@ -117,6 +116,25 @@ void shouldBytes() throws Exception {
var tx2 = NodeUpdateTransaction.fromBytes(tx.toBytes());
assertThat(tx2.toString()).isEqualTo(tx.toString());
}
@Test
void testSerializeDeserialize() throws Exception {
var tx = new NodeUpdateTransaction().setDescription(TEST_DESCRIPTION);
var tx2 = new NodeUpdateTransaction().setDescription(TEST_DESCRIPTION);
var tx2Bytes = tx2.toBytes();
NodeUpdateTransaction deserializedTx2 = (NodeUpdateTransaction) Transaction.fromBytes(tx2Bytes);
assertThat(tx.getGossipCaCertificate()).isEqualTo(deserializedTx2.getGossipCaCertificate());
assertThat(tx.getGrpcCertificateHash()).isEqualTo(deserializedTx2.getGrpcCertificateHash());
}

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

@Test
void fromScheduledTransaction() {
Expand Down

0 comments on commit a5faab7

Please sign in to comment.