-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: withold gravity batches until valset update (#1195)
# Related Github tickets - VolumeFi#1464 # Background This change adds a new constraint when handing out gravity batches to be relayed. They will now be withheld while valset updates are pending on the target chain, just like with SLC. I had to cut some corners to get this out sooner rather than later, but I also added a more decentralised event bus system for decoupling systems. It's currently very bare bone and as singleton hard to test, but if it grows, we can refactor into something more injectible. # Testing completed - [x] test coverage exists or has been added/updated - [ ] tested in a private testnet # Breaking changes - [x] I have checked my code for breaking changes - [x] If there are breaking changes, there is a supporting migration.
- Loading branch information
1 parent
3ce9062
commit 91cd199
Showing
15 changed files
with
212 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package eventbus | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/palomachain/paloma/util/liblog" | ||
) | ||
|
||
var gravityBatchBuilt = newEvent[GravityBatchBuiltEvent]() | ||
|
||
type ( | ||
EventHandler[E any] func(context.Context, E) error | ||
Event[E any] struct { | ||
subscribers map[string]EventHandler[E] | ||
} | ||
) | ||
|
||
func newEvent[E any]() Event[E] { | ||
return Event[E]{ | ||
subscribers: make(map[string]EventHandler[E]), | ||
} | ||
} | ||
|
||
func (e Event[E]) Publish(ctx context.Context, event E) { | ||
keys := make([]string, 0, len(e.subscribers)) | ||
for k := range e.subscribers { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
for _, k := range keys { | ||
if e.subscribers[k] != nil { | ||
logger := liblog.FromSDKLogger(sdk.UnwrapSDKContext(ctx).Logger()). | ||
WithComponent("eventbus"). | ||
WithFields("event", event). | ||
WithFields("subscriber", k) | ||
logger.Debug("Handling event") | ||
if err := e.subscribers[k](ctx, event); err != nil { | ||
logger.WithError(err).Error("Failed to handle event") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (e Event[E]) Subscribe(id string, fn EventHandler[E]) { | ||
e.subscribers[id] = fn | ||
} | ||
|
||
func (e Event[E]) Unsubscribe(id string) { | ||
e.subscribers[id] = nil | ||
} | ||
|
||
type GravityBatchBuiltEvent struct { | ||
ChainReferenceID string | ||
} | ||
|
||
func GravityBatchBuilt() *Event[GravityBatchBuiltEvent] { | ||
return &gravityBatchBuilt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package eventbus_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"cosmossdk.io/log" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/palomachain/paloma/util/eventbus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEventBus(t *testing.T) { | ||
ctx := sdk.Context{}. | ||
WithLogger(log.NewNopLogger()). | ||
WithContext(context.Background()) | ||
eventbus.GravityBatchBuilt().Publish(ctx, eventbus.GravityBatchBuiltEvent{ | ||
ChainReferenceID: "test-chain", | ||
}) | ||
|
||
calls := make(map[string]int) | ||
fn := func(_ context.Context, e eventbus.GravityBatchBuiltEvent) error { | ||
calls[e.ChainReferenceID]++ | ||
return nil | ||
} | ||
|
||
eventbus.GravityBatchBuilt().Subscribe("test-1", fn) | ||
require.Len(t, calls, 0, "should be empty") | ||
|
||
eventbus.GravityBatchBuilt().Publish(ctx, eventbus.GravityBatchBuiltEvent{ | ||
ChainReferenceID: "test-chain", | ||
}) | ||
|
||
require.NotNil(t, calls["test-chain"], "should have executed one.") | ||
require.Equal(t, 1, calls["test-chain"], "should have executed one.") | ||
|
||
eventbus.GravityBatchBuilt().Subscribe("test-2", fn) | ||
eventbus.GravityBatchBuilt().Publish(ctx, eventbus.GravityBatchBuiltEvent{ | ||
ChainReferenceID: "test-chain", | ||
}) | ||
require.Equal(t, 3, calls["test-chain"], "should execute both subscribers.") | ||
|
||
eventbus.GravityBatchBuilt().Unsubscribe("test-1") | ||
eventbus.GravityBatchBuilt().Publish(ctx, eventbus.GravityBatchBuiltEvent{ | ||
ChainReferenceID: "test-chain", | ||
}) | ||
require.Equal(t, 4, calls["test-chain"], "should have removed one subscriber.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
package types | ||
|
||
const ConsensusTurnstoneMessage = "evm-turnstone-message" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.