validator

package
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2020 License: GPL-3.0, GPL-3.0 Imports: 23 Imported by: 0

README

验证模块

使用场景

验证模块的使用原则是不相信任何的输入,而输入主要包括P2P网络的输入和客户端输入。 验证模块主要是在P2P收到一个广播过来的Unit或者Tx的时候进行验证。另外gptn作为一个客户端时,在生成交易,并将交易添加到交易池时也需要对Tx进行验证。

Tx的验证

P2P收到一条新交易时的验证
本地交易池添加交易时的验证

Unit的验证

P2P收到一个新单元时的验证
  1. 验证Header是否正确
  2. 验证每一条Tx是否正确(如果已经在验证缓存,就不需要重复验证)
  3. 验证每一条Tx是否存在双花(这里不能用缓存)
  4. 验证手续费的分配是否正确(Coinbase验证)
双花交易的判断
  • 一笔交易里面包含的Input中引用了同一个UTXO
  • 一个单元里面包含的多个Tx中引用了同一个UTXO
  • 一笔交易引用的UTXO与交易池中的另一个交易相同

Documentation

Index

Constants

View Source
const ENABLE_CONTRACT_DEVELOPER_CHECK_TIME = 1577808000 //2020-1-1
View Source
const ENABLE_CONTRACT_RWSET_CHECK_TIME = 2588262400 //2020-5-1
View Source
const ENABLE_CONTRACT_SIGN_CHECK_TIME = 1588348800 //2019-12-1
View Source
const ENABLE_TX_FEE_CHECK_TIME = 1570870800 //2019-10-12 17:00:00
View Source
const ENABLE_TX_FULL_CHECK_TIME = 1588348800

Variables

This section is empty.

Functions

func IsOrphanError

func IsOrphanError(err error) bool

func NewValidateError

func NewValidateError(code ValidationCode) error

func ValidateUnitBasic added in v1.0.1

func ValidateUnitBasic(unit *modules.Unit) error

不基于数据库,进行Unit最基本的验证

Types

type BuildTempContractDagFunc added in v1.0.6

type BuildTempContractDagFunc func(dag dboperation.IContractDag) dboperation.IContractDag

type ContractTxCheckFunc added in v1.0.5

type ContractTxCheckFunc func(tx *modules.Transaction, rwM rwset.TxManager, dag dboperation.IContractDag) bool

type IDagQuery

type IDagQuery interface {
	GetTransactionOnly(hash common.Hash) (*modules.Transaction, error)
	IsTransactionExist(hash common.Hash) (bool, error)
	GetHeaderByHash(common.Hash) (*modules.Header, error)
	CheckReadSetValid(contractId []byte, readSet []modules.ContractReadSet) bool
}

type IPropQuery

type IPropQuery interface {
	GetSlotAtTime(when time.Time) uint32
	GetScheduledMediator(slotNum uint32) common.Address
	GetNewestUnitTimestamp(token modules.AssetId) (int64, error)
	GetNewestUnit(token modules.AssetId) (common.Hash, *modules.ChainIndex, error)
	GetChainParameters() *core.ChainParameters
}

type IStateQuery

type IStateQuery interface {
	GetContractTpl(tplId []byte) (*modules.ContractTemplate, error)
	GetContractJury(contractId []byte) (*modules.ElectionNode, error)
	GetContractState(id []byte, field string) ([]byte, *modules.StateVersion, error)
	GetContractStatesByPrefix(id []byte, prefix string) (map[string]*modules.ContractStateValue, error)
	GetMediators() map[common.Address]bool
	GetMediator(add common.Address) *core.Mediator
	GetBlacklistAddress() ([]common.Address, *modules.StateVersion, error)
	GetJurorByAddrHash(addrHash common.Hash) (*modules.JurorDeposit, error)
	GetJurorReward(jurorAdd common.Address) common.Address
	IsContractDeveloper(addr common.Address) bool
}

type IUtxoQuery

type IUtxoQuery interface {
	GetUtxoEntry(outpoint *modules.OutPoint) (*modules.Utxo, error)
	GetStxoEntry(outpoint *modules.OutPoint) (*modules.Stxo, error)
}

type Validate

type Validate struct {
	// contains filtered or unexported fields
}

