[#219] morph: Refactor notary preparator
All checks were successful
ci/woodpecker/push/pre-commit Pipeline was successful

Resolve funlen linter for Preparator.Prepare method.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-05 18:21:14 +03:00
parent d07e40d6fe
commit fe87735073

View file

@ -103,55 +103,8 @@ func notaryPreparator(prm PreparatorPrm) NotaryPreparator {
// transaction is expected to be received one more time
// from the Notary service but already signed. This happens
// since every notary call is a new notary request in fact.
//
// nolint: funlen
func (p Preparator) Prepare(nr *payload.P2PNotaryRequest) (NotaryEvent, error) {
// notary request's main tx is expected to have
// three or four witnesses: one for proxy contract,
// one for alphabet multisignature, one optional for
// notary's invoker and one is for notary contract
ln := len(nr.MainTransaction.Scripts)
switch ln {
case 3, 4:
default:
return nil, errUnexpectedWitnessAmount
}
invokerWitness := ln == 4
// alphabet node should handle only notary requests
// that have been sent unsigned(by storage nodes) =>
// such main TXs should have dummy scripts as an
// invocation script
//
// this check prevents notary flow recursion
if !bytes.Equal(nr.MainTransaction.Scripts[1].InvocationScript, p.dummyInvocationScript) {
return nil, ErrTXAlreadyHandled
}
currentAlphabet, err := p.alphaKeys()
if err != nil {
return nil, fmt.Errorf("could not fetch Alphabet public keys: %w", err)
}
err = p.validateCosigners(ln, nr.MainTransaction.Signers, currentAlphabet)
if err != nil {
return nil, err
}
// validate main TX's notary attribute
err = p.validateAttributes(nr.MainTransaction.Attributes, currentAlphabet, invokerWitness)
if err != nil {
return nil, err
}
// validate main TX's witnesses
err = p.validateWitnesses(nr.MainTransaction.Scripts, currentAlphabet, invokerWitness)
if err != nil {
return nil, err
}
// validate main TX expiration
err = p.validateExpiration(nr.FallbackTransaction)
err := p.validateNotaryRequest(nr)
if err != nil {
return nil, err
}
@ -219,6 +172,60 @@ func (p Preparator) Prepare(nr *payload.P2PNotaryRequest) (NotaryEvent, error) {
}, nil
}
func (p Preparator) validateNotaryRequest(nr *payload.P2PNotaryRequest) error {
// notary request's main tx is expected to have
// three or four witnesses: one for proxy contract,
// one for alphabet multisignature, one optional for
// notary's invoker and one is for notary contract
ln := len(nr.MainTransaction.Scripts)
switch ln {
case 3, 4:
default:
return errUnexpectedWitnessAmount
}
invokerWitness := ln == 4
// alphabet node should handle only notary requests
// that have been sent unsigned(by storage nodes) =>
// such main TXs should have dummy scripts as an
// invocation script
//
// this check prevents notary flow recursion
if !bytes.Equal(nr.MainTransaction.Scripts[1].InvocationScript, p.dummyInvocationScript) {
return ErrTXAlreadyHandled
}
currentAlphabet, err := p.alphaKeys()
if err != nil {
return fmt.Errorf("could not fetch Alphabet public keys: %w", err)
}
err = p.validateCosigners(ln, nr.MainTransaction.Signers, currentAlphabet)
if err != nil {
return err
}
// validate main TX's notary attribute
err = p.validateAttributes(nr.MainTransaction.Attributes, currentAlphabet, invokerWitness)
if err != nil {
return err
}
// validate main TX's witnesses
err = p.validateWitnesses(nr.MainTransaction.Scripts, currentAlphabet, invokerWitness)
if err != nil {
return err
}
// validate main TX expiration
err = p.validateExpiration(nr.FallbackTransaction)
if err != nil {
return err
}
return nil
}
func (p Preparator) validateParameterOpcodes(ops []Op) error {
l := len(ops)