Skip to content

Commit 1dd112a

Browse files
committed
fix: update ApplicationAddress type to string and implement GetLatestApp method
1 parent 6fa1cf3 commit 1dd112a

File tree

6 files changed

+77
-59
lines changed

6 files changed

+77
-59
lines changed

pkg/convenience/model/type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const (
3737
type ApplicationState uint8
3838

3939
type ConvenienceApplication struct {
40-
ID uint64 `db:"id"`
41-
Name string `db:"name"`
42-
ApplicationAddress common.Address `db:"application_address"`
40+
ID uint64 `db:"id"`
41+
Name string `db:"name"`
42+
ApplicationAddress string `db:"application_address"`
4343
}
4444

4545
type ConvenienceNotice struct {

pkg/convenience/repository/application_repository.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package repository
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57
"fmt"
68
"log/slog"
79
"strings"
@@ -34,6 +36,25 @@ func (a *ApplicationRepository) CreateTables() error {
3436
return err
3537
}
3638

39+
func (a *ApplicationRepository) GetLatestApp(ctx context.Context) (*model.ConvenienceApplication, error) {
40+
query := `SELECT * FROM convenience_application ORDER BY id DESC LIMIT 1`
41+
stmt, err := a.Db.PreparexContext(ctx, query)
42+
if err != nil {
43+
return nil, err
44+
}
45+
defer stmt.Close()
46+
var app model.ConvenienceApplication
47+
err = stmt.GetContext(ctx, &app)
48+
if err != nil {
49+
if errors.Is(err, sql.ErrNoRows) {
50+
return nil, nil
51+
}
52+
53+
return nil, err
54+
}
55+
return &app, nil
56+
}
57+
3758
func (a *ApplicationRepository) Create(ctx context.Context, rawApp *model.ConvenienceApplication) (*model.ConvenienceApplication, error) {
3859
insertSql := `INSERT INTO convenience_application (
3960
id,
@@ -50,7 +71,7 @@ func (a *ApplicationRepository) Create(ctx context.Context, rawApp *model.Conven
5071
_, err := exec.ExecContext(ctx, insertSql,
5172
rawApp.ID,
5273
rawApp.Name,
53-
rawApp.ApplicationAddress.Hex(),
74+
rawApp.ApplicationAddress,
5475
)
5576

5677
if err != nil {

pkg/convenience/repository/application_repository_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/cartesi/rollups-graphql/pkg/commons"
1212
configtest "github.com/cartesi/rollups-graphql/pkg/convenience/config_test"
1313
"github.com/cartesi/rollups-graphql/pkg/convenience/model"
14-
"github.com/ethereum/go-ethereum/common"
1514
"github.com/jmoiron/sqlx"
1615
"github.com/stretchr/testify/suite"
1716
)
@@ -54,10 +53,11 @@ func TestApplicationRepositorySuite(t *testing.T) {
5453
}
5554

5655
func newApp() *model.ConvenienceApplication {
56+
address := configtest.DEFAULT_TEST_APP_CONTRACT
5757
return &model.ConvenienceApplication{
5858
ID: 1,
5959
Name: "app1",
60-
ApplicationAddress: common.HexToAddress(configtest.DEFAULT_TEST_APP_CONTRACT),
60+
ApplicationAddress: address,
6161
}
6262
}
6363

@@ -74,12 +74,12 @@ func (s *ApplicationRepositorySuite) TestCreateApplication() {
7474
func (s *ApplicationRepositorySuite) TestFindApplication() {
7575
ctx := context.Background()
7676
counter := 10
77-
add := configtest.DEFAULT_TEST_APP_CONTRACT
77+
anotherAppContract := configtest.DEFAULT_TEST_APP_CONTRACT
7878

7979
for i := 0; i < counter; i++ {
8080
app := newApp()
8181
app.Name = fmt.Sprintf("app%d", i)
82-
app.ApplicationAddress = common.HexToAddress(add)
82+
app.ApplicationAddress = anotherAppContract
8383
_, err := s.repository.Create(ctx, app)
8484
s.NoError(err)
8585
}
@@ -88,27 +88,27 @@ func (s *ApplicationRepositorySuite) TestFindApplication() {
8888
filter := []*model.ConvenienceFilter{
8989
{
9090
Field: &key,
91-
Eq: &add,
91+
Eq: &anotherAppContract,
9292
},
9393
}
9494
count, err := s.repository.Count(ctx, filter)
9595
s.NoError(err)
9696
s.Equal(counter, int(count))
9797

98-
add = "0x544a3B76B84b1E98c13437A1591E713Dd314387F"
98+
anotherAppContract = "0x544a3B76B84b1E98c13437A1591E713Dd314387F"
9999

100100
for i := 0; i < counter; i++ {
101101
app := newApp()
102102
app.Name = fmt.Sprintf("app%d", i)
103-
app.ApplicationAddress = common.HexToAddress(add)
103+
app.ApplicationAddress = anotherAppContract
104104
_, err := s.repository.Create(ctx, app)
105105
s.NoError(err)
106106
}
107107

108108
filter = []*model.ConvenienceFilter{
109109
{
110110
Field: &key,
111-
Eq: &add,
111+
Eq: &anotherAppContract,
112112
},
113113
}
114114
count, err = s.repository.Count(ctx, filter)

pkg/convenience/synchronizer_node/raw_repository.go

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/cartesi/rollups-graphql/pkg/convenience/model"
1111
"github.com/cartesi/rollups-graphql/pkg/convenience/repository"
12+
"github.com/ethereum/go-ethereum/common"
1213
"github.com/jmoiron/sqlx"
1314
_ "github.com/lib/pq"
1415
)
@@ -22,6 +23,20 @@ type RawInputAppAddress struct {
2223
Address []byte `db:"iapplication_address"`
2324
}
2425

26+
type RawApplication struct {
27+
ID uint64 `db:"id"`
28+
Name string `db:"name"`
29+
ApplicationAddress common.Address `db:"application_address"`
30+
}
31+
32+
func (r *RawApplication) ToConvenience() model.ConvenienceApplication {
33+
return model.ConvenienceApplication{
34+
ID: r.ID,
35+
Name: r.Name,
36+
ApplicationAddress: r.ApplicationAddress.Hex(),
37+
}
38+
}
39+
2540
type RawInput struct {
2641
Index uint64 `db:"index"` // numeric(20,0)
2742
RawData []byte `db:"raw_data"`
@@ -91,8 +106,12 @@ func NewRawRepository(connectionURL string, db *sqlx.DB) *RawRepository {
91106
return &RawRepository{connectionURL, db}
92107
}
93108

94-
func (s *RawRepository) GetApplicationRef(ctx context.Context, appID uint64) ([]model.ConvenienceApplication, error) {
95-
apps := []model.ConvenienceApplication{}
109+
func (s *RawRepository) GetApplicationRef(ctx context.Context, app *model.ConvenienceApplication) ([]RawApplication, error) {
110+
if app == nil {
111+
return s.FindAllAppsRef(ctx)
112+
}
113+
114+
apps := []RawApplication{}
96115
query := `
97116
SELECT
98117
id,
@@ -101,19 +120,19 @@ func (s *RawRepository) GetApplicationRef(ctx context.Context, appID uint64) ([]
101120
FROM
102121
application
103122
WHERE
104-
id >= $1
123+
id > $1
105124
ORDER BY id ASC
106125
LIMIT $2
107126
`
108-
res, err := s.Db.QueryxContext(ctx, query, appID, LIMIT)
127+
res, err := s.Db.QueryxContext(ctx, query, app.ID, LIMIT)
109128

110129
if err != nil {
111-
slog.Error("Failed to execute query in CheckStatusApplicationRef", "error", err)
130+
slog.Error("Failed to execute query in GetApplicationRef", "error", err)
112131
return nil, err
113132
}
114133

115134
for res.Next() {
116-
var app model.ConvenienceApplication
135+
var app RawApplication
117136
err := res.StructScan(&app)
118137
if err != nil {
119138
slog.Error("Failed to scan row into Application struct", "error", err)
@@ -125,36 +144,7 @@ func (s *RawRepository) GetApplicationRef(ctx context.Context, appID uint64) ([]
125144
return apps, nil
126145
}
127146

128-
func (s *RawRepository) GetLatestApp(ctx context.Context) (*model.ConvenienceApplication, error) {
129-
var output model.ConvenienceApplication
130-
query := `
131-
SELECT
132-
id,
133-
name,
134-
iapplication_address as application_address
135-
FROM
136-
application
137-
ORDER BY
138-
id ASC
139-
LIMIT 1
140-
`
141-
err := s.Db.GetContext(ctx, &output, query)
142-
143-
if err != nil {
144-
if errors.Is(err, sql.ErrNoRows) {
145-
slog.Warn("No application found")
146-
return nil, nil
147-
}
148-
slog.Error("Failed to get latest application ref", "error", err)
149-
return nil, err
150-
}
151-
152-
slog.Debug("Latest App fetched", "id", output.ID, "name", output.Name, "address", output.ApplicationAddress)
153-
154-
return &output, nil
155-
}
156-
157-
func (s *RawRepository) FindAllAppsRef(ctx context.Context) ([]model.ConvenienceApplication, error) {
147+
func (s *RawRepository) FindAllAppsRef(ctx context.Context) ([]RawApplication, error) {
158148
query := `
159149
SELECT
160150
id,
@@ -167,15 +157,15 @@ func (s *RawRepository) FindAllAppsRef(ctx context.Context) ([]model.Convenience
167157
LIMIT $1
168158
`
169159

170-
apps := []model.ConvenienceApplication{}
160+
apps := []RawApplication{}
171161
result, err := s.Db.QueryxContext(ctx, query, LIMIT)
172162
if err != nil {
173163
slog.Error("Failed to execute query in FindAllAppsRef", "error", err)
174164
return nil, err
175165
}
176166

177167
for result.Next() {
178-
var app model.ConvenienceApplication
168+
var app RawApplication
179169
err := result.StructScan(&app)
180170
if err != nil {
181171
slog.Error("Failed to scan row into Application struct", "error", err)

pkg/convenience/synchronizer_node/synchronizer_app_create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ func (s SynchronizerAppCreator) SyncApps(ctx context.Context) error {
6868
}
6969

7070
func (s *SynchronizerAppCreator) syncApps(ctx context.Context) error {
71-
lastAppRef, err := s.RawRepository.GetLatestApp(ctx)
71+
lastAppRef, err := s.AppRepository.GetLatestApp(ctx)
7272
if err != nil {
7373
return err
7474
}
75-
apps, err := s.RawRepository.GetApplicationRef(ctx, lastAppRef.ID)
75+
apps, err := s.RawRepository.GetApplicationRef(ctx, lastAppRef)
7676
if err != nil {
7777
return err
7878
}
79-
for _, app := range apps {
79+
for _, rawApp := range apps {
80+
app := rawApp.ToConvenience()
8081
_, err = s.AppRepository.Create(ctx, &app)
8182
if err != nil {
8283
return err

pkg/convenience/synchronizer_node/synchronizer_app_create_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ func (s *SynchronizerAppCreate) TestAppCreate() {
7373
apps, err := s.rawNodeV2Repository.FindAllAppsRef(s.ctx)
7474
s.Require().NoError(err)
7575
s.Require().NotEmpty(apps)
76-
size := min(LIMIT, uint64(len(apps)))
76+
// size := min(LIMIT, uint64(len(apps)))
77+
size := 1
78+
7779
firstApp := apps[0]
7880
s.Equal("echo-dapp", firstApp.Name)
79-
err = s.synchronizerAppCreator.SyncApps(s.ctx)
80-
s.Require().NoError(err)
81-
count, err := s.appRepository.Count(s.ctx, nil)
82-
s.Require().NoError(err)
83-
s.Require().Equal(size, count)
81+
s.Equal("0x36B9E60ACb181da458aa8870646395CD27cD0E6E", firstApp.ApplicationAddress.Hex())
82+
83+
for i := 0; i < 2; i++ {
84+
err = s.synchronizerAppCreator.SyncApps(s.ctx)
85+
s.Require().NoError(err)
86+
count, err := s.appRepository.Count(s.ctx, nil)
87+
s.Require().NoError(err)
88+
s.Require().Equal(size, int(count))
89+
}
8490
}

0 commit comments

Comments
 (0)