core: simplify code expression

This commit is contained in:
JukLee0ira 2024-06-03 16:10:15 +08:00
parent 8d1aa25de5
commit 9ffacc595d

View file

@ -32,7 +32,6 @@ import (
) )
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go //go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
var ( var (
ErrInvalidSig = errors.New("invalid transaction v, r, s values") ErrInvalidSig = errors.New("invalid transaction v, r, s values")
ErrUnexpectedProtection = errors.New("transaction type does not supported EIP-155 protected signatures") ErrUnexpectedProtection = errors.New("transaction type does not supported EIP-155 protected signatures")
@ -45,11 +44,11 @@ var (
errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction") errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction")
errEmptyTypedTx = errors.New("empty typed transaction bytes") errEmptyTypedTx = errors.New("empty typed transaction bytes")
errNoSigner = errors.New("missing signing methods") errNoSigner = errors.New("missing signing methods")
skipNonceDestinationAddress = map[string]bool{ skipNonceDestinationAddress = map[common.Address]bool{
common.XDCXAddr: true, common.XDCXAddrBinary: true,
common.TradingStateAddr: true, common.TradingStateAddrBinary: true,
common.XDCXLendingAddress: true, common.XDCXLendingAddressBinary: true,
common.XDCXLendingFinalizedTradeAddress: true, common.XDCXLendingFinalizedTradeAddressBinary: true,
} }
) )
@ -406,121 +405,68 @@ func (tx *Transaction) TxCost(number *big.Int) *big.Int {
} }
func (tx *Transaction) IsSpecialTransaction() bool { func (tx *Transaction) IsSpecialTransaction() bool {
if tx.To() == nil { to := tx.To()
return false return to != nil && (*to == common.RandomizeSMCBinary || *to == common.BlockSignersBinary)
}
toBytes := tx.To().Bytes()
randomizeSMCBytes := common.RandomizeSMCBinary.Bytes()
blockSignersBytes := common.BlockSignersBinary.Bytes()
return bytes.Equal(toBytes, randomizeSMCBytes) || bytes.Equal(toBytes, blockSignersBytes)
} }
func (tx *Transaction) IsTradingTransaction() bool { func (tx *Transaction) IsTradingTransaction() bool {
if tx.To() == nil { to := tx.To()
return false return to != nil && *to == common.XDCXAddrBinary
}
if *tx.To() != common.XDCXAddrBinary {
return false
}
return true
} }
func (tx *Transaction) IsLendingTransaction() bool { func (tx *Transaction) IsLendingTransaction() bool {
if tx.To() == nil { to := tx.To()
return false return to != nil && *to == common.XDCXLendingAddressBinary
}
if *tx.To() != common.XDCXLendingAddressBinary {
return false
}
return true
} }
func (tx *Transaction) IsLendingFinalizedTradeTransaction() bool { func (tx *Transaction) IsLendingFinalizedTradeTransaction() bool {
if tx.To() == nil { to := tx.To()
return false return to != nil && *to == common.XDCXLendingFinalizedTradeAddressBinary
}
if *tx.To() != common.XDCXLendingFinalizedTradeAddressBinary {
return false
}
return true
} }
func (tx *Transaction) IsSkipNonceTransaction() bool { func (tx *Transaction) IsSkipNonceTransaction() bool {
if tx.To() == nil { to := tx.To()
return false return to != nil && skipNonceDestinationAddress[*to]
}
if skip := skipNonceDestinationAddress[tx.To().String()]; skip {
return true
}
return false
} }
func (tx *Transaction) IsSigningTransaction() bool { func (tx *Transaction) IsSigningTransaction() bool {
if tx.To() == nil { to := tx.To()
if to == nil || *to != common.BlockSignersBinary {
return false return false
} }
data := tx.Data()
if *tx.To() != common.BlockSignersBinary { if len(data) != (32*2 + 4) {
return false return false
} }
method := common.ToHex(data[0:4])
method := common.ToHex(tx.Data()[0:4]) return method == common.SignMethod
if method != common.SignMethod {
return false
}
if len(tx.Data()) != (32*2 + 4) {
return false
}
return true
} }
func (tx *Transaction) IsVotingTransaction() (bool, *common.Address) { func (tx *Transaction) IsVotingTransaction() (bool, *common.Address) {
if tx.To() == nil { to := tx.To()
if to == nil || *to != common.MasternodeVotingSMCBinary {
return false, nil return false, nil
} }
b := (*tx.To() == common.MasternodeVotingSMCBinary) var end int
data := tx.Data()
if !b { method := common.ToHex(data[0:4])
return b, nil if method == common.VoteMethod || method == common.ProposeMethod || method == common.ResignMethod {
end = len(data)
} else if method == common.UnvoteMethod {
end = len(data) - 32
} else {
return false, nil
} }
method := common.ToHex(tx.Data()[0:4]) addr := data[end-20 : end]
if b = (method == common.VoteMethod); b { m := common.BytesToAddress(addr)
addr := tx.Data()[len(tx.Data())-20:] return true, &m
m := common.BytesToAddress(addr)
return b, &m
}
if b = (method == common.UnvoteMethod); b {
addr := tx.Data()[len(tx.Data())-32-20 : len(tx.Data())-32]
m := common.BytesToAddress(addr)
return b, &m
}
if b = (method == common.ProposeMethod); b {
addr := tx.Data()[len(tx.Data())-20:]
m := common.BytesToAddress(addr)
return b, &m
}
if b = (method == common.ResignMethod); b {
addr := tx.Data()[len(tx.Data())-20:]
m := common.BytesToAddress(addr)
return b, &m
}
return b, nil
} }
func (tx *Transaction) IsXDCXApplyTransaction() bool { func (tx *Transaction) IsXDCXApplyTransaction() bool {
if tx.To() == nil { to := tx.To()
if to == nil {
return false return false
} }
@ -528,26 +474,22 @@ func (tx *Transaction) IsXDCXApplyTransaction() bool {
if common.IsTestnet { if common.IsTestnet {
addr = common.XDCXListingSMCTestNet addr = common.XDCXListingSMCTestNet
} }
if *tx.To() != addr { if *to != addr {
return false return false
} }
data := tx.Data()
method := common.ToHex(tx.Data()[0:4])
if method != common.XDCXApplyMethod {
return false
}
// 4 bytes for function name // 4 bytes for function name
// 32 bytes for 1 parameter // 32 bytes for 1 parameter
if len(tx.Data()) != (32 + 4) { if len(data) != (32 + 4) {
return false return false
} }
return true method := common.ToHex(data[0:4])
return method == common.XDCXApplyMethod
} }
func (tx *Transaction) IsXDCZApplyTransaction() bool { func (tx *Transaction) IsXDCZApplyTransaction() bool {
if tx.To() == nil { to := tx.To()
if to == nil {
return false return false
} }
@ -555,22 +497,17 @@ func (tx *Transaction) IsXDCZApplyTransaction() bool {
if common.IsTestnet { if common.IsTestnet {
addr = common.TRC21IssuerSMCTestNet addr = common.TRC21IssuerSMCTestNet
} }
if *tx.To() != addr { if *to != addr {
return false return false
} }
data := tx.Data()
method := common.ToHex(tx.Data()[0:4])
if method != common.XDCZApplyMethod {
return false
}
// 4 bytes for function name // 4 bytes for function name
// 32 bytes for 1 parameter // 32 bytes for 1 parameter
if len(tx.Data()) != (32 + 4) { if len(data) != (32 + 4) {
return false return false
} }
method := common.ToHex(data[0:4])
return true return method == common.XDCZApplyMethod
} }
func (tx *Transaction) String() string { func (tx *Transaction) String() string {