Skip to content

Commit

Permalink
Create additional from/to bytes tests - part 1 (#1214)
Browse files Browse the repository at this point in the history
* fix: contract update from/to bytes

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* fix: account create/update and contract create

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

---------

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored Jan 14, 2025
1 parent 0487a73 commit 4148235
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 30 deletions.
13 changes: 13 additions & 0 deletions sdk/account_allowance_approve_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/wrapperspb"
)
Expand Down Expand Up @@ -251,3 +252,15 @@ func TestUnitAccountAllowanceApproveTransactionScheduleProtobuf(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected.String(), actual.String())
}

func TestUnitAllowanceApproveTransactionFromToBytes(t *testing.T) {
tx := NewAccountAllowanceApproveTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountAllowanceApproveTransaction).buildProtoBody())
}
13 changes: 13 additions & 0 deletions sdk/account_allowance_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
protobuf "google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -148,3 +149,15 @@ func TestUnitAccountAllowanceDeleteTransactionSetNothing(t *testing.T) {
_, err = transaction.GetSignatures()
require.NoError(t, err)
}

func TestUnitAllowanceDeleteTransactionFromToBytes(t *testing.T) {
tx := NewAccountAllowanceDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountAllowanceDeleteTransaction).buildProtoBody())
}
14 changes: 11 additions & 3 deletions sdk/account_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ func NewAccountCreateTransaction() *AccountCreateTransaction {
}

