Skip to content

Commit

Permalink
chore: add more tests and fix from/to bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov committed Jan 16, 2025
1 parent 37f8c41 commit 19af892
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 35 deletions.
23 changes: 13 additions & 10 deletions sdk/ethereum_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,13 @@ func (tx EthereumTransaction) validateNetworkOnIDs(client *Client) error {
}

func (tx EthereumTransaction) build() *services.TransactionBody {
body := &services.EthereumTransactionBody{
EthereumData: tx.ethereumData,
MaxGasAllowance: tx.MaxGasAllowed,
}

if tx.callData != nil {
body.CallData = tx.callData._ToProtobuf()
}

return &services.TransactionBody{
TransactionID: tx.transactionID._ToProtobuf(),
TransactionFee: tx.transactionFee,
TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
Memo: tx.Transaction.memo,
Data: &services.TransactionBody_EthereumTransaction{
EthereumTransaction: body,
EthereumTransaction: tx.buildProtoBody(),
},
}
}
Expand All @@ -148,6 +139,18 @@ func (tx EthereumTransaction) buildScheduled() (*services.SchedulableTransaction
return nil, errors.New("cannot schedule `EthereumTransaction`")
}

func (tx EthereumTransaction) buildProtoBody() *services.EthereumTransactionBody {
body := &services.EthereumTransactionBody{
EthereumData: tx.ethereumData,
MaxGasAllowance: tx.MaxGasAllowed,
}

if tx.callData != nil {
body.CallData = tx.callData._ToProtobuf()
}
return body
}

func (tx EthereumTransaction) constructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
return tx.buildScheduled()
}
Expand Down
13 changes: 13 additions & 0 deletions sdk/ethereum_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
protobuf "google.golang.org/protobuf/proto"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -133,3 +134,15 @@ func TestUnitEthereumTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitEthereumTransactionFromToBytes(t *testing.T) {
tx := NewEthereumTransaction()

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

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

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(EthereumTransaction).buildProtoBody())
}
6 changes: 5 additions & 1 deletion sdk/file_append_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ func NewFileAppendTransaction() *FileAppendTransaction {
}

func _FileAppendTransactionFromProtobuf(tx Transaction[*FileAppendTransaction], pb *services.TransactionBody) FileAppendTransaction {
var contents []byte = make([]byte, 0)
if pb.GetFileAppend().GetContents() != nil {
contents = pb.GetFileAppend().GetContents()
}
fileAppend := FileAppendTransaction{
maxChunks: 20,
contents: pb.GetFileAppend().GetContents(),
contents: contents,
chunkSize: 2048,
fileID: _FileIDFromProtobuf(pb.GetFileAppend().GetFileID()),
}
Expand Down
12 changes: 12 additions & 0 deletions sdk/file_append_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,15 @@ func TestUnitFileAppendTransactionSerialization(t *testing.T) {
require.Equal(t, fileID, result.GetFileID())
require.Equal(t, transaction.GetContents(), result.GetContents())
}

func TestUnitFileAppendTransactionFromToBytes(t *testing.T) {
tx := NewFileAppendTransaction()

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

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

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(FileAppendTransaction).buildProtoBody())
}
8 changes: 6 additions & 2 deletions sdk/file_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ func NewFileCreateTransaction() *FileCreateTransaction {
}

