From 33e1e61343c273e30b924d4f13494a9e5668da3b Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Mon, 17 May 2021 11:07:08 +0300 Subject: [PATCH] config: make `MaxValidUntilBlockIncrement` configurable --- pkg/config/protocol_config.go | 4 ++++ pkg/core/blockchain.go | 9 ++++++++- pkg/core/helper_test.go | 2 +- pkg/core/native/notary.go | 5 +++-- pkg/core/native_notary_test.go | 2 +- pkg/core/transaction/transaction.go | 4 ---- pkg/services/oracle/response.go | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/config/protocol_config.go b/pkg/config/protocol_config.go index 41711eb21..063bd17bb 100644 --- a/pkg/config/protocol_config.go +++ b/pkg/config/protocol_config.go @@ -26,6 +26,10 @@ type ( MaxTraceableBlocks uint32 `yaml:"MaxTraceableBlocks"` // MaxTransactionsPerBlock is the maximum amount of transactions per block. MaxTransactionsPerBlock uint16 `yaml:"MaxTransactionsPerBlock"` + // MaxValidUntilBlockIncrement is the upper increment size of blockchain height in blocks + // exceeding that a transaction should fail validation. It is set to estimated daily number + // of blocks with 15s interval. + MaxValidUntilBlockIncrement uint32 `yaml:"MaxValidUntilBlockIncrement"` // NativeUpdateHistories is the list of histories of native contracts updates. NativeUpdateHistories map[string][]uint32 `yaml:"NativeActivations"` // P2PSigExtensions enables additional signature-related logic. diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 9aeb78a93..91ed7da26 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -190,6 +190,13 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L log.Info("MaxTransactionsPerBlock is not set or wrong, using default value", zap.Uint16("MaxTransactionsPerBlock", cfg.MaxTransactionsPerBlock)) } + if cfg.MaxValidUntilBlockIncrement == 0 { + const secondsPerDay = int(24 * time.Hour / time.Second) + + cfg.MaxValidUntilBlockIncrement = uint32(secondsPerDay / cfg.SecondsPerBlock) + log.Info("MaxValidUntilBlockIncrement is not set or wrong, using default value", + zap.Uint32("MaxValidUntilBlockIncrement", cfg.MaxValidUntilBlockIncrement)) + } committee, err := committeeFromConfig(cfg) if err != nil { return nil, err @@ -1426,7 +1433,7 @@ func (bc *Blockchain) verifyAndPoolTx(t *transaction.Transaction, pool *mempool. height := bc.BlockHeight() isPartialTx := data != nil - if t.ValidUntilBlock <= height || !isPartialTx && t.ValidUntilBlock > height+transaction.MaxValidUntilBlockIncrement { + if t.ValidUntilBlock <= height || !isPartialTx && t.ValidUntilBlock > height+bc.config.MaxValidUntilBlockIncrement { return fmt.Errorf("%w: ValidUntilBlock = %d, current height = %d", ErrTxExpired, t.ValidUntilBlock, height) } // Policying. diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 24c8cafd1..17b9a87ce 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -210,7 +210,7 @@ func TestCreateBasicChain(t *testing.T) { // Prepare some transaction for future submission. txSendRaw := newNEP17Transfer(bc.contracts.NEO.Hash, priv0ScriptHash, priv1.GetScriptHash(), int64(fixedn.Fixed8FromInt64(1000))) - txSendRaw.ValidUntilBlock = transaction.MaxValidUntilBlockIncrement + txSendRaw.ValidUntilBlock = bc.config.MaxValidUntilBlockIncrement txSendRaw.Nonce = 0x1234 txSendRaw.Signers = []transaction.Signer{{ Account: priv0ScriptHash, diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index 269051784..e29224a0c 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -392,8 +392,9 @@ func (n *Notary) GetMaxNotValidBeforeDelta(dao dao.DAO) uint32 { // setMaxNotValidBeforeDelta is Notary contract method and sets the maximum NotValidBefore delta. func (n *Notary) setMaxNotValidBeforeDelta(ic *interop.Context, args []stackitem.Item) stackitem.Item { value := toUint32(args[0]) - if value > transaction.MaxValidUntilBlockIncrement/2 || value < uint32(ic.Chain.GetConfig().ValidatorsCount) { - panic(fmt.Errorf("MaxNotValidBeforeDelta cannot be more than %d or less than %d", transaction.MaxValidUntilBlockIncrement/2, ic.Chain.GetConfig().ValidatorsCount)) + maxInc := ic.Chain.GetConfig().MaxValidUntilBlockIncrement + if value > maxInc/2 || value < uint32(ic.Chain.GetConfig().ValidatorsCount) { + panic(fmt.Errorf("MaxNotValidBeforeDelta cannot be more than %d or less than %d", maxInc/2, ic.Chain.GetConfig().ValidatorsCount)) } if !n.NEO.checkCommittee(ic) { panic("invalid committee signature") diff --git a/pkg/core/native_notary_test.go b/pkg/core/native_notary_test.go index 22d17f1e7..ddce9f085 100644 --- a/pkg/core/native_notary_test.go +++ b/pkg/core/native_notary_test.go @@ -326,5 +326,5 @@ func TestMaxNotValidBeforeDelta(t *testing.T) { chain := newTestChain(t) testGetSet(t, chain, chain.contracts.Notary.Hash, "MaxNotValidBeforeDelta", - 140, int64(chain.GetConfig().ValidatorsCount), transaction.MaxValidUntilBlockIncrement/2) + 140, int64(chain.GetConfig().ValidatorsCount), int64(chain.config.MaxValidUntilBlockIncrement/2)) } diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index 893c72024..25809657c 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -19,10 +19,6 @@ const ( // MaxTransactionSize is the upper limit size in bytes that a transaction can reach. It is // set to be 102400. MaxTransactionSize = 102400 - // MaxValidUntilBlockIncrement is the upper increment size of blockhain height in blocks - // exceeding that a transaction should fail validation. It is set to estimated daily number - // of blocks with 15s interval. - MaxValidUntilBlockIncrement = 5760 // MaxAttributes is maximum number of attributes including signers that can be contained // within a transaction. It is set to be 16. MaxAttributes = 16 diff --git a/pkg/services/oracle/response.go b/pkg/services/oracle/response.go index fea159b10..aef834c29 100644 --- a/pkg/services/oracle/response.go +++ b/pkg/services/oracle/response.go @@ -84,7 +84,7 @@ func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) { func (o *Oracle) CreateResponseTx(gasForResponse int64, height uint32, resp *transaction.OracleResponse) (*transaction.Transaction, error) { tx := transaction.New(o.oracleResponse, 0) tx.Nonce = uint32(resp.ID) - tx.ValidUntilBlock = height + transaction.MaxValidUntilBlockIncrement + tx.ValidUntilBlock = height + o.Chain.GetConfig().MaxValidUntilBlockIncrement tx.Attributes = []transaction.Attribute{{ Type: transaction.OracleResponseT, Value: resp,