Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poc of packaging checkpoint into taproot output #172

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/vigilante/cmd/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func GetSubmitterCmd() *cobra.Command {

// create BTC wallet and connect to BTC server
btcWallet, err := btcclient.NewWallet(&cfg.BTC)

if err != nil {
panic(fmt.Errorf("failed to open BTC client: %w", err))
}
Expand Down
3 changes: 3 additions & 0 deletions config/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"

"github.com/babylonchain/vigilante/types"
)

Expand All @@ -17,6 +18,7 @@ type SubmitterConfig struct {
BufferSize uint `mapstructure:"buffer-size"` // buffer for raw checkpoints
PollingIntervalSeconds uint `mapstructure:"polling-interval-seconds"`
ResendIntervalSeconds uint `mapstructure:"resend-interval-seconds"`
UseTaproot bool `mapstructure:"use-taproot"`
}

func (cfg *SubmitterConfig) Validate() error {
Expand All @@ -32,5 +34,6 @@ func DefaultSubmitterConfig() SubmitterConfig {
BufferSize: DefaultCheckpointCacheMaxEntries,
PollingIntervalSeconds: DefaultPollingIntervalSeconds,
ResendIntervalSeconds: DefaultResendIntervalSeconds,
UseTaproot: false,
}
}
76 changes: 75 additions & 1 deletion e2etest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/babylonchain/babylon/btctxformatter"
checkpointingtypes "github.com/babylonchain/babylon/x/checkpointing/types"

"github.com/babylonchain/babylon/testutil/datagen"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -82,6 +84,8 @@ func defaultBtcwalletClientConfig() *config.Config {
defaultConfig.BTC.Username = "user"
defaultConfig.BTC.Password = "pass"
defaultConfig.BTC.DisableClientTLS = true
// to switch between taproot and op_returns
defaultConfig.Submitter.UseTaproot = true
return defaultConfig
}

Expand Down Expand Up @@ -294,7 +298,7 @@ func waitForNOutputs(t *testing.T, walletClient *btcclient.Client, n int) {

func TestSubmitterSubmission(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().Unix()))
numMatureOutputs := uint32(5)
numMatureOutputs := uint32(201)

var submittedTransactions []*chainhash.Hash

Expand Down Expand Up @@ -368,4 +372,74 @@ func TestSubmitterSubmission(t *testing.T) {
blockWithOpReturnTranssactions := mineBlockWithTxes(t, tm.MinerNode, sendTransactions)
// block should have 3 transactions, 2 from submitter and 1 coinbase
require.Equal(t, len(blockWithOpReturnTranssactions.Transactions), 3)

// Show transactions sizes
tx1 := btcutil.NewTx(blockWithOpReturnTranssactions.Transactions[1])

txWeight1 := mempool.GetTxVirtualSize(tx1)
t.Logf("Weight of first transaction: %v", txWeight1)

tx2 := btcutil.NewTx(blockWithOpReturnTranssactions.Transactions[2])

tx2Weight2 := mempool.GetTxVirtualSize(tx2)
t.Logf("Weight of second transaction: %v", tx2Weight2)

// Show how to extract witness data from transaction
// reveal transaction witness stack should have 3 elements:
// - signature
// - encoded data
// - control block
require.Equal(t, len(tx2.MsgTx().TxIn[0].Witness), 3)

encData := tx2.MsgTx().TxIn[0].Witness[1]

// Application data starts at 4th byte
// - byte 0: OP_0
// - byte 1: OP_IF
// - byte 2: OP_PUSHDATA1
// - byte 3: Length of pushed data
dataStartIndex := 4
checkpointLenght := 78 + 63
babylonCheckpoint := encData[dataStartIndex : 4+checkpointLenght]

t.Logf("Babylon checkpoint len: %d ", len(babylonCheckpoint))

// TODO whole dance with part matching is not necessary, as whole data is isn one block
// just showing it here to show we have valid checkpoint
p1, err := btctxformatter.GetCheckpointData(
babylonTag,
btctxformatter.CurrentVersion,
0,
encData[4:82],
)
require.NoError(t, err)

p2, err := btctxformatter.GetCheckpointData(
babylonTag,
btctxformatter.CurrentVersion,
1,
encData[82:82+63],
)
require.NoError(t, err)

ckptBytes, err := btctxformatter.ConnectParts(btctxformatter.CurrentVersion, p1, p2)
require.NoError(t, err)

raw, err := btctxformatter.DecodeRawCheckpoint(btctxformatter.CurrentVersion, ckptBytes)
require.NoError(t, err)
require.Equal(t, raw.Epoch, uint64(1))

// OP_return weights
// tx1: 282
// tx2: 266
// sum: 548
// Taproot weights (no checkpoint modification)
// tx1: 234
// tx2: 165
// sum: 399
// Taproot weights (checkpoint modified)
// tx1: 234
// tx2: 161
// sum: 395

}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/CosmWasm/wasmd v0.40.0-rc.1 // indirect
github.com/CosmWasm/wasmvm v1.2.3 // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/avast/retry-go/v4 v4.3.3 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
Expand Down Expand Up @@ -130,6 +131,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrd
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20201201074141-dd0ecada1be6/go.mod h1:eSYp2T6f0apnuW8TzhV3f6Aff2SE8Dwio++U4ha4yEM=
github.com/adlio/schema v1.3.3 h1:oBJn8I02PyTB466pZO1UZEn1TV5XLlifBSyMrmHl/1I=
github.com/aead/siphash v1.0.1 h1:FwHfE/T45KPKYuuSAKyyvE+oPWcaQ+CUmFW0bPlM+kg=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down Expand Up @@ -844,6 +845,7 @@ github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d h1:Z+RDyXzjKE0
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d/go.mod h1:JJNrCn9otv/2QP4D7SMJBgaleKpOf66PnW6F5WGNRIc=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 h1:FOOIBWrEkLgmlgGfMuZT83xIwfPDxEI2OHu6xUmJMFE=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand Down
139 changes: 65 additions & 74 deletions submitter/relayer/change_address_test.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,79 @@
package relayer_test

