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 vfusco committed Sep 9, 2024
1 parent cf978c5 commit 40b2fd7
Show file tree
Hide file tree
Showing 8 changed files with 697 additions and 24 deletions.
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)
}
7 changes: 7 additions & 0 deletions internal/node/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ type Application struct {
LastProcessedBlock uint64
Status ApplicationStatus
IConsensusAddress Address
// Temporary ------------------------------------
MachineIncCycles uint64
MachineMaxCycles uint64
MachineAdvanceTimeout uint32
MachineInspectTimeout uint32
MachineMaxConcurrentInspects uint32
// ----------------------------------------------
}

type Epoch struct {
Expand Down
60 changes: 46 additions & 14 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,35 +91,57 @@ 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,
status,
iconsensus_address)
iconsensus_address,
machine_inc_cycles,
machine_max_cycles,
machine_advance_timeout,
machine_inspect_timeout,
machine_max_concurrent_inspects)
VALUES
(@contractAddress,
@templateHash,
@templateUri,
@lastProcessedBlock,
@status,
@iConsensusAddress)`
@iConsensusAddress,
@machineIncCycles,
@machineMaxCycles,
@machineAdvanceTimeout,
@machineInspectTimeout,
@machineMaxConcurrentInspects)
RETURNING
id
`

args := pgx.NamedArgs{
"contractAddress": app.ContractAddress,
"templateHash": app.TemplateHash,
"lastProcessedBlock": app.LastProcessedBlock,
"status": app.Status,
"iConsensusAddress": app.IConsensusAddress,
"contractAddress": app.ContractAddress,
"templateHash": app.TemplateHash,
"templateUri": app.TemplateUri,
"lastProcessedBlock": app.LastProcessedBlock,
"status": app.Status,
"iConsensusAddress": app.IConsensusAddress,
"machineIncCycles": app.MachineIncCycles,
"machineMaxCycles": app.MachineMaxCycles,
"machineAdvanceTimeout": app.MachineAdvanceTimeout,
"machineInspectTimeout": app.MachineInspectTimeout,
"machineMaxConcurrentInspects": app.MachineMaxConcurrentInspects,
}

_, 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 @@ -140,7 +169,8 @@ func (pg *Database) InsertEpoch(
@status,
@applicationAddress)
RETURNING
id`
id
`

args := pgx.NamedArgs{
"index": epoch.Index,
Expand Down Expand Up @@ -286,7 +316,9 @@ func (pg *Database) InsertSnapshot(
(@inputId,
@appAddress,
@uri)
RETURNING id`
RETURNING
id
`

args := pgx.NamedArgs{
"inputId": snapshot.InputId,
Expand Down
6 changes: 3 additions & 3 deletions internal/repository/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func (s *RepositorySuite) SetupDatabase() {
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 @@ -256,7 +256,7 @@ func (s *RepositorySuite) TestApplicationFailsDuplicateRow() {
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 40b2fd7

Please sign in to comment.