Skip to content

Commit

Permalink
feat: alter AppContract type to []byte and update related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhilt committed Jan 27, 2025
1 parent 8904a38 commit 63212e4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 74 deletions.
2 changes: 1 addition & 1 deletion pkg/convenience/model/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type FastReport struct {
Index int `db:"output_index"`
InputIndex int `db:"input_index"`
Payload string `db:"payload"`
AppContract string `json:"app_contract" db:"app_contract"`
AppContract []byte `json:"app_contract" db:"app_contract"`
AppID uint64 `db:"app_id"`
}

Expand Down
46 changes: 2 additions & 44 deletions pkg/convenience/repository/report_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,6 @@ func (r *ReportRepository) CreateTables() error {
return err
}

func (r *ReportRepository) CreateFastReport(ctx context.Context, report cModel.FastReport) (*cModel.FastReport, error) {
if r.AutoCount {
count, err := r.Count(ctx, nil)
if err != nil {
slog.Error("database error", "err", err)
return nil, err
}
report.Index = int(count)
}
if report.AppContract == "" {
return nil, fmt.Errorf("report is missing app_contract")
}
insertSql := `INSERT INTO convenience_reports (
output_index,
payload,
input_index,
app_contract,
app_id) VALUES ($1, $2, $3, $4, $5)`

var hexPayload string
if !strings.HasPrefix(report.Payload, "0x") {
hexPayload = "0x" + report.Payload
} else {
hexPayload = report.Payload
}
exec := DBExecutor{r.Db}
_, err := exec.ExecContext(
ctx,
insertSql,
report.Index,
hexPayload,
report.InputIndex,
report.AppContract,
report.AppID,
)
if err != nil {
slog.Error("database error", "err", err)
return nil, err
}
return &report, nil
}

