[#787] morph: Calculate VUB and nonce when hash is nil

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-11-08 12:28:34 +03:00
parent 2393d13e4d
commit c8a62ffedd
3 changed files with 25 additions and 13 deletions

View file

@ -117,7 +117,7 @@ func (s *Server) voteForSidechainValidator(prm governance.VoteValidatorPrm) erro
) )
if prm.Hash != nil { if prm.Hash != nil {
nonce, vub, err = s.morphClient.CalculateNonceAndVUB(*prm.Hash) nonce, vub, err = s.morphClient.CalculateNonceAndVUB(prm.Hash)
if err != nil { if err != nil {
return fmt.Errorf("could not calculate nonce and `validUntilBlock` values: %w", err) return fmt.Errorf("could not calculate nonce and `validUntilBlock` values: %w", err)
} }

View file

@ -289,7 +289,7 @@ func (c *Client) UpdateNotaryList(prm UpdateNotaryListPrm) error {
panic(notaryNotEnabledPanicMsg) panic(notaryNotEnabledPanicMsg)
} }
nonce, vub, err := c.CalculateNonceAndVUB(prm.hash) nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)
if err != nil { if err != nil {
return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err) return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err)
} }
@ -337,7 +337,7 @@ func (c *Client) UpdateNeoFSAlphabetList(prm UpdateAlphabetListPrm) error {
panic(notaryNotEnabledPanicMsg) panic(notaryNotEnabledPanicMsg)
} }
nonce, vub, err := c.CalculateNonceAndVUB(prm.hash) nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)
if err != nil { if err != nil {
return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err) return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err)
} }
@ -763,17 +763,19 @@ func CalculateNotaryDepositAmount(c *Client, gasMul, gasDiv int64) (fixedn.Fixed
// CalculateNonceAndVUB calculates nonce and ValidUntilBlock values // CalculateNonceAndVUB calculates nonce and ValidUntilBlock values
// based on transaction hash. // based on transaction hash.
func (c *Client) CalculateNonceAndVUB(hash util.Uint256) (nonce uint32, vub uint32, err error) { func (c *Client) CalculateNonceAndVUB(hash *util.Uint256) (nonce uint32, vub uint32, err error) {
return c.calculateNonceAndVUB(hash, false) return c.calculateNonceAndVUB(hash, false)
} }
// CalculateNonceAndVUBControl calculates nonce and rounded ValidUntilBlock values // CalculateNonceAndVUBControl calculates nonce and rounded ValidUntilBlock values
// based on transaction hash for use in control transactions. // based on transaction hash for use in control transactions.
func (c *Client) CalculateNonceAndVUBControl(hash util.Uint256) (nonce uint32, vub uint32, err error) { func (c *Client) CalculateNonceAndVUBControl(hash *util.Uint256) (nonce uint32, vub uint32, err error) {
return c.calculateNonceAndVUB(hash, true) return c.calculateNonceAndVUB(hash, true)
} }
func (c *Client) calculateNonceAndVUB(hash util.Uint256, roundBlockHeight bool) (nonce uint32, vub uint32, err error) { // If hash specified, transaction's height and hash are used to compute VUB and nonce.
// If not, then current block height used to compute VUB and nonce.
func (c *Client) calculateNonceAndVUB(hash *util.Uint256, roundBlockHeight bool) (nonce uint32, vub uint32, err error) {
c.switchLock.RLock() c.switchLock.RLock()
defer c.switchLock.RUnlock() defer c.switchLock.RUnlock()
@ -785,11 +787,18 @@ func (c *Client) calculateNonceAndVUB(hash util.Uint256, roundBlockHeight bool)
return 0, 0, nil return 0, 0, nil
} }
nonce = binary.LittleEndian.Uint32(hash.BytesLE()) var height uint32
height, err := c.getTransactionHeight(hash) if hash != nil {
if err != nil { height, err = c.getTransactionHeight(*hash)
return 0, 0, fmt.Errorf("could not get transaction height: %w", err) if err != nil {
return 0, 0, fmt.Errorf("could not get transaction height: %w", err)
}
} else {
height, err = c.rpcActor.GetBlockCount()
if err != nil {
return 0, 0, fmt.Errorf("could not get chain height: %w", err)
}
} }
// For control transactions, we round down the block height to control the // For control transactions, we round down the block height to control the
@ -800,7 +809,10 @@ func (c *Client) calculateNonceAndVUB(hash util.Uint256, roundBlockHeight bool)
height = height / inc * inc height = height / inc * inc
} }
return nonce, height + c.notary.txValidTime, nil if hash != nil {
return binary.LittleEndian.Uint32(hash.BytesLE()), height + c.notary.txValidTime, nil
}
return height + c.notary.txValidTime, height + c.notary.txValidTime, nil
} }
func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) { func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) {

View file

@ -154,9 +154,9 @@ func (s StaticClient) Invoke(prm InvokePrm) (InvokeRes, error) {
if prm.hash != nil { if prm.hash != nil {
if prm.controlTX { if prm.controlTX {
nonce, vub, err = s.client.CalculateNonceAndVUBControl(*prm.hash) nonce, vub, err = s.client.CalculateNonceAndVUBControl(prm.hash)
} else { } else {
nonce, vub, err = s.client.CalculateNonceAndVUB(*prm.hash) nonce, vub, err = s.client.CalculateNonceAndVUB(prm.hash)
} }
if err != nil { if err != nil {
return InvokeRes{}, fmt.Errorf("could not calculate nonce and VUB for notary alphabet invoke: %w", err) return InvokeRes{}, fmt.Errorf("could not calculate nonce and VUB for notary alphabet invoke: %w", err)