Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Jun 14, 2024
1 parent 136c0d9 commit 450cda1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 22 deletions.
43 changes: 22 additions & 21 deletions go.work.sum

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func (s *BabylonSDKTestSuite) Test3BeginBlock() {
s.NoError(err)
}

// TODO: trigger EndBlock via s.ConsumerChain rather than ConsumerApp
func (s *BabylonSDKTestSuite) Test4EndBlock() {
_, err := s.ConsumerApp.BabylonKeeper.EndBlocker(s.ConsumerChain.GetContext())
s.NoError(err)
}

// TearDownSuite runs once after all the suite's tests have been run
func (s *BabylonSDKTestSuite) TearDownSuite() {
// Cleanup code here
Expand Down
Binary file modified tests/testdata/babylon_contract.wasm
Binary file not shown.
Binary file modified tests/testdata/btc_staking.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15cbae22a2cfe7c0590b401ae885c68bb1802530
77a165d570b7f78d315d94f7adde850cd8874547
9 changes: 9 additions & 0 deletions x/babylon/contract/out_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "time"
// SudoMsg is a message sent from the Babylon module to a smart contract
type SudoMsg struct {
BeginBlockMsg *BeginBlock `json:"begin_block,omitempty"`
EndBlockMsg *EndBlock `json:"end_block,omitempty"`
}

type BeginBlock struct {
Expand All @@ -14,3 +15,11 @@ type BeginBlock struct {
ChainID string `json:"chain_id"` // ChainId is the chain ID of the block
AppHashHex string `json:"app_hash_hex"` // AppHashHex is the app hash of the block in hex
}

type EndBlock struct {
Height int64 `json:"height"` // Height is the height of the block
HashHex string `json:"hash_hex"` // HashHex is the hash of the block in hex
Time time.Time `json:"time"` // Time is the time of the block
ChainID string `json:"chain_id"` // ChainId is the chain ID of the block
AppHashHex string `json:"app_hash_hex"` // AppHashHex is the app hash of the block in hex
}
5 changes: 5 additions & 0 deletions x/babylon/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ func (k *Keeper) BeginBlocker(ctx sdk.Context) error {
// EndBlocker is called after every block
func (k *Keeper) EndBlocker(ctx sdk.Context) ([]abci.ValidatorUpdate, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

if err := k.SendEndBlockMsg(ctx); err != nil {
return []abci.ValidatorUpdate{}, err
}

return []abci.ValidatorUpdate{}, nil
}
26 changes: 26 additions & 0 deletions x/babylon/keeper/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ func (k Keeper) SendBeginBlockMsg(ctx sdk.Context) error {
return k.doSudoCall(ctx, addr, msg)
}

// SendEndBlockMsg sends a EndBlock sudo message to the BTC staking contract via sudo
func (k Keeper) SendEndBlockMsg(ctx sdk.Context) error {
// get address of the BTC staking contract
addrStr := k.GetParams(ctx).BtcStakingContractAddress
if len(addrStr) == 0 {
// the BTC staking contract address is not set yet, skip sending EndBlockMsg
return nil
}
addr := sdk.MustAccAddressFromBech32(addrStr)

// construct the sudo message
headerInfo := ctx.HeaderInfo()
msg := contract.SudoMsg{
EndBlockMsg: &contract.EndBlock{
Height: headerInfo.Height,
HashHex: hex.EncodeToString(headerInfo.Hash),
Time: headerInfo.Time,
ChainID: headerInfo.ChainID,
AppHashHex: hex.EncodeToString(headerInfo.AppHash),
},
}

// send the sudo call
return k.doSudoCall(ctx, addr, msg)
}

// caller must ensure gas limits are set proper and handle panics
func (k Keeper) doSudoCall(ctx sdk.Context, contractAddr sdk.AccAddress, msg contract.SudoMsg) error {
bz, err := json.Marshal(msg)
Expand Down

0 comments on commit 450cda1

Please sign in to comment.