Skip to content

Commit

Permalink
bridge, staking: chg: bug fix (#1149)
Browse files Browse the repository at this point in the history
* chg: bug fix

* chg: fix lint
  • Loading branch information
marcello33 authored Mar 18, 2024
1 parent 98ba715 commit e95c69d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
6 changes: 5 additions & 1 deletion bridge/setu/listener/rootchain_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (rl *RootChainListener) handleStakedLog(vLog types.Log, selectedEvent *abi.
rl.Logger.Error("Error while parsing event", "name", selectedEvent.Name, "error", err)
}

if !util.IsPubKeyFirstByteValid(pubkey[0:1]) {
rl.Logger.Error("public key first byte mismatch", "expected", "0x04", "received", pubkey[0:1])
}

if bytes.Equal(event.SignerPubkey, pubkey[1:]) {
// topup has to be processed first before validator join. so adding delay.
delay := util.TaskDelayBetweenEachVal
Expand Down Expand Up @@ -106,7 +110,7 @@ func (rl *RootChainListener) handleSignerChangeLog(vLog types.Log, selectedEvent
rl.Logger.Error("Error while parsing event", "name", selectedEvent.Name, "error", err)
}

if bytes.Equal(event.SignerPubkey, pubkey[1:]) {
if bytes.Equal(event.SignerPubkey, pubkey[1:]) && util.IsPubKeyFirstByteValid(pubkey[0:1]) {
rl.SendTaskWithDelay("sendSignerChangeToHeimdall", selectedEvent.Name, logBytes, 0, event)
} else if isCurrentValidator, delay := util.CalculateTaskDelay(rl.cliCtx, event); isCurrentValidator {
rl.SendTaskWithDelay("sendSignerChangeToHeimdall", selectedEvent.Name, logBytes, delay, event)
Expand Down
9 changes: 9 additions & 0 deletions bridge/setu/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,12 @@ func LogElapsedTimeForStateSyncedEvent(event interface{}, functionName string, s
"stateSyncId", typedEvent.Id,
"timeElapsed", timeElapsed)
}

// IsPubKeyFirstByteValid checks the validity of the first byte of the public key.
// It must be 0x04 for uncompressed public keys
func IsPubKeyFirstByteValid(pubKey []byte) bool {
prefix := make([]byte, 1)
prefix[0] = byte(0x04)

return bytes.Equal(prefix, pubKey[0:1])
}
11 changes: 11 additions & 0 deletions helper/tx.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package helper

import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -155,6 +157,15 @@ func (c *ContractCaller) SendTick(signedData []byte, sigs []byte, slashManagerAd
// StakeFor stakes for a validator
func (c *ContractCaller) StakeFor(val common.Address, stakeAmount *big.Int, feeAmount *big.Int, acceptDelegation bool, stakeManagerAddress common.Address, stakeManagerInstance *stakemanager.Stakemanager) error {
signerPubkey := GetPubKey()

prefix := make([]byte, 1)
prefix[0] = byte(0x04)

if !bytes.Equal(prefix, signerPubkey[0:1]) {
Logger.Error("public key first byte mismatch", "expected", "0x04", "received", signerPubkey[0:1])
return errors.New("public key first byte mismatch")
}

signerPubkeyBytes := signerPubkey[1:] // remove 04 prefix

// pack data based on method definition
Expand Down
8 changes: 8 additions & 0 deletions staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func SendValidatorJoinTx(cdc *codec.Codec) *cobra.Command {
return fmt.Errorf("Invalid tx for validator join")
}

if !util.IsPubKeyFirstByteValid(pubkey.Bytes()[0:1]) {
return fmt.Errorf("public key first byte mismatch")
}

if !bytes.Equal(event.SignerPubkey, pubkey.Bytes()[1:]) {
return fmt.Errorf("Public key mismatch with event log")
}
Expand Down Expand Up @@ -276,6 +280,10 @@ func SendValidatorUpdateTx(cdc *codec.Codec) *cobra.Command {
}
pubkey := hmTypes.NewPubKey(pubkeyBytes)

if !util.IsPubKeyFirstByteValid(pubkey.Bytes()[0:1]) {
return fmt.Errorf("public key first byte mismatch")
}

txhash := viper.GetString(FlagTxHash)
if txhash == "" {
return fmt.Errorf("transaction hash has to be supplied")
Expand Down
11 changes: 11 additions & 0 deletions staking/side_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

authTypes "github.com/maticnetwork/heimdall/auth/types"
"github.com/maticnetwork/heimdall/bridge/setu/util"
"github.com/maticnetwork/heimdall/common"
hmCommon "github.com/maticnetwork/heimdall/common"
"github.com/maticnetwork/heimdall/helper"
Expand Down Expand Up @@ -88,6 +89,11 @@ func SideHandleMsgValidatorJoin(ctx sdk.Context, msg types.MsgValidatorJoin, k K
pubkey := msg.SignerPubKey
signer := pubkey.Address()

if !util.IsPubKeyFirstByteValid(pubkey[0:1]) {
k.Logger(ctx).Error("public key first byte mismatch", "expected", "0x04", "received", pubkey[0:1])
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}

// check signer pubkey in message corresponds
if !bytes.Equal(pubkey.Bytes()[1:], eventLog.SignerPubkey) {
k.Logger(ctx).Error(
Expand Down Expand Up @@ -237,6 +243,11 @@ func SideHandleMsgSignerUpdate(ctx sdk.Context, msg types.MsgSignerUpdate, k Kee
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}

if !util.IsPubKeyFirstByteValid(newPubKey.Bytes()[0:1]) {
k.Logger(ctx).Error("public key first byte mismatch", "expected", "0x04", "received", newPubKey.Bytes()[0:1])
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}

if !bytes.Equal(eventLog.SignerPubkey, newPubKey.Bytes()[1:]) {
k.Logger(ctx).Error("Newsigner pubkey in txhash and msg dont match", "msgPubKey", newPubKey.String(), "pubkeyTx", hmTypes.NewPubKey(eventLog.SignerPubkey[:]).String())
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
Expand Down
3 changes: 2 additions & 1 deletion staking/side_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
ethTypes "github.com/ethereum/go-ethereum/core/types"

"github.com/maticnetwork/heimdall/app"
"github.com/maticnetwork/heimdall/bridge/setu/util"
chSim "github.com/maticnetwork/heimdall/checkpoint/simulation"
"github.com/maticnetwork/heimdall/common"
errs "github.com/maticnetwork/heimdall/common"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (suite *SideHandlerTestSuite) TestSideHandleMsgValidatorJoin() {
amount, _ := big.NewInt(0).SetString("1000000000000000000", 10)

privKey1 := secp256k1.GenPrivKey()
pubkey := hmTypes.NewPubKey(privKey1.PubKey().Bytes())
pubkey := hmTypes.NewPubKey(util.AppendPrefix(privKey1.PubKey().Bytes()))
address := pubkey.Address()

chainParams := app.ChainKeeper.GetParams(ctx)
Expand Down
3 changes: 2 additions & 1 deletion staking/simulation/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"math/big"

"github.com/maticnetwork/heimdall/bridge/setu/util"
"github.com/maticnetwork/heimdall/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
Expand All @@ -12,7 +13,7 @@ import (
func GenRandomVal(count int, startBlock uint64, power int64, timeAlive uint64, randomise bool, startID uint64) (validators []types.Validator) {
for i := 0; i < count; i++ {
privKey1 := secp256k1.GenPrivKey()
pubkey := types.NewPubKey(privKey1.PubKey().Bytes())
pubkey := types.NewPubKey(util.AppendPrefix(privKey1.PubKey().Bytes()))

if randomise {
startBlock = generateRandNumber(10)
Expand Down

0 comments on commit e95c69d

Please sign in to comment.