Skip to content

Commit

Permalink
Merge pull request #113 from ElrondNetwork/fix-api
Browse files Browse the repository at this point in the history
Fix api
  • Loading branch information
miiu96 authored Dec 9, 2020
2 parents be87752 + 6852fc3 commit 271ab18
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
5 changes: 5 additions & 0 deletions process/accountProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package process

import (
"errors"
"fmt"
"net/http"

"github.com/ElrondNetwork/elrond-go/core"
Expand Down Expand Up @@ -102,6 +103,10 @@ func (ap *AccountProcessor) GetValueForKey(address string, key string) (string,

// GetTransactions resolves the request and returns a slice of transaction for the specific address
func (ap *AccountProcessor) GetTransactions(address string) ([]data.DatabaseTransaction, error) {
if _, err := ap.pubKeyConverter.Decode(address); err != nil {
return nil, fmt.Errorf("%w, %v", ErrInvalidAddress, err)
}

return ap.connector.GetTransactionsByAddress(address)
}

Expand Down
25 changes: 25 additions & 0 deletions process/accountProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"testing"

"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core/pubkeyConverter"
"github.com/ElrondNetwork/elrond-go/data/state/factory"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-proxy-go/data"
"github.com/ElrondNetwork/elrond-proxy-go/process"
Expand Down Expand Up @@ -281,3 +283,26 @@ func TestAccountProcessor_GetShardIDForAddressShouldError(t *testing.T) {
assert.Equal(t, uint32(0), shardID)
assert.Equal(t, expectedError, err)
}

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

converter, _ := factory.NewPubkeyConverter(config.PubkeyConfig{
Length: 32,
Type: "bech32",
})
ap, _ := process.NewAccountProcessor(
&mock.ProcessorStub{},
converter,
&mock.ElasticSearchConnectorMock{},
)

_, err := ap.GetTransactions("invalidAddress")
assert.True(t, errors.Is(err, process.ErrInvalidAddress))

_, err = ap.GetTransactions("")
assert.True(t, errors.Is(err, process.ErrInvalidAddress))

_, err = ap.GetTransactions("erd1ycega644rvjtgtyd8hfzt6hl5ymaa8ml2nhhs5cv045cz5vxm00q022myr")
assert.Nil(t, err)
}
30 changes: 28 additions & 2 deletions process/database/elasticSearchConnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func NewElasticSearchConnector(url, username, password string) (*elasticSearchCo

// GetTransactionsByAddress gets transactions TO or FROM the specified address
func (esc *elasticSearchConnector) GetTransactionsByAddress(address string) ([]data.DatabaseTransaction, error) {
query := txsByAddrQuery(address)
decodedBody, err := esc.doSearchRequest(query, "transactions", numTopTransactions)
decodedBody, err := esc.doSearchRequestTx(address, "transactions", numTopTransactions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -151,6 +150,33 @@ func (esc *elasticSearchConnector) doSearchRequest(query object, index string, s
return decodedBody, nil
}

func (esc *elasticSearchConnector) doSearchRequestTx(address string, index string, size int) (object, error) {
query := fmt.Sprintf("sender:%s OR receiver:%s", address, address)
res, err := esc.client.Search(
esc.client.Search.WithIndex(index),
esc.client.Search.WithSize(size),
esc.client.Search.WithQuery(query),
esc.client.Search.WithSort("timestamp:desc"),
)
if err != nil {
return nil, fmt.Errorf("cannot get data from database: %w", err)
}

defer func() {
_ = res.Body.Close()
}()
if res.IsError() {
return nil, fmt.Errorf("cannot get data from database: %v", res)
}

var decodedBody map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&decodedBody); err != nil {
return nil, err
}

return decodedBody, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (esc *elasticSearchConnector) IsInterfaceNil() bool {
return esc == nil
Expand Down
26 changes: 0 additions & 26 deletions process/database/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,6 @@ func encodeQuery(query object) (bytes.Buffer, error) {
return buff, nil
}

func txsByAddrQuery(addr string) object {
return object{
"query": object{
"bool": object{
"should": []interface{}{
object{
"match": object{
"sender": addr,
},
},
object{
"match": object{
"receiver": addr,
},
},
},
},
},
"sort": object{
"timestamp": object{
"order": "desc",
},
},
}
}

func blockByNonceAndShardIDQuery(nonce uint64, shardID uint32) object {
return object{
"query": object{
Expand Down
21 changes: 21 additions & 0 deletions process/mock/elasticSearchConnectorMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mock

import "github.com/ElrondNetwork/elrond-proxy-go/data"

type ElasticSearchConnectorMock struct {
}

// GetTransactionsByAddress -
func (escm *ElasticSearchConnectorMock) GetTransactionsByAddress(_ string) ([]data.DatabaseTransaction, error) {
return nil, nil
}

// GetAtlasBlockByShardIDAndNonce -
func (escm *ElasticSearchConnectorMock) GetAtlasBlockByShardIDAndNonce(_ uint32, _ uint64) (data.AtlasBlock, error) {
return data.AtlasBlock{}, nil
}

// IsInterfaceNil -
func (escm *ElasticSearchConnectorMock) IsInterfaceNil() bool {
return escm == nil
}

0 comments on commit 271ab18

Please sign in to comment.