dao: move conflict record value length to a separate const
Conflicts-related code contains more and more these magic numbers, and there's no good in it even if all the usages are commented. This approach produces bugs like #3426. No functional changes, just a refactoring. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
parent
9a1075d332
commit
2ad4c86712
1 changed files with 10 additions and 5 deletions
|
@ -35,6 +35,11 @@ var (
|
||||||
ErrInternalDBInconsistency = errors.New("internal DB inconsistency")
|
ErrInternalDBInconsistency = errors.New("internal DB inconsistency")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// conflictRecordValueLen is the length of value of transaction conflict record.
|
||||||
|
// It consists of 1-byte [storage.ExecTransaction] prefix and 4-bytes block index
|
||||||
|
// in the LE form.
|
||||||
|
const conflictRecordValueLen = 1 + 4
|
||||||
|
|
||||||
// Simple is memCached wrapper around DB, simple DAO implementation.
|
// Simple is memCached wrapper around DB, simple DAO implementation.
|
||||||
type Simple struct {
|
type Simple struct {
|
||||||
Version Version
|
Version Version
|
||||||
|
@ -323,7 +328,7 @@ func (dao *Simple) GetTxExecResult(hash util.Uint256) (uint32, *transaction.Tran
|
||||||
// decodeTxAndExecResult decodes transaction, its height and execution result from
|
// decodeTxAndExecResult decodes transaction, its height and execution result from
|
||||||
// the given executable bytes. It performs no executable prefix check.
|
// the given executable bytes. It performs no executable prefix check.
|
||||||
func decodeTxAndExecResult(buf []byte) (uint32, *transaction.Transaction, *state.AppExecResult, error) {
|
func decodeTxAndExecResult(buf []byte) (uint32, *transaction.Transaction, *state.AppExecResult, error) {
|
||||||
if len(buf) == 1+4 { // conflict record stub.
|
if len(buf) == conflictRecordValueLen { // conflict record stub.
|
||||||
return 0, nil, nil, storage.ErrKeyNotFound
|
return 0, nil, nil, storage.ErrKeyNotFound
|
||||||
}
|
}
|
||||||
r := io.NewBinReaderFromBuf(buf)
|
r := io.NewBinReaderFromBuf(buf)
|
||||||
|
@ -619,7 +624,7 @@ func (dao *Simple) GetTransaction(hash util.Uint256) (*transaction.Transaction,
|
||||||
// It may be a block.
|
// It may be a block.
|
||||||
return nil, 0, storage.ErrKeyNotFound
|
return nil, 0, storage.ErrKeyNotFound
|
||||||
}
|
}
|
||||||
if len(b) == 1+4 { // storage.ExecTransaction + index
|
if len(b) == conflictRecordValueLen {
|
||||||
// It's a conflict record stub.
|
// It's a conflict record stub.
|
||||||
return nil, 0, storage.ErrKeyNotFound
|
return nil, 0, storage.ErrKeyNotFound
|
||||||
}
|
}
|
||||||
|
@ -699,7 +704,7 @@ func (dao *Simple) HasTransaction(hash util.Uint256, signers []transaction.Signe
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bytes) < 5 { // (storage.ExecTransaction + index) for conflict record
|
if len(bytes) < conflictRecordValueLen { // (storage.ExecTransaction + index) for conflict record
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if bytes[0] != storage.ExecTransaction {
|
if bytes[0] != storage.ExecTransaction {
|
||||||
|
@ -708,7 +713,7 @@ func (dao *Simple) HasTransaction(hash util.Uint256, signers []transaction.Signe
|
||||||
// we need to adjust Go behaviour post-factum. Ref. #3427 and 0x289c235dcdab8be7426d05f0fbb5e86c619f81481ea136493fa95deee5dbb7cc.
|
// we need to adjust Go behaviour post-factum. Ref. #3427 and 0x289c235dcdab8be7426d05f0fbb5e86c619f81481ea136493fa95deee5dbb7cc.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len(bytes) != 5 {
|
if len(bytes) != conflictRecordValueLen {
|
||||||
return ErrAlreadyExists // fully-qualified transaction
|
return ErrAlreadyExists // fully-qualified transaction
|
||||||
}
|
}
|
||||||
if len(signers) == 0 {
|
if len(signers) == 0 {
|
||||||
|
@ -864,7 +869,7 @@ func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32,
|
||||||
val := buf.Bytes()
|
val := buf.Bytes()
|
||||||
dao.Store.Put(key, val)
|
dao.Store.Put(key, val)
|
||||||
|
|
||||||
val = val[:5] // storage.ExecTransaction (1 byte) + index (4 bytes)
|
val = val[:conflictRecordValueLen] // storage.ExecTransaction (1 byte) + index (4 bytes)
|
||||||
attrs := tx.GetAttributes(transaction.ConflictsT)
|
attrs := tx.GetAttributes(transaction.ConflictsT)
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
// Conflict record stub.
|
// Conflict record stub.
|
||||||
|
|
Loading…
Reference in a new issue