func _AccountCreateTransactionFromProtobuf(tx Transaction[*AccountCreateTransaction], pb *services.TransactionBody) AccountCreateTransaction {
key, _ := _KeyFromProtobuf(pb.GetCryptoCreateAccount().GetKey())
renew := _DurationFromProtobuf(pb.GetCryptoCreateAccount().GetAutoRenewPeriod())
var key Key = nil
if pb.GetCryptoCreateAccount().GetKey() != nil {
key, _ = _KeyFromProtobuf(pb.GetCryptoCreateAccount().GetKey())
}

var autoRenew *time.Duration
if pb.GetCryptoCreateAccount().GetAutoRenewPeriod() != nil {
autoRenewVal := _DurationFromProtobuf(pb.GetCryptoCreateAccount().GetAutoRenewPeriod())
autoRenew = &autoRenewVal
}

var stakedNodeID *int64
if pb.GetCryptoCreateAccount().GetStakedNodeId() != 0 {
Expand All @@ -62,7 +70,7 @@ func _AccountCreateTransactionFromProtobuf(tx Transaction[*AccountCreateTransact
accountCreateTransaction := AccountCreateTransaction{
key: key,
initialBalance: pb.GetCryptoCreateAccount().InitialBalance,
autoRenewPeriod: &renew,
autoRenewPeriod: autoRenew,
memo: pb.GetCryptoCreateAccount().GetMemo(),
receiverSignatureRequired: pb.GetCryptoCreateAccount().ReceiverSigRequired,
maxAutomaticTokenAssociations: pb.GetCryptoCreateAccount().MaxAutomaticTokenAssociations,
Expand Down
12 changes: 12 additions & 0 deletions sdk/account_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,15 @@ func TestUnitAccountCreateSetStakedAccountID(t *testing.T) {
require.Equal(t, int64(0), tx.GetStakedNodeID())
require.Equal(t, account, tx.GetStakedAccountID())
}

func TestUnitAccountCreateTransactionFromToBytes(t *testing.T) {
tx := NewAccountCreateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountCreateTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/account_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,15 @@ func TestUnitAccountDeleteTransactionTransactionMock(t *testing.T) {
Execute(client)
require.NoError(t, err)
}

func TestUnitAccountDeleteTransactionFromToBytes(t *testing.T) {
tx := NewAccountDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountDeleteTransaction).buildProtoBody())
}
31 changes: 24 additions & 7 deletions sdk/account_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func NewAccountUpdateTransaction() *AccountUpdateTransaction {
}

func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransaction], pb *services.TransactionBody) AccountUpdateTransaction {
key, _ := _KeyFromProtobuf(pb.GetCryptoUpdateAccount().GetKey())
var key Key
if pb.GetCryptoUpdateAccount().GetKey() != nil {
key, _ = _KeyFromProtobuf(pb.GetCryptoUpdateAccount().GetKey())
}

var receiverSignatureRequired bool

switch s := pb.GetCryptoUpdateAccount().GetReceiverSigRequiredField().(type) {
Expand All @@ -60,10 +64,23 @@ func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransact
receiverSignatureRequired = s.ReceiverSigRequiredWrapper.Value // nolint
}

autoRenew := _DurationFromProtobuf(pb.GetCryptoUpdateAccount().AutoRenewPeriod)
expiration := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().ExpirationTime)
var autoRenew *time.Duration
if pb.GetCryptoUpdateAccount().GetAutoRenewPeriod() != nil {
autoRenewVal := _DurationFromProtobuf(pb.GetCryptoUpdateAccount().GetAutoRenewPeriod())
autoRenew = &autoRenewVal
}

var expiration *time.Time
if pb.GetCryptoUpdateAccount().GetExpirationTime() != nil {
expirationVal := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().GetExpirationTime())
expiration = &expirationVal
}

stakedNodeID := pb.GetCryptoUpdateAccount().GetStakedNodeId()
var stakedNodeID *int64
if pb.GetCryptoUpdateAccount().GetStakedNodeId() != 0 {
stakedNodeIdVal := pb.GetCryptoUpdateAccount().GetStakedNodeId()
stakedNodeID = &stakedNodeIdVal
}

var stakeNodeAccountID *AccountID
if pb.GetCryptoUpdateAccount().GetStakedAccountId() != nil {
Expand All @@ -73,13 +90,13 @@ func _AccountUpdateTransactionFromProtobuf(tx Transaction[*AccountUpdateTransact
accountUpdateTransaction := AccountUpdateTransaction{
accountID: _AccountIDFromProtobuf(pb.GetCryptoUpdateAccount().GetAccountIDToUpdate()),
key: key,
autoRenewPeriod: &autoRenew,
autoRenewPeriod: autoRenew,
memo: pb.GetCryptoUpdateAccount().GetMemo().Value,
receiverSignatureRequired: receiverSignatureRequired,
expirationTime: &expiration,
expirationTime: expiration,
maxAutomaticTokenAssociations: pb.GetCryptoUpdateAccount().MaxAutomaticTokenAssociations.GetValue(),
stakedAccountID: stakeNodeAccountID,
stakedNodeID: &stakedNodeID,
stakedNodeID: stakedNodeID,
declineReward: pb.GetCryptoUpdateAccount().GetDeclineReward().GetValue(),
}

Expand Down
12 changes: 12 additions & 0 deletions sdk/account_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,15 @@ func TestUnitAccountUpdateTransactionCoverage(t *testing.T) {
b.AddSignature(key.PublicKey(), sig)
}
}

func TestUnitAccountUpdateTransactionFromToBytes(t *testing.T) {
tx := NewAccountUpdateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(AccountUpdateTransaction).buildProtoBody())
}
22 changes: 17 additions & 5 deletions sdk/contract_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,21 @@ func NewContractCreateTransaction() *ContractCreateTransaction {
}

func _ContractCreateTransactionFromProtobuf(tx Transaction[*ContractCreateTransaction], pb *services.TransactionBody) ContractCreateTransaction {
key, _ := _KeyFromProtobuf(pb.GetContractCreateInstance().GetAdminKey())
autoRenew := _DurationFromProtobuf(pb.GetContractCreateInstance().GetAutoRenewPeriod())
stakedNodeID := pb.GetContractCreateInstance().GetStakedNodeId()
var key Key = nil
if pb.GetContractCreateInstance().GetAdminKey() != nil {
key, _ = _KeyFromProtobuf(pb.GetContractCreateInstance().GetAdminKey())
}
var autoRenew *time.Duration
if pb.GetContractCreateInstance().GetAutoRenewPeriod() != nil {
autoRenewVal := _DurationFromProtobuf(pb.GetContractCreateInstance().GetAutoRenewPeriod())
autoRenew = &autoRenewVal
}

var stakedNodeID *int64
if pb.GetContractCreateInstance().GetStakedNodeId() != 0 {
stakedNodeIdVal := pb.GetContractCreateInstance().GetStakedNodeId()
stakedNodeID = &stakedNodeIdVal
}

var stakeNodeAccountID *AccountID
if pb.GetContractCreateInstance().GetStakedAccountId() != nil {
Expand All @@ -64,14 +76,14 @@ func _ContractCreateTransactionFromProtobuf(tx Transaction[*ContractCreateTransa
adminKey: key,
gas: pb.GetContractCreateInstance().Gas,
initialBalance: pb.GetContractCreateInstance().InitialBalance,
autoRenewPeriod: &autoRenew,
autoRenewPeriod: autoRenew,
parameters: pb.GetContractCreateInstance().ConstructorParameters,
memo: pb.GetContractCreateInstance().GetMemo(),
initcode: pb.GetContractCreateInstance().GetInitcode(),
autoRenewAccountID: autoRenewAccountID,
maxAutomaticTokenAssociations: pb.GetContractCreateInstance().MaxAutomaticTokenAssociations,
stakedAccountID: stakeNodeAccountID,
stakedNodeID: &stakedNodeID,
stakedNodeID: stakedNodeID,
declineReward: pb.GetContractCreateInstance().GetDeclineReward(),
}
tx.childTransaction = &contractCreateTransaction
Expand Down
12 changes: 12 additions & 0 deletions sdk/contract_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,15 @@ func TestUnitContractCreateTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitContractCreateTransactionFromToBytes(t *testing.T) {
tx := NewContractCreateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractCreateTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/contract_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,15 @@ func TestUnitContractDeleteTransactionMock(t *testing.T) {
Execute(client)
require.NoError(t, err)
}

func TestUnitContractDeleteTransactionFromToBytes(t *testing.T) {
tx := NewContractDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractDeleteTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/contract_execute_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,15 @@ func TestUnitContractExecuteTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitContractExecuteTransactionFromToBytes(t *testing.T) {
tx := NewContractExecuteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractExecuteTransaction).buildProtoBody())
}
37 changes: 22 additions & 15 deletions sdk/contract_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@ func NewContractUpdateTransaction() *ContractUpdateTransaction {
}

func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransaction], pb *services.TransactionBody) ContractUpdateTransaction {
key, _ := _KeyFromProtobuf(pb.GetContractUpdateInstance().AdminKey)
autoRenew := _DurationFromProtobuf(pb.GetContractUpdateInstance().GetAutoRenewPeriod())
expiration := _TimeFromProtobuf(pb.GetContractUpdateInstance().GetExpirationTime())
var key Key = nil
if pb.GetContractUpdateInstance().GetAdminKey() != nil {
key, _ = _KeyFromProtobuf(pb.GetContractUpdateInstance().GetAdminKey())
}
var autoRenew *time.Duration
if pb.GetContractUpdateInstance().GetAutoRenewPeriod() != nil {
autoRenewVal := _DurationFromProtobuf(pb.GetContractUpdateInstance().GetAutoRenewPeriod())
autoRenew = &autoRenewVal
}
var expiration *time.Time
if pb.GetContractUpdateInstance().GetExpirationTime() != nil {
expirationVal := _TimeFromProtobuf(pb.GetContractUpdateInstance().GetExpirationTime())
expiration = &expirationVal
}
var memo string

switch m := pb.GetContractUpdateInstance().GetMemoField().(type) {
Expand All @@ -75,7 +86,11 @@ func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransa
memo = m.MemoWrapper.Value
}

stakedNodeID := pb.GetContractUpdateInstance().GetStakedNodeId()
var stakedNodeID *int64
if pb.GetContractUpdateInstance().GetStakedNodeId() != 0 {
stakedNodeIdVal := pb.GetContractUpdateInstance().GetStakedNodeId()
stakedNodeID = &stakedNodeIdVal
}

var stakeNodeAccountID *AccountID
if pb.GetContractUpdateInstance().GetStakedAccountId() != nil {
Expand All @@ -90,13 +105,13 @@ func _ContractUpdateTransactionFromProtobuf(tx Transaction[*ContractUpdateTransa
contractUpdateTransaction := ContractUpdateTransaction{
contractID: _ContractIDFromProtobuf(pb.GetContractUpdateInstance().GetContractID()),
adminKey: key,
autoRenewPeriod: &autoRenew,
expirationTime: &expiration,
autoRenewPeriod: autoRenew,
expirationTime: expiration,
memo: memo,
autoRenewAccountID: autoRenewAccountID,
maxAutomaticTokenAssociations: pb.GetContractUpdateInstance().MaxAutomaticTokenAssociations.GetValue(),
stakedAccountID: stakeNodeAccountID,
stakedNodeID: &stakedNodeID,
stakedNodeID: stakedNodeID,
declineReward: pb.GetContractUpdateInstance().GetDeclineReward().GetValue(),
}
tx.childTransaction = &contractUpdateTransaction
Expand Down Expand Up @@ -384,14 +399,6 @@ func (tx ContractUpdateTransaction) buildProtoBody() *services.ContractUpdateTra
}
}

if tx.adminKey != nil {
body.AdminKey = tx.adminKey._ToProtoKey()
}

if tx.contractID != nil {
body.ContractID = tx.contractID._ToProtobuf()
}

if tx.stakedAccountID != nil {
body.StakedId = &services.ContractUpdateTransactionBody_StakedAccountId{StakedAccountId: tx.stakedAccountID._ToProtobuf()}
} else if tx.stakedNodeID != nil {
Expand Down
12 changes: 12 additions & 0 deletions sdk/contract_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,15 @@ func TestUnitContractUpdateTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitContractUpdateTransactionFromToBytes(t *testing.T) {
tx := NewContractUpdateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ContractUpdateTransaction).buildProtoBody())
}

0 comments on commit 4148235

Please sign in to comment.