From 5d5d84504d153502753a582f3ed230af34673229 Mon Sep 17 00:00:00 2001 From: Ryan Smith Date: Thu, 2 Nov 2023 10:30:36 -0700 Subject: [PATCH] abi2: bugfix. validating required block fields --- abi2/abi2.go | 15 +++++++++++---- abi2/abi2_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/abi2/abi2.go b/abi2/abi2.go index 18419ae3..cd67c732 100644 --- a/abi2/abi2.go +++ b/abi2/abi2.go @@ -603,7 +603,7 @@ func New(name string, ev Event, bd []BlockData, table Table) (Integration, error return ig, fmt.Errorf("validating %s: %w", name, err) } for _, input := range ev.Selected() { - c, err := col(table, input.Column) + c, err := col(ig.Table, input.Column) if err != nil { return ig, err } @@ -614,8 +614,8 @@ func New(name string, ev Event, bd []BlockData, table Table) (Integration, error }) ig.numSelected++ } - for _, data := range bd { - c, err := col(table, data.Column) + for _, data := range ig.Block { + c, err := col(ig.Table, data.Column) if err != nil { return ig, err } @@ -677,6 +677,7 @@ func (ig *Integration) validateCols() error { } ucols = map[string]struct{}{} uinputs = map[string]struct{}{} + ubd = map[string]struct{}{} ) for _, c := range ig.Table.Cols { if _, ok := ucols[c.Name]; ok { @@ -690,6 +691,12 @@ func (ig *Integration) validateCols() error { } uinputs[inp.Name] = struct{}{} } + for _, bd := range ig.Block { + if _, ok := ubd[bd.Name]; ok { + return fmt.Errorf("duplicate block data field: %s", bd.Name) + } + ubd[bd.Name] = struct{}{} + } for name, cfg := range required { switch { case len(cfg.i.Name) > 0: @@ -697,7 +704,7 @@ func (ig *Integration) validateCols() error { ig.Event.Inputs = append(ig.Event.Inputs, cfg.i) } case len(cfg.b.Name) > 0: - if _, ok := uinputs[name]; !ok { + if _, ok := ubd[name]; !ok { ig.Block = append(ig.Block, cfg.b) } } diff --git a/abi2/abi2_test.go b/abi2/abi2_test.go index 1ddfb668..66e0691d 100644 --- a/abi2/abi2_test.go +++ b/abi2/abi2_test.go @@ -312,6 +312,33 @@ func TestNumIndexed(t *testing.T) { diff.Test(t, t.Errorf, 3, event.numIndexed()) } +func TestNew(t *testing.T) { + var ( + table = Table{ + Name: "foo", + Cols: []Column{ + Column{Name: "block_num", Type: "numeric"}, + Column{Name: "b", Type: "bytea"}, + Column{Name: "c", Type: "bytea"}, + }, + } + block = []BlockData{ + BlockData{Name: "block_num", Column: "block_num"}, + } + event = Event{ + Name: "bar", + Inputs: []Input{ + Input{Indexed: true, Name: "a"}, + Input{Indexed: true, Name: "b", Column: "b"}, + Input{Indexed: true, Name: "c", Column: "c"}, + }, + } + ) + ig, err := New("foo", event, block, table) + diff.Test(t, t.Errorf, nil, err) + diff.Test(t, t.Errorf, 5, len(ig.Columns)) +} + func TestValidate_AddRequired(t *testing.T) { ig := Integration{ name: "foo",