Skip to content

Commit

Permalink
[R4R] - {develop}: fix consensys audit issue: cs-6.33 (#1188)
Browse files Browse the repository at this point in the history
usr pointer instead of struct for tss manager

Co-authored-by: Raymond <6427270+wukongcheng@users.noreply.github.com>
  • Loading branch information
HaoyangLiu and wukongcheng authored Jul 2, 2023
1 parent 1e75491 commit d950bbd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
7 changes: 4 additions & 3 deletions tss/manager/agreement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"encoding/json"
"errors"
"fmt"
"sync"

"github.com/influxdata/influxdb/pkg/slices"
"github.com/mantlenetworkio/mantle/l2geth/log"
tss "github.com/mantlenetworkio/mantle/tss/common"
"github.com/mantlenetworkio/mantle/tss/manager/types"
"github.com/mantlenetworkio/mantle/tss/ws/server"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
"sync"
)

func (m Manager) agreement(ctx types.Context, request interface{}, method tss.Method) (types.Context, error) {
func (m *Manager) agreement(ctx types.Context, request interface{}, method tss.Method) (types.Context, error) {
respChan := make(chan server.ResponseMsg)
stopChan := make(chan struct{})
if err := m.wsServer.RegisterResChannel("ASK_"+ctx.RequestId(), respChan, stopChan); err != nil {
Expand Down Expand Up @@ -120,7 +121,7 @@ func (m Manager) agreement(ctx types.Context, request interface{}, method tss.Me
return ctx, nil
}

func (m Manager) askNodes(ctx types.Context, request []byte, method tss.Method, stopChan chan struct{}, errSendChan chan struct{}) {
func (m *Manager) askNodes(ctx types.Context, request []byte, method tss.Method, stopChan chan struct{}, errSendChan chan struct{}) {
log.Info("start to sendTonNodes", "number", len(ctx.AvailableNodes()))
nodes := ctx.AvailableNodes()
for i := range nodes {
Expand Down
6 changes: 3 additions & 3 deletions tss/manager/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
tmtypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)

func (m Manager) observeElection() {
func (m *Manager) observeElection() {

queryTicker := time.NewTicker(m.taskInterval + 30*time.Second)
for {
Expand Down Expand Up @@ -79,7 +79,7 @@ func (m Manager) observeElection() {
}
}

func (m Manager) generateKey(tssMembers []string, threshold int) (string, error) {
func (m *Manager) generateKey(tssMembers []string, threshold int) (string, error) {
availableNodes := m.availableNodes(tssMembers)
if len(availableNodes) < len(tssMembers) {
return "", errors.New("not enough available nodes to generate CPK")
Expand Down Expand Up @@ -162,7 +162,7 @@ func (m Manager) generateKey(tssMembers []string, threshold int) (string, error)
return base, nil
}

func (m Manager) callKeygen(availableNodes []string, threshold int, requestId string, sendError chan struct{}) {
func (m *Manager) callKeygen(availableNodes []string, threshold int, requestId string, sendError chan struct{}) {
for _, node := range availableNodes {
nodeRequest := tss.KeygenRequest{
Nodes: availableNodes,
Expand Down
48 changes: 24 additions & 24 deletions tss/manager/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,55 @@ type Manager struct {
func NewManager(wsServer server.IWebsocketManager,
tssQueryService types.TssQueryService,
store types.ManagerStore,
config tss.Configuration) (Manager, error) {
config tss.Configuration) (*Manager, error) {
taskIntervalDur, err := time.ParseDuration(config.TimedTaskInterval)
if err != nil {
return Manager{}, err
return nil, err
}
receiptConfirmTimeoutDur, err := time.ParseDuration(config.L1ReceiptConfirmTimeout)
if err != nil {
return Manager{}, err
return nil, err
}
keygenTimeoutDur, err := time.ParseDuration(config.Manager.KeygenTimeout)
if err != nil {
return Manager{}, err
return nil, err
}
cpkConfirmTimeoutDur, err := time.ParseDuration(config.Manager.CPKConfirmTimeout)
if err != nil {
return Manager{}, err
return nil, err
}
askTimeoutDur, err := time.ParseDuration(config.Manager.AskTimeout)
if err != nil {
return Manager{}, err
return nil, err
}
signTimeoutDur, err := time.ParseDuration(config.Manager.SignTimeout)
if err != nil {
return Manager{}, err
return nil, err
}

l1Cli, err := ethclient.Dial(config.L1Url)
if err != nil {
return Manager{}, err
return nil, err
}
tssStakingSlashingCaller, err := tsh.NewTssStakingSlashingCaller(common.HexToAddress(config.TssStakingSlashContractAddress), l1Cli)
if err != nil {
return Manager{}, err
return nil, err
}
tssGroupManagerCaller, err := tgm.NewTssGroupManagerCaller(common.HexToAddress(config.TssGroupContractAddress), l1Cli)
if err != nil {
return Manager{}, err
return nil, err
}
privKey, err := crypto.HexToECDSA(config.Manager.PrivateKey)
if err != nil {
return Manager{}, err
return nil, err
}

chainId, err := l1Cli.ChainID(context.Background())
if err != nil {
return Manager{}, err
return nil, err
}

return Manager{
return &Manager{
wsServer: wsServer,
tssQueryService: tssQueryService,
store: store,
Expand All @@ -134,25 +134,25 @@ func NewManager(wsServer server.IWebsocketManager,
}

// Start launch a manager
func (m Manager) Start() {
func (m *Manager) Start() {
log.Info("manager is starting......")
go m.observeElection()
go m.slashing()
}

func (m Manager) Stop() {
func (m *Manager) Stop() {
close(m.stopChan)
}

func (m Manager) stopGenerateKey() {
func (m *Manager) stopGenerateKey() {
m.stopGenKey = true
}

func (m Manager) recoverGenerateKey() {
func (m *Manager) recoverGenerateKey() {
m.stopGenKey = false
}

func (m Manager) SignStateBatch(request tss.SignStateRequest) ([]byte, error) {
func (m *Manager) SignStateBatch(request tss.SignStateRequest) ([]byte, error) {
log.Info("received sign state request", "start block", request.StartBlock, "len", len(request.StateRoots), "index", request.OffsetStartsAtIndex)
offsetStartsAtIndex, _ := new(big.Int).SetString(request.OffsetStartsAtIndex, 10)
digestBz, err := tss.StateBatchHash(request.StateRoots, offsetStartsAtIndex)
Expand Down Expand Up @@ -284,7 +284,7 @@ func (m Manager) SignStateBatch(request tss.SignStateRequest) ([]byte, error) {
return responseBytes, nil
}

func (m Manager) SignRollBack(request tss.SignStateRequest) ([]byte, error) {
func (m *Manager) SignRollBack(request tss.SignStateRequest) ([]byte, error) {
log.Info("received roll back request", "request", request.String())

tssInfo, err := m.tssQueryService.QueryActiveInfo()
Expand Down Expand Up @@ -338,11 +338,11 @@ func (m Manager) SignRollBack(request tss.SignStateRequest) ([]byte, error) {
return responseBytes, nil
}

func (m Manager) SignTxBatch() error {
func (m *Manager) SignTxBatch() error {
return errors.New("not support for now")
}

func (m Manager) availableNodes(tssMembers []string) []string {
func (m *Manager) availableNodes(tssMembers []string) []string {
aliveNodes := m.wsServer.AliveNodes()
m.metics.OnlineNodesCount.Set(float64(len(aliveNodes)))
log.Info("check available nodes", "expected", fmt.Sprintf("%v", tssMembers), "alive nodes", fmt.Sprintf("%v", aliveNodes))
Expand All @@ -362,7 +362,7 @@ func randomRequestId() string {
return time.Now().Format("20060102150405") + code
}

func (m Manager) afterSignStateBatch(ctx types.Context, stateBatch [][32]byte, absentNodes []string) error {
func (m *Manager) afterSignStateBatch(ctx types.Context, stateBatch [][32]byte, absentNodes []string) error {
batchRoot, err := tss.GetMerkleRoot(stateBatch)
if err != nil {
return err
Expand All @@ -380,15 +380,15 @@ func (m Manager) afterSignStateBatch(ctx types.Context, stateBatch [][32]byte, a
return nil
}

func (m Manager) getStateSignature(digestBz []byte) []byte {
func (m *Manager) getStateSignature(digestBz []byte) []byte {
m.sigCacheLock.RLock()
defer m.sigCacheLock.RUnlock()
var key [32]byte
copy(key[:], digestBz)
return m.stateSignatureCache[key]
}

func (m Manager) setStateSignature(digestBz []byte, sig []byte) {
func (m *Manager) setStateSignature(digestBz []byte, sig []byte) {
m.sigCacheLock.Lock()
defer m.sigCacheLock.Unlock()
var key [32]byte
Expand Down
4 changes: 2 additions & 2 deletions tss/manager/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (mock *mockWsManager) SendMsg(request server.RequestMsg) error {
return mock.afterMsgSent(request, mock.responseCh)
}

func setup(afterMsgSent afterMsgSendFunc, queryAliveNodes queryAliveNodesFunc) (Manager, tss.SignStateRequest) {
func setup(afterMsgSent afterMsgSendFunc, queryAliveNodes queryAliveNodesFunc) (*Manager, tss.SignStateRequest) {
mock := mockWsManager{
afterMsgSent: afterMsgSent,
queryAliveNodes: queryAliveNodes,
Expand All @@ -42,7 +42,7 @@ func setup(afterMsgSent afterMsgSendFunc, queryAliveNodes queryAliveNodesFunc) (
if err != nil {
panic(err)
}
manager := Manager{
manager := &Manager{
wsServer: &mock,
store: storage,

Expand Down
4 changes: 2 additions & 2 deletions tss/manager/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Counter) satisfied(minNumber int) []string {
return ret
}

func (m Manager) sign(ctx types.Context, request interface{}, digestBz []byte, method tss.Method) (tss.SignResponse, []string, error) {
func (m *Manager) sign(ctx types.Context, request interface{}, digestBz []byte, method tss.Method) (tss.SignResponse, []string, error) {
respChan := make(chan server.ResponseMsg)
stopChan := make(chan struct{})

Expand Down Expand Up @@ -138,7 +138,7 @@ func (m Manager) sign(ctx types.Context, request interface{}, digestBz []byte, m
return *validSignResponse, culprits, nil
}

func (m Manager) sendToNodes(ctx types.Context, request interface{}, method tss.Method, errSendChan chan struct{}) {
func (m *Manager) sendToNodes(ctx types.Context, request interface{}, method tss.Method, errSendChan chan struct{}) {
nodes := ctx.Approvers()
nodeRequest := tss.NodeSignRequest{
ClusterPublicKey: ctx.TssInfos().ClusterPubKey,
Expand Down
6 changes: 3 additions & 3 deletions tss/manager/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var sendState = SendState{
lock: &sync.Mutex{},
}

func (m Manager) slashing() {
func (m *Manager) slashing() {
queryTicker := time.NewTicker(m.taskInterval)
for {
signingInfos := m.store.ListSlashingInfo()
Expand All @@ -49,7 +49,7 @@ func (m Manager) slashing() {
}
}

func (m Manager) handleSlashing(si slash.SlashingInfo) {
func (m *Manager) handleSlashing(si slash.SlashingInfo) {
log.Info("start to handleSlashing", "address", si.Address.String(), "batch_index", si.BatchIndex, "slash_type", si.SlashType, "election id", si.ElectionId)
currentBlockNumber, err := m.l1Cli.BlockNumber(context.Background())
if err != nil {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (m Manager) handleSlashing(si slash.SlashingInfo) {
return
}

func (m Manager) submitSlashing(signResp tss.SignResponse, si slash.SlashingInfo, mesTx []byte) error {
func (m *Manager) submitSlashing(signResp tss.SignResponse, si slash.SlashingInfo, mesTx []byte) error {
txData, err := m.txBuilder(mesTx, signResp.Signature)
if err != nil {
return err
Expand Down

0 comments on commit d950bbd

Please sign in to comment.