[#486] morph/client: Add option to setup custom alphabet source in notary

To enable notary support in main chain, notary subsystem should not get
alphabet keys from (main chain) committee. This key fetcher is now
separated and may be overwritten by `WithAlphabetSource` option.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-04-20 18:11:35 +03:00 committed by Alex Vanin
parent c84fe1360e
commit dd1ace12f7

View file

@ -23,14 +23,19 @@ type (
roundTime uint32 // extra amount of blocks to synchronize sidechain height diff of inner ring nodes roundTime uint32 // extra amount of blocks to synchronize sidechain height diff of inner ring nodes
fallbackTime uint32 // amount of blocks before fallbackTx will be sent fallbackTime uint32 // amount of blocks before fallbackTx will be sent
alphabetSource AlphabetKeys // source of alphabet node keys to prepare witness
notary util.Uint160 notary util.Uint160
proxy util.Uint160 proxy util.Uint160
} }
notaryCfg struct { notaryCfg struct {
txValidTime, roundTime, fallbackTime uint32 txValidTime, roundTime, fallbackTime uint32
alphabetSource AlphabetKeys
} }
AlphabetKeys func() (keys.PublicKeys, error)
NotaryOption func(*notaryCfg) NotaryOption func(*notaryCfg)
) )
@ -48,19 +53,20 @@ const (
var errUnexpectedItems = errors.New("invalid number of NEO VM arguments on stack") var errUnexpectedItems = errors.New("invalid number of NEO VM arguments on stack")
func defaultNotaryConfig() *notaryCfg { func defaultNotaryConfig(c *Client) *notaryCfg {
return &notaryCfg{ return &notaryCfg{
txValidTime: defaultNotaryValidTime, txValidTime: defaultNotaryValidTime,
roundTime: defaultNotaryRoundTime, roundTime: defaultNotaryRoundTime,
fallbackTime: defaultNotaryFallbackTime, fallbackTime: defaultNotaryFallbackTime,
alphabetSource: c.Committee,
} }
} }
// EnableNotarySupport creates notary structure in client that provides // EnableNotarySupport creates notary structure in client that provides
// ability for client to get inner ring list from netmap contract and // ability for client to get alphabet keys from committee or provided source
// use proxy contract script hash to create tx for notary contract. // and use proxy contract script hash to create tx for notary contract.
func (c *Client) EnableNotarySupport(proxy, netmap util.Uint160, opts ...NotaryOption) error { func (c *Client) EnableNotarySupport(proxy util.Uint160, opts ...NotaryOption) error {
cfg := defaultNotaryConfig() cfg := defaultNotaryConfig(c)
for _, opt := range opts { for _, opt := range opts {
opt(cfg) opt(cfg)
@ -77,6 +83,7 @@ func (c *Client) EnableNotarySupport(proxy, netmap util.Uint160, opts ...NotaryO
txValidTime: cfg.txValidTime, txValidTime: cfg.txValidTime,
roundTime: cfg.roundTime, roundTime: cfg.roundTime,
fallbackTime: cfg.fallbackTime, fallbackTime: cfg.fallbackTime,
alphabetSource: cfg.alphabetSource,
} }
return nil return nil
@ -203,7 +210,7 @@ func (c *Client) notaryInvokeAsCommittee(contract util.Uint160, method string, a
} }
func (c *Client) notaryInvoke(committee bool, contract util.Uint160, method string, args ...interface{}) error { func (c *Client) notaryInvoke(committee bool, contract util.Uint160, method string, args ...interface{}) error {
alphabetList, err := c.Committee() // prepare arguments for test invocation alphabetList, err := c.notary.alphabetSource() // prepare arguments for test invocation
if err != nil { if err != nil {
return err return err
} }
@ -472,6 +479,16 @@ func WithFallbackTime(t uint32) NotaryOption {
} }
} }
// WithAlphabetSource returns a notary support option for client
// that specifies function to return list of alphabet node keys.
// By default notary subsystem uses committee as a source. This is
// valid for side chain but notary in main chain should override it.
func WithAlphabetSource(t AlphabetKeys) NotaryOption {
return func(c *notaryCfg) {
c.alphabetSource = t
}
}
const alreadyOnChainErrorMessage = "already on chain" const alreadyOnChainErrorMessage = "already on chain"
// Neo RPC node can return `core.ErrInvalidAttribute` error with // Neo RPC node can return `core.ErrInvalidAttribute` error with