Skip to content

Commit

Permalink
Merge commit '50fd0a2229ffcf87bb7a6236482829bbd94fad94'
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Akhterov <[email protected]>
  • Loading branch information
janaakhterov committed Mar 1, 2022
2 parents bb4acf3 + 50fd0a2 commit bec6f8a
Show file tree
Hide file tree
Showing 40 changed files with 1,597 additions and 441 deletions.
80 changes: 80 additions & 0 deletions account_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package hedera
import (
"testing"

"github.com/hashgraph/hedera-protobufs-go/services"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,3 +43,79 @@ func TestUnitAccountCreateTransactionValidateWrong(t *testing.T) {
assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum rmkyk, network: testnet", err.Error())
}
}

func TestUnitMockAccountCreateTransaction(t *testing.T) {
responses := [][]interface{}{{
status.New(codes.Unavailable, "node is UNAVAILABLE").Err(),
status.New(codes.Internal, "Received RST_STREAM with code 0").Err(),
}, {
&services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY,
},
&services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
},
&services.Response{
Response: &services.Response_TransactionGetReceipt{
TransactionGetReceipt: &services.TransactionGetReceiptResponse{
Header: &services.ResponseHeader{
Cost: 0,
ResponseType: services.ResponseType_COST_ANSWER,
},
},
},
},
&services.Response{
Response: &services.Response_TransactionGetReceipt{
TransactionGetReceipt: &services.TransactionGetReceiptResponse{
Header: &services.ResponseHeader{
Cost: 0,
ResponseType: services.ResponseType_ANSWER_ONLY,
},
Receipt: &services.TransactionReceipt{
Status: services.ResponseCodeEnum_RECEIPT_NOT_FOUND,
},
},
},
},
&services.Response{
Response: &services.Response_TransactionGetReceipt{
TransactionGetReceipt: &services.TransactionGetReceiptResponse{
Header: &services.ResponseHeader{
Cost: 0,
ResponseType: services.ResponseType_ANSWER_ONLY,
},
Receipt: &services.TransactionReceipt{
Status: services.ResponseCodeEnum_SUCCESS,
AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{
AccountNum: 234,
}},
},
},
},
},
}}

client, server := NewMockClientAndServer(responses)

newKey, err := PrivateKeyGenerateEd25519()
require.NoError(t, err)

newBalance := NewHbar(2)

tran := TransactionIDGenerate(AccountID{Account: 3})

resp, err := NewAccountCreateTransaction().
SetNodeAccountIDs([]AccountID{{Account: 3}, {Account: 4}}).
SetKey(newKey).
SetTransactionID(tran).
SetInitialBalance(newBalance).
SetMaxAutomaticTokenAssociations(100).
Execute(client)
require.NoError(t, err)

receipt, err := resp.GetReceipt(client)
require.NoError(t, err)
require.Equal(t, receipt.AccountID, &AccountID{Account: 234})
server.Close()
}
32 changes: 6 additions & 26 deletions account_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type AccountID struct {
checksum *string
}

type _AccountIDs struct {
type _AccountIDs struct { //nolint
accountIDs []AccountID
}

Expand Down Expand Up @@ -290,37 +290,17 @@ func (id AccountID) Compare(given AccountID) int {
}
}

