config: move notary module config to ApplicationConfiguration

This commit is contained in:
Anna Shaleva 2021-02-16 13:49:56 +03:00
parent 4d0681d898
commit bcb82b457d
9 changed files with 48 additions and 46 deletions

View file

@ -27,4 +27,5 @@ type ApplicationConfiguration struct {
RPC rpc.Config `yaml:"RPC"`
UnlockWallet Wallet `yaml:"UnlockWallet"`
Oracle OracleConfiguration `yaml:"Oracle"`
P2PNotary P2PNotary `yaml:"P2PNotary"`
}

View file

@ -20,8 +20,6 @@ type (
RemoveUntraceableBlocks bool `yaml:"RemoveUntraceableBlocks"`
// MaxTraceableBlocks is the length of the chain accessible to smart contracts.
MaxTraceableBlocks uint32 `yaml:"MaxTraceableBlocks"`
// P2PNotary stores configuration for P2P notary node service
P2PNotary P2PNotary `yaml:"P2PNotary"`
// P2PSigExtensions enables additional signature-related logic.
P2PSigExtensions bool `yaml:"P2PSigExtensions"`
// ReservedAttributes allows to have reserved attributes range for experimental or private purposes.

View file

@ -34,15 +34,20 @@ import (
const notaryModulePath = "../services/notary/"
func getTestNotary(t *testing.T, bc *Blockchain, walletPath, pass string, onTx func(tx *transaction.Transaction) error) (*wallet.Account, *notary.Notary, *mempool.Pool) {
bc.config.P2PNotary = config.P2PNotary{
mainCfg := config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: path.Join(notaryModulePath, walletPath),
Password: pass,
},
}
cfg := notary.Config{
MainCfg: mainCfg,
Chain: bc,
Log: zaptest.NewLogger(t),
}
mp := mempool.New(10, 1, true)
ntr, err := notary.NewNotary(bc, mp, zaptest.NewLogger(t), onTx)
ntr, err := notary.NewNotary(cfg, mp, onTx)
require.NoError(t, err)
w, err := wallet.NewWalletFromFile(path.Join(notaryModulePath, walletPath))

View file

@ -139,14 +139,19 @@ func newServerFromConstructors(config ServerConfig, chain blockchainer.Blockchai
}
if chain.P2PSigExtensionsEnabled() {
s.notaryFeer = NewNotaryFeer(chain)
s.notaryRequestPool = mempool.New(chain.GetConfig().P2PNotaryRequestPayloadPoolSize, 1, chain.GetConfig().P2PNotary.Enabled)
s.notaryRequestPool = mempool.New(chain.GetConfig().P2PNotaryRequestPayloadPoolSize, 1, config.P2PNotaryCfg.Enabled)
chain.RegisterPostBlock(func(bc blockchainer.Blockchainer, txpool *mempool.Pool, _ *block.Block) {
s.notaryRequestPool.RemoveStale(func(t *transaction.Transaction) bool {
return bc.IsTxStillRelevant(t, txpool, true)
}, s.notaryFeer)
})
if chain.GetConfig().P2PNotary.Enabled {
n, err := notary.NewNotary(chain, s.notaryRequestPool, s.log, func(tx *transaction.Transaction) error {
if config.P2PNotaryCfg.Enabled {
cfg := notary.Config{
MainCfg: config.P2PNotaryCfg,
Chain: chain,
Log: log,
}
n, err := notary.NewNotary(cfg, s.notaryRequestPool, func(tx *transaction.Transaction) error {
r := s.RelayTxn(tx)
if r != RelaySucceed {
return fmt.Errorf("can't pool notary tx: hash %s, reason: %d", tx.Hash().StringLE(), byte(r))
@ -159,7 +164,7 @@ func newServerFromConstructors(config ServerConfig, chain blockchainer.Blockchai
s.notaryModule = n
chain.SetNotary(n)
}
} else if chain.GetConfig().P2PNotary.Enabled {
} else if config.P2PNotaryCfg.Enabled {
return nil, errors.New("P2PSigExtensions are disabled, but Notary service is enable")
}
s.bQueue = newBlockQueue(maxBlockBatch, chain, log, func(b *block.Block) {

View file

@ -69,6 +69,9 @@ type (
// OracleCfg is oracle module configuration.
OracleCfg config.OracleConfiguration
// P2PNotaryCfg is notary module configuration.
P2PNotaryCfg config.P2PNotary
}
)
@ -100,5 +103,6 @@ func NewServerConfig(cfg config.Config) ServerConfig {
Wallet: wc,
TimePerBlock: time.Duration(protoConfig.SecondsPerBlock) * time.Second,
OracleCfg: appConfig.Oracle,
P2PNotaryCfg: appConfig.P2PNotary,
}
}

View file

@ -39,7 +39,7 @@ func getUnitTestChain(t *testing.T, enableOracle bool, enableNotary bool) (*core
if enableNotary {
cfg.ProtocolConfiguration.P2PSigExtensions = true
cfg.ProtocolConfiguration.P2PNotaryRequestPayloadPoolSize = 1000
cfg.ProtocolConfiguration.P2PNotary = config.P2PNotary{
cfg.ApplicationConfiguration.P2PNotary = config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: notaryPath,
@ -47,7 +47,7 @@ func getUnitTestChain(t *testing.T, enableOracle bool, enableNotary bool) (*core
},
}
} else {
cfg.ProtocolConfiguration.P2PNotary.Enabled = false
cfg.ApplicationConfiguration.P2PNotary.Enabled = false
}
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, logger)
require.NoError(t, err, "could not create chain")

View file

@ -14,7 +14,7 @@ import (
)
func getTestNotary(t *testing.T, bc blockchainer.Blockchainer, walletPath, pass string) (*wallet.Account, *Notary, *mempool.Pool) {
bc.(*fakechain.FakeChain).ProtocolConfiguration.P2PNotary = config.P2PNotary{
mainCfg := config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: walletPath,
@ -22,7 +22,12 @@ func getTestNotary(t *testing.T, bc blockchainer.Blockchainer, walletPath, pass
},
}
mp := mempool.New(10, 1, true)
ntr, err := NewNotary(bc, mp, zaptest.NewLogger(t), nil)
cfg := Config{
MainCfg: mainCfg,
Chain: bc,
Log: zaptest.NewLogger(t),
}
ntr, err := NewNotary(cfg, mp, nil)
require.NoError(t, err)
w, err := wallet.NewWalletFromFile(walletPath)

View file

@ -80,9 +80,8 @@ type request struct {
}
// NewNotary returns new Notary module.
func NewNotary(bc blockchainer.Blockchainer, mp *mempool.Pool, log *zap.Logger, onTransaction func(tx *transaction.Transaction) error) (*Notary, error) {
cfg := bc.GetConfig().P2PNotary
w := cfg.UnlockWallet
func NewNotary(cfg Config, mp *mempool.Pool, onTransaction func(tx *transaction.Transaction) error) (*Notary, error) {
w := cfg.MainCfg.UnlockWallet
wallet, err := wallet.NewWalletFromFile(w.Path)
if err != nil {
return nil, err
@ -100,12 +99,8 @@ func NewNotary(bc blockchainer.Blockchainer, mp *mempool.Pool, log *zap.Logger,
}
return &Notary{
requests: make(map[util.Uint256]*request),
Config: Config{
MainCfg: cfg,
Chain: bc,
Log: log,
},
requests: make(map[util.Uint256]*request),
Config: cfg,
wallet: wallet,
onTransaction: onTransaction,
mp: mp,

View file

@ -19,40 +19,29 @@ import (
func TestWallet(t *testing.T) {
bc := fakechain.NewFakeChain()
mainCfg := config.P2PNotary{Enabled: true}
cfg := Config{
MainCfg: mainCfg,
Chain: bc,
Log: zaptest.NewLogger(t),
}
t.Run("unexisting wallet", func(t *testing.T) {
bc.ProtocolConfiguration.P2PNotary = config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: "./testdata/does_not_exists.json",
Password: "one",
},
}
_, err := NewNotary(bc, mempool.New(1, 1, true), zaptest.NewLogger(t), nil)
cfg.MainCfg.UnlockWallet.Path = "./testdata/does_not_exists.json"
_, err := NewNotary(cfg, mempool.New(1, 1, true), nil)
require.Error(t, err)
})
t.Run("bad password", func(t *testing.T) {
bc.ProtocolConfiguration.P2PNotary = config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: "./testdata/notary1.json",
Password: "invalid",
},
}
_, err := NewNotary(bc, mempool.New(1, 1, true), zaptest.NewLogger(t), nil)
cfg.MainCfg.UnlockWallet.Path = "./testdata/notary1.json"
cfg.MainCfg.UnlockWallet.Password = "invalid"
_, err := NewNotary(cfg, mempool.New(1, 1, true), nil)
require.Error(t, err)
})
t.Run("good", func(t *testing.T) {
bc.ProtocolConfiguration.P2PNotary = config.P2PNotary{
Enabled: true,
UnlockWallet: config.Wallet{
Path: "./testdata/notary1.json",
Password: "one",
},
}
_, err := NewNotary(bc, mempool.New(1, 1, true), zaptest.NewLogger(t), nil)
cfg.MainCfg.UnlockWallet.Path = "./testdata/notary1.json"
cfg.MainCfg.UnlockWallet.Password = "one"
_, err := NewNotary(cfg, mempool.New(1, 1, true), nil)
require.NoError(t, err)
})
}