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

Adds Ethereum signatures verification + refactor the orchestrator/relayer for testing #314

Merged
merged 19 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 18 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
8 changes: 2 additions & 6 deletions contrib/single-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ CHAINID="test"
coins="1000000000000000uceles"
celestia-appd init $CHAINID --chain-id $CHAINID
celestia-appd keys add validator1 --keyring-backend="test"
celestia-appd keys add validator2 --keyring-backend="test"
celestia-appd keys add validator3 --keyring-backend="test"
# this won't work because the some proto types are decalared twice and the logs output to stdout (dependency hell involving iavl)
celestia-appd add-genesis-account $(celestia-appd keys show validator1 -a --keyring-backend="test") $coins
celestia-appd add-genesis-account $(celestia-appd keys show validator2 -a --keyring-backend="test") $coins
celestia-appd add-genesis-account $(celestia-appd keys show validator3 -a --keyring-backend="test") $coins
celestia-appd gentx validator1 5000000000uceles \
--keyring-backend="test" \
--chain-id $CHAINID \
--orchestrator-address celes14v2rvt9az00vcd636j5q96aynzkyu0x85wuqas \
--ethereum-address 0x91DEd26b5f38B065FC0204c7929Da6b2A21277Cd
--orchestrator-address $(celestia-appd keys show validator1 -a --keyring-backend="test") \
--ethereum-address 0x966e6f22781EF6a6A82BBB4DB3df8E225DfD9488

celestia-appd collect-gentxs

Expand Down
1,063 changes: 1,063 additions & 0 deletions go.sum

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions x/qgb/abci.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package qgb

import (
"fmt"
"github.com/celestiaorg/celestia-app/x/qgb/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/celestiaorg/celestia-app/x/qgb/keeper"
"github.com/celestiaorg/celestia-app/x/qgb/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// EndBlocker is called at the end of every block
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
// TODO cleanup
fmt.Println("running the qgb endblocker ======================================================")
// get the last valsets to compare against
//get the last valsets to compare against
rach-id marked this conversation as resolved.
Show resolved Hide resolved
latestValset := k.GetLatestValset(ctx)
lastUnbondingHeight := k.GetLastUnBondingBlockHeight(ctx)

Expand Down
130 changes: 83 additions & 47 deletions x/qgb/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,56 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/big"
"testing"
"time"
)

// TODO add test for all the possible scenarios defined in msg_server.go
var (
blockTime = time.Date(2020, 9, 14, 15, 20, 10, 0, time.UTC)

validatorAccPrivateKey = secp256k1.GenPrivKey()
validatorAccPublicKey = validatorAccPrivateKey.PubKey()
validatorAccAddress = sdk.AccAddress(validatorAccPublicKey.Address())
validatorValAddress = sdk.ValAddress(validatorAccPublicKey.Address())

orchEthPrivateKey, _ = crypto.GenerateKey()
orchEthPublicKey = orchEthPrivateKey.Public().(*ecdsa.PublicKey)
orchEthAddress = crypto.PubkeyToAddress(*orchEthPublicKey).Hex()
ethAddr, _ = stakingtypes.NewEthAddress(orchEthAddress)

orchPrivateKey = secp256k1.GenPrivKey()
orchPublicKey = orchPrivateKey.PubKey()
orchAddress = sdk.AccAddress(orchPublicKey.Address())
)

