From 1b703d90f80b5798d687f5b56e9a67872eeb0e71 Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Mon, 13 Mar 2023 10:29:03 +0200 Subject: [PATCH 1/6] upgrade indexer --- go.mod | 2 +- go.sum | 4 ++-- vm/systemSmartContracts/esdt.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index b3193c94a46..5c63c6a5ffa 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-core-go v1.1.33 github.com/multiversx/mx-chain-crypto-go v1.2.5 - github.com/multiversx/mx-chain-es-indexer-go v1.3.13 + github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821 github.com/multiversx/mx-chain-logger-go v1.0.11 github.com/multiversx/mx-chain-p2p-go v1.0.10 github.com/multiversx/mx-chain-storage-go v1.0.7 diff --git a/go.sum b/go.sum index 7595f688474..4585429b608 100644 --- a/go.sum +++ b/go.sum @@ -601,8 +601,8 @@ github.com/multiversx/mx-chain-core-go v1.1.33 h1:qk+TlaOhHpu+9VncL3yowjY4KU8uJ0 github.com/multiversx/mx-chain-core-go v1.1.33/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk= github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXypizy8zPH0NpTGZk= github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4= -github.com/multiversx/mx-chain-es-indexer-go v1.3.13 h1:n32q0Xe6YfUIjfK3ryKe70WnsBQseaDrwb2tGDpTTAk= -github.com/multiversx/mx-chain-es-indexer-go v1.3.13/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821 h1:SI+9VI+4VJUdOmUO7hQAY81tN523UI1e3TH7Eu31DxE= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE= github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA= github.com/multiversx/mx-chain-p2p-go v1.0.10 h1:CYCuI0SP8Pt9K0TcJjUyxK7ByvWi2FXNUihy0iCEVIA= diff --git a/vm/systemSmartContracts/esdt.go b/vm/systemSmartContracts/esdt.go index 89a8ad39772..4f49327ac0b 100644 --- a/vm/systemSmartContracts/esdt.go +++ b/vm/systemSmartContracts/esdt.go @@ -427,7 +427,7 @@ func (e *esdt) registerMetaESDT(args *vmcommon.ContractCallInput) vmcommon.Retur logEntry := &vmcommon.LogEntry{ Identifier: []byte(args.Function), Address: args.CallerAddr, - Topics: [][]byte{tokenIdentifier, args.Arguments[0], args.Arguments[1], []byte(metaESDT)}, + Topics: [][]byte{tokenIdentifier, args.Arguments[0], args.Arguments[1], []byte(metaESDT), big.NewInt(int64(numOfDecimals)).Bytes()}, } e.eei.AddLogEntry(logEntry) From a1304ac4ff380999148653296816cf33f0b55363 Mon Sep 17 00:00:00 2001 From: Bogdan Rosianu Date: Mon, 13 Mar 2023 12:21:47 +0200 Subject: [PATCH 2/6] always return empty responses when accounts are not found --- node/node.go | 64 +++++++++-- node/node_test.go | 287 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 342 insertions(+), 9 deletions(-) diff --git a/node/node.go b/node/node.go index ce56b3a02e4..d7dfdb95786 100644 --- a/node/node.go +++ b/node/node.go @@ -157,13 +157,11 @@ func (n *Node) GetConsensusGroupSize() int { func (n *Node) GetBalance(address string, options api.AccountQueryOptions) (*big.Int, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - apiBlockInfo, ok := extractApiBlockInfoIfErrAccountNotFoundAtBlock(err) - if ok { - return big.NewInt(0), apiBlockInfo, nil - } - if err == ErrCannotCastAccountHandlerToUserAccountHandler { - return big.NewInt(0), api.BlockInfo{}, nil + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return big.NewInt(0), adaptedBlockInfo, nil } + return nil, api.BlockInfo{}, err } @@ -174,6 +172,11 @@ func (n *Node) GetBalance(address string, options api.AccountQueryOptions) (*big func (n *Node) GetUsername(address string, options api.AccountQueryOptions) (string, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return "", adaptedBlockInfo, nil + } + return "", api.BlockInfo{}, err } @@ -185,6 +188,11 @@ func (n *Node) GetUsername(address string, options api.AccountQueryOptions) (str func (n *Node) GetCodeHash(address string, options api.AccountQueryOptions) ([]byte, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return make([]byte, 0), adaptedBlockInfo, nil + } + return nil, api.BlockInfo{}, err } @@ -200,6 +208,7 @@ func (n *Node) GetAllIssuedESDTs(tokenType string, ctx context.Context) ([]strin userAccount, _, err := n.loadUserAccountHandlerByPubKey(vm.ESDTSCAddress, api.AccountQueryOptions{}) if err != nil { + // don't return 0 values here - not finding the ESDT SC address is an error that should be returned return nil, err } @@ -277,6 +286,11 @@ func (n *Node) getEsdtDataFromLeaf(leaf core.KeyValueHolder, userAccount state.U func (n *Node) GetKeyValuePairs(address string, options api.AccountQueryOptions, ctx context.Context) (map[string]string, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return make(map[string]string), adaptedBlockInfo, nil + } + return nil, api.BlockInfo{}, err } @@ -331,6 +345,11 @@ func (n *Node) GetValueForKey(address string, key string, options api.AccountQue userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return "", adaptedBlockInfo, nil + } + return "", api.BlockInfo{}, err } @@ -347,6 +366,13 @@ func (n *Node) GetESDTData(address, tokenID string, nonce uint64, options api.Ac // TODO: refactor here as to ensure userAccount and systemAccount are on the same root-hash userAccount, _, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return &esdt.ESDigitalToken{ + Value: big.NewInt(0), + }, adaptedBlockInfo, nil + } + return nil, api.BlockInfo{}, err } @@ -512,6 +538,11 @@ func (n *Node) GetAllESDTTokens(address string, options api.AccountQueryOptions, // TODO: refactor here as to ensure userAccount and systemAccount are on the same root-hash userAccount, _, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { + return make(map[string]*esdt.ESDigitalToken), adaptedBlockInfo, nil + } + return nil, api.BlockInfo{}, err } @@ -839,13 +870,13 @@ func (n *Node) CreateTransaction( func (n *Node) GetAccount(address string, options api.AccountQueryOptions) (api.AccountResponse, api.BlockInfo, error) { account, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - apiBlockInfo, ok := extractApiBlockInfoIfErrAccountNotFoundAtBlock(err) - if ok { + adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) + if adaptErr == nil { return api.AccountResponse{ Address: address, Balance: "0", DeveloperReward: "0", - }, apiBlockInfo, nil + }, adaptedBlockInfo, nil } return api.AccountResponse{}, api.BlockInfo{}, err @@ -870,6 +901,21 @@ func (n *Node) GetAccount(address string, options api.AccountQueryOptions) (api. }, blockInfo, nil } +func adaptBlockInfoIfError(err error) (api.BlockInfo, error) { + if err == nil { + return api.BlockInfo{}, nil + } + + apiBlockInfo, ok := extractApiBlockInfoIfErrAccountNotFoundAtBlock(err) + if ok { + return apiBlockInfo, nil + } + if err == ErrCannotCastAccountHandlerToUserAccountHandler { + return api.BlockInfo{}, nil + } + return api.BlockInfo{}, err +} + // GetCode returns the code for the given code hash func (n *Node) GetCode(codeHash []byte, options api.AccountQueryOptions) ([]byte, api.BlockInfo) { return n.loadAccountCode(codeHash, options) diff --git a/node/node_test.go b/node/node_test.go index 59d0c18cfc9..40479c69e22 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -62,6 +62,25 @@ import ( "github.com/stretchr/testify/require" ) +type testBlockInfo struct { +} + +func (t testBlockInfo) asApiResult() api.BlockInfo { + return api.BlockInfo{ + Nonce: 37, + Hash: hex.EncodeToString([]byte("hash")), + RootHash: hex.EncodeToString([]byte("root")), + } +} + +func (t testBlockInfo) asForProcessing() common.BlockInfo { + hash := []byte("hash") + rHash := []byte("root") + return holders.NewBlockInfo(hash, 37, rHash) +} + +var dummyBlockInfo = testBlockInfo{} + func createMockPubkeyConverter() *mock.PubkeyConverterMock { return mock.NewPubkeyConverterMock(32) } @@ -192,6 +211,44 @@ func TestGetBalance_AccountNotFoundShouldReturnZeroBalance(t *testing.T) { assert.Equal(t, big.NewInt(0), balance) } +func TestNode_GetBalanceAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + balance, bInfo, err := n.GetBalance(testscommon.TestAddressAlice, api.AccountQueryOptions{}) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Empty(t, balance) +} + func TestGetBalance(t *testing.T) { t.Parallel() @@ -256,6 +313,44 @@ func TestGetUsername(t *testing.T) { assert.Equal(t, string(expectedUsername), username) } +func TestNode_GetCodeHashAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + codeHash, bInfo, err := n.GetCodeHash("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", api.AccountQueryOptions{}) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Empty(t, codeHash) +} + func TestGetCodeHash(t *testing.T) { t.Parallel() @@ -289,6 +384,44 @@ func TestGetCodeHash(t *testing.T) { assert.Equal(t, expectedCodeHash, codeHash) } +func TestNode_GetKeyValuePairsAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + pairs, bInfo, err := n.GetKeyValuePairs(testscommon.TestAddressAlice, api.AccountQueryOptions{}, context.Background()) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Len(t, pairs, 0) +} + func TestNode_GetKeyValuePairs(t *testing.T) { t.Parallel() @@ -469,6 +602,44 @@ func TestNode_GetKeyValuePairsContextShouldTimeout(t *testing.T) { assert.Equal(t, node.ErrTrieOperationsTimeout, err) } +func TestNode_GetValueForKeyAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + value, bInfo, err := n.GetValueForKey(testscommon.TestAddressAlice, "0a0a", api.AccountQueryOptions{}) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Empty(t, value) +} + func TestNode_GetValueForKey(t *testing.T) { t.Parallel() @@ -511,6 +682,46 @@ func TestNode_GetValueForKey(t *testing.T) { assert.Equal(t, hex.EncodeToString(v1), value) } +func TestNode_GetESDTDataAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + esdtToken := "newToken" + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + esdtTokenData, bInfo, err := n.GetESDTData(testscommon.TestAddressAlice, esdtToken, 0, api.AccountQueryOptions{}) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, "0", esdtTokenData.Value.String()) +} + func TestNode_GetESDTData(t *testing.T) { t.Parallel() @@ -787,6 +998,44 @@ func TestNode_GetAllESDTTokensContextShouldTimeout(t *testing.T) { assert.Equal(t, node.ErrTrieOperationsTimeout, err) } +func TestNode_GetAllESDTsAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + tokens, bInfo, err := n.GetAllESDTTokens(testscommon.TestAddressAlice, api.AccountQueryOptions{}, context.Background()) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Len(t, tokens, 0) +} + func TestNode_GetAllESDTTokensShouldReturnEsdtAndFormattedNft(t *testing.T) { t.Parallel() @@ -2941,6 +3190,44 @@ func TestNode_GetAccountAccountsRepositoryFailsShouldErr(t *testing.T) { assert.ErrorIs(t, err, errExpected) } +func TestNode_GetAccountAccNotFoundShouldReturnEmpty(t *testing.T) { + t.Parallel() + + accDB := &stateMock.AccountsStub{ + GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + }, + RecreateTrieCalled: func(_ []byte) error { + return nil + }, + } + + dataComponents := getDefaultDataComponents() + coreComponents := getDefaultCoreComponents() + coreComponents.IntMarsh = getMarshalizer() + coreComponents.VmMarsh = getMarshalizer() + coreComponents.Hash = getHasher() + + stateComponents := getDefaultStateComponents() + args := state.ArgsAccountsRepository{ + FinalStateAccountsWrapper: accDB, + CurrentStateAccountsWrapper: accDB, + HistoricalStateAccountsWrapper: accDB, + } + stateComponents.AccountsRepo, _ = state.NewAccountsRepository(args) + + n, _ := node.NewNode( + node.WithDataComponents(dataComponents), + node.WithCoreComponents(coreComponents), + node.WithStateComponents(stateComponents), + ) + + acc, bInfo, err := n.GetAccount(testscommon.TestAddressAlice, api.AccountQueryOptions{}) + require.Nil(t, err) + require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, api.AccountResponse{Address: testscommon.TestAddressAlice, Balance: "0", DeveloperReward: "0"}, acc) +} + func TestNode_GetAccountAccountExistsShouldReturn(t *testing.T) { t.Parallel() From e2dea11a0b32856d57fc1e4ba0884fab79d526d2 Mon Sep 17 00:00:00 2001 From: Bogdan Rosianu Date: Mon, 13 Mar 2023 13:55:17 +0200 Subject: [PATCH 3/6] fixes after review --- node/node.go | 44 +++++++++++++++++++++++--------------------- node/node_test.go | 32 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/node/node.go b/node/node.go index d7dfdb95786..b6553c127ed 100644 --- a/node/node.go +++ b/node/node.go @@ -157,8 +157,8 @@ func (n *Node) GetConsensusGroupSize() int { func (n *Node) GetBalance(address string, options api.AccountQueryOptions) (*big.Int, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return big.NewInt(0), adaptedBlockInfo, nil } @@ -172,8 +172,8 @@ func (n *Node) GetBalance(address string, options api.AccountQueryOptions) (*big func (n *Node) GetUsername(address string, options api.AccountQueryOptions) (string, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return "", adaptedBlockInfo, nil } @@ -188,8 +188,8 @@ func (n *Node) GetUsername(address string, options api.AccountQueryOptions) (str func (n *Node) GetCodeHash(address string, options api.AccountQueryOptions) ([]byte, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return make([]byte, 0), adaptedBlockInfo, nil } @@ -286,8 +286,8 @@ func (n *Node) getEsdtDataFromLeaf(leaf core.KeyValueHolder, userAccount state.U func (n *Node) GetKeyValuePairs(address string, options api.AccountQueryOptions, ctx context.Context) (map[string]string, api.BlockInfo, error) { userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return make(map[string]string), adaptedBlockInfo, nil } @@ -345,8 +345,8 @@ func (n *Node) GetValueForKey(address string, key string, options api.AccountQue userAccount, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return "", adaptedBlockInfo, nil } @@ -366,8 +366,8 @@ func (n *Node) GetESDTData(address, tokenID string, nonce uint64, options api.Ac // TODO: refactor here as to ensure userAccount and systemAccount are on the same root-hash userAccount, _, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return &esdt.ESDigitalToken{ Value: big.NewInt(0), }, adaptedBlockInfo, nil @@ -538,8 +538,8 @@ func (n *Node) GetAllESDTTokens(address string, options api.AccountQueryOptions, // TODO: refactor here as to ensure userAccount and systemAccount are on the same root-hash userAccount, _, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return make(map[string]*esdt.ESDigitalToken), adaptedBlockInfo, nil } @@ -870,8 +870,8 @@ func (n *Node) CreateTransaction( func (n *Node) GetAccount(address string, options api.AccountQueryOptions) (api.AccountResponse, api.BlockInfo, error) { account, blockInfo, err := n.loadUserAccountHandlerByAddress(address, options) if err != nil { - adaptedBlockInfo, adaptErr := adaptBlockInfoIfError(err) - if adaptErr == nil { + adaptedBlockInfo, isEmptyAccount := extractBlockInfoIfNewAccount(err) + if isEmptyAccount { return api.AccountResponse{ Address: address, Balance: "0", @@ -901,19 +901,21 @@ func (n *Node) GetAccount(address string, options api.AccountQueryOptions) (api. }, blockInfo, nil } -func adaptBlockInfoIfError(err error) (api.BlockInfo, error) { +func extractBlockInfoIfNewAccount(err error) (api.BlockInfo, bool) { if err == nil { - return api.BlockInfo{}, nil + return api.BlockInfo{}, true } apiBlockInfo, ok := extractApiBlockInfoIfErrAccountNotFoundAtBlock(err) if ok { - return apiBlockInfo, nil + return apiBlockInfo, true } + // we need this check since (in some situations) this error is also returned when a nil account handler is passed (empty account) if err == ErrCannotCastAccountHandlerToUserAccountHandler { - return api.BlockInfo{}, nil + return api.BlockInfo{}, true } - return api.BlockInfo{}, err + + return api.BlockInfo{}, false } // GetCode returns the code for the given code hash diff --git a/node/node_test.go b/node/node_test.go index 40479c69e22..c1d1b47a4a4 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -65,7 +65,7 @@ import ( type testBlockInfo struct { } -func (t testBlockInfo) asApiResult() api.BlockInfo { +func (t testBlockInfo) apiResult() api.BlockInfo { return api.BlockInfo{ Nonce: 37, Hash: hex.EncodeToString([]byte("hash")), @@ -73,7 +73,7 @@ func (t testBlockInfo) asApiResult() api.BlockInfo { } } -func (t testBlockInfo) asForProcessing() common.BlockInfo { +func (t testBlockInfo) forProcessing() common.BlockInfo { hash := []byte("hash") rHash := []byte("root") return holders.NewBlockInfo(hash, 37, rHash) @@ -216,7 +216,7 @@ func TestNode_GetBalanceAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -245,7 +245,7 @@ func TestNode_GetBalanceAccNotFoundShouldReturnEmpty(t *testing.T) { balance, bInfo, err := n.GetBalance(testscommon.TestAddressAlice, api.AccountQueryOptions{}) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Empty(t, balance) } @@ -318,7 +318,7 @@ func TestNode_GetCodeHashAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -347,7 +347,7 @@ func TestNode_GetCodeHashAccNotFoundShouldReturnEmpty(t *testing.T) { codeHash, bInfo, err := n.GetCodeHash("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", api.AccountQueryOptions{}) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Empty(t, codeHash) } @@ -389,7 +389,7 @@ func TestNode_GetKeyValuePairsAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -418,7 +418,7 @@ func TestNode_GetKeyValuePairsAccNotFoundShouldReturnEmpty(t *testing.T) { pairs, bInfo, err := n.GetKeyValuePairs(testscommon.TestAddressAlice, api.AccountQueryOptions{}, context.Background()) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Len(t, pairs, 0) } @@ -607,7 +607,7 @@ func TestNode_GetValueForKeyAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -636,7 +636,7 @@ func TestNode_GetValueForKeyAccNotFoundShouldReturnEmpty(t *testing.T) { value, bInfo, err := n.GetValueForKey(testscommon.TestAddressAlice, "0a0a", api.AccountQueryOptions{}) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Empty(t, value) } @@ -689,7 +689,7 @@ func TestNode_GetESDTDataAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -718,7 +718,7 @@ func TestNode_GetESDTDataAccNotFoundShouldReturnEmpty(t *testing.T) { esdtTokenData, bInfo, err := n.GetESDTData(testscommon.TestAddressAlice, esdtToken, 0, api.AccountQueryOptions{}) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Equal(t, "0", esdtTokenData.Value.String()) } @@ -1003,7 +1003,7 @@ func TestNode_GetAllESDTsAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -1032,7 +1032,7 @@ func TestNode_GetAllESDTsAccNotFoundShouldReturnEmpty(t *testing.T) { tokens, bInfo, err := n.GetAllESDTTokens(testscommon.TestAddressAlice, api.AccountQueryOptions{}, context.Background()) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Len(t, tokens, 0) } @@ -3195,7 +3195,7 @@ func TestNode_GetAccountAccNotFoundShouldReturnEmpty(t *testing.T) { accDB := &stateMock.AccountsStub{ GetAccountWithBlockInfoCalled: func(address []byte, options common.RootHashHolder) (vmcommon.AccountHandler, common.BlockInfo, error) { - return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.asForProcessing()) + return nil, nil, state.NewErrAccountNotFoundAtBlock(dummyBlockInfo.forProcessing()) }, RecreateTrieCalled: func(_ []byte) error { return nil @@ -3224,7 +3224,7 @@ func TestNode_GetAccountAccNotFoundShouldReturnEmpty(t *testing.T) { acc, bInfo, err := n.GetAccount(testscommon.TestAddressAlice, api.AccountQueryOptions{}) require.Nil(t, err) - require.Equal(t, dummyBlockInfo.asApiResult(), bInfo) + require.Equal(t, dummyBlockInfo.apiResult(), bInfo) require.Equal(t, api.AccountResponse{Address: testscommon.TestAddressAlice, Balance: "0", DeveloperReward: "0"}, acc) } From 330e9c1a82d7059f87a9c99d2ec372a09b2933da Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Mon, 13 Mar 2023 14:42:55 +0200 Subject: [PATCH 4/6] last commit --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5c63c6a5ffa..4b9a0ef7275 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-core-go v1.1.33 github.com/multiversx/mx-chain-crypto-go v1.2.5 - github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821 + github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4 github.com/multiversx/mx-chain-logger-go v1.0.11 github.com/multiversx/mx-chain-p2p-go v1.0.10 github.com/multiversx/mx-chain-storage-go v1.0.7 diff --git a/go.sum b/go.sum index 4585429b608..cd78bc5db3b 100644 --- a/go.sum +++ b/go.sum @@ -601,8 +601,8 @@ github.com/multiversx/mx-chain-core-go v1.1.33 h1:qk+TlaOhHpu+9VncL3yowjY4KU8uJ0 github.com/multiversx/mx-chain-core-go v1.1.33/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk= github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXypizy8zPH0NpTGZk= github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821 h1:SI+9VI+4VJUdOmUO7hQAY81tN523UI1e3TH7Eu31DxE= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313082458-2979bd238821/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4 h1:IBqfuSt2L3nTO3DHIOcdCGoEL9Y/RPTSviNxGFZQV8I= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE= github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA= github.com/multiversx/mx-chain-p2p-go v1.0.10 h1:CYCuI0SP8Pt9K0TcJjUyxK7ByvWi2FXNUihy0iCEVIA= From fb6cb0d5175bad3cbf0babf07d897aab5a738c7a Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Mon, 13 Mar 2023 16:50:18 +0200 Subject: [PATCH 5/6] last commit from indexer --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4b9a0ef7275..7f0a1648f6e 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-core-go v1.1.33 github.com/multiversx/mx-chain-crypto-go v1.2.5 - github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4 + github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba github.com/multiversx/mx-chain-logger-go v1.0.11 github.com/multiversx/mx-chain-p2p-go v1.0.10 github.com/multiversx/mx-chain-storage-go v1.0.7 diff --git a/go.sum b/go.sum index cd78bc5db3b..23bc8d87353 100644 --- a/go.sum +++ b/go.sum @@ -601,8 +601,8 @@ github.com/multiversx/mx-chain-core-go v1.1.33 h1:qk+TlaOhHpu+9VncL3yowjY4KU8uJ0 github.com/multiversx/mx-chain-core-go v1.1.33/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk= github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXypizy8zPH0NpTGZk= github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4 h1:IBqfuSt2L3nTO3DHIOcdCGoEL9Y/RPTSviNxGFZQV8I= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313121143-b267eb5f6eb4/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba h1:SSReszOV9hMPXvHSKDHUWOqKI6qQTgH8kPqdVLujDHQ= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE= github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA= github.com/multiversx/mx-chain-p2p-go v1.0.10 h1:CYCuI0SP8Pt9K0TcJjUyxK7ByvWi2FXNUihy0iCEVIA= From 77a2d9ac25978fe7befff906e5e9a566262f0ed1 Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Mon, 13 Mar 2023 17:51:06 +0200 Subject: [PATCH 6/6] proper tag --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7f0a1648f6e..d3f3434ade0 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-core-go v1.1.33 github.com/multiversx/mx-chain-crypto-go v1.2.5 - github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba + github.com/multiversx/mx-chain-es-indexer-go v1.3.14 github.com/multiversx/mx-chain-logger-go v1.0.11 github.com/multiversx/mx-chain-p2p-go v1.0.10 github.com/multiversx/mx-chain-storage-go v1.0.7 diff --git a/go.sum b/go.sum index 23bc8d87353..6d8780bcd35 100644 --- a/go.sum +++ b/go.sum @@ -601,8 +601,8 @@ github.com/multiversx/mx-chain-core-go v1.1.33 h1:qk+TlaOhHpu+9VncL3yowjY4KU8uJ0 github.com/multiversx/mx-chain-core-go v1.1.33/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk= github.com/multiversx/mx-chain-crypto-go v1.2.5 h1:tuq3BUNMhKud5DQbZi9DiVAAHUXypizy8zPH0NpTGZk= github.com/multiversx/mx-chain-crypto-go v1.2.5/go.mod h1:teqhNyWEqfMPgNn8sgWXlgtJ1a36jGCnhs/tRpXW6r4= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba h1:SSReszOV9hMPXvHSKDHUWOqKI6qQTgH8kPqdVLujDHQ= -github.com/multiversx/mx-chain-es-indexer-go v1.3.14-0.20230313144405-7c1ad2c727ba/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14 h1:cDaBsY8i2PuCQZ6DMf3LgxB0ocErM3TWlKGGzGqGX7E= +github.com/multiversx/mx-chain-es-indexer-go v1.3.14/go.mod h1:IV42GfhkqQ5vVO0OzGaF/ejp8TQrLkNo4LSB3TPnVhg= github.com/multiversx/mx-chain-logger-go v1.0.11 h1:DFsHa+sc5fKwhDR50I8uBM99RTDTEW68ESyr5ALRDwE= github.com/multiversx/mx-chain-logger-go v1.0.11/go.mod h1:1srDkP0DQucWQ+rYfaq0BX2qLnULsUdRPADpYUTM6dA= github.com/multiversx/mx-chain-p2p-go v1.0.10 h1:CYCuI0SP8Pt9K0TcJjUyxK7ByvWi2FXNUihy0iCEVIA=