Skip to content

Commit 4e40f52

Browse files
authored
Merge pull request #6535 from oasisprotocol/peternose/trivial/rename-msg-execution
go/consensus/cometbft/api: Remove message execution from context
2 parents 8fc03cb + ed64c05 commit 4e40f52

24 files changed

Lines changed: 208 additions & 144 deletions

.changelog/6535.trivial.md

Whitespace-only changes.

go/consensus/cometbft/abci/messages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func (md *messageDispatcher) Subscribe(kind any, ms api.MessageSubscriber) {
2525
}
2626

2727
// Implements api.MessageDispatcher.
28-
func (md *messageDispatcher) Publish(ctx *api.Context, kind, msg any) (any, error) {
28+
func (md *messageDispatcher) Publish(ctx *api.Context, msg api.Message) (any, error) {
2929
var (
3030
result any
3131
errs error
3232
numSubscribers int
3333
)
34-
for _, ms := range md.subscriptions[kind] {
34+
for _, ms := range md.subscriptions[msg.Kind] {
3535
// Check whether the subscriber can be toggled.
3636
if togMs, ok := ms.(api.TogglableMessageSubscriber); ok {
3737
enabled, err := togMs.Enabled(ctx)
@@ -47,7 +47,7 @@ func (md *messageDispatcher) Publish(ctx *api.Context, kind, msg any) (any, erro
4747
numSubscribers++
4848

4949
// Deliver the message.
50-
if resp, err := ms.ExecuteMessage(ctx, kind, msg); err != nil {
50+
if resp, err := ms.ExecuteMessage(ctx, msg); err != nil {
5151
errs = errors.Join(errs, err)
5252
} else {
5353
switch {

go/consensus/cometbft/abci/messages_test.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type testSubscriber struct {
3333
}
3434

3535
// Implements api.MessageSubscriber.
36-
func (s *testSubscriber) ExecuteMessage(_ *api.Context, _, msg any) (any, error) {
37-
switch m := msg.(type) {
36+
func (s *testSubscriber) ExecuteMessage(_ *api.Context, msg api.Message) (any, error) {
37+
switch m := msg.Data.(type) {
3838
case *testMessage:
3939
s.msgs = append(s.msgs, m.foo)
4040
if s.fail {
@@ -66,40 +66,58 @@ func TestMessageDispatcher(t *testing.T) {
6666
md := newMessageDispatcher()
6767

6868
// Publish without subscribers should work.
69-
res, err := md.Publish(ctx, testMessageA, &testMessage{foo: 42})
69+
res, err := md.Publish(ctx, api.Message{
70+
Kind: testMessageA,
71+
Data: &testMessage{foo: 42},
72+
})
7073
require.Error(err, "Publish")
7174
require.Equal(api.ErrNoSubscribers, err)
7275
require.Nil(res, "Publish results should be empty")
7376

7477
// With a disabled subscriber should behave same as with no subscribers.
7578
var ms testSubscriber
7679
md.Subscribe(testMessageA, &ms)
77-
res, err = md.Publish(ctx, testMessageA, &testMessage{foo: 42})
80+
res, err = md.Publish(ctx, api.Message{
81+
Kind: testMessageA,
82+
Data: &testMessage{foo: 42},
83+
})
7884
require.Error(err, "Publish")
7985
require.Equal(api.ErrNoSubscribers, err)
8086
require.Nil(res, "Publish results should be empty")
8187
require.Empty(ms.msgs, "no messages should be delivered when subscriber is disabled")
8288

8389
// With an enabled subscriber.
8490
ms.enabled = true
85-
res, err = md.Publish(ctx, testMessageA, &testMessage{foo: 42})
91+
res, err = md.Publish(ctx, api.Message{
92+
Kind: testMessageA,
93+
Data: &testMessage{foo: 42},
94+
})
8695
require.NoError(err, "Publish")
8796
require.EqualValues(int32(42), res, "correct publish message result")
8897
require.EqualValues([]int32{42}, ms.msgs, "correct messages should be delivered")
8998

90-
res, err = md.Publish(ctx, testMessageA, &testMessage{foo: 43})
99+
res, err = md.Publish(ctx, api.Message{
100+
Kind: testMessageA,
101+
Data: &testMessage{foo: 43},
102+
})
91103
require.NoError(err, "Publish")
92104
require.EqualValues(int32(43), res, "correct publish message result")
93105
require.EqualValues([]int32{42, 43}, ms.msgs, "correct messages should be delivered")
94106

95-
res, err = md.Publish(ctx, testMessageB, &testMessage{foo: 44})
107+
res, err = md.Publish(ctx, api.Message{
108+
Kind: testMessageB,
109+
Data: &testMessage{foo: 44},
110+
})
96111
require.Error(err, "Publish")
97112
require.Equal(api.ErrNoSubscribers, err)
98113
require.Nil(res, "publish message results should be empty")
99114
require.EqualValues([]int32{42, 43}, ms.msgs, "correct messages should be delivered")
100115

101116
// Returning an error.
102-
res, err = md.Publish(ctx, testMessageA, &errorMessage{})
117+
res, err = md.Publish(ctx, api.Message{
118+
Kind: testMessageA,
119+
Data: &errorMessage{},
120+
})
103121
require.Error(err, "Publish")
104122
require.Nil(nil, res, "publish message results should be empty")
105123
require.True(errors.Is(err, errTest), "returned error should be the correct one")
@@ -110,7 +128,10 @@ func TestMessageDispatcher(t *testing.T) {
110128
noResult: true,
111129
}
112130
md.Subscribe(testMessageA, &ms2)
113-
res, err = md.Publish(ctx, testMessageA, &testMessage{foo: 44})
131+
res, err = md.Publish(ctx, api.Message{
132+
Kind: testMessageA,
133+
Data: &testMessage{foo: 44},
134+
})
114135
require.NoError(err, "Publish")
115136
require.EqualValues(int32(44), res, "correct publish message result")
116137
require.EqualValues([]int32{42, 43, 44}, ms.msgs, "correct messages should be delivered")
@@ -119,6 +140,9 @@ func TestMessageDispatcher(t *testing.T) {
119140
// Multiple subscribers returning results.
120141
ms2.noResult = false
121142
require.Panics(func() {
122-
_, _ = md.Publish(ctx, testMessageA, &testMessage{foo: 44})
143+
_, _ = md.Publish(ctx, api.Message{
144+
Kind: testMessageA,
145+
Data: &testMessage{foo: 44},
146+
})
123147
}, "multiple subscribers returning results should panic")
124148
}

go/consensus/cometbft/abci/mux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,9 @@ func (mux *abciMux) finishInitialization() error {
945945
ctx := mux.state.NewContext(api.ContextEndBlock)
946946
defer ctx.Close()
947947

948-
if _, err := mux.md.Publish(ctx, api.MessageStateSyncCompleted, nil); err != nil {
948+
if _, err := mux.md.Publish(ctx, api.Message{
949+
Kind: api.MessageStateSyncCompleted,
950+
}); err != nil {
949951
mux.logger.Error("failed to dispatch state sync completed message",
950952
"err", err,
951953
)

go/consensus/cometbft/abci/snapshots.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ func (mux *abciMux) ApplySnapshotChunk(req types.RequestApplySnapshotChunk) type
270270
ctx := mux.state.NewContext(api.ContextEndBlock)
271271
defer ctx.Close()
272272

273-
if _, err = mux.md.Publish(ctx, api.MessageStateSyncCompleted, nil); err != nil {
273+
if _, err = mux.md.Publish(ctx, api.Message{
274+
Kind: api.MessageStateSyncCompleted,
275+
}); err != nil {
274276
mux.logger.Error("failed to dispatch state sync completed message",
275277
"err", err,
276278
)

go/consensus/cometbft/abci/subcall.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
const maxSubcallDepth = 8
1212

1313
// ExecuteMessage implements api.MessageSubscriber.
14-
func (mux *abciMux) ExecuteMessage(ctx *api.Context, kind, msg any) (any, error) {
15-
switch kind {
14+
func (mux *abciMux) ExecuteMessage(ctx *api.Context, msg api.Message) (any, error) {
15+
switch msg.Kind {
1616
case api.MessageExecuteSubcall:
1717
// Subcall execution request.
18-
info, ok := msg.(*api.SubcallInfo)
18+
info, ok := msg.Data.(*api.SubcallInfo)
1919
if !ok {
2020
return nil, fmt.Errorf("invalid subcall info")
2121
}

go/consensus/cometbft/api/app.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ import (
1212
// ErrNoSubscribers is the error returned when publishing a message that noone is subscribed to.
1313
var ErrNoSubscribers = errors.New("no subscribers to given message kind")
1414

15+
// Message is a message sent between applications.
16+
type Message struct {
17+
// Sender identifies the application that sent the message.
18+
Sender string
19+
// Kind specifies the receiver-defined message type.
20+
Kind any
21+
// Data contains the receiver-defined message data.
22+
Data any
23+
}
24+
1525
// MessageSubscriber is a message subscriber interface.
1626
type MessageSubscriber interface {
1727
// ExecuteMessage executes a given message.
18-
ExecuteMessage(ctx *Context, kind, msg any) (any, error)
28+
ExecuteMessage(ctx *Context, msg Message) (any, error)
1929
}
2030

2131
// TogglableMessageSubscriber is a message subscriber that can be disabled.
@@ -29,13 +39,12 @@ type MessageDispatcher interface {
2939
// Subscribe subscribes a given message subscriber to messages of a specific kind.
3040
Subscribe(kind any, ms MessageSubscriber)
3141

32-
// Publish publishes a message of a given kind by dispatching to all subscribers.
33-
// Subscribers can return a result, but at most one subscriber should return a
34-
// non-nil result to any published message. Panics in case more than one subscriber
35-
// returns a non-nil result.
42+
// Publish publishes a message to all subscribers./ Subscribers can return a result,
43+
// but at most one subscriber should return a non-nil result to any published message.
44+
// Panics in case more than one subscriber returns a non-nil result.
3645
//
3746
// In case there are no subscribers ErrNoSubscribers is returned.
38-
Publish(ctx *Context, kind, msg any) (any, error)
47+
Publish(ctx *Context, msg Message) (any, error)
3948
}
4049

4150
// NoopMessageDispatcher is a no-op message dispatcher that performs no dispatch.
@@ -46,7 +55,7 @@ func (nd *NoopMessageDispatcher) Subscribe(any, MessageSubscriber) {
4655
}
4756

4857
// Publish implements MessageDispatcher.
49-
func (nd *NoopMessageDispatcher) Publish(*Context, any, any) (any, error) {
58+
func (nd *NoopMessageDispatcher) Publish(*Context, Message) (any, error) {
5059
return nil, nil
5160
}
5261

go/consensus/cometbft/api/context.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ type Context struct {
6565
mode ContextMode
6666
currentTime time.Time
6767

68-
isMessageExecution bool
69-
isTransaction bool
68+
isTransaction bool
7069

7170
data any
7271
events []types.Event
@@ -217,22 +216,21 @@ func (c *Context) CallDepth() int {
217216

218217
// NewChild creates a new child context that shares state with the current context.
219218
//
220-
// If you want isolated state and events use NewTransaction instad.
219+
// If you want isolated state and events use NewTransaction instead.
221220
func (c *Context) NewChild() *Context {
222221
cc := &Context{
223-
parent: c,
224-
mode: c.mode,
225-
currentTime: c.currentTime,
226-
isMessageExecution: c.isMessageExecution,
227-
gasAccountant: c.gasAccountant,
228-
txSigner: c.txSigner,
229-
callerAddress: c.callerAddress,
230-
appState: c.appState,
231-
state: c.state,
232-
lastHeight: c.lastHeight,
233-
blockCtx: c.blockCtx,
234-
initialHeight: c.initialHeight,
235-
logger: c.logger,
222+
parent: c,
223+
mode: c.mode,
224+
currentTime: c.currentTime,
225+
gasAccountant: c.gasAccountant,
226+
txSigner: c.txSigner,
227+
callerAddress: c.callerAddress,
228+
appState: c.appState,
229+
state: c.state,
230+
lastHeight: c.lastHeight,
231+
blockCtx: c.blockCtx,
232+
initialHeight: c.initialHeight,
233+
logger: c.logger,
236234
}
237235
cc.Context = context.WithValue(c.Context, contextKey{}, cc)
238236
return cc
@@ -295,13 +293,6 @@ func (c *Context) WithSimulation() *Context {
295293
return child
296294
}
297295

298-
// WithMessageExecution creates a child context and sets the message execution flag.
299-
func (c *Context) WithMessageExecution() *Context {
300-
child := c.NewChild()
301-
child.isMessageExecution = true
302-
return child
303-
}
304-
305296
// IsInitChain returns true if this ia an init chain context.
306297
func (c *Context) IsInitChain() bool {
307298
return c.mode == ContextInitChain
@@ -317,11 +308,6 @@ func (c *Context) IsSimulation() bool {
317308
return c.mode == ContextSimulateTx
318309
}
319310

320-
// IsMessageExecution returns true if this is a message execution context.
321-
func (c *Context) IsMessageExecution() bool {
322-
return c.isMessageExecution
323-
}
324-
325311
// EmitData emits data to be serialized as transaction output.
326312
//
327313
// Note: The use of this has mostly been replaced with EmitEvent, please

go/consensus/cometbft/api/context_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ func TestChildContext(t *testing.T) {
8484
child.EmitEvent(NewEventBuilder("test").TypedAttribute(&FooEvent{Bar: []byte("bar")}))
8585
child.Close()
8686
require.Empty(ctx.GetEvents(), "events should not propagate in simulation mode")
87-
88-
child = ctx.WithMessageExecution()
89-
require.True(child.IsMessageExecution(), "child should have message execution enabled")
90-
require.False(ctx.IsMessageExecution(), "parent should not have message execution enabled")
91-
child.Close()
9287
}
9388

9489
func TestTransactionContext(t *testing.T) {

0 commit comments

Comments
 (0)