Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Jul 3, 2023
1 parent 1d8c49b commit f9c0c7c
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 181 deletions.
8 changes: 8 additions & 0 deletions data/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ type Event struct {
Order int `json:"order"`
}

// StatusInfo holds the fields for the transaction status
type StatusInfo struct {
CompletedEvent bool `json:"completedEvent"`
ErrorEvent bool `json:"errorEvent"`
Status string `json:"status"`
}

// PreparedLogsResults is the DTO that holds all the results after processing
type PreparedLogsResults struct {
Tokens TokensHandler
TokensSupply TokensHandler
ScDeploys map[string]*ScDeployInfo
Delegators map[string]*Delegator
TxHashStatusInfo map[string]*StatusInfo
TokensInfo []*TokenInfo
NFTsDataUpdates []*NFTDataUpdate
TokenRolesAndProperties *tokeninfo.TokenRolesAndProperties
Expand Down
3 changes: 2 additions & 1 deletion data/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Transaction struct {
Version uint32 `json:"version,omitempty"`
GuardianAddress string `json:"guardian,omitempty"`
GuardianSignature string `json:"guardianSignature,omitempty"`
ErrorEvent bool `json:"errorEvent,omitempty"`
CompletedEvent bool `json:"completedEvent,omitempty"`
SmartContractResults []*ScResult `json:"-"`
Hash string `json:"-"`
BlockHash string `json:"-"`
Expand All @@ -67,7 +69,6 @@ type PreparedResults struct {
Transactions []*Transaction
ScResults []*ScResult
Receipts []*Receipt
TxHashStatus map[string]string
TxHashFee map[string]*FeeData
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/elastic/go-elasticsearch/v7 v7.12.0
github.com/multiversx/mx-chain-communication-go v1.0.3
github.com/multiversx/mx-chain-core-go v1.2.7
github.com/multiversx/mx-chain-core-go v1.2.8-0.20230630085500-96880f1bce4c
github.com/multiversx/mx-chain-logger-go v1.0.11
github.com/multiversx/mx-chain-vm-common-go v1.4.1
github.com/stretchr/testify v1.8.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ github.com/multiversx/mx-chain-communication-go v1.0.3/go.mod h1:7oTI77XfWmRWwVE
github.com/multiversx/mx-chain-core-go v1.1.30/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.2.1/go.mod h1:8gGEQv6BWuuJwhd25qqhCOZbBSv9mk+hLeKvinSaSMk=
github.com/multiversx/mx-chain-core-go v1.2.5/go.mod h1:jzYFSiYBuO0dGpGFXnZWSwcwcKP7Flyn/X41y4zIQrQ=
github.com/multiversx/mx-chain-core-go v1.2.7 h1:cP4h/B4Arnz0K6ieRd1OAf+z5gBejiMA6lzcWewR7kk=
github.com/multiversx/mx-chain-core-go v1.2.7/go.mod h1:jzYFSiYBuO0dGpGFXnZWSwcwcKP7Flyn/X41y4zIQrQ=
github.com/multiversx/mx-chain-core-go v1.2.8-0.20230630085500-96880f1bce4c h1:UfPvZLUYZUsI+ZtxHFroyr3q+CDPgkT0n3eqKNdrJuM=
github.com/multiversx/mx-chain-core-go v1.2.8-0.20230630085500-96880f1bce4c/go.mod h1:jzYFSiYBuO0dGpGFXnZWSwcwcKP7Flyn/X41y4zIQrQ=
github.com/multiversx/mx-chain-crypto-go v1.2.6/go.mod h1:rOj0Rr19HTOYt9YTeym7RKxlHt91NXln3LVKjHKVmA0=
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=
Expand Down
2 changes: 1 addition & 1 deletion mock/dbTransactionsHandlerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (tps *DBTransactionProcessorStub) SerializeReceipts(recs []*data.Receipt, b
}

// SerializeTransactions -
func (tps *DBTransactionProcessorStub) SerializeTransactions(_ []*data.Transaction, _ map[string]string, _ uint32, _ *data.BufferSlice, _ string) error {
func (tps *DBTransactionProcessorStub) SerializeTransactions(_ []*data.Transaction, _ map[string]*data.StatusInfo, _ uint32, _ *data.BufferSlice, _ string) error {
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions process/elasticproc/elasticProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ func (ei *elasticProcessor) SaveTransactions(obh *outport.OutportBlockWithHeader
logsData := ei.logsAndEventsProc.ExtractDataFromLogs(obh.TransactionPool.Logs, preparedResults, headerTimestamp, obh.Header.GetShardID(), obh.NumberOfShards)

buffers := data.NewBufferSlice(ei.bulkRequestMaxSize)
err := ei.indexTransactions(preparedResults.Transactions, preparedResults.TxHashStatus, obh.Header, buffers)
err := ei.indexTransactions(preparedResults.Transactions, logsData.TxHashStatusInfo, obh.Header, buffers)
if err != nil {
return err
}

err = ei.prepareAndIndexOperations(preparedResults.Transactions, preparedResults.TxHashStatus, obh.Header, preparedResults.ScResults, buffers, ei.isImportDB())
err = ei.prepareAndIndexOperations(preparedResults.Transactions, logsData.TxHashStatusInfo, obh.Header, preparedResults.ScResults, buffers, ei.isImportDB())
if err != nil {
return err
}
Expand Down Expand Up @@ -539,17 +539,17 @@ func (ei *elasticProcessor) indexScDeploys(deployData map[string]*data.ScDeployI
return ei.logsAndEventsProc.SerializeSCDeploys(deployData, buffSlice, elasticIndexer.SCDeploysIndex)
}

func (ei *elasticProcessor) indexTransactions(txs []*data.Transaction, txHashStatus map[string]string, header coreData.HeaderHandler, bytesBuff *data.BufferSlice) error {
func (ei *elasticProcessor) indexTransactions(txs []*data.Transaction, txHashStatusInfo map[string]*data.StatusInfo, header coreData.HeaderHandler, bytesBuff *data.BufferSlice) error {
if !ei.isIndexEnabled(elasticIndexer.TransactionsIndex) {
return nil
}

return ei.transactionsProc.SerializeTransactions(txs, txHashStatus, header.GetShardID(), bytesBuff, elasticIndexer.TransactionsIndex)
return ei.transactionsProc.SerializeTransactions(txs, txHashStatusInfo, header.GetShardID(), bytesBuff, elasticIndexer.TransactionsIndex)
}

func (ei *elasticProcessor) prepareAndIndexOperations(
txs []*data.Transaction,
txHashStatus map[string]string,
txHashStatusInfo map[string]*data.StatusInfo,
header coreData.HeaderHandler,
scrs []*data.ScResult,
buffSlice *data.BufferSlice,
Expand All @@ -561,7 +561,7 @@ func (ei *elasticProcessor) prepareAndIndexOperations(

processedTxs, processedSCRs := ei.operationsProc.ProcessTransactionsAndSCRs(txs, scrs, isImportDB, header.GetShardID())

err := ei.transactionsProc.SerializeTransactions(processedTxs, txHashStatus, header.GetShardID(), buffSlice, elasticIndexer.OperationsIndex)
err := ei.transactionsProc.SerializeTransactions(processedTxs, txHashStatusInfo, header.GetShardID(), buffSlice, elasticIndexer.OperationsIndex)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion process/elasticproc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type DBTransactionsHandler interface {
GetHexEncodedHashesForRemove(header coreData.HeaderHandler, body *block.Body) ([]string, []string)

SerializeReceipts(receipts []*data.Receipt, buffSlice *data.BufferSlice, index string) error
SerializeTransactions(transactions []*data.Transaction, txHashStatus map[string]string, selfShardID uint32, buffSlice *data.BufferSlice, index string) error
SerializeTransactions(transactions []*data.Transaction, txHashStatusInfo map[string]*data.StatusInfo, selfShardID uint32, buffSlice *data.BufferSlice, index string) error
SerializeTransactionsFeeData(txHashRefund map[string]*data.FeeData, buffSlice *data.BufferSlice, index string) error
SerializeScResults(scResults []*data.ScResult, buffSlice *data.BufferSlice, index string) error
}
Expand Down
54 changes: 42 additions & 12 deletions process/elasticproc/logsevents/informativeLogsProcessor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package logsevents

import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
)

const (
writeLogOperation = "writeLog"
signalErrorOperation = "signalError"
"github.com/multiversx/mx-chain-es-indexer-go/data"
)

type informativeLogsProcessor struct {
Expand All @@ -16,8 +13,10 @@ type informativeLogsProcessor struct {
func newInformativeLogsProcessor() *informativeLogsProcessor {
return &informativeLogsProcessor{
operations: map[string]struct{}{
writeLogOperation: {},
signalErrorOperation: {},
core.WriteLogIdentifier: {},
core.SignalErrorOperation: {},
core.CompletedTxEventIdentifier: {},
core.InternalVMErrorsOperation: {},
},
}
}
Expand All @@ -31,23 +30,54 @@ func (ilp *informativeLogsProcessor) processEvent(args *argsProcessEvent) argOut

tx, ok := args.txs[args.txHashHexEncoded]
if !ok {
return argOutputProcessEvent{
processed: true,
}
return processEventNoTx(args)
}

switch identifier {
case writeLogOperation:
case core.CompletedTxEventIdentifier:
{
tx.CompletedEvent = true
}
case core.WriteLogIdentifier:
{
tx.Status = transaction.TxStatusSuccess.String()
}
case signalErrorOperation:
case core.SignalErrorOperation, core.InternalVMErrorsOperation:
{
tx.Status = transaction.TxStatusFail.String()
tx.ErrorEvent = true
}
}

return argOutputProcessEvent{
processed: true,
}
}

func processEventNoTx(args *argsProcessEvent) argOutputProcessEvent {
scr, ok := args.scrs[args.txHashHexEncoded]
if !ok {
return argOutputProcessEvent{
processed: true,
}
}

record := &data.StatusInfo{}
switch string(args.event.GetIdentifier()) {
case core.CompletedTxEventIdentifier:
{
record.CompletedEvent = true
}
case core.SignalErrorOperation, core.InternalVMErrorsOperation:
{
record.Status = transaction.TxStatusFail.String()
record.ErrorEvent = true
}
}

args.txHashStatusInfoProc.addRecord(scr.OriginalTxHash, record)

return argOutputProcessEvent{
processed: true,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logsevents
import (
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestInformativeLogsProcessorWriteLog(t *testing.T) {

event := &transaction.Event{
Address: []byte("addr"),
Identifier: []byte(writeLogOperation),
Identifier: []byte(core.WriteLogIdentifier),
}
args := &argsProcessEvent{
timestamp: 1234,
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestInformativeLogsProcessorSignalError(t *testing.T) {

event := &transaction.Event{
Address: []byte("addr"),
Identifier: []byte(signalErrorOperation),
Identifier: []byte(core.SignalErrorOperation),
}
args := &argsProcessEvent{
timestamp: 1234,
Expand Down
7 changes: 7 additions & 0 deletions process/elasticproc/logsevents/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ type argsProcessEvent struct {
txHashHexEncoded string
scDeploys map[string]*data.ScDeployInfo
txs map[string]*data.Transaction
scrs map[string]*data.ScResult
event coreData.EventHandler
tokens data.TokensHandler
tokensSupply data.TokensHandler
tokenRolesAndProperties *tokeninfo.TokenRolesAndProperties
txHashStatusInfoProc txHashStatusInfoHandler
timestamp uint64
logAddress []byte
selfShardID uint32
Expand All @@ -30,3 +32,8 @@ type argOutputProcessEvent struct {
type eventsProcessor interface {
processEvent(args *argsProcessEvent) argOutputProcessEvent
}

type txHashStatusInfoHandler interface {
addRecord(hash string, statusInfo *data.StatusInfo)
getAllRecords() map[string]*data.StatusInfo
}
1 change: 1 addition & 0 deletions process/elasticproc/logsevents/logsAndEventsProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (lep *logsAndEventsProcessor) ExtractDataFromLogs(
Delegators: lep.logsData.delegators,
NFTsDataUpdates: lep.logsData.nftsDataUpdates,
TokenRolesAndProperties: lep.logsData.tokenRolesAndProperties,
TxHashStatusInfo: lep.logsData.txHashStatusInfoProc.getAllRecords(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions process/elasticproc/logsevents/logsData.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type logsData struct {
timestamp uint64
txHashStatusInfoProc txHashStatusInfoHandler
tokens data.TokensHandler
tokensSupply data.TokensHandler
txsMap map[string]*data.Transaction
Expand Down Expand Up @@ -36,6 +37,7 @@ func newLogsData(
ld.delegators = make(map[string]*data.Delegator)
ld.nftsDataUpdates = make([]*data.NFTDataUpdate, 0)
ld.tokenRolesAndProperties = tokeninfo.NewTokenRolesAndProperties()
ld.txHashStatusInfoProc = newTxHashStatusInfo()

return ld
}
37 changes: 37 additions & 0 deletions process/elasticproc/logsevents/statusInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package logsevents

import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-es-indexer-go/data"
)

type txHashStatusInfoProc struct {
hashStatusInfo map[string]*data.StatusInfo
}

// NewTxHashStatusInfo will create a new instance of TxHashStatusInfo
func newTxHashStatusInfo() *txHashStatusInfoProc {
return &txHashStatusInfoProc{
hashStatusInfo: make(map[string]*data.StatusInfo),
}
}

// AddRecord will add a new record for the given hash
func (ths *txHashStatusInfoProc) addRecord(hash string, statusInfo *data.StatusInfo) {
statusInfoFromMap, found := ths.hashStatusInfo[hash]
if !found {
ths.hashStatusInfo[hash] = statusInfo
}

if statusInfoFromMap.Status != transaction.TxStatusFail.String() {
statusInfoFromMap.Status = statusInfo.Status
}

statusInfoFromMap.ErrorEvent = statusInfoFromMap.ErrorEvent || statusInfo.ErrorEvent
statusInfoFromMap.CompletedEvent = statusInfoFromMap.CompletedEvent || statusInfo.CompletedEvent
}

// GetAllRecords will return all the records
func (ths *txHashStatusInfoProc) getAllRecords() map[string]*data.StatusInfo {
return ths.hashStatusInfo
}
1 change: 1 addition & 0 deletions process/elasticproc/logsevents/statusInfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package logsevents
Loading

0 comments on commit f9c0c7c

Please sign in to comment.