From a78c263d1b4360979f775cb2f21295234beecc52 Mon Sep 17 00:00:00 2001 From: Charitha Bandi <45089429+charithabandi@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:58:22 -0500 Subject: [PATCH] voting: prevent inserting resolution with null type This modifies the insert_resolution function to prevent inserting a new resolution with a null type column, which was possible with the previous function if there was no matching type in the resolution_types table. --- internal/voting/broadcast/broadcast_test.go | 10 ++++++++++ internal/voting/sql.go | 2 +- internal/voting/vote_test.go | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/voting/broadcast/broadcast_test.go b/internal/voting/broadcast/broadcast_test.go index dcd620a0d..6945cf403 100644 --- a/internal/voting/broadcast/broadcast_test.go +++ b/internal/voting/broadcast/broadcast_test.go @@ -16,6 +16,7 @@ import ( "github.com/kwilteam/kwil-db/core/log" "github.com/kwilteam/kwil-db/core/types" "github.com/kwilteam/kwil-db/core/types/transactions" + "github.com/kwilteam/kwil-db/extensions/resolutions" dbtest "github.com/kwilteam/kwil-db/internal/sql/pg/test" "github.com/kwilteam/kwil-db/internal/voting" "github.com/kwilteam/kwil-db/internal/voting/broadcast" @@ -23,6 +24,15 @@ import ( const maxVoteIDsPerTx = 100 +const testType = "test" + +func init() { + err := resolutions.RegisterResolution(testType, resolutions.ModAdd, resolutions.ResolutionConfig{}) + if err != nil { + panic(err) + } +} + func Test_Broadcaster(t *testing.T) { type testCase struct { name string diff --git a/internal/voting/sql.go b/internal/voting/sql.go index 5208c01b3..1b9eaa98f 100644 --- a/internal/voting/sql.go +++ b/internal/voting/sql.go @@ -39,7 +39,7 @@ const ( tableResolutions = `CREATE TABLE IF NOT EXISTS ` + votingSchemaName + `.resolutions ( id BYTEA PRIMARY KEY, -- id is an rfc4122 uuid derived from the body body BYTEA, -- body is the actual resolution info - type BYTEA, -- type is the type of resolution + type BYTEA NOT NULL, -- type is the type of resolution vote_body_proposer BYTEA, -- vote_body_proposer is the identifier of the node that supplied the vote body expiration INT8 NOT NULL, -- expiration is the blockheight at which the resolution expires extra_vote_id BOOLEAN NOT NULL DEFAULT FALSE, -- If vote_body_proposer had sent VoteID before VoteBody, this is set to true diff --git a/internal/voting/vote_test.go b/internal/voting/vote_test.go index 085a9cb86..0d6713cc9 100644 --- a/internal/voting/vote_test.go +++ b/internal/voting/vote_test.go @@ -47,8 +47,11 @@ func Test_Voting(t *testing.T) { fn: func(t *testing.T, db sql.DB) { ctx := context.Background() + err := CreateResolution(ctx, db, dummyEvent, 10, []byte("a")) + require.Error(t, err) + // Can't approve non-existent resolutions - err := ApproveResolution(ctx, db, testEvent.ID(), []byte("a")) + err = ApproveResolution(ctx, db, testEvent.ID(), []byte("a")) require.Error(t, err) err = CreateResolution(ctx, db, testEvent, 10, []byte("a")) @@ -325,4 +328,8 @@ var testEvent = &types.VotableEvent{ Type: testType, } +var dummyEvent = &types.VotableEvent{ + Body: []byte("test"), + Type: "blah", +} var testConfirmationThreshold = big.NewRat(2, 3)