func _FileCreateTransactionFromProtobuf(tx Transaction[*FileCreateTransaction], pb *services.TransactionBody) FileCreateTransaction {
keys, _ := _KeyListFromProtobuf(pb.GetFileCreate().GetKeys())
var keys *KeyList
if pb.GetFileCreate().GetKeys() != nil {
keysValue, _ := _KeyListFromProtobuf(pb.GetFileCreate().GetKeys())
keys = &keysValue
}
expiration := _TimeFromProtobuf(pb.GetFileCreate().GetExpirationTime())

fileCreateTransaction := FileCreateTransaction{
keys: &keys,
keys: keys,
expirationTime: &expiration,
contents: pb.GetFileCreate().GetContents(),
memo: pb.GetMemo(),
Expand Down
13 changes: 13 additions & 0 deletions sdk/file_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"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 @@ -247,3 +248,15 @@ func TestUnitFileCreateTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitFileCreateTransactionFromToBytes(t *testing.T) {
tx := NewFileCreateTransaction()

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

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

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

func TestUnitFileDeleteTransactionFromToBytes(t *testing.T) {
tx := NewFileDeleteTransaction()

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

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

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(FileDeleteTransaction).buildProtoBody())
}
16 changes: 12 additions & 4 deletions sdk/file_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ func NewFileUpdateTransaction() *FileUpdateTransaction {
}

func _FileUpdateTransactionFromProtobuf(tx Transaction[*FileUpdateTransaction], pb *services.TransactionBody) FileUpdateTransaction {
keys, _ := _KeyListFromProtobuf(pb.GetFileUpdate().GetKeys())
expiration := _TimeFromProtobuf(pb.GetFileUpdate().GetExpirationTime())
var keys *KeyList
if pb.GetFileUpdate().GetKeys() != nil {
keysVal, _ := _KeyListFromProtobuf(pb.GetFileUpdate().GetKeys())
keys = &keysVal
}
var expiration *time.Time
if pb.GetFileUpdate().GetExpirationTime() != nil {
expirationVal := _TimeFromProtobuf(pb.GetFileUpdate().GetExpirationTime())
expiration = &expirationVal
}

fileUpdateTransaction := FileUpdateTransaction{
fileID: _FileIDFromProtobuf(pb.GetFileUpdate().GetFileID()),
keys: &keys,
expirationTime: &expiration,
keys: keys,
expirationTime: expiration,
contents: pb.GetFileUpdate().GetContents(),
memo: pb.GetFileUpdate().GetMemo().Value,
}
Expand Down
12 changes: 12 additions & 0 deletions sdk/file_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,15 @@ func TestUnitFileUpdateTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}

func TestUnitFileUpdateTransactionFromToBytes(t *testing.T) {
tx := NewFileUpdateTransaction()

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

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

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(FileUpdateTransaction).buildProtoBody())
}
48 changes: 30 additions & 18 deletions sdk/live_hash_add_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ func NewLiveHashAddTransaction() *LiveHashAddTransaction {
}

func _LiveHashAddTransactionFromProtobuf(tx Transaction[*LiveHashAddTransaction], pb *services.TransactionBody) LiveHashAddTransaction {
keys, _ := _KeyListFromProtobuf(pb.GetCryptoAddLiveHash().LiveHash.GetKeys())
duration := _DurationFromProtobuf(pb.GetCryptoAddLiveHash().LiveHash.Duration)
var keys *KeyList
if pb.GetCryptoAddLiveHash().GetLiveHash().GetKeys() != nil {
keysVal, _ := _KeyListFromProtobuf(pb.GetCryptoAddLiveHash().GetLiveHash().GetKeys())
keys = &keysVal
}
var duration time.Duration
if pb.GetCryptoAddLiveHash().GetLiveHash().GetDuration() != nil {
duration = _DurationFromProtobuf(pb.GetCryptoAddLiveHash().LiveHash.Duration)
}

var hash []byte = nil
if pb.GetCryptoAddLiveHash().LiveHash != nil {
hash = pb.GetCryptoAddLiveHash().LiveHash.Hash
}
liveHashAddTransaction := LiveHashAddTransaction{
accountID: _AccountIDFromProtobuf(pb.GetCryptoAddLiveHash().GetLiveHash().GetAccountId()),
hash: pb.GetCryptoAddLiveHash().LiveHash.Hash,
keys: &keys,
hash: hash,
keys: keys,
duration: &duration,
}
tx.childTransaction = &liveHashAddTransaction
Expand Down Expand Up @@ -165,24 +176,25 @@ func (tx LiveHashAddTransaction) buildScheduled() (*services.SchedulableTransact
}

func (tx LiveHashAddTransaction) buildProtoBody() *services.CryptoAddLiveHashTransactionBody {
body := &services.CryptoAddLiveHashTransactionBody{
LiveHash: &services.LiveHash{},
}
body := &services.CryptoAddLiveHashTransactionBody{}

if tx.accountID != nil {
body.LiveHash.AccountId = tx.accountID._ToProtobuf()
}
if tx.hash != nil {
body.LiveHash = &services.LiveHash{}
if tx.accountID != nil {
body.LiveHash.AccountId = tx.accountID._ToProtobuf()
}

if tx.duration != nil {
body.LiveHash.Duration = _DurationToProtobuf(*tx.duration)
}
if tx.duration != nil {
body.LiveHash.Duration = _DurationToProtobuf(*tx.duration)
}

if tx.keys != nil {
body.LiveHash.Keys = tx.keys._ToProtoKeyList()
}
if tx.keys != nil {
body.LiveHash.Keys = tx.keys._ToProtoKeyList()
}

if tx.hash != nil {
body.LiveHash.Hash = tx.hash
if tx.hash != nil {
body.LiveHash.Hash = tx.hash
}
}

return body
Expand Down
12 changes: 12 additions & 0 deletions sdk/live_hash_add_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,15 @@ func TestUnitLiveHashAddTransactionMock(t *testing.T) {
_, err = freez.Sign(newKey).Execute(client)
require.NoError(t, err)
}

func TestUnitLiveHashAddTransactionFromToBytes(t *testing.T) {
tx := NewLiveHashAddTransaction()

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

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

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

0 comments on commit 19af892

Please sign in to comment.