transaction

package
v0.0.0-...-7ece11e Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 15 Imported by: 9

README

Notes

This is the note for developing transaction package. The devs should read these notes when implement new type of tx or new version of tx.

Based on the design of TxBase, TxBase can be treated as metadata.Transaction and can call the right version itself. The way it figures out which version to delegate into is simply checking its .Version attribute.

However, if the txVersion has not implemented the required function, the chain may self-loop recursively and crash.

Some noted functions that need to be implemented when embed TxBase:

  • CheckAuthorizedSender
  • Init
  • Verify
  • ValidateTxSalary (must code this even if that version dont have initTxSalary)

When adding new transaction version, please take a look at NewTxPrivacyFromVersionNumber.

Documentation

Overview

Package transaction manages the creation & verification of all transaction types

Index

Constants

View Source
const (
	UnexpectedError = iota
	WrongTokenTxTypeError
	CustomTokenExistedError
	WrongInputError
	WrongSigError
	DoubleSpendError
	TxNotExistError
	RandomCommitmentError
	InvalidSanityDataPRVError
	InvalidSanityDataPrivacyTokenError
	InvalidDoubleSpendPRVError
	InvalidDoubleSpendPrivacyTokenError
	InputCoinIsVeryLargeError
	PaymentInfoIsVeryLargeError
	SumInputCoinsAndOutputCoinsError
	InvalidInputCoinVersionErr
	TokenIDInvalidError
	TokenIDExistedError
	TokenIDExistedByCrossShardError
	PrivateKeySenderInvalidError
	SignTxError
	DecompressPaymentAddressError
	CanNotGetCommitmentFromIndexError
	CanNotDecompressCommitmentFromIndexError
	InitWithnessError
	WithnessProveError
	EncryptOutputError
	DecompressSigPubKeyError
	InitTxSignatureFromBytesError
	VerifyTxSigFailError
	DuplicatedOutputSndError
	SndExistedError
	InputCommitmentIsNotExistedError
	TxProofVerifyFailError
	VerifyOneOutOfManyProofFailedErr
	BatchTxProofVerifyFailError
	VerifyMinerCreatedTxBeforeGettingInBlockError
	CommitOutputCoinError
	GetShardIDByPublicKeyError

	NormalTokenPRVJsonError
	NormalTokenJsonError //nolint:revive

	PrivacyTokenInitFeeParamsError
	PrivacyTokenInitPRVError
	PrivacyTokenInitTokenDataError
	PrivacyTokenPRVJsonError
	PrivacyTokenJsonError //nolint:revive
	PrivacyTokenTxTypeNotHandleError

	ExceedSizeTx
	ExceedSizeInfoTxError
	ExceedSizeInfoOutCoinError

	RejectInvalidLockTime
	RejectTxSize
	RejectTxVersion
	RejectTxPublickeySigSize
	RejectTxType
	RejectTxInfoSize
	RejectTxMedataWithBlockChain

	GetCommitmentsInDatabaseError
	InvalidPaymentAddressError
	OnetimeAddressAlreadyExists
)
View Source
const (
	NormalCoinType                   = utils.NormalCoinType
	CustomTokenPrivacyType           = utils.CustomTokenPrivacyType
	CustomTokenInit                  = utils.CustomTokenInit
	CustomTokenTransfer              = utils.CustomTokenTransfer
	CustomTokenCrossShard            = utils.CustomTokenCrossShard
	CurrentTxVersion                 = utils.CurrentTxVersion
	TxVersion0Number                 = utils.TxVersion0Number
	TxVersion1Number                 = utils.TxVersion1Number
	TxVersion2Number                 = utils.TxVersion2Number
	TxConversionVersion12Number      = utils.TxConversionVersion12Number
	ValidateTimeForOneoutOfManyProof = utils.ValidateTimeForOneoutOfManyProof
	MaxSizeInfo                      = utils.MaxSizeInfo
	MaxSizeUint32                    = utils.MaxSizeUint32
	MaxSizeByte                      = utils.MaxSizeByte
)