func (r *ReportRepository) CreateReport(ctx context.Context, report cModel.Report) (cModel.Report, error) {
if r.AutoCount {
count, err := r.Count(ctx, nil)
Expand Down Expand Up @@ -173,8 +131,8 @@ func (r *ReportRepository) queryByOutputIndexAndAppContract(
func (r *ReportRepository) FindLastReport(ctx context.Context) (*cModel.FastReport, error) {
var report cModel.FastReport
err := r.Db.GetContext(ctx, &report, `
SELECT * FROM convenience_reports
ORDER BY
SELECT * FROM convenience_reports
ORDER BY
output_index DESC,
app_id DESC
LIMIT 1`)
Expand Down
11 changes: 0 additions & 11 deletions pkg/convenience/repository/report_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ func (s *ReportRepositorySuite) TestCreateReport() {
s.NoError(err)
}

func (s *ReportRepositorySuite) TestCreateFastReport() {
ctx := context.Background()
_, err := s.reportRepository.CreateFastReport(ctx, cModel.FastReport{
AppContract: configtest.DEFAULT_TEST_APP_CONTRACT,
Index: 1,
InputIndex: 2,
Payload: "1122",
})
s.NoError(err)
}

func (s *ReportRepositorySuite) TestCreateReportAndFind() {
ctx := context.Background()
appContract := common.HexToAddress(configtest.DEFAULT_TEST_APP_CONTRACT[2:])
Expand Down
31 changes: 15 additions & 16 deletions pkg/convenience/synchronizer_node/raw_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cartesi/rollups-graphql/pkg/convenience/model"
"github.com/cartesi/rollups-graphql/pkg/convenience/repository"
"github.com/ethereum/go-ethereum/common"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ type RawReport struct {
InputIndex uint64 `db:"input_index"`
ApplicationId int `db:"input_epoch_application_id"`
RawData []byte `db:"raw_data"`
AppContract string `db:"app_contract"`
AppContract []byte `db:"app_contract"`
}

type Output struct {
Expand Down Expand Up @@ -134,7 +133,7 @@ func (s *RawRepository) First50RawInputsGteRefWithStatus(ctx context.Context, in
slog.Error("Failed to scan row into RawInput struct", "error", err)
return nil, err
}
input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
// input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
inputs = append(inputs, input)
}
slog.Debug("First50RawInputsGteRefWithStatus", "status", status, "results", len(inputs))
Expand Down Expand Up @@ -188,7 +187,7 @@ func (s *RawRepository) FindAllRawInputs(ctx context.Context) ([]RawInput, error
slog.Error("Failed to scan row into RawInput struct", "error", err)
return nil, err
}
input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
// input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
inputs = append(inputs, input)
}
slog.Debug("FindAllRawInputs", "results", len(inputs))
Expand Down Expand Up @@ -221,7 +220,7 @@ func (s *RawRepository) FindAllInputsGtRef(ctx context.Context, inputRef *reposi
application a
ON
a.id = i.epoch_application_id
WHERE
WHERE
(i.epoch_application_id = $1 AND i.index > $2)
OR
(i.epoch_application_id <> $1 AND i.created_at >= $3)
Expand All @@ -242,7 +241,7 @@ func (s *RawRepository) FindAllInputsGtRef(ctx context.Context, inputRef *reposi
slog.Error("Failed to scan row into RawInput struct", "error", err)
return nil, err
}
input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
// input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress[2:]))
inputs = append(inputs, input)
}
slog.Debug("FindAllInputsGtRef", "results", len(inputs))
Expand All @@ -269,11 +268,11 @@ func (s *RawRepository) FindAllReportsGt(ctx context.Context, ourReport *model.F
application a
ON
a.id = i.epoch_application_id
WHERE
WHERE
(r.index > $1)
OR
(r.index = $1 AND r.input_epoch_application_id > $2)
ORDER BY
ORDER BY
r.index ASC,
r.input_epoch_application_id ASC
LIMIT $3
Expand Down Expand Up @@ -321,7 +320,7 @@ func (s *RawRepository) FindInputByOutput(ctx context.Context, filter FilterID)
i.snapshot_uri,
a.iapplication_address as application_address
FROM input i
INNER JOIN
INNER JOIN
application a ON a.id = i.epoch_application_id
WHERE i.index = $1
LIMIT 1`
Expand All @@ -343,7 +342,7 @@ func (s *RawRepository) FindInputByOutput(ctx context.Context, filter FilterID)
slog.Error("Failed to get context for input in FindInputByOutput", "error", err)
return nil, err
}
input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress))
// input.ApplicationAddress = common.Hex2Bytes(string(input.ApplicationAddress))

return &input, nil
}
Expand All @@ -368,7 +367,7 @@ func (s *RawRepository) findAllOutputsLimited(ctx context.Context) ([]Output, er
ON i.index = o.input_index
INNER JOIN application a
ON a.id = o.input_epoch_application_id
ORDER BY
ORDER BY
o.created_at ASC, o.index ASC, o.input_epoch_application_id ASC
LIMIT $1`
result, err := s.Db.QueryxContext(ctx, query, LIMIT)
Expand All @@ -385,7 +384,7 @@ func (s *RawRepository) findAllOutputsLimited(ctx context.Context) ([]Output, er
slog.Error("Failed to scan row into Output struct", "error", err)
return nil, err
}
output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
// output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
outputs = append(outputs, output)
}

Expand Down Expand Up @@ -436,7 +435,7 @@ func (s *RawRepository) FindAllOutputsGtRefLimited(ctx context.Context, outputRe
slog.Error("Failed to scan row into Output struct", "error", err)
return nil, err
}
output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
// output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
outputs = append(outputs, output)
}

Expand Down Expand Up @@ -472,7 +471,7 @@ func (s *RawRepository) FindAllOutputsWithProofGte(ctx context.Context, filter *
(o.index > $2)
)
ORDER BY
o.index ASC,
o.index ASC,
o.input_epoch_application_id ASC
LIMIT $3
`, filter.AppID, filter.OutputIndex, LIMIT)
Expand Down Expand Up @@ -518,7 +517,7 @@ func (s *RawRepository) FindAllOutputsExecutedAfter(ctx context.Context, outputR
(
o.execution_transaction_hash IS NOT NULL
)
AND
AND
(
(o.updated_at > $1)
OR
Expand All @@ -545,7 +544,7 @@ func (s *RawRepository) FindAllOutputsExecutedAfter(ctx context.Context, outputR
slog.Error("Failed to scan row into Output struct", "error", err)
return nil, err
}
output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
// output.AppContract = common.Hex2Bytes(string(output.AppContract[2:]))
outputs = append(outputs, output)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/convenience/synchronizer_node/synchronizer_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (s *SynchronizerReport) syncReports(ctx context.Context) error {
return err
}
for _, rawReport := range rawReports {
_, err = s.ReportRepository.CreateFastReport(ctx, model.FastReport{
AppContract: rawReport.AppContract,
_, err = s.ReportRepository.CreateReport(ctx, model.Report{
AppContract: common.BytesToAddress(rawReport.AppContract),
Index: int(rawReport.Index),
InputIndex: int(rawReport.InputIndex),
Payload: common.Bytes2Hex(rawReport.RawData),
Expand Down

0 comments on commit 63212e4

Please sign in to comment.