Skip to content

Commit

Permalink
Export to the report the info of the time taken to find the weakness
Browse files Browse the repository at this point in the history
  • Loading branch information
faustocarva committed Nov 24, 2023
1 parent 4e49534 commit 5a705bb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions data/repo/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,24 @@ func (r *transactionRepo) FindRunningAndCreatedBeforeThreshold(tx *gorm.DB, date
}
return transactions, nil
}

func (r *transactionRepo) FindTimeTakenToWeakness(tx *gorm.DB, taskId string, weaknessType common.OracleType) (uint32, error) {
var totalTimeInSeconds uint32
query := `
SELECT
strftime('%s', timestamp) - strftime('%s', (
SELECT MIN(timestamp)
FROM transactions
WHERE task_id = ?)
)
FROM transactions
WHERE detected_weaknesses like ?
ORDER BY timestamp
LIMIT 1
`

if err := tx.Raw(query, taskId, "%"+weaknessType+"%").Scan(&totalTimeInSeconds).Error; err != nil {
return 0, err
}
return totalTimeInSeconds, nil
}
11 changes: 11 additions & 0 deletions listener/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (l *reporterListener) processEvent(ctx context.Context, evt bus.TaskFinishE

heatmap := l.initHeatMap(contract.CFG)
aggregatedWeakneses := make([]string, 0)
timeToWeaknessMap := make(map[string]uint32)
var totalCoverage float64 = 0
var averageCoverage float64 = 0
var criticalInstructionsHits uint64 = 0
Expand All @@ -89,6 +90,15 @@ func (l *reporterListener) processEvent(ctx context.Context, evt bus.TaskFinishE
averageCoverage = totalCoverage / float64(len(transactions))
}

detectedWeaknesses := common.GetUniqueSlice(aggregatedWeakneses)
for _, weakness := range detectedWeaknesses {
timeToWeakness, err := l.transactionService.FindTimeTakenToWeakness(task.Id, common.OracleType(weakness))
if err != nil {
l.logger.Sugar().Errorf("an error occurred when retrieving transactions of this task: %v", err)
}
timeToWeaknessMap[weakness] = timeToWeakness
}

report := common.TaskReport{
TaskId: task.Id,
TaskStatus: task.Status,
Expand All @@ -100,6 +110,7 @@ func (l *reporterListener) processEvent(ctx context.Context, evt bus.TaskFinishE
MinDistance: distance.ComputeMinDistance(contract.DistanceMap, task.AggregatedExecutedInstructions),
MinDistanceByTime: l.computeMinDistanceByTime(contract.DistanceMap, transactions),
DetectedWeaknesses: common.GetUniqueSlice(aggregatedWeakneses),
TimeToWeakness: timeToWeaknessMap,
CriticalInstructionsHits: criticalInstructionsHits,
AverageCoverage: averageCoverage,
Instructions: l.buildInstructionsMap(contract.CFG),
Expand Down
1 change: 1 addition & 0 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type TaskReport struct {
MinDistance uint64 `json:"minDistance"`
MinDistanceByTime TimeSeriesData `json:"minDistanceByTime"`
DetectedWeaknesses []string `json:"detectedWeaknesses"`
TimeToWeakness map[string]uint32 `json:"timeToWeaknesses"`
CriticalInstructionsHits uint64 `json:"criticalInstructionsHits"`
AverageCoverage float64 `json:"averageCoverage"`
Instructions map[string]string `json:"instructions"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/interfaces/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/dogefuzz/dogefuzz/entities"
"github.com/dogefuzz/dogefuzz/pkg/common"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -41,4 +42,5 @@ type TransactionRepo interface {
FindDoneByTaskId(tx *gorm.DB, taskId string) ([]entities.Transaction, error)
FindDoneTransactionsByFunctionIdAndOrderByTimestamp(tx *gorm.DB, functionId string, limit int64) ([]entities.Transaction, error)
FindRunningAndCreatedBeforeThreshold(tx *gorm.DB, dateThreshold time.Time) ([]entities.Transaction, error)
FindTimeTakenToWeakness(tx *gorm.DB, taskId string, weaknessType common.OracleType) (uint32, error)
}
1 change: 1 addition & 0 deletions pkg/interfaces/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type TransactionService interface {
FindDoneByTaskId(taskId string) ([]*dto.TransactionDTO, error)
FindDoneTransactionsByFunctionIdAndOrderByTimestamp(functionId string, limit int64) ([]*dto.TransactionDTO, error)
FindRunningAndCreatedBeforeThreshold(dateThreshold time.Time) ([]*dto.TransactionDTO, error)
FindTimeTakenToWeakness(taskId string, weaknessType common.OracleType) (uint32, error)
}

type VandalService interface {
Expand Down
10 changes: 10 additions & 0 deletions service/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/dogefuzz/dogefuzz/data/repo"
"github.com/dogefuzz/dogefuzz/pkg/common"
"github.com/dogefuzz/dogefuzz/pkg/dto"
"github.com/dogefuzz/dogefuzz/pkg/interfaces"
)
Expand Down Expand Up @@ -157,3 +158,12 @@ func (s *transactionService) FindRunningAndCreatedBeforeThreshold(dateThreshold
}
return transactionDTOs, nil
}

func (s *transactionService) FindTimeTakenToWeakness(taskId string, weaknessType common.OracleType) (uint32, error) {
timeToWeakness, err := s.transactionRepo.FindTimeTakenToWeakness(s.connection.GetDB(), taskId, weaknessType)
if err != nil {
return 0, err
}
return timeToWeakness, nil

}

0 comments on commit 5a705bb

Please sign in to comment.