Variables

View Source
var ErrCodeMessage = utils.ErrCodeMessage
View Source
var Logger = &utils.Logger

Logger is the logger instance for this package

Functions

func BuildCoinBaseTxByCoinID

func BuildCoinBaseTxByCoinID(params *BuildCoinBaseTxByCoinIDParams) (metadata.Transaction, error)

BuildCoinBaseTxByCoinID is used to create a salary transaction. It must take its own defined parameter struct. Deprecated: this is not used in v2 code

func EstimateTxSize

func EstimateTxSize(estimateTxSizeParam *tx_generic.EstimateTxSizeParam) uint64

EstimateTxSize gives an estimate of TX size based on the number of inputs, outputs, token parameters & metadata. It handles both version 1 & version 2 TXs. Since the transaction has not been created yet, we estimate using the upper bound of coin & proof sizes.

func GetFullBurnData

func GetFullBurnData(tx metadata.Transaction) (bool, coin.Coin, coin.Coin, *common.Hash, error)

GetFullBurnData is a helper that indicates whether or not a TX is burning any coin. Returned values beyond the first will contain the coins (PRV or pToken) being burnt and its token ID.

func GetTxTokenDataFromTransaction

func GetTxTokenDataFromTransaction(tx metadata.Transaction) *tx_generic.TxTokenData

GetTxTokenDataFromTransaction is an alternative getter for the TokenData field. It takes the most generic TX interface and casts it to a token transaction. Upon any error, it returns nil.

func GetTxVersionFromCoins

func GetTxVersionFromCoins(coins []privacy.PlainCoin) (int8, error)

func InitConversion

func InitConversion(tx *TxVersion2, params *TxConvertVer1ToVer2InitParams) error

func InitTokenConversion

func InitTokenConversion(txToken *TxTokenVersion2, params *TxTokenConvertVer1ToVer2InitParams) error

func NewBatchTransaction

func NewBatchTransaction(txs []metadata.Transaction) *batchTransaction

NewBatchTransaction creates a batchTransaction object from the given TX array. Batched transactions save verification time by batching many Bulletproof verifications together in one multi-exponent operation.

One can then call ".Validate(" to validate all TXs in this batch. This does not cover sanity checks & double-spend checks, those are handled separately. The batch can have transactions from both versions.

Outside of Bulletproofs, other verification steps are done normally. Batching is applicable to PRV transfers, not pToken transfers.

func NewRandomCommitmentsProcessParam

func NewRandomCommitmentsProcessParam(usableInputCoins []privacy.PlainCoin, randNum int, stateDB *statedb.StateDB, shardID byte, tokenID *common.Hash) *tx_generic.RandomCommitmentsProcessParam

func NewTransactionFromJsonBytes

func NewTransactionFromJsonBytes(data []byte) (metadata.Transaction, error)

NewTransactionFromJsonBytes parses a transaction from raw JSON, assuming it is a PRV transfer. This is a legacy function; it will be replaced by DeserializeTransactionJSON.

func NewTransactionFromParams

func NewTransactionFromParams(params *tx_generic.TxPrivacyInitParams) (metadata.Transaction, error)

NewTransactionFromParams is a helper that returns a new transaction whose version matches the parameter object. The result is a PRV-transfer transaction that only has the version ready, nothing else. One needs to call ".Init(params)" after this to get a valid TX.

func NewTransactionTokenFromJsonBytes

func NewTransactionTokenFromJsonBytes(data []byte) (tx_generic.TransactionToken, error)

NewTransactionTokenFromJsonBytes parses a transaction from raw JSON, assuming it is a pToken transfer. This is a legacy function; it will be replaced by DeserializeTransactionJSON.

func NewTransactionTokenFromParams

func NewTransactionTokenFromParams(params *tx_generic.TxTokenParams) (tx_generic.TransactionToken, error)

NewTransactionTokenFromParams is a helper that returns a new transaction whose version matches the parameter object. The result is a pToken transaction that only has the version ready, nothing else. One needs to call ".Init(params)" after this to get a valid TX.

func RandomCommitmentsProcess

func RandomCommitmentsProcess(param *tx_generic.RandomCommitmentsProcessParam) (commitmentIndexs []uint64, myCommitmentIndexs []uint64, commitments [][]byte)

func WithBeaconHeight

func WithBeaconHeight(vE metadata.ValidationEnviroment, bcHeight uint64) *tx_generic.ValidationEnv

func WithConfirmedTime

func WithConfirmedTime(vE metadata.ValidationEnviroment, confirmedTime int64) *tx_generic.ValidationEnv

Types

type BuildCoinBaseTxByCoinIDParams

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

BuildCoinBaseTxByCoinIDParams defines the parameters for BuildCoinBaseTxByCoinID

func NewBuildCoinBaseTxByCoinIDParams

func NewBuildCoinBaseTxByCoinIDParams(payToAddress *privacy.PaymentAddress,
	amount uint64,
	payByPrivateKey *privacy.PrivateKey,
	stateDB *statedb.StateDB,
	meta *metadata.WithDrawRewardResponse,
	coinID common.Hash,
	txType int,
	coinName string,
	shardID byte,
	bridgeStateDB *statedb.StateDB) *BuildCoinBaseTxByCoinIDParams

type EstimateTxSizeParam

type EstimateTxSizeParam = tx_generic.EstimateTxSizeParam

func NewEstimateTxSizeParam

func NewEstimateTxSizeParam(version, numInputCoins, numPayments int,
	hasPrivacy bool, metadata metadata.Metadata,
	privacyCustomTokenParams *TokenParam,
	limitFee uint64) *EstimateTxSizeParam

type OTADeclaration

type OTADeclaration = metadata.OTADeclaration

func GetOTADeclarationsFromTx

func GetOTADeclarationsFromTx(tx metadata.Transaction) []OTADeclaration

type TokenParam

type TokenParam = tx_generic.TokenParam

type TransactionError

type TransactionError = utils.TransactionError //nolint:revive

func NewTransactionErr

func NewTransactionErr(key int, err error, params ...interface{}) *TransactionError

type TransactionToken

type TransactionToken = tx_generic.TransactionToken

type TxChoice

type TxChoice struct {
	Version1      *TxVersion1      `json:"TxVersion1,omitempty"`
	TokenVersion1 *TxTokenVersion1 `json:"TxTokenVersion1,omitempty"`
	Version2      *TxVersion2      `json:"TxVersion2,omitempty"`
	TokenVersion2 *TxTokenVersion2 `json:"TxTokenVersion2,omitempty"`
}

TxChoice is a helper struct for parsing transactions of all types from JSON. After parsing succeeds, one of its fields will have the TX object; others will be nil. This can be used to assert the transaction type.

func DeserializeTransactionJSON

func DeserializeTransactionJSON(data []byte) (*TxChoice, error)

DeserializeTransactionJSON parses a transaction from raw JSON into a TxChoice object. It covers all transaction types.

func (*TxChoice) ToTx

func (ch *TxChoice) ToTx() metadata.Transaction

ToTx returns a generic transaction from a TxChoice object. Use this when the underlying TX type is irrelevant.

type TxConvertVer1ToVer2InitParams

type TxConvertVer1ToVer2InitParams = tx_ver2.TxConvertVer1ToVer2InitParams

func NewTxConvertVer1ToVer2InitParams

func NewTxConvertVer1ToVer2InitParams(senderSK *privacy.PrivateKey,
	paymentInfo []*privacy.PaymentInfo,
	inputCoins []privacy.PlainCoin,
	fee uint64,
	stateDB *statedb.StateDB,
	tokenID *common.Hash,
	metaData metadata.Metadata,
	info []byte) *TxConvertVer1ToVer2InitParams

