Skip to content

Commit

Permalink
Merge pull request #6247 from multiversx/MX-15556-Bugfix-Account-With…
Browse files Browse the repository at this point in the history
…Keys

fixed error on withKeys
  • Loading branch information
raduchis authored Jul 17, 2024
2 parents 6b9f696 + e778090 commit 3f3e5c4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (n *Node) GetKeyValuePairs(address string, options api.AccountQueryOptions,
}

if check.IfNil(userAccount.DataTrie()) {
return map[string]string{}, api.BlockInfo{}, nil
return map[string]string{}, blockInfo, nil
}

mapToReturn, err := n.getKeys(userAccount, ctx)
Expand Down Expand Up @@ -962,6 +962,10 @@ func (n *Node) GetAccountWithKeys(address string, options api.AccountQueryOption

var keys map[string]string
if options.WithKeys {
if accInfo.account == nil || accInfo.account.DataTrie() == nil {
return accInfo.accountResponse, accInfo.block, nil
}

keys, err = n.getKeys(accInfo.account, ctx)
if err != nil {
return api.AccountResponse{}, api.BlockInfo{}, err
Expand Down
48 changes: 48 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3536,6 +3536,54 @@ func TestNode_GetAccountAccountWithKeysShouldWork(t *testing.T) {
require.Equal(t, hex.EncodeToString(v2), recovAccnt.Pairs[hex.EncodeToString(k2)])
}

func TestNode_GetAccountAccountWithKeysNeverUsedAccountShouldWork(t *testing.T) {
t.Parallel()

accDB := &stateMock.AccountsStub{
GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) {
return nil, nil, nil
},
RecreateTrieCalled: func(options common.RootHashHolder) error {
return nil
},
}

n := getNodeWithAccount(accDB)

recovAccnt, blockInfo, err := n.GetAccountWithKeys(testscommon.TestAddressBob, api.AccountQueryOptions{WithKeys: true}, context.Background())

require.Nil(t, err)
require.Equal(t, uint64(0), recovAccnt.Nonce)
require.Equal(t, testscommon.TestAddressBob, recovAccnt.Address)
require.Equal(t, api.BlockInfo{}, blockInfo)
}

func TestNode_GetAccountAccountWithKeysNilDataTrieShouldWork(t *testing.T) {
t.Parallel()

accnt := createAcc(testscommon.TestPubKeyBob)
accnt.SetDataTrie(nil)
_ = accnt.AddToBalance(big.NewInt(1))

accDB := &stateMock.AccountsStub{
GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) {
return accnt, nil, nil
},
RecreateTrieCalled: func(options common.RootHashHolder) error {
return nil
},
}

n := getNodeWithAccount(accDB)

recovAccnt, blockInfo, err := n.GetAccountWithKeys(testscommon.TestAddressBob, api.AccountQueryOptions{WithKeys: true}, context.Background())

require.Nil(t, err)
require.Equal(t, uint64(0), recovAccnt.Nonce)
require.Equal(t, testscommon.TestAddressBob, recovAccnt.Address)
require.Equal(t, api.BlockInfo{}, blockInfo)
}

func getNodeWithAccount(accDB *stateMock.AccountsStub) *node.Node {
coreComponents := getDefaultCoreComponents()
dataComponents := getDefaultDataComponents()
Expand Down

0 comments on commit 3f3e5c4

Please sign in to comment.