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 Mar 25, 2020
2 parents 5524514 + 6bdec31 commit c1ce75e
Show file tree
Hide file tree
Showing 197 changed files with 4,542 additions and 12,196 deletions.
12 changes: 6 additions & 6 deletions api/address/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// FacadeHandler interface defines methods that can be used from `elrondFacade` context variable
type FacadeHandler interface {
GetBalance(address string) (*big.Int, error)
GetAccount(address string) (*state.Account, error)
GetAccount(address string) (state.UserAccountHandler, error)
IsInterfaceNil() bool
}

Expand Down Expand Up @@ -74,13 +74,13 @@ func GetBalance(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"balance": balance.String()})
}

func accountResponseFromBaseAccount(address string, account *state.Account) accountResponse {
func accountResponseFromBaseAccount(address string, account state.UserAccountHandler) accountResponse {
return accountResponse{
Address: address,
Nonce: account.Nonce,
Balance: account.Balance.String(),
Nonce: account.GetNonce(),
Balance: account.GetBalance().String(),
Code: hex.EncodeToString(account.GetCode()),
CodeHash: account.CodeHash,
RootHash: account.RootHash,
CodeHash: account.GetCodeHash(),
RootHash: account.GetRootHash(),
}
}
16 changes: 8 additions & 8 deletions api/address/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/mock"
"github.com/ElrondNetwork/elrond-go/data/state"
mock2 "github.com/ElrondNetwork/elrond-go/node/mock"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -198,7 +199,7 @@ func TestGetAccount_FailWhenFacadeGetAccountFails(t *testing.T) {
t.Parallel()
returnedError := "i am an error"
facade := mock.Facade{
GetAccountHandler: func(address string) (*state.Account, error) {
GetAccountHandler: func(address string) (state.UserAccountHandler, error) {
return nil, errors.New(returnedError)
},
}
Expand All @@ -219,13 +220,12 @@ func TestGetAccount_FailWhenFacadeGetAccountFails(t *testing.T) {
func TestGetAccount_ReturnsSuccessfully(t *testing.T) {
t.Parallel()
facade := mock.Facade{
GetAccountHandler: func(address string) (*state.Account, error) {
return &state.Account{
AccountData: state.AccountData{
Nonce: 1,
Balance: big.NewInt(100),
},
}, nil
GetAccountHandler: func(address string) (state.UserAccountHandler, error) {
acc, _ := state.NewUserAccount(&mock2.AddressMock{})
_ = acc.AddToBalance(big.NewInt(100))
acc.IncreaseNonce(1)

return acc, nil
},
}
ws := startNodeServer(&facade)
Expand Down
31 changes: 19 additions & 12 deletions api/logs/logSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
const disconnectMessage = -1

type logSender struct {
marshalizer marshal.Marshalizer
conn wsConn
writer *logWriter
log logger.Logger
lastLogPattern string
marshalizer marshal.Marshalizer
conn wsConn
writer *logWriter
log logger.Logger
lastProfile logger.Profile
}

// NewLogSender returns a new component that is able to communicate with the log viewer application.
Expand Down Expand Up @@ -67,17 +67,18 @@ func (ls *logSender) registerLogWriter() error {
// will start sending logs information and in the same time monitor the current connection.
// When the connection ends it will revert the previous log pattern.
func (ls *logSender) StartSendingBlocking() {
ls.lastLogPattern = logger.GetLogLevelPattern()
ls.lastProfile = logger.GetCurrentProfile()

defer func() {
_ = ls.conn.Close()
_ = ls.writer.Close()
_ = logger.RemoveLogObserver(ls.writer)
_ = logger.SetLogLevel(ls.lastLogPattern)
ls.log.Info("reverted log pattern", "pattern", ls.lastLogPattern)
_ = ls.lastProfile.Apply()

ls.log.Info("reverted log profile", "profile", ls.lastProfile.String())
}()

err := ls.waitForPatternMessage()
err := ls.waitForProfile()
if err != nil {
ls.log.Error(err.Error())
return
Expand All @@ -87,14 +88,20 @@ func (ls *logSender) StartSendingBlocking() {
ls.doSendContinuously()
}

func (ls *logSender) waitForPatternMessage() error {
func (ls *logSender) waitForProfile() error {
_, message, err := ls.conn.ReadMessage()
if err != nil {
return err
}

ls.log.Info("websocket log pattern received", "pattern", string(message))
err = logger.SetLogLevel(string(message))
profile, err := logger.UnmarshalProfile(message)
if err != nil {
return err
}

ls.log.Info("websocket log profile received", "profile", profile.String())

err = profile.Apply()
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion api/logs/logSender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func createMockLogSender() (*logs.LogSender, *mock.WsConnStub, io.Writer) {
return nil
})
conn.SetReadMessageHandler(func() (messageType int, p []byte, err error) {
return websocket.TextMessage, []byte("*:INFO"), nil
profile := logger.Profile{LogLevelPatterns: "*:INFO"}
profileJson, _ := profile.Marshal()
return websocket.TextMessage, profileJson, nil
})

ls, _ := logs.NewLogSender(
Expand Down
4 changes: 2 additions & 2 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Facade struct {
TpsBenchmarkHandler func() *statistics.TpsBenchmark
GetHeartbeatsHandler func() ([]heartbeat.PubKeyHeartbeat, error)
BalanceHandler func(string) (*big.Int, error)
GetAccountHandler func(address string) (*state.Account, error)
GetAccountHandler func(address string) (state.UserAccountHandler, error)
GenerateTransactionHandler func(sender string, receiver string, value *big.Int, code string) (*transaction.Transaction, error)
GetTransactionHandler func(hash string) (*transaction.Transaction, error)
CreateTransactionHandler func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64, gasLimit uint64, data []byte, signatureHex string) (*transaction.Transaction, []byte, error)
Expand Down Expand Up @@ -89,7 +89,7 @@ func (f *Facade) GetBalance(address string) (*big.Int, error) {
}

// GetAccount is the mock implementation of a handler's GetAccount method
func (f *Facade) GetAccount(address string) (*state.Account, error) {
func (f *Facade) GetAccount(address string) (state.UserAccountHandler, error) {
return f.GetAccountHandler(address)
}

Expand Down
50 changes: 43 additions & 7 deletions cmd/logviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type config struct {
logLevelPatterns string
logFile bool
useWss bool
withCorrelation bool
withLoggerName bool
}

var (
Expand Down Expand Up @@ -79,6 +81,20 @@ VERSION:
Destination: &argsConfig.useWss,
}

// withCorrelation is used when the user wants to include the log correlation elements in logs
withCorrelation = cli.BoolFlag{
Name: "correlation",
Usage: "Will include log correlation elements",
Destination: &argsConfig.withCorrelation,
}

// withLoggerName is used when the user wants to include the logger name in logs
withLoggerName = cli.BoolFlag{
Name: "logger-name",
Usage: "Will include logger name",
Destination: &argsConfig.withLoggerName,
}

// workingDirectory defines a flag for the path for the working directory.
workingDirectory = cli.StringFlag{
Name: "working-directory",
Expand Down Expand Up @@ -124,6 +140,8 @@ func initCliFlags() {
logFile,
workingDirectory,
useWss,
withCorrelation,
withLoggerName,
}
cliApp.Authors = []cli.Author{
{
Expand Down Expand Up @@ -161,9 +179,17 @@ func startLogViewer(ctx *cli.Context) error {
}()
}

profile := &logger.Profile{
LogLevelPatterns: argsConfig.logLevelPatterns,
WithCorrelation: argsConfig.withCorrelation,
WithLoggerName: argsConfig.withLoggerName,
}

profile.Apply()

go func() {
for {
webSocket, err = openWebSocket(argsConfig.address, argsConfig.logLevelPatterns)
webSocket, err = openWebSocket(argsConfig.address, profile)
if err != nil {
log.Error(fmt.Sprintf("logviewer websocket error, retrying in %v...", retryDuration), "error", err.Error())
time.Sleep(retryDuration)
Expand Down Expand Up @@ -205,22 +231,30 @@ func prepareLogFile() error {
return logger.AddLogObserver(logsFile, &logger.PlainFormatter{})
}

func openWebSocket(address string, logLevelPatterns string) (*websocket.Conn, error) {
func openWebSocket(address string, profile *logger.Profile) (*websocket.Conn, error) {
scheme := ws

if argsConfig.useWss {
scheme = wss
}

u := url.URL{
Scheme: scheme,
Host: address,
Path: wsLogPath,
}

conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}

err = conn.WriteMessage(websocket.TextMessage, []byte(logLevelPatterns))
profileMessage, err := profile.Marshal()
if err != nil {
return nil, err
}

err = conn.WriteMessage(websocket.TextMessage, profileMessage)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -275,10 +309,12 @@ func outputMessage(message []byte) {
}

recoveredLogLine := &logger.LogLine{
Message: logLine.Message,
LogLevel: logger.LogLevel(logLine.LogLevel),
Args: make([]interface{}, len(logLine.Args)),
Timestamp: time.Unix(0, logLine.Timestamp),
LoggerName: logLine.LoggerName,
Correlation: logLine.Correlation,
Message: logLine.Message,
LogLevel: logger.LogLevel(logLine.LogLevel),
Args: make([]interface{}, len(logLine.Args)),
Timestamp: time.Unix(0, logLine.Timestamp),
}
for i, str := range logLine.Args {
recoveredLogLine.Args[i] = str
Expand Down
16 changes: 12 additions & 4 deletions cmd/logviewer/testing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func handleLogRequest(w http.ResponseWriter, r *http.Request) {
_ = conn.Close()
}()

err = waitForPatternMessage(conn)
err = waitForProfile(conn)
if err != nil {
log.Error(err.Error())
return
Expand All @@ -141,15 +141,21 @@ func handleLogRequest(w http.ResponseWriter, r *http.Request) {
sendContinouslyWithMonitor(conn)
}

func waitForPatternMessage(conn *websocket.Conn) error {
func waitForProfile(conn *websocket.Conn) error {
_, message, err := conn.ReadMessage()
if err != nil {
return err
}

log.Info("pattern received", "pattern", string(message))
profile, err := logger.UnmarshalProfile(message)
if err != nil {
return err
}

log.Info("websocket log profile received", "profile", profile.String())

mutLogLevelPattern.Lock()
logLevels, patterns, err = logger.ParseLogLevelAndMatchingString(string(message))
logLevels, patterns, err = logger.ParseLogLevelAndMatchingString(profile.LogLevelPatterns)
mutLogLevelPattern.Unlock()
if err != nil {
return err
Expand Down Expand Up @@ -247,6 +253,8 @@ func generateLogLine(idxCrtLogLevel *int, count *int) logger.LogLineHandler {
}

logLine := &logger.LogLineWrapper{}
logLine.LoggerName = "websocket test"
logLine.Correlation = logger.LogCorrelationMessage{Shard: "foobar", Epoch: 42}
logLine.Message = "websocket test message"
logLine.LogLevel = int32(logLevel)
logLine.Args = []string{"count", fmt.Sprintf("%v", *count)}
Expand Down
21 changes: 5 additions & 16 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,7 @@ func StateComponentsFactory(args *stateComponentsFactoryArgs) (*State, error) {
return nil, errors.New("could not create bls address converter: " + err.Error())
}

accountFactory, err := factoryState.NewAccountFactoryCreator(factoryState.UserAccount)
if err != nil {
return nil, errors.New("could not create account factory: " + err.Error())
}

accountFactory := factoryState.NewAccountCreator()
merkleTrie := args.core.TriesContainer.Get([]byte(factory.UserAccountTrie))
accountsAdapter, err := state.NewAccountsDB(merkleTrie, args.core.Hasher, args.core.InternalMarshalizer, accountFactory)
if err != nil {
Expand All @@ -335,11 +331,7 @@ func StateComponentsFactory(args *stateComponentsFactoryArgs) (*State, error) {
return nil, errors.New("initial balances could not be processed " + err.Error())
}

accountFactory, err = factoryState.NewAccountFactoryCreator(factoryState.ValidatorAccount)
if err != nil {
return nil, errors.New("could not create peer account factory: " + err.Error())
}

accountFactory = factoryState.NewPeerAccountCreator()
merkleTrie = args.core.TriesContainer.Get([]byte(factory.PeerAccountTrie))
peerAdapter, err := state.NewPeerAccountsDB(merkleTrie, args.core.Hasher, args.core.InternalMarshalizer, accountFactory)
if err != nil {
Expand Down Expand Up @@ -1608,13 +1600,8 @@ func createInMemoryShardCoordinatorAndAccount(
return nil, nil, err
}

accountFactory, err := factoryState.NewAccountFactoryCreator(factoryState.UserAccount)
if err != nil {
return nil, nil, err
}

accounts, err := generateInMemoryAccountsAdapter(
accountFactory,
factoryState.NewAccountCreator(),
coreComponents.Hasher,
coreComponents.InternalMarshalizer,
)
Expand Down Expand Up @@ -2097,6 +2084,8 @@ func newMetaBlockProcessor(
}

transactionProcessor, err := transaction.NewMetaTxProcessor(
core.Hasher,
core.InternalMarshalizer,
stateComponents.AccountsAdapter,
stateComponents.AddressConverter,
shardCoordinator,
Expand Down
Loading

0 comments on commit c1ce75e

Please sign in to comment.