Skip to content

Commit

Permalink
feat(repository): add repository for machine related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 authored and mpolitzer committed Sep 25, 2024
1 parent bba7722 commit f63c12d
Show file tree
Hide file tree
Showing 9 changed files with 651 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ linters:
enable:
- exhaustive
- goconst
- godox
#- godox
- gofmt
- lll
- mnd
Expand Down
2 changes: 1 addition & 1 deletion cmd/cartesi-rollups-cli/root/app/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func run(cmd *cobra.Command, args []string) {
IConsensusAddress: common.HexToAddress(iConsensusAddress),
}

err := cmdcommom.Database.InsertApplication(ctx, &application)
_, err := cmdcommom.Database.InsertApplication(ctx, &application)
cobra.CheckErr(err)
fmt.Printf("Application %v successfully added\n", application.ContractAddress)
}
34 changes: 27 additions & 7 deletions internal/repository/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ type Database struct {
db *pgxpool.Pool
}

var ErrInsertRow = errors.New("unable to insert row")
var (
ErrInsertRow = errors.New("unable to insert row")
ErrUpdateRow = errors.New("unable to update row")
ErrCopyFrom = errors.New("unable to COPY FROM")

ErrBeginTx = errors.New("unable to begin transaction")
ErrCommitTx = errors.New("unable to commit transaction")
)

func Connect(
ctx context.Context,
Expand Down Expand Up @@ -84,11 +91,12 @@ func (pg *Database) InsertNodeConfig(
func (pg *Database) InsertApplication(
ctx context.Context,
app *Application,
) error {
) (uint64, error) {
query := `
INSERT INTO application
(contract_address,
template_hash,
template_uri,
last_processed_block,
last_claim_check_block,
last_output_check_block,
Expand All @@ -97,28 +105,34 @@ func (pg *Database) InsertApplication(
VALUES
(@contractAddress,
@templateHash,
@templateUri,
@lastProcessedBlock,
@lastClaimCheckBlock,
@lastOutputCheckBlock,
@status,
@iConsensusAddress)`
@iConsensusAddress)
RETURNING
id
`

args := pgx.NamedArgs{
"contractAddress": app.ContractAddress,
"templateHash": app.TemplateHash,
"templateUri": app.TemplateUri,
"lastProcessedBlock": app.LastProcessedBlock,
"lastClaimCheckBlock": app.LastClaimCheckBlock,
"lastOutputCheckBlock": app.LastOutputCheckBlock,
"status": app.Status,
"iConsensusAddress": app.IConsensusAddress,
}

_, err := pg.db.Exec(ctx, query, args)
var id uint64
err := pg.db.QueryRow(ctx, query, args).Scan(&id)
if err != nil {
return fmt.Errorf("%w: %w", ErrInsertRow, err)
return 0, fmt.Errorf("%w: %w", ErrInsertRow, err)
}

return nil
return id, nil
}

func (pg *Database) InsertEpoch(
Expand Down Expand Up @@ -295,7 +309,9 @@ func (pg *Database) InsertSnapshot(
(@inputId,
@appAddress,
@uri)
RETURNING id`
RETURNING
id
`

args := pgx.NamedArgs{
"inputId": snapshot.InputId,
Expand Down Expand Up @@ -358,6 +374,7 @@ func (pg *Database) GetApplication(
id uint64
contractAddress Address
templateHash Hash
templateUri string
lastProcessedBlock uint64
lastClaimCheckBlock uint64
lastOutputCheckBlock uint64
Expand All @@ -370,6 +387,7 @@ func (pg *Database) GetApplication(
id,
contract_address,
template_hash,
template_uri,
last_processed_block,
last_claim_check_block,
last_output_check_block,
Expand All @@ -388,6 +406,7 @@ func (pg *Database) GetApplication(
&id,
&contractAddress,
&templateHash,
&templateUri,
&lastProcessedBlock,
&lastClaimCheckBlock,
&lastOutputCheckBlock,
Expand All @@ -408,6 +427,7 @@ func (pg *Database) GetApplication(
Id: id,
ContractAddress: contractAddress,
TemplateHash: templateHash,
TemplateUri: templateUri,
LastProcessedBlock: lastProcessedBlock,
LastClaimCheckBlock: lastClaimCheckBlock,
LastOutputCheckBlock: lastOutputCheckBlock,
Expand Down
10 changes: 7 additions & 3 deletions internal/repository/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *RepositorySuite) SetupDatabase() {
ContractAddress: common.HexToAddress("deadbeef"),
IConsensusAddress: common.HexToAddress("ffffff"),
TemplateHash: common.HexToHash("deadbeef"),
TemplateUri: "path/to/template/uri/0",
LastProcessedBlock: 1,
Status: ApplicationStatusRunning,
}
Expand All @@ -80,14 +81,15 @@ func (s *RepositorySuite) SetupDatabase() {
ContractAddress: common.HexToAddress("feadbeef"),
IConsensusAddress: common.HexToAddress("ffffff"),
TemplateHash: common.HexToHash("deadbeef"),
TemplateUri: "path/to/template/uri/0",
LastProcessedBlock: 1,
Status: ApplicationStatusNotRunning,
}

err = s.database.InsertApplication(s.ctx, &app)
_, err = s.database.InsertApplication(s.ctx, &app)
s.Require().Nil(err)

err = s.database.InsertApplication(s.ctx, &app2)
_, err = s.database.InsertApplication(s.ctx, &app2)
s.Require().Nil(err)

genericHash := common.HexToHash("deadbeef")
Expand Down Expand Up @@ -245,6 +247,7 @@ func (s *RepositorySuite) TestApplicationExists() {
ContractAddress: common.HexToAddress("deadbeef"),
IConsensusAddress: common.HexToAddress("ffffff"),
TemplateHash: common.HexToHash("deadbeef"),
TemplateUri: "path/to/template/uri/0",
LastProcessedBlock: 1,
Status: ApplicationStatusRunning,
}
Expand All @@ -266,11 +269,12 @@ func (s *RepositorySuite) TestApplicationFailsDuplicateRow() {
ContractAddress: common.HexToAddress("deadbeef"),
IConsensusAddress: common.HexToAddress("ffffff"),
TemplateHash: common.HexToHash("deadbeef"),
TemplateUri: "path/to/template/uri/0",
LastProcessedBlock: 1,
Status: ApplicationStatusRunning,
}

err := s.database.InsertApplication(s.ctx, &app)
_, err := s.database.InsertApplication(s.ctx, &app)
s.Require().ErrorContains(err, "duplicate key value")
}

Expand Down
Loading

0 comments on commit f63c12d

Please sign in to comment.