Skip to content

Commit

Permalink
Merge branch 'development' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Apr 14, 2020
2 parents 9f43a7c + fcb0e91 commit 8c789e6
Show file tree
Hide file tree
Showing 416 changed files with 11,193 additions and 7,086 deletions.
47 changes: 17 additions & 30 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mock

import (
"errors"
"encoding/hex"
"math/big"

"github.com/ElrondNetwork/elrond-go/core/statistics"
Expand All @@ -15,7 +15,6 @@ import (

// Facade is the mock implementation of a node router handler
type Facade struct {
Running bool
ShouldErrorStart bool
ShouldErrorStop bool
TpsBenchmarkHandler func() *statistics.TpsBenchmark
Expand All @@ -31,6 +30,7 @@ type Facade struct {
StatusMetricsHandler func() external.StatusMetricsHandler
ValidatorStatisticsHandler func() (map[string]*state.ValidatorApiResponse, error)
ComputeTransactionGasLimitHandler func(tx *transaction.Transaction) (uint64, error)
NodeConfigCalled func() map[string]interface{}
}

// RestApiInterface -
Expand All @@ -48,19 +48,6 @@ func (f *Facade) PprofEnabled() bool {
return false
}

// IsNodeRunning is the mock implementation of a handler's IsNodeRunning method
func (f *Facade) IsNodeRunning() bool {
return f.Running
}

// StartNode is the mock implementation of a handler's StartNode method
func (f *Facade) StartNode() error {
if f.ShouldErrorStart {
return errors.New("error")
}
return nil
}

// TpsBenchmark is the mock implementation for retreiving the TpsBenchmark
func (f *Facade) TpsBenchmark() *statistics.TpsBenchmark {
if f.TpsBenchmarkHandler != nil {
Expand All @@ -69,15 +56,6 @@ func (f *Facade) TpsBenchmark() *statistics.TpsBenchmark {
return nil
}

// StopNode is the mock implementation of a handler's StopNode method
func (f *Facade) StopNode() error {
if f.ShouldErrorStop {
return errors.New("error")
}
f.Running = false
return nil
}

// GetHeartbeats returns the slice of heartbeat info
func (f *Facade) GetHeartbeats() ([]heartbeat.PubKeyHeartbeat, error) {
return f.GetHeartbeatsHandler()
Expand All @@ -93,12 +71,6 @@ func (f *Facade) GetAccount(address string) (state.UserAccountHandler, error) {
return f.GetAccountHandler(address)
}

// GenerateTransaction is the mock implementation of a handler's GenerateTransaction method
func (f *Facade) GenerateTransaction(sender string, receiver string, value *big.Int,
code string) (*transaction.Transaction, error) {
return f.GenerateTransactionHandler(sender, receiver, value, code)
}

// CreateTransaction is mock implementation of a handler's CreateTransaction method
func (f *Facade) CreateTransaction(
nonce uint64,
Expand Down Expand Up @@ -148,6 +120,21 @@ func (f *Facade) ComputeTransactionGasLimit(tx *transaction.Transaction) (uint64
return f.ComputeTransactionGasLimitHandler(tx)
}

// NodeConfig -
func (f *Facade) NodeConfig() map[string]interface{} {
return f.NodeConfigCalled()
}

// EncodeAddressPubkey -
func (f *Facade) EncodeAddressPubkey(pk []byte) (string, error) {
return hex.EncodeToString(pk), nil
}

// DecodeAddressPubkey -
func (f *Facade) DecodeAddressPubkey(pk string) ([]byte, error) {
return hex.DecodeString(pk)
}

// IsInterfaceNil returns true if there is no value under the interface
func (f *Facade) IsInterfaceNil() bool {
return f == nil
Expand Down
2 changes: 0 additions & 2 deletions api/node/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

// FacadeHandler interface defines methods that can be used from `elrondFacade` context variable
type FacadeHandler interface {
IsNodeRunning() bool
StartNode() error
GetHeartbeats() ([]heartbeat.PubKeyHeartbeat, error)
TpsBenchmark() *statistics.TpsBenchmark
StatusMetrics() external.StatusMetricsHandler
Expand Down
4 changes: 1 addition & 3 deletions api/node/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ func TestHeartbeatStatus_FailsWithoutFacade(t *testing.T) {
func TestHeartbeatstatus_FailsWithWrongFacadeTypeConversion(t *testing.T) {
t.Parallel()

facade := mock.Facade{}
facade.Running = true
ws := startNodeServerWrongFacade()
req, _ := http.NewRequest("GET", "/node/heartbeatstatus", nil)
resp := httptest.NewRecorder()
Expand Down Expand Up @@ -124,7 +122,7 @@ func TestHeartbeatstatus(t *testing.T) {

hbStatus := []heartbeat.PubKeyHeartbeat{
{
HexPublicKey: "pk1",
PublicKey: "pk1",
TimeStamp: time.Now(),
MaxInactiveTime: heartbeat.Duration{Duration: 0},
IsActive: true,
Expand Down
29 changes: 23 additions & 6 deletions api/transaction/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (

// TxService interface defines methods that can be used from `elrondFacade` context variable
type TxService interface {
CreateTransaction(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64,
CreateTransaction(nonce uint64, value string, receiver string, sender string, gasPrice uint64,
gasLimit uint64, data []byte, signatureHex string) (*transaction.Transaction, []byte, error)
ValidateTransaction(tx *transaction.Transaction) error
SendBulkTransactions([]*transaction.Transaction) (uint64, error)
GetTransaction(hash string) (*transaction.Transaction, error)
ComputeTransactionGasLimit(tx *transaction.Transaction) (uint64, error)
EncodeAddressPubkey(pk []byte) (string, error)
IsInterfaceNil() bool
}

Expand Down Expand Up @@ -188,21 +189,37 @@ func GetTransaction(c *gin.Context) {
return
}

c.JSON(http.StatusOK, gin.H{"transaction": txResponseFromTransaction(tx)})
response, err := txResponseFromTransaction(ef, tx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, gin.H{"transaction": response})
}

func txResponseFromTransaction(tx *transaction.Transaction) TxResponse {
func txResponseFromTransaction(ef TxService, tx *transaction.Transaction) (TxResponse, error) {
response := TxResponse{}
sender, err := ef.EncodeAddressPubkey(tx.SndAddr)
if err != nil {
return response, fmt.Errorf("%w for sender adddress", err)
}

receiver, err := ef.EncodeAddressPubkey(tx.RcvAddr)
if err != nil {
return response, fmt.Errorf("%w for sender adddress", err)
}

response.Nonce = tx.Nonce
response.Sender = hex.EncodeToString(tx.SndAddr)
response.Receiver = hex.EncodeToString(tx.RcvAddr)
response.Sender = sender
response.Receiver = receiver
response.Data = tx.Data
response.Signature = hex.EncodeToString(tx.Signature)
response.Value = tx.Value.String()
response.GasLimit = tx.GasLimit
response.GasPrice = tx.GasPrice

return response
return response, nil
}

// ComputeTransactionGasLimit returns how many gas units a transaction wil consume
Expand Down
9 changes: 5 additions & 4 deletions api/vmValues/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// FacadeHandler interface defines methods that can be used from `elrondFacade` context variable
type FacadeHandler interface {
ExecuteSCQuery(*process.SCQuery) (*vmcommon.VMOutput, error)
DecodeAddressPubkey(pk string) ([]byte, error)
IsInterfaceNil() bool
}

Expand Down Expand Up @@ -87,7 +88,7 @@ func doExecuteQuery(context *gin.Context) (*vmcommon.VMOutput, error) {
return nil, errors.ErrInvalidJSONRequest
}

command, err := createSCQuery(&request)
command, err := createSCQuery(facade, &request)
if err != nil {
return nil, err
}
Expand All @@ -100,10 +101,10 @@ func doExecuteQuery(context *gin.Context) (*vmcommon.VMOutput, error) {
return vmOutput, nil
}

func createSCQuery(request *VMValueRequest) (*process.SCQuery, error) {
decodedAddress, err := hex.DecodeString(request.ScAddress)
func createSCQuery(fh FacadeHandler, request *VMValueRequest) (*process.SCQuery, error) {
decodedAddress, err := fh.DecodeAddressPubkey(request.ScAddress)
if err != nil {
return nil, fmt.Errorf("'%s' is not a valid hex string: %s", request.ScAddress, err.Error())
return nil, fmt.Errorf("'%s' is not a valid address: %s", request.ScAddress, err.Error())
}

arguments := make([][]byte, len(request.Args))
Expand Down
4 changes: 2 additions & 2 deletions api/vmValues/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestCreateSCQuery_ArgumentIsNotHexShouldErr(t *testing.T) {
Args: []string{"bad arg"},
}

_, err := createSCQuery(&request)
_, err := createSCQuery(&mock.Facade{}, &request)
require.NotNil(t, err)
require.Contains(t, err.Error(), "'bad arg' is not a valid hex string")
}
Expand All @@ -180,7 +180,7 @@ func TestAllRoutes_FacadeErrorsShouldErr(t *testing.T) {
func TestAllRoutes_WhenBadAddressShouldErr(t *testing.T) {
t.Parallel()

errExpected := errors.New("not a valid hex string")
errExpected := errors.New("not a valid address")
facade := mock.Facade{
ExecuteSCQueryHandler: func(query *process.SCQuery) (vmOutput *vmcommon.VMOutput, e error) {
return &vmcommon.VMOutput{}, nil
Expand Down
Loading

0 comments on commit 8c789e6

Please sign in to comment.