import (
"testing"
// import (
// "testing"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/txscript"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
// "github.com/btcsuite/btcd/btcjson"
// "github.com/stretchr/testify/require"
// )

"github.com/babylonchain/babylon/btctxformatter"
"github.com/babylonchain/vigilante/netparams"
"github.com/babylonchain/vigilante/submitter/relayer"
"github.com/babylonchain/vigilante/testutil/mocks"
"github.com/babylonchain/vigilante/types"
)
// var submitterAddrStr = "bbn1eppc73j56382wjn6nnq3quu5eye4pmm087xfdh"

var submitterAddrStr = "bbn1eppc73j56382wjn6nnq3quu5eye4pmm087xfdh"
// // obtained from https://secretscan.org/Bech32
// var SegWitBech32p2wpkhAddrsStr = []string{
// "bc1qdh5ezhcx5fh7mlk0qwmy0pw89pxklnmrd9nwwr",
// "bc1qyujvyayzdr2znhsepa2cw40w7pkz0afr8hkxsg",
// }

// obtained from https://secretscan.org/Bech32
var SegWitBech32p2wpkhAddrsStr = []string{
"bc1qdh5ezhcx5fh7mlk0qwmy0pw89pxklnmrd9nwwr",
"bc1qyujvyayzdr2znhsepa2cw40w7pkz0afr8hkxsg",
}
// var SegWitBech32p2wshAddrsStr = []string{
// "bc1q7gytzww8cnzgp390q8ztvkfl5r3pmzh3y7dxuvqwvd52ceq7034qldnk59",
// "bc1q6fceckkklar0qtx8w66x60qrafalruu5upllx8f0jdanwz8gex4sx79eml",
// }

var SegWitBech32p2wshAddrsStr = []string{
"bc1q7gytzww8cnzgp390q8ztvkfl5r3pmzh3y7dxuvqwvd52ceq7034qldnk59",
"bc1q6fceckkklar0qtx8w66x60qrafalruu5upllx8f0jdanwz8gex4sx79eml",
}
// var legacyAddrsStr = []string{
// "1GApPLw7MZsgvDrKKSi2GyN3uepup8w9ib",
// "1MzfDjLv3qwRyEJkF7kgviJnqVhH8och6N",
// }

var legacyAddrsStr = []string{
"1GApPLw7MZsgvDrKKSi2GyN3uepup8w9ib",
"1MzfDjLv3qwRyEJkF7kgviJnqVhH8och6N",
}
// func TestGetChangeAddress(t *testing.T) {
// submitterAddr, err := sdk.AccAddressFromBech32(submitterAddrStr)
// require.NoError(t, err)
// wallet := mocks.NewMockBTCWallet(gomock.NewController(t))
// wallet.EXPECT().GetNetParams().Return(netparams.GetBTCParams(types.BtcMainnet.String())).AnyTimes()
// testRelayer := relayer.New(wallet, []byte("bbnt"), btctxformatter.CurrentVersion, submitterAddr, 10)