// TestMsgValsetConfirm ensures that the valset confirm message sets a validator set confirm
// in the store
func TestMsgValsetConfirm(t *testing.T) {
var (
blockTime = time.Date(2020, 9, 14, 15, 20, 10, 0, time.UTC)
blockHeight int64 = 200
signature = "7c331bd8f2f586b04a2e2cafc6542442ef52e8b8be49533fa6b8962e822bc01e295a62733abfd65a412a8de8286f2794134c160c27a2827bdb71044b94b003cc1c"
)
blockHeight := int64(200)

input, ctx := keeper.SetupFiveValChain(t)
k := input.QgbKeeper
h := NewHandler(*input.QgbKeeper)

// create new validator
err := createNewValidator(input)
require.NoError(t, err)

// set a validator set in the store
vs, err := k.GetCurrentValset(ctx)
require.NoError(t, err)
vs.Height = uint64(1)
vs.Nonce = uint64(1)
k.StoreValset(ctx, vs)

signBytes, err := vs.SignBytes(types.BridgeId)
require.NoError(t, err)
signatureBytes, err := types.NewEthereumSignature(signBytes.Bytes(), orchEthPrivateKey)
signature := hex.EncodeToString(signatureBytes)
require.NoError(t, err)

// try wrong eth address
msg := &types.MsgValsetConfirm{
Nonce: 1,
Expand All @@ -61,8 +87,8 @@ func TestMsgValsetConfirm(t *testing.T) {

msg = &types.MsgValsetConfirm{
Nonce: 1,
Orchestrator: keeper.OrchAddrs[0].String(),
EthAddress: keeper.EthAddrs[0].GetAddress(),
Orchestrator: orchAddress.String(),
EthAddress: orchEthAddress,
Signature: signature,
}
ctx = ctx.WithBlockTime(blockTime).WithBlockHeight(blockHeight)
Expand All @@ -72,69 +98,43 @@ func TestMsgValsetConfirm(t *testing.T) {

// TestMsgDataCommitmentConfirm ensures that the data commitment confirm message sets a commitment in the store
func TestMsgDataCommitmentConfirm(t *testing.T) {
var (
blockTime = time.Date(2020, 9, 14, 15, 20, 10, 0, time.UTC)

validatorAccPrivateKey = secp256k1.GenPrivKey()
validatorAccPublicKey = validatorAccPrivateKey.PubKey()
validatorAccAddress = sdk.AccAddress(validatorAccPublicKey.Address())
validatorValAddress = sdk.ValAddress(validatorAccPublicKey.Address())

orchEthPrivateKey, _ = crypto.GenerateKey()
orchEthPublicKey = orchEthPrivateKey.Public().(*ecdsa.PublicKey)
orchEthAddress = crypto.PubkeyToAddress(*orchEthPublicKey).Hex()
ethAddr, _ = stakingtypes.NewEthAddress(orchEthAddress)

orchPrivateKey = secp256k1.GenPrivKey()
orchPublicKey = orchPrivateKey.PubKey()
orchAddress = sdk.AccAddress(orchPublicKey.Address())
)

// Init chain
input, ctx := keeper.SetupFiveValChain(t)
k := input.QgbKeeper

// Create a new validator
acc := input.AccountKeeper.NewAccount(
input.Context,
authtypes.NewBaseAccount(validatorAccAddress, validatorAccPublicKey, uint64(120), 0),
)
require.NoError(t, input.BankKeeper.MintCoins(input.Context, types.ModuleName, keeper.InitCoins))
// nolint
input.BankKeeper.SendCoinsFromModuleToAccount(input.Context, types.ModuleName, acc.GetAddress(), keeper.InitCoins)
input.AccountKeeper.SetAccount(input.Context, acc)

sh := staking.NewHandler(input.StakingKeeper)
_, err := sh(
input.Context,
keeper.NewTestMsgCreateValidator(validatorValAddress, validatorAccPublicKey, keeper.StakingAmount, orchAddress, *ethAddr),
)
err := createNewValidator(input)
require.NoError(t, err)
staking.EndBlocker(input.Context, input.StakingKeeper)

h := NewHandler(*input.QgbKeeper)
ctx = ctx.WithBlockTime(blockTime)

commitment := "102030"
bytesCommitment, err := hex.DecodeString(commitment)
require.NoError(t, err)
dataHash := types.DataCommitmentTupleRootSignBytes(
types.BridgeId,
big.NewInt(100/types.DataCommitmentWindow),
bytesCommitment,
)

// Signs the commitment using the orth eth private key
signature, err := types.NewEthereumSignature([]byte("commitment"), orchEthPrivateKey)
if err != nil {
panic(err)
}
signature, err := types.NewEthereumSignature(dataHash.Bytes(), orchEthPrivateKey)
require.NoError(t, err)

// Sending a data commitment confirm
setDCCMsg := &types.MsgDataCommitmentConfirm{
Signature: hex.EncodeToString(signature),
ValidatorAddress: orchAddress.String(),
EthAddress: orchEthAddress,
Commitment: "commitment",
Commitment: commitment,
BeginBlock: 1,
EndBlock: 100,
}
result, err := h(ctx, setDCCMsg)
require.NoError(t, err)

// Checking if it was correctly submitted
actualCommitment := k.GetDataCommitmentConfirm(ctx, "commitment", orchAddress)
actualCommitment := k.GetDataCommitmentConfirm(ctx, commitment, orchAddress)
assert.Equal(t, setDCCMsg, actualCommitment)

// Checking if the event was successfully sent
Expand All @@ -145,3 +145,39 @@ func TestMsgDataCommitmentConfirm(t *testing.T) {
assert.Equal(t, types.AttributeKeyDataCommitmentConfirmKey, string(actualEvent.Attributes[1].Key))
assert.Equal(t, setDCCMsg.String(), string(actualEvent.Attributes[1].Value))
}

// TODO add more parameters to this
func createNewValidator(input keeper.TestInput) error {
// Create a new validator
acc := input.AccountKeeper.NewAccount(
input.Context,
authtypes.NewBaseAccount(validatorAccAddress, validatorAccPublicKey, uint64(120), 0),
)
err := input.BankKeeper.MintCoins(input.Context, types.ModuleName, keeper.InitCoins)
if err != nil {
return err
}
// nolint
err = input.BankKeeper.SendCoinsFromModuleToAccount(input.Context, types.ModuleName, acc.GetAddress(), keeper.InitCoins)
if err != nil {
return err
}
input.AccountKeeper.SetAccount(input.Context, acc)

sh := staking.NewHandler(input.StakingKeeper)
_, err = sh(
input.Context,
keeper.NewTestMsgCreateValidator(
validatorValAddress,
validatorAccPublicKey,
keeper.StakingAmount,
orchAddress,
*ethAddr,
),
)
if err != nil {
return err
}
staking.EndBlocker(input.Context, input.StakingKeeper)
return nil
}
Loading