func (accountIDs _AccountIDs) Len() int {
func (accountIDs _AccountIDs) Len() int { //nolint
return len(accountIDs.accountIDs)
}
func (accountIDs _AccountIDs) Swap(i, j int) {
func (accountIDs _AccountIDs) Swap(i, j int) { //nolint
accountIDs.accountIDs[i], accountIDs.accountIDs[j] = accountIDs.accountIDs[j], accountIDs.accountIDs[i]
}

func (accountIDs _AccountIDs) Less(i, j int) bool {
if accountIDs.accountIDs[i].Shard < accountIDs.accountIDs[j].Shard { //nolint
func (accountIDs _AccountIDs) Less(i, j int) bool { //nolint
if accountIDs.accountIDs[i].Compare(accountIDs.accountIDs[j]) < 0 { //nolint
return true
} else if accountIDs.accountIDs[i].Shard > accountIDs.accountIDs[j].Shard {
return false
}

if accountIDs.accountIDs[i].Realm < accountIDs.accountIDs[j].Realm { //nolint
return true
} else if accountIDs.accountIDs[i].Realm > accountIDs.accountIDs[j].Realm {
return false
}

if accountIDs.accountIDs[i].AliasKey != nil && accountIDs.accountIDs[j].AliasKey != nil {
if accountIDs.accountIDs[i].String() < accountIDs.accountIDs[j].String() { //nolint
return true
} else if accountIDs.accountIDs[i].String() > accountIDs.accountIDs[j].String() {
return false
}
}

if accountIDs.accountIDs[i].Account < accountIDs.accountIDs[j].Account { //nolint
return true
} else { //nolint
return false
}
return false
}
3 changes: 3 additions & 0 deletions account_records_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (query *AccountRecordsQuery) GetCost(client *Client) (Hbar, error) {
}

func _AccountRecordsQueryShouldRetry(_ _Request, response _Response) _ExecutionState {
if response.query.GetCryptoGetAccountRecords() == nil {
println("nil adf")
}
return _QueryShouldRetry(Status(response.query.GetCryptoGetAccountRecords().Header.NodeTransactionPrecheckCode))
}

Expand Down
51 changes: 51 additions & 0 deletions account_records_query_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package hedera
import (
"testing"

"github.com/hashgraph/hedera-protobufs-go/services"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,3 +41,52 @@ func TestUnitAccountRecordQueryValidateWrong(t *testing.T) {
assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum rmkyk, network: testnet", err.Error())
}
}

func TestUnitMockAccountRecordsQuery(t *testing.T) {
responses := [][]interface{}{{
&services.Response{
Response: &services.Response_CryptoGetAccountRecords{
CryptoGetAccountRecords: &services.CryptoGetAccountRecordsResponse{
Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY, ResponseType: services.ResponseType_ANSWER_ONLY},
},
},
},
&services.Response{
Response: &services.Response_CryptoGetAccountRecords{
CryptoGetAccountRecords: &services.CryptoGetAccountRecordsResponse{
Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 1},
AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
Records: []*services.TransactionRecord{
{
TransactionHash: []byte{1},
ConsensusTimestamp: &services.Timestamp{Nanos: 12313123, Seconds: 2313},
TransactionID: &services.TransactionID{
TransactionValidStart: &services.Timestamp{Nanos: 12313123, Seconds: 2313},
AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
Scheduled: false,
Nonce: 0,
},
Memo: "",
TransactionFee: 0,
},
},
},
},
},
}}

client, server := NewMockClientAndServer(responses)

recordsQuery, err := NewAccountRecordsQuery().
SetNodeAccountIDs([]AccountID{{Account: 3}}).
SetAccountID(AccountID{Account: 1800}).
SetMaxQueryPayment(NewHbar(1)).
SetQueryPayment(HbarFromTinybar(25)).
Execute(client)
require.NoError(t, err)

require.Equal(t, len(recordsQuery), 1)
require.Equal(t, recordsQuery[0].TransactionID.AccountID.Account, uint64(1800))

server.Close()
}
70 changes: 70 additions & 0 deletions account_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package hedera

import (
"encoding/hex"
"testing"

"github.com/hashgraph/hedera-protobufs-go/services"
protobuf "google.golang.org/protobuf/proto"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,3 +43,69 @@ func TestUnitAccountUpdateTransactionValidateWrong(t *testing.T) {
assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum rmkyk, network: testnet", err.Error())
}
}

func TestUnitMockAccountUpdateTransaction(t *testing.T) {
call := func(request *services.Transaction) *services.TransactionResponse {
require.NotEmpty(t, request.SignedTransactionBytes)
signedTransaction := services.SignedTransaction{}
_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)

require.NotEmpty(t, signedTransaction.BodyBytes)
transactionBody := services.TransactionBody{}
_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)

require.NotNil(t, transactionBody.TransactionID)
transactionId := transactionBody.TransactionID.String()
require.NotEqual(t, "", transactionId)

sigMap := signedTransaction.GetSigMap()
require.NotNil(t, sigMap)

for _, sigPair := range sigMap.SigPair {
verified := false

switch k := sigPair.Signature.(type) {
case *services.SignaturePair_Ed25519:
pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
case *services.SignaturePair_ECDSASecp256K1:
pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
}
require.True(t, verified)
}

if bod, ok := transactionBody.Data.(*services.TransactionBody_CryptoUpdateAccount); ok {
require.Equal(t, bod.CryptoUpdateAccount.Memo.Value, "no")
require.Equal(t, bod.CryptoUpdateAccount.AccountIDToUpdate.GetAccountNum(), int64(123))
alias := services.Key{}
_ = protobuf.Unmarshal(bod.CryptoUpdateAccount.Alias, &alias)
require.Equal(t, hex.EncodeToString(alias.GetEd25519()), "1480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2")
}

return &services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
}
}
responses := [][]interface{}{{
call,
}}

client, server := NewMockClientAndServer(responses)
//302a300506032b65700321001480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2
newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420278184257eb568d0e5fcfc1df99828b039b4776da05855dc5af105996e6200d1")
require.NoError(t, err)

tran := TransactionIDGenerate(AccountID{Account: 3})

_, err = NewAccountUpdateTransaction().
SetNodeAccountIDs([]AccountID{{Account: 3}}).
SetTransactionID(tran).
SetAccountMemo("no").
SetAccountID(AccountID{Account: 123}).
SetAliasKey(newKey.PublicKey()).
Execute(client)
require.NoError(t, err)

server.Close()
}
Loading

0 comments on commit bec6f8a

Please sign in to comment.