func (*Validate) CheckTxIsExist

func (validate *Validate) CheckTxIsExist(tx *modules.Transaction) bool

func (*Validate) SetContractTxCheckFun added in v1.0.5

func (validate *Validate) SetContractTxCheckFun(checkFun ContractTxCheckFunc)
func (validate *Validate) ContractTxCheck(rwM rwset.TxManager, tx *modules.Transaction) bool {
	if validate.contractCheckFun != nil {
		return validate.contractCheckFun(rwM,dag, tx)
	}
	return true
}

func (*Validate) SetUtxoQuery added in v1.0.7

func (validate *Validate) SetUtxoQuery(query IUtxoQuery)
func (v *Validate) SetBuildTempContractDagFunc(buildFunc BuildTempContractDagFunc) {
	v.buildTempDagFunc = buildFunc
	log.Debug("SetBuildTempContractDagFunc ok")
}

func (*Validate) ValidateHeader

func (validate *Validate) ValidateHeader(h *modules.Header) ValidationCode

func (*Validate) ValidateTx

func (validate *Validate) ValidateTx(tx *modules.Transaction, isFullTx bool) ([]*modules.Addition, ValidationCode, error)

* 检查unit中所有交易的合法性,返回所有交易的交易费总和 check all transactions in one unit return all transactions' fee

func (validate *Validate) ValidateTransactions(txs modules.Transactions) error {
	code := validate.validateTransactions(txs)
	return NewValidateError(code)
}

验证一个Tx是否满足被接受的条件,并返回该Tx的手续费分配情况,验证结果,如果验证结果不是Valid,则err有值

func (*Validate) ValidateTxFeeEnough added in v1.0.4

func (validate *Validate) ValidateTxFeeEnough(tx *modules.Transaction, extSize float64, extTime float64) ValidationCode

extSize :byte, extTime :s

func (*Validate) ValidateUnitExceptGroupSig

func (validate *Validate) ValidateUnitExceptGroupSig(unit *modules.Unit) ValidationCode

* 验证Unit Validate unit(除群签名以外), 新生产的unit暂时还没有群签名

