Skip to content

Commit

Permalink
voting: prevent inserting resolution with null type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
charithabandi authored Oct 1, 2024
1 parent be7bd1e commit a78c263
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions internal/voting/broadcast/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ 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"
)

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
Expand Down
2 changes: 1 addition & 1 deletion internal/voting/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion internal/voting/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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)

0 comments on commit a78c263

Please sign in to comment.