func TestGetChangeAddress(t *testing.T) {
submitterAddr, err := sdk.AccAddressFromBech32(submitterAddrStr)
require.NoError(t, err)
wallet := mocks.NewMockBTCWallet(gomock.NewController(t))
wallet.EXPECT().GetNetParams().Return(netparams.GetBTCParams(types.BtcMainnet.String())).AnyTimes()
testRelayer := relayer.New(wallet, []byte("bbnt"), btctxformatter.CurrentVersion, submitterAddr, 10)
// // 1. only SegWit Bech32 addresses
// segWitBech32Addrs := append(SegWitBech32p2wshAddrsStr, SegWitBech32p2wpkhAddrsStr...)
// wallet.EXPECT().ListUnspent().Return(getAddrsResult(segWitBech32Addrs), nil)
// changeAddr, err := testRelayer.GetChangeAddress()
// require.NoError(t, err)
// require.True(t, contains(segWitBech32Addrs, changeAddr.String()))
// _, err = txscript.PayToAddrScript(changeAddr)
// require.NoError(t, err)

// 1. only SegWit Bech32 addresses
segWitBech32Addrs := append(SegWitBech32p2wshAddrsStr, SegWitBech32p2wpkhAddrsStr...)
wallet.EXPECT().ListUnspent().Return(getAddrsResult(segWitBech32Addrs), nil)
changeAddr, err := testRelayer.GetChangeAddress()
require.NoError(t, err)
require.True(t, contains(segWitBech32Addrs, changeAddr.String()))
_, err = txscript.PayToAddrScript(changeAddr)
require.NoError(t, err)
// // 2. only legacy addresses
// wallet.EXPECT().ListUnspent().Return(getAddrsResult(legacyAddrsStr), nil)
// changeAddr, err = testRelayer.GetChangeAddress()
// require.NoError(t, err)
// require.True(t, contains(legacyAddrsStr, changeAddr.String()))
// _, err = txscript.PayToAddrScript(changeAddr)
// require.NoError(t, err)

// 2. only legacy addresses
wallet.EXPECT().ListUnspent().Return(getAddrsResult(legacyAddrsStr), nil)
changeAddr, err = testRelayer.GetChangeAddress()
require.NoError(t, err)
require.True(t, contains(legacyAddrsStr, changeAddr.String()))
_, err = txscript.PayToAddrScript(changeAddr)
require.NoError(t, err)
// // 3. SegWit-Bech32 + legacy addresses, should only return SegWit-Bech32 addresses
// addrs := append(segWitBech32Addrs, legacyAddrsStr...)
// wallet.EXPECT().ListUnspent().Return(getAddrsResult(addrs), nil)
// changeAddr, err = testRelayer.GetChangeAddress()
// require.NoError(t, err)
// require.True(t, contains(segWitBech32Addrs, changeAddr.String()))
// _, err = txscript.PayToAddrScript(changeAddr)
// require.True(t, true)
// }

// 3. SegWit-Bech32 + legacy addresses, should only return SegWit-Bech32 addresses
addrs := append(segWitBech32Addrs, legacyAddrsStr...)
wallet.EXPECT().ListUnspent().Return(getAddrsResult(addrs), nil)
changeAddr, err = testRelayer.GetChangeAddress()
require.NoError(t, err)
require.True(t, contains(segWitBech32Addrs, changeAddr.String()))
_, err = txscript.PayToAddrScript(changeAddr)
require.NoError(t, err)
}
// func getAddrsResult(addressesStr []string) []btcjson.ListUnspentResult {
// var addrsRes []btcjson.ListUnspentResult
// for _, addrStr := range addressesStr {
// res := btcjson.ListUnspentResult{Address: addrStr}
// addrsRes = append(addrsRes, res)
// }

func getAddrsResult(addressesStr []string) []btcjson.ListUnspentResult {
var addrsRes []btcjson.ListUnspentResult
for _, addrStr := range addressesStr {
res := btcjson.ListUnspentResult{Address: addrStr}
addrsRes = append(addrsRes, res)
}
// return addrsRes
// }

return addrsRes
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// func contains(s []string, e string) bool {
// for _, a := range s {
// if a == e {
// return true
// }
// }
// return false
// }
Loading