code.vegaprotocol.io/vega@v0.79.0/core/validators/signatures_snapshot.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package validators
    17  
    18  import (
    19  	"sort"
    20  
    21  	"code.vegaprotocol.io/vega/libs/num"
    22  	"code.vegaprotocol.io/vega/logging"
    23  	snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    24  )
    25  
    26  func (s *ERC20Signatures) SerialisePendingSignatures() *snapshot.ToplogySignatures {
    27  	pending := make([]*snapshot.PendingERC20MultisigControlSignature, 0, len(s.pendingSignatures))
    28  	for e, data := range s.pendingSignatures {
    29  		pending = append(pending,
    30  			&snapshot.PendingERC20MultisigControlSignature{
    31  				NodeId:          data.NodeID,
    32  				Nonce:           data.Nonce.String(),
    33  				EthereumAddress: e,
    34  				Added:           data.Added,
    35  				EpochSeq:        data.EpochSeq,
    36  			},
    37  		)
    38  	}
    39  	sort.SliceStable(pending, func(i, j int) bool {
    40  		return pending[i].EthereumAddress < pending[j].EthereumAddress
    41  	})
    42  
    43  	issued := make([]*snapshot.IssuedERC20MultisigControlSignature, 0, len(s.issuedSignatures))
    44  	for resID, data := range s.issuedSignatures {
    45  		issued = append(issued, &snapshot.IssuedERC20MultisigControlSignature{
    46  			ResourceId:       resID,
    47  			EthereumAddress:  data.EthAddress,
    48  			SubmitterAddress: data.SubmitterAddress,
    49  			ChainId:          data.ChainID,
    50  		})
    51  	}
    52  	sort.SliceStable(issued, func(i, j int) bool {
    53  		return issued[i].ResourceId < issued[j].ResourceId
    54  	})
    55  
    56  	return &snapshot.ToplogySignatures{
    57  		PendingSignatures: pending,
    58  		IssuedSignatures:  issued,
    59  	}
    60  }
    61  
    62  func (s *ERC20Signatures) RestorePendingSignatures(sigs *snapshot.ToplogySignatures) {
    63  	for _, data := range sigs.PendingSignatures {
    64  		nonce, overflow := num.UintFromString(data.Nonce, 10)
    65  		if overflow {
    66  			s.log.Panic("Uint string not save/restored properly", logging.String("nonce", data.Nonce))
    67  		}
    68  		sd := &signatureData{
    69  			Nonce:      nonce,
    70  			NodeID:     data.NodeId,
    71  			EthAddress: data.EthereumAddress,
    72  			EpochSeq:   data.EpochSeq,
    73  			Added:      data.Added,
    74  		}
    75  		s.pendingSignatures[data.EthereumAddress] = sd
    76  	}
    77  
    78  	for _, data := range sigs.IssuedSignatures {
    79  		chainID := data.ChainId
    80  		if chainID == "" {
    81  			// we're upgrading from a version with only one bridge, set the chainID to the primary bridge chainID
    82  			// TODO confirm .ChainID is populated, we should have propagated network parameters by now
    83  			chainID = s.primaryMultisig.ChainID()
    84  		}
    85  
    86  		s.issuedSignatures[data.ResourceId] = issuedSignature{
    87  			EthAddress:       data.EthereumAddress,
    88  			SubmitterAddress: data.SubmitterAddress,
    89  			ChainID:          chainID,
    90  		}
    91  	}
    92  }