func (validate *Validate) ValidateUnit(unit *modules.Unit, isGenesis bool) byte {

func (*Validate) ValidateUnitGroupSign

func (validate *Validate) ValidateUnitGroupSign(h *modules.Header) error

todo 验证群签名接口,需要验证群签的正确性和有效性

type ValidationCode

type ValidationCode byte
const (
	TxValidationCode_VALID                        ValidationCode = 0
	TxValidationCode_INVALID_CONTRACT_TEMPLATE    ValidationCode = 1
	TxValidationCode_INVALID_FEE                  ValidationCode = 2
	TxValidationCode_BAD_COMMON_HEADER            ValidationCode = 3
	TxValidationCode_BAD_CREATOR_SIGNATURE        ValidationCode = 4
	TxValidationCode_INVALID_ENDORSER_TRANSACTION ValidationCode = 5
	TxValidationCode_INVALID_CONFIG_TRANSACTION   ValidationCode = 6
	TxValidationCode_UNSUPPORTED_TX_PAYLOAD       ValidationCode = 7
	TxValidationCode_BAD_PROPOSAL_TXID            ValidationCode = 8
	TxValidationCode_DUPLICATE_TXID               ValidationCode = 9
	TxValidationCode_ENDORSEMENT_POLICY_FAILURE   ValidationCode = 10
	TxValidationCode_MVCC_READ_CONFLICT           ValidationCode = 11
	TxValidationCode_PHANTOM_READ_CONFLICT        ValidationCode = 12
	TxValidationCode_UNKNOWN_TX_TYPE              ValidationCode = 13
	TxValidationCode_STATE_DATA_NOT_FOUND         ValidationCode = 14
	TxValidationCode_MARSHAL_TX_ERROR             ValidationCode = 15
	TxValidationCode_NIL_TXACTION                 ValidationCode = 16
	TxValidationCode_EXPIRED_CHAINCODE            ValidationCode = 17
	TxValidationCode_CHAINCODE_VERSION_CONFLICT   ValidationCode = 18
	TxValidationCode_BAD_HEADER_EXTENSION         ValidationCode = 19
	TxValidationCode_BAD_CHANNEL_HEADER           ValidationCode = 20
	TxValidationCode_BAD_RESPONSE_PAYLOAD         ValidationCode = 21
	TxValidationCode_BAD_RWSET                    ValidationCode = 22
	TxValidationCode_INVALID_SIGNATURE            ValidationCode = 23
	TxValidationCode_INVALID_CONTRACT_SIGN        ValidationCode = 24
	TxValidationCode_INVALID_MSG                  ValidationCode = 25
	TxValidationCode_INVALID_PAYMMENTLOAD         ValidationCode = 26
	TxValidationCode_INVALID_PAYMMENT_INPUT       ValidationCode = 27
	TxValidationCode_INVALID_PAYMMENT_INPUT_COUNT ValidationCode = 28
	TxValidationCode_INVALID_COINBASE             ValidationCode = 29
	TxValidationCode_ADDRESS_IN_BLACKLIST         ValidationCode = 30
	TxValidationCode_INVALID_AMOUNT               ValidationCode = 31
	TxValidationCode_INVALID_ASSET                ValidationCode = 32
	TxValidationCode_INVALID_CONTRACT             ValidationCode = 33
	TxValidationCode_INVALID_DATAPAYLOAD          ValidationCode = 34
	TxValidationCode_INVALID_DOUBLE_SPEND         ValidationCode = 35
	TxValidationCode_INVALID_TOKEN_STATUS         ValidationCode = 36
	TxValidationCode_NOT_COMPARE_SIZE             ValidationCode = 37
	TxValidationCode_NOT_TPL_DEVELOPER            ValidationCode = 38

	TxValidationCode_ORPHAN               ValidationCode = 255
	TxValidationCode_INVALID_OTHER_REASON ValidationCode = 251
	TxValidationCode_NOT_VALIDATED        ValidationCode = 250
	UNIT_STATE_AUTHOR_SIGNATURE_PASSED    ValidationCode = 101
	UNIT_STATE_INVALID_MEDIATOR_SCHEDULE  ValidationCode = 102
	UNIT_STATE_INVALID_AUTHOR_SIGNATURE   ValidationCode = 103
	UNIT_STATE_INVALID_GROUP_SIGNATURE    ValidationCode = 104
	UNIT_STATE_HAS_INVALID_TRANSACTIONS   ValidationCode = 105
	UNIT_STATE_INVALID_AUTHOR             ValidationCode = 106
	UNIT_STATE_INVALID_EXTRA_DATA         ValidationCode = 107
	UNIT_STATE_INVALID_HEADER             ValidationCode = 108
	UNIT_STATE_INVALID_HEADER_NUMBER      ValidationCode = 109
	UNIT_STATE_INVALID_HEADER_TXROOT      ValidationCode = 110
	UNIT_STATE_INVALID_HEADER_TIME        ValidationCode = 111
	UNIT_STATE_ORPHAN                     ValidationCode = 254
)

type Validator

type Validator interface {
	//验证一个交易是否是合法交易
	//isCoinbase
	//isFullTx这个交易是完整的可打包的交易,还是陪审团没处理或者正在处理中的交易
	ValidateTx(tx *modules.Transaction, isFullTx bool) ([]*modules.Addition, ValidationCode, error)
	//验证一个Unit中的所有交易是否是合法交易
	//ValidateTransactions(txs modules.Transactions) error
	//除了群签名外,验证Unit是否是合法Unit,包括其中的所有交易都会逐一验证
	ValidateUnitExceptGroupSig(unit *modules.Unit) ValidationCode
	//ValidateUnitExceptPayment(unit *modules.Unit) error
	//验证一个Header是否合法(Mediator签名有效),主要用于轻节点
	ValidateHeader(h *modules.Header) ValidationCode
	ValidateUnitGroupSign(h *modules.Header) error
	ValidateTxFeeEnough(tx *modules.Transaction, extSize float64, extTime float64) ValidationCode
	CheckTxIsExist(tx *modules.Transaction) bool

	SetContractTxCheckFun(checkFun ContractTxCheckFunc)
	SetUtxoQuery(query IUtxoQuery)
}

func NewValidate

func NewValidate(dagdb IDagQuery, utxoRep IUtxoQuery, statedb IStateQuery, propquery IPropQuery,
	contractDag dboperation.IContractDag,
	cache palletcache.ICache, light bool, enableGasFee bool) Validator

func NewValidatorAllPass added in v1.0.6

func NewValidatorAllPass(dagdb IDagQuery, utxoRep IUtxoQuery, statedb IStateQuery, propquery IPropQuery,
	contractDag dboperation.IContractDag,
	cache palletcache.ICache, light, enableGasFee bool) Validator

type ValidatorAllPass added in v1.0.3

type ValidatorAllPass struct {
}

ValidatorAllPass used for UT

func (*ValidatorAllPass) CheckTxIsExist added in v1.0.3

func (v *ValidatorAllPass) CheckTxIsExist(tx *modules.Transaction) bool

func (*ValidatorAllPass) SetContractTxCheckFun added in v1.0.6

func (v *ValidatorAllPass) SetContractTxCheckFun(checkFun ContractTxCheckFunc)

func (*ValidatorAllPass) SetUtxoQuery added in v1.0.7

func (v *ValidatorAllPass) SetUtxoQuery(query IUtxoQuery)

func (*ValidatorAllPass) ValidateHeader added in v1.0.3

func (v *ValidatorAllPass) ValidateHeader(h *modules.Header) ValidationCode

ValidateUnitExceptPayment(unit *modules.Unit) error 验证一个Header是否合法(Mediator签名有效)

func (*ValidatorAllPass) ValidateTx added in v1.0.3

func (v *ValidatorAllPass) ValidateTx(tx *modules.Transaction, isFullTx bool) ([]*modules.Addition, ValidationCode, error)

func (*ValidatorAllPass) ValidateTxFeeEnough added in v1.0.6

func (v *ValidatorAllPass) ValidateTxFeeEnough(tx *modules.Transaction, extSize float64, extTime float64) ValidationCode

func (*ValidatorAllPass) ValidateUnitExceptGroupSig added in v1.0.3

func (v *ValidatorAllPass) ValidateUnitExceptGroupSig(unit *modules.Unit) ValidationCode

验证一个Unit中的所有交易是否是合法交易 ValidateTransactions(txs modules.Transactions) error 除了群签名外,验证Unit是否是合法Unit,包括其中的所有交易都会逐一验证

func (*ValidatorAllPass) ValidateUnitGroupSign added in v1.0.3

func (v *ValidatorAllPass) ValidateUnitGroupSign(h *modules.Header) error

type ValidatorBuilderFunc added in v1.0.6

type ValidatorBuilderFunc func(dagdb IDagQuery, utxoRep IUtxoQuery, statedb IStateQuery, propquery IPropQuery,
	contractDag dboperation.IContractDag,
	cache palletcache.ICache, light bool,
	enableGasFee bool) Validator

type ValidatorCache added in v1.0.1

type ValidatorCache struct {
	// contains filtered or unexported fields
}

如果已经验证通过的对象,那么不需要重复做全部验证

func NewValidatorCache added in v1.0.1

func NewValidatorCache(cache palletcache.ICache) *ValidatorCache

func (*ValidatorCache) AddHeaderValidateResult added in v1.0.1

func (s *ValidatorCache) AddHeaderValidateResult(unitHash common.Hash, code ValidationCode)

func (*ValidatorCache) AddTxValidateResult added in v1.0.1

func (s *ValidatorCache) AddTxValidateResult(txId common.Hash, validateResult []*modules.Addition)

func (*ValidatorCache) AddUnitValidateResult added in v1.0.1

func (s *ValidatorCache) AddUnitValidateResult(unitHash common.Hash, code ValidationCode)

func (*ValidatorCache) HasHeaderValidateResult added in v1.0.1

func (s *ValidatorCache) HasHeaderValidateResult(unitHash common.Hash) (bool, ValidationCode)

func (*ValidatorCache) HasTxValidateResult added in v1.0.1

func (s *ValidatorCache) HasTxValidateResult(txId common.Hash) (bool, []*modules.Addition)

func (*ValidatorCache) HasUnitValidateResult added in v1.0.1

func (s *ValidatorCache) HasUnitValidateResult(unitHash common.Hash) (bool, ValidationCode)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL