Skip to content

Commit

Permalink
fixup! feat(db): Add Epoch and Snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Aug 7, 2024
1 parent fb61db3 commit abc3376
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 48 deletions.
10 changes: 6 additions & 4 deletions internal/node/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ type Output struct {
Hash *Hash
OutputHashesSiblings []Hash
InputId uint64
AppAddress Address
}

type Report struct {
Id uint64
Index uint64
RawData Bytes
InputId uint64
Id uint64
Index uint64
RawData Bytes
InputId uint64
AppAddress Address
}

type Snapshot struct {
Expand Down
74 changes: 42 additions & 32 deletions internal/repository/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,21 @@ func (pg *Database) InsertOutput(
(index,
raw_data,
output_hashes_siblings,
input_id)
input_id,
application_address)
VALUES
(@index,
@rawData,
@outputHashesSiblings,
@inputId)`
@inputId,
@appAddress)`

args := pgx.NamedArgs{
"inputId": output.InputId,
"index": output.Index,
"rawData": output.RawData,
"outputHashesSiblings": output.OutputHashesSiblings,
"appAddress": output.AppAddress,
}

_, err := pg.db.Exec(ctx, query, args)
Expand All @@ -245,16 +248,19 @@ func (pg *Database) InsertReport(
INSERT INTO report
(index,
raw_data,
input_id)
input_id,
application_address)
VALUES
(@index,
@rawData,
@inputId)`
@inputId,
@appAddress)`

args := pgx.NamedArgs{
"inputId": report.InputId,
"index": report.Index,
"rawData": report.RawData,
"inputId": report.InputId,
"index": report.Index,
"rawData": report.RawData,
"appAddress": report.AppAddress,
}

_, err := pg.db.Exec(ctx, query, args)
Expand All @@ -268,7 +274,10 @@ func (pg *Database) InsertReport(
func (pg *Database) InsertSnapshot(
ctx context.Context,
snapshot *Snapshot,
) error {
) (uint64, error) {

var id uint64

query := `
INSERT INTO snapshot
(input_id,
Expand All @@ -277,20 +286,21 @@ func (pg *Database) InsertSnapshot(
VALUES
(@inputId,
@appAddress,
@uri)`
@uri)
RETURNING id`

args := pgx.NamedArgs{
"inputId": snapshot.InputId,
"appAddress": snapshot.AppAddress,
"uri": snapshot.URI,
}

_, err := pg.db.Exec(ctx, query, args)
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) GetNodeConfig(
Expand Down Expand Up @@ -543,6 +553,7 @@ func (pg *Database) GetOutput(
hash *Hash
outputHashesSiblings []Hash
inputId uint64
appAddress Address
)

query := `
Expand All @@ -552,15 +563,12 @@ func (pg *Database) GetOutput(
o.raw_data,
o.hash,
o.output_hashes_siblings,
o.input_id
o.input_id,
o.application_address
FROM
output o
INNER JOIN
input i
ON
o.input_id=i.id
WHERE
o.index=@index AND i.application_address=@appAddress`
o.index=@index AND o.application_address=@appAddress`

args := pgx.NamedArgs{
"index": indexKey,
Expand All @@ -574,6 +582,7 @@ func (pg *Database) GetOutput(
&hash,
&outputHashesSiblings,
&inputId,
&appAddress,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
Expand All @@ -590,6 +599,7 @@ func (pg *Database) GetOutput(
Hash: hash,
OutputHashesSiblings: outputHashesSiblings,
InputId: inputId,
AppAddress: appAddress,
}

return &output, nil
Expand All @@ -601,25 +611,23 @@ func (pg *Database) GetReport(
appAddressKey Address,
) (*Report, error) {
var (
id uint64
index uint64
rawData []byte
inputId uint64
id uint64
index uint64
rawData []byte
inputId uint64
appAddress Address
)
query := `
SELECT
r.id,
r.index,
r.raw_data,
r.input_id
r.input_id,
r.application_address
FROM
report r
INNER JOIN
input i
ON
r.input_id=i.id
WHERE
r.index=@index AND i.application_address=@appAddress`
r.index=@index AND r.application_address=@appAddress`

args := pgx.NamedArgs{
"index": indexKey,
Expand All @@ -630,6 +638,7 @@ func (pg *Database) GetReport(
&index,
&rawData,
&inputId,
&appAddress,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
Expand All @@ -640,10 +649,11 @@ func (pg *Database) GetReport(
}

report := Report{
Id: id,
Index: index,
RawData: rawData,
InputId: inputId,
Id: id,
Index: index,
RawData: rawData,
InputId: inputId,
AppAddress: appAddress,
}

return &report, nil
Expand Down
49 changes: 41 additions & 8 deletions internal/repository/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (s *RepositorySuite) SetupDatabase() {
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: siblings,
AppAddress: app.ContractAddress,
}

err = s.database.InsertOutput(s.ctx, &output0)
Expand All @@ -182,6 +183,7 @@ func (s *RepositorySuite) SetupDatabase() {
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: siblings,
AppAddress: app.ContractAddress,
}

err = s.database.InsertOutput(s.ctx, &output1)
Expand All @@ -192,6 +194,7 @@ func (s *RepositorySuite) SetupDatabase() {
InputId: 2,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: siblings,
AppAddress: app.ContractAddress,
}

err = s.database.InsertOutput(s.ctx, &output2)
Expand All @@ -202,15 +205,17 @@ func (s *RepositorySuite) SetupDatabase() {
InputId: 3,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: siblings,
AppAddress: app.ContractAddress,
}

err = s.database.InsertOutput(s.ctx, &output3)
s.Require().Nil(err)

report := Report{
Index: 1,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
Index: 1,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
AppAddress: app.ContractAddress,
}

err = s.database.InsertReport(s.ctx, &report)
Expand All @@ -222,8 +227,9 @@ func (s *RepositorySuite) SetupDatabase() {
URI: "/some/path",
}

err = s.database.InsertSnapshot(s.ctx, &snapshot)
id, err := s.database.InsertSnapshot(s.ctx, &snapshot)
s.Require().Nil(err)
s.Require().NotEqual(0, id)
}

func (s *RepositorySuite) TestApplicationExists() {
Expand Down Expand Up @@ -321,6 +327,7 @@ func (s *RepositorySuite) TestOutputExists() {
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: siblings,
AppAddress: common.HexToAddress("0xdeadbeef"),
}

response, err := s.database.GetOutput(s.ctx, 1, common.HexToAddress("deadbeef"))
Expand All @@ -340,12 +347,26 @@ func (s *RepositorySuite) TestOutputFailsDuplicateRow() {
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: nil,
AppAddress: common.HexToAddress("deadbeef"),
}

err := s.database.InsertOutput(s.ctx, &output)
s.Require().ErrorContains(err, "duplicate key value")
}

func (s *RepositorySuite) TestOutputFailsDifferentAppAddress() {
output := Output{
Index: 5,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
OutputHashesSiblings: nil,
AppAddress: common.HexToAddress("feadbeef"),
}

err := s.database.InsertOutput(s.ctx, &output)
s.Require().ErrorContains(err, "violates check constraint")
}

func (s *RepositorySuite) TestOutputFailsInputDoesntExist() {
output := Output{
Index: 10,
Expand All @@ -360,10 +381,11 @@ func (s *RepositorySuite) TestOutputFailsInputDoesntExist() {

func (s *RepositorySuite) TestReportExists() {
report := Report{
Id: 1,
Index: 1,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
Id: 1,
Index: 1,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
AppAddress: common.HexToAddress("0xdeadbeef"),
}

response, err := s.database.GetReport(s.ctx, 1, common.HexToAddress("deadbeef"))
Expand All @@ -388,6 +410,17 @@ func (s *RepositorySuite) TestReportFailsDuplicateRow() {
s.Require().ErrorContains(err, "duplicate key value")
}

func (s *RepositorySuite) TestReportFailsDifferenteAddress() {
report := Report{
Index: 1,
InputId: 1,
RawData: common.Hex2Bytes("deadbeef"),
}

err := s.database.InsertReport(s.ctx, &report)
s.Require().ErrorContains(err, "duplicate key value")
}

func (s *RepositorySuite) TestReportFailsInputDoesntExist() {
report := Report{
Index: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ CREATE FUNCTION public.f_maxuint64()
LANGUAGE sql IMMUTABLE PARALLEL SAFE AS
'SELECT 18446744073709551615';



CREATE TABLE "application"
(
"id" SERIAL,
Expand Down Expand Up @@ -59,6 +61,11 @@ CREATE TABLE "input"

CREATE INDEX "input_idx" ON "input"("block_number");

CREATE FUNCTION public.f_getappaddressfrominput(inputId BIGINT)
RETURNS BYTEA
LANGUAGE sql AS
'SELECT application_address FROM input i WHERE inputId=i.id';

CREATE TABLE "output"
(
"id" BIGSERIAL,
Expand All @@ -67,8 +74,11 @@ CREATE TABLE "output"
"hash" BYTEA,
"output_hashes_siblings" BYTEA[],
"input_id" BIGINT NOT NULL,
"application_address" BYTEA NOT NULL CHECK ("application_address" = f_getappaddressfrominput("input_id")),
CONSTRAINT "output_pkey" PRIMARY KEY ("id"),
CONSTRAINT "output_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id")
CONSTRAINT "output_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id"),
CONSTRAINT "output_application_address_fkey" FOREIGN KEY ("application_address") REFERENCES "application"("contract_address"),
UNIQUE("index", "application_address")
);

CREATE UNIQUE INDEX "output_idx" ON "output"("index");
Expand All @@ -79,8 +89,11 @@ CREATE TABLE "report"
"index" NUMERIC(20,0) NOT NULL CHECK ("index" >= 0 AND "index" <= f_maxuint64()),
"raw_data" BYTEA NOT NULL,
"input_id" BIGINT NOT NULL,
"application_address" BYTEA NOT NULL,
CONSTRAINT "report_pkey" PRIMARY KEY ("id"),
CONSTRAINT "report_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id")
CONSTRAINT "report_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id"),
CONSTRAINT "report_application_address_fkey" FOREIGN KEY ("application_address") REFERENCES "application"("contract_address"),
UNIQUE("index", "application_address")
);

CREATE UNIQUE INDEX "report_idx" ON "report"("index");
Expand All @@ -93,7 +106,8 @@ CREATE TABLE "snapshot"
"uri" VARCHAR(4096) NOT NULL,
CONSTRAINT "snapshot_pkey" PRIMARY KEY ("id"),
CONSTRAINT "snapshot_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id"),
CONSTRAINT "snapshot_application_address_fkey" FOREIGN KEY ("application_address") REFERENCES "application"("contract_address")
CONSTRAINT "snapshot_application_address_fkey" FOREIGN KEY ("application_address") REFERENCES "application"("contract_address"),
UNIQUE("input_id", "application_address")
);

CREATE TABLE "node_config"
Expand All @@ -106,4 +120,3 @@ CREATE TABLE "node_config"
"epoch_length" INT NOT NULL
);


Loading

0 comments on commit abc3376

Please sign in to comment.