Skip to content

Commit

Permalink
fix: make sure the headers form a correct chain of btc blocks in the …
Browse files Browse the repository at this point in the history
…upgrade
  • Loading branch information
RafilxTenfen committed Aug 1, 2024
1 parent 801ac1f commit 3c9ae61
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 68 deletions.
118 changes: 62 additions & 56 deletions app/upgrades/signetlaunch/data_btc_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,94 @@ package signetlaunch
const NewBtcHeadersStr = `{
"btc_headers": [
{
"header": "0000002095156a30ef4e6c9764c0ce83fac51a37365b2092bd39bdbfd038f5b98f00000077f14ee2b34b7dcbb7e3fc624184b919b74cd047837728593140ee21e59cc5bee4c9a766644d011e4e202800",
"hash": "0000010a767aed4930b7cf4aa0f90b4cc23d8e5375142c0a626aba09ff411f06",
"height": 206500,
"work": "180357618"
"header": "00000020f61eee3b63a380a477a063af32b2bbc97c9ff9f01f2c4225e973988108000000f575c83235984e7dc4afc1f30944c170462e84437ab6f2d52e16878a79e4678bd1914d5fae77031eccf40700",
"hash": "00000086d6b2636cb2a392d45edc4ec544a10024d30141c9adf4bfd9de533b53",
"height": 1,
"work": "77414720"
},
{
"header": "00000020061f41ff09ba6a620a2c1475538e3dc24c0bf9a04acfb73049ed7a760a0100004a7b4cec7be4c56a3dfdb833e94a5ff5ce4bdf5b5b0a599a39ef717fffe914b4ecd0a766644d011e822f6102",
"hash": "0000000a36d6c67675cbbbf1f8dc5cafd5def468b5b5e063768ddc3147e065d7",
"height": 206501,
"work": "180357618"
"header": "00000020533b53ded9bff4adc94101d32400a144c54edc5ed492a3b26c63b2d686000000b38fef50592017cfafbcab88eb3d9cf50b2c801711cad8299495d26df5e54812e7914d5fae77031ecfdd0b00",
"hash": "00000032bb881de703dcc968e8258080c7ed4a2933e3a35888fa0b2f75f36029",
"height": 2,
"work": "77414720"
},
{
"header": "00000020d765e04731dc8d7663e0b5b568f4ded5af5cdcf8f1bbcb7576c6d6360a000000fb1a404fe53effdb9c0769966614fbe7cd7d7d537068339fc38a4c41e14ed6b1bfd1a766644d011eddc8aa00",
"hash": "0000008a5a47ffe1e621ee2d4c8f4baeef953da8e915d6837ccbaa7a7b877d2c",
"height": 206502,
"work": "180357618"
"header": "000000202960f3752f0bfa8858a3e333294aedc7808025e868c9dc03e71d88bb320000007765fcd3d5b4966beb338bba2675dc2cf2ad28d4ad1d83bdb6f286e7e27ac1f807924d5fae77031e81d60b00",
"hash": "000000e8daac2a2e973ecaab46dc948181c638adecf5ae0fd5d3e13aa14364b0",
"height": 3,
"work": "77414720"
},
{
"header": "000000202c7d877b7aaacb7c83d615e9a83d95efae4b8f4c2dee21e6e1ff475a8a000000aa2d38699ab5195fb8d75512e639f3467aacb0346bf5478d529c969c6aa398fb64d3a766644d011ee2dc5f01",
"hash": "000000c7b179e7283c8641d1d1ff6a0b202213a19463490a79163aef0ccfad1e",
"height": 206503,
"work": "180357618"
"header": "00000020b06443a13ae1d3d50faef5ecad38c6818194dc46abca3e972e2aacdae800000069a5829097e80fee00ac49a56ea9f82d741a6af84d32b3bc455cf31871e2a8ac27924d5fae77031e9c910500",
"hash": "00000194763f1233e40afd5f6eb2e64abd2e5e158a4139198a224f8a7503d47e",
"height": 4,
"work": "77414720"
},
{
"header": "000000201eadcf0cef3a16790a496394a11322200b6affd1d141863c28e779b1c70000006b58a84c6d1ba0c1841592fd5855f54e354e2b2a4d9467f57c9a22cb7b8a6b4984d3a766644d011e50d96c01",
"hash": "000000fc83896a94e89aae093c648bac3841de08a0dbe4ab1719db22796442b8",
"height": 206504,
"work": "180357618"
"header": "000000207ed403758a4f228a1939418a155e2ebd4ae6b26e5ffd0ae433123f7694010000542e80b609c5bc58af5bdf492e26d4f60cd43a3966c2e063c50444c29b3757a636924d5fae77031ee8601d00",
"hash": "000003776c984edd753b876f0e70d08450f92dd76a768df03d2c42d1880aea5b",
"height": 5,
"work": "77414720"
},
{
"header": "00000020b842647922db1917abe4dba008de4138ac8b643c09ae9ae8946a8983fc0000001e2eafc39aed81b433b9dc5fbf4916460f414c9333ace37bb99a300950ff76241fd5a766644d011ecffa8c00",
"hash": "000000a839a452cfa49121973e1ecd17838b74fd3238abfc5e28a8d04fa76fd9",
"height": 206505,
"work": "180357618"
"header": "000000205bea0a88d1422c3df08d766ad72df95084d0700e6f873b75dd4e986c7703000002b57516d33ed60c2bdd9f93d6d5614083324c837e68e5ba6e04287a7285633585924d5fae77031ed1719600",
"hash": "000001b62ff822d0b1c68e231670eba2cf8d494075f9616447194b370db6f3da",
"height": 6,
"work": "77414720"
},
{
"header": "00000020d96fa74fd0a8285efcab3832fd748b8317cd1e3e972191a4cf52a439a800000092e19ee24b41b4debca252714de700a8573c610ad277866abd238ae527d899d40dd6a766644d011e053a5c00",
"hash": "000000edb41b0c424a7ffbbaf5704daf494d9209013ca10b3f76d66610d77d99",
"height": 206506,
"work": "180357618"
"header": "00000020daf3b60d374b19476461f97540498dcfa2eb7016238ec6b1d022f82fb60100007a7ae65b53cb988c2ec92d2384996713821d5645ffe61c9acea60da75cd5edfa1a944d5fae77031e9dbb0500",
"hash": "000000b6f018c5b53240616c7868eab7e1338de4200ec25b65a5e1c2f3c57c45",
"height": 7,
"work": "77414720"
},
{
"header": "00000020997dd71066d6763f0ba13c0109924d49af4d70f5bafb7f4a420c1bb4ed000000fa2603a7097ff5d5fb2ca0b5dc33fce8ae8cdc44916b736cabdeef8abab05a7b38d6a766644d011ee7bbe900",
"hash": "0000006516e2ff853d1fc48e476df001420e7e5b740ea178b7edc5f557e27c58",
"height": 206507,
"work": "180357618"
"header": "00000020457cc5f3c2e1a5655bc20e20e48d33e1b7ea68786c614032b5c518f0b6000000541f36942d82c6e7248275ff15c8933487fbe1819c67a9ecc0f4b70bb7e6cf672a944d5fae77031e8f398600",
"hash": "000002213230ed813e42076f753f666244db421b3e36a3a3ba31384feb61eba2",
"height": 8,
"work": "77414720"
},
{
"header": "00000020587ce257f5c5edb778a10e745b7e0e4201f06d478ec41f3d85ffe21665000000cde05d3c4355c5aef220d5b16ffb8339ae979c513f8af0bc5c74d21ac8a4fd2e45d7a766644d011eabacbe00",
"hash": "0000001ec310dc0a5113ae10f00d944f01562c0a4c50c8ee41ff00b164cb81cc",
"height": 206508,
"work": "180357618"
"header": "00000020a2eb61eb4f3831baa3a3363e1b42db4462663f756f07423e81ed30322102000077224de7dea0f8d0ec22b1d2e2e255f0a987b96fe7200e1a2e6373f48a2f5b7894954d5fae77031e36867e00",
"hash": "000001e163d0a8e294c3406a713ba4363f422f1322a1d6ab6de4e54b51e868a8",
"height": 9,
"work": "77414720"
},
{
"header": "00000020cc81cb64b100ff41eec8504c0a2c56014f940df010ae13510adc10c31e0000006433905cd152679dc2620d09a44203589743d88c181a0342811225ff823b484f85d8a766644d011e077d7300",
"hash": "000000e04fd41669fac0a6fbf595e795d841ca1f1f3df82410b7be8d8956c0e0",
"height": 206509,
"work": "180357618"
"header": "00000020a868e8514be5e46dabd6a122132f423f36a43b716a40c394e2a8d063e1010000f4c6c717e99d800c699c25a2006a75a0c5c09f432a936f385e6fce139cdbd1a5e9964d5fae77031e7d026e00",
"hash": "000001418e9af8310ef3644093dbc4a8ad9edb0c9ac80397f3479ad1729d965b",
"height": 10,
"work": "77414720"
},
{
"header": "00000020e0c056898dbeb71024f83d1f1fca41d895e795f5fba6c0fa6916d44fe0000000dfb1582ec3ecf5c35a7ca4a75624d17517373371923a19dc1729f62f24402f0371daa766644d011e605fcd00",
"hash": "000000dc9ea403aadca79175d75ab6a09f21178955e1aa92ad134d1f35639317",
"height": 206510,
"work": "180357618"
"header": "000000205b969d72d19a47f39703c89a0cdb9eada8c4db934064f30e31f89a8e41010000949eef89068ffc76bf4dca6762e26581d410d0df40edf147d4ffdc6dea404a1512984d5fae77031ee67c1200",
"hash": "0000034cdfe52a76f70f137be5b44dd2d10b305d03a42229767ec5b524d81094",
"height": 11,
"work": "77414720"
},
{
"header": "00000020179363351f4d13ad92aae1558917219fa0b65ad77591a7dcaa03a49edc0000007b03a2a905ac70c86e3568978c8a1e5f4aa720999d3dbd2e2bdfffbc3b7ecf6306dba766644d011ed6383600",
"hash": "0000008cb36ab72c793033a25ccb5ea266a63c6d8a3a8774db9574bd2f099a26",
"height": 206511,
"work": "180357618"
"header": "000000209410d824b5c57e762922a4035d300bd1d24db4e57b130ff7762ae5df4c030000532299955b2dc6bd7c13c267d3c0990fefdf7aec3bcbab5b2c85d0d36316f93644984d5fae77031ecdea1600",
"hash": "000000375d267acf5e788946bd885735d215a98e13eb1b1dcb79403a4549f609",
"height": 12,
"work": "77414720"
},
{
"header": "00000020269a092fbd7495db74873a8a6d3ca666a25ecb5ca23330792cb76ab38c000000d6ad13ceaa5c27b1e1e2d68b0080cc40b82b44faf895e3f819edfae1709aa74587dba766644d011e93fc8a00",
"hash": "000000e698603e02a0ae61186f6b6294f8f58732bcfb1bf7c5c8cc8c25b17134",
"height": 206512,
"work": "180357618"
"header": "0000002009f649453a4079cb1d1beb138ea915d2355788bd4689785ecf7a265d3700000010bd26b43a88350e614736674431e62cc7c77dc577d07edd80620a02339d5fab82984d5fae77031efe682400",
"hash": "000000aa98270ecd860f5b0f34eed287b66a24b80738eeb9b5e7a65791ffb935",
"height": 13,
"work": "77414720"
},
{
"header": "000000203471b1258cccc8c5f71bfbbc3287f5f894626b6f1861aea0023e6098e60000002d05f3d038ee2535da13f928afa100e9dac12598c11afc19b0e952a22bc924d1ecdba766644d011e5eb27d00",
"hash": "0000001ea82bcd9bb5ada7c186a3f1b02413fd0e5d873e44825ca628e5511496",
"height": 206513,
"work": "180357618"
"header": "0000002035b9ff9157a6e7b5b9ee3807b8246ab687d2ee340f5b0f86cd0e2798aa00000028ef48260b3c0d45bbe5321335b05dde8fcb130e063202457884585298b8a5dde4984d5fae77031ec0a08600",
"hash": "00000150de45736bb5158f63a94b9570540d68871eaa8fa03141d16ffb2f1086",
"height": 14,
"work": "77414720"
},
{
"header": "0000002086102ffb6fd14131a08faa1e87680d5470954ba9638f15b56b7345de500100009f423c63aa6d39330082b58808013a85af5a7f338a7a3587f0a85b587665e6174e9a4d5fae77031e79353a00",
"hash": "0000013aadf507653857ebf15a210e31aa7be6d02d1608602a1d4906a08cf528",
"height": 15,
"work": "77414720"
}
]
}`
22 changes: 15 additions & 7 deletions app/upgrades/signetlaunch/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/babylonlabs-io/babylon/app/keepers"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/app/upgrades"
bbn "github.com/babylonlabs-io/babylon/types"
btclightkeeper "github.com/babylonlabs-io/babylon/x/btclightclient/keeper"
btclighttypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
)
Expand Down Expand Up @@ -82,23 +83,30 @@ func LoadBTCHeadersFromData() ([]*btclighttypes.BTCHeaderInfo, error) {
func insertBtcHeaders(
ctx sdk.Context,
k *btclightkeeper.Keeper,
headers []*btclighttypes.BTCHeaderInfo,
btcHeaders []*btclighttypes.BTCHeaderInfo,
) error {
if len(headers) == 0 {
if len(btcHeaders) == 0 {
return errors.New("no headers to insert")
}

// sort by height to make sure it is deterministic
sort.Slice(headers, func(i, j int) bool {
return headers[i].Height > headers[j].Height
sort.Slice(btcHeaders, func(i, j int) bool {
return btcHeaders[i].Height <= btcHeaders[j].Height
})

for _, header := range headers {
if err := header.Validate(); err != nil {
headersBytes := make([]bbn.BTCHeaderBytes, len(btcHeaders))
for i, btcHeader := range btcHeaders {
h := btcHeader
if err := h.Validate(); err != nil {
return err
}

headersBytes[i] = *h.Header
}

if err := k.InsertHeaders(ctx, headersBytes); err != nil {
return err
}
k.InsertHeaderInfos(ctx, headers)

allBlocks := k.GetMainChainFromWithLimit(ctx, 0, 1)
isRetarget := btclighttypes.IsRetargetBlock(allBlocks[0], &chaincfg.SigNetParams)
Expand Down
34 changes: 29 additions & 5 deletions app/upgrades/signetlaunch/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package signetlaunch_test

import (
"bytes"
"fmt"
"testing"
"time"
Expand All @@ -11,6 +12,8 @@ import (
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/babylonlabs-io/babylon/app"
v1 "github.com/babylonlabs-io/babylon/app/upgrades/signetlaunch"
"github.com/babylonlabs-io/babylon/x/btclightclient"
btclighttypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
Expand All @@ -36,13 +39,31 @@ func (s *UpgradeTestSuite) SetupTest() {
s.app = app.Setup(s.T(), false)
s.ctx = s.app.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "babylon-1", Time: time.Now().UTC()})
s.preModule = upgrade.NewAppModule(s.app.UpgradeKeeper, s.app.AccountKeeper.AddressCodec())

var btcHeaderZero btclighttypes.BTCHeaderInfo
// signet btc header 0
btcHeaderZeroStr := `{
"header": "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a008f4d5fae77031e8ad22203",
"hash": "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6",
"work": "77414720"
}`
buff := bytes.NewBufferString(btcHeaderZeroStr)

err := s.app.EncodingConfig().Codec.UnmarshalJSON(buff.Bytes(), &btcHeaderZero)
s.NoError(err)

k := s.app.BTCLightClientKeeper
btclightclient.InitGenesis(s.ctx, s.app.BTCLightClientKeeper, btclighttypes.GenesisState{
Params: k.GetParams(s.ctx),
BtcHeaders: []*btclighttypes.BTCHeaderInfo{&btcHeaderZero},
})
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

func (s *UpgradeTestSuite) TestUpgradePayments() {
func (s *UpgradeTestSuite) TestUpgrade() {
oldHeadersLen := 0

testCases := []struct {
Expand Down Expand Up @@ -80,15 +101,18 @@ func (s *UpgradeTestSuite) TestUpgradePayments() {
// ensure the btc headers were added
allBtcHeaders := s.app.BTCLightClientKeeper.GetMainChainFrom(s.ctx, 0)

btcHeaders, err := v1.LoadBTCHeadersFromData()
btcHeadersInserted, err := v1.LoadBTCHeadersFromData()
s.NoError(err)
lenHeadersInserted := len(btcHeaders)
lenHeadersInserted := len(btcHeadersInserted)

newHeadersLen := len(allBtcHeaders)
s.Equal(newHeadersLen, oldHeadersLen+lenHeadersInserted)

// ensure the headers were inserted at the end
s.Equal(allBtcHeaders[newHeadersLen-lenHeadersInserted:], btcHeaders)
// ensure the headers were inserted as expected
for i, btcHeaderInserted := range btcHeadersInserted {
btcHeaderInState := allBtcHeaders[oldHeadersLen+i]
s.True(btcHeaderInserted.Eq(btcHeaderInState))
}
},
},
}
Expand Down

0 comments on commit 3c9ae61

Please sign in to comment.