type TxPrivacyInitParams

type TxPrivacyInitParams = tx_generic.TxPrivacyInitParams

func NewTxPrivacyInitParams

func NewTxPrivacyInitParams(senderSK *privacy.PrivateKey,
	paymentInfo []*privacy.PaymentInfo,
	inputCoins []privacy.PlainCoin,
	fee uint64,
	hasPrivacy bool,
	stateDB *statedb.StateDB,
	tokenID *common.Hash,
	metaData metadata.Metadata,
	info []byte) *TxPrivacyInitParams

type TxSalaryOutputParams

type TxSalaryOutputParams struct {
	Amount          uint64
	ReceiverAddress *privacy.PaymentAddress
	PublicKey       *privacy.Point
	TxRandom        *privacy.TxRandom
	TokenID         *common.Hash
	Info            []byte
	Type            string
}

TxSalaryOutputParams is a helper struct for "mint"-type transactions. It first tries to create a TX in version 2; if that fails, it falls back to the older version. The receiver is defined by either the ReceiverAddress field (for non-privacy use), or the fields PublicKey & TxRandom combined.

func (TxSalaryOutputParams) BuildTxSalary

func (pr TxSalaryOutputParams) BuildTxSalary(privateKey *privacy.PrivateKey, stateDB *statedb.StateDB, mdMaker func(privacy.Coin) metadata.Metadata) (metadata.Transaction, error)

BuildTXSalary is called from the parameter struct to actually create a "mint" transaction. It takes parameters:

  • private key : to sign the TX
  • db pointer : to get other coins for the ring signature
  • metadata maker (optional): a closure that returns a metadata object (this allows making the metadata based on the output coin)

type TxSigPubKeyVer2

type TxSigPubKeyVer2 = tx_ver2.SigPubKey

type TxTokenConvertVer1ToVer2InitParams

type TxTokenConvertVer1ToVer2InitParams = tx_ver2.TxTokenConvertVer1ToVer2InitParams

func NewTxTokenConvertVer1ToVer2InitParams

func NewTxTokenConvertVer1ToVer2InitParams(senderSK *privacy.PrivateKey,
	feeInputs []privacy.PlainCoin,
	feePayments []*privacy.PaymentInfo,
	tokenInputs []privacy.PlainCoin,
	tokenPayments []*privacy.PaymentInfo,
	fee uint64,
	stateDB *statedb.StateDB,
	bridgeStateDB *statedb.StateDB,
	tokenID *common.Hash,
	metaData metadata.Metadata,
	info []byte) *TxTokenConvertVer1ToVer2InitParams

type TxTokenData

type TxTokenData = tx_generic.TxTokenData

type TxTokenParams

type TxTokenParams = tx_generic.TxTokenParams

func NewTxTokenParams

func NewTxTokenParams(senderKey *privacy.PrivateKey, paymentInfo []*privacy.PaymentInfo, inputCoin []privacy.PlainCoin, feeNativeCoin uint64, tokenParams *TokenParam, transactionStateDB *statedb.StateDB, metaData metadata.Metadata, hasPrivacyCoin bool, hasPrivacyToken bool, shardID byte, info []byte, bridgeStateDB *statedb.StateDB) *TxTokenParams

type TxTokenVersion1

type TxTokenVersion1 = tx_ver1.TxToken

type TxTokenVersion2

type TxTokenVersion2 = tx_ver2.TxToken

type TxVersion1

type TxVersion1 = tx_ver1.Tx

type TxVersion2

type TxVersion2 = tx_ver2.Tx

Directories

Path Synopsis
Package coinIndexer implements a UTXO cache for v2 output coins.
Package coinIndexer implements a UTXO cache for v2 output coins.

Jump to

Keyboard shortcuts

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