Documentation ¶
Overview ¶
Package coinIndexer implements a UTXO cache for v2 output coins. Privacy V2 boosted up the privacy of a transaction but also resulted in retrieving output coins of users becoming more and more costly in terms of time and computing resources. This cache layer makes the retrieval easier. However, users have to submit their OTA keys to the cache (for the cache to be able to tell which output coins belong to them). This also reduces the anonymity level of a user. The good news is that the cache layer only knows which output coins belong to an OTAKey, it does not know their values.
Index ¶
- Constants
- func GetNextLowerHeight(upper, floor uint64) uint64
- func OTAKeyFromRaw(b [64]byte) privacy.OTAKey
- func OTAKeyToRaw(vk privacy.OTAKey) [64]byte
- func QueryBatchDbCoinVer2(idxParams map[string]*IndexParam, shardID byte, tokenID *common.Hash, ...) (map[string][]privacy.Coin, error)
- func QueryBatchDbCoinVer2ByIndices(idxParams map[string]*IndexParam, shardID byte, tokenID *common.Hash, ...) (map[string][]privacy.Coin, error)
- func QueryDbCoinVer1(pubKey []byte, tokenID *common.Hash, db *statedb.StateDB) ([]privacy.Coin, error)
- func QueryDbCoinVer2(otaKey privacy.OTAKey, tokenID *common.Hash, startHeight, destHeight uint64, ...) ([]privacy.Coin, error)
- type CoinIndexer
- func (ci *CoinIndexer) AddOTAKey(otaKey privacy.OTAKey, state int) error
- func (ci *CoinIndexer) AddTokenID(tokenID common.Hash)
- func (ci *CoinIndexer) CacheCoinPublicKey(coinPublicKey *privacy.Point) error
- func (ci *CoinIndexer) GetAllTokenIDs() map[common.Hash]interface{}
- func (ci *CoinIndexer) GetIndexedOutCoin(otaKey privacy.OTAKey, tokenID *common.Hash, txDb *statedb.StateDB, ...) ([]privacy.Coin, int, error)
- func (ci *CoinIndexer) GetManagedOTAKeys() *sync.Map
- func (ci *CoinIndexer) HasOTAKey(k [64]byte) (bool, int)
- func (ci *CoinIndexer) IsAuthorizedRunning() bool
- func (ci *CoinIndexer) IsQueueFull(shardID byte) bool
- func (ci *CoinIndexer) IsValidAccessToken(accessToken string) bool
- func (ci *CoinIndexer) ReIndexOutCoin(idxParams *IndexParam)
- func (ci *CoinIndexer) ReIndexOutCoinBatch(idxParams []*IndexParam, txDb *statedb.StateDB, id string)
- func (ci *CoinIndexer) ReIndexOutCoinBatchByIndices(idxParams []*IndexParam, txDb *statedb.StateDB, id string)
- func (ci *CoinIndexer) RemoveOTAKey(otaKey privacy.OTAKey) error
- func (ci *CoinIndexer) Start(cfg *IndexerInitialConfig)
- func (ci *CoinIndexer) Stop()
- func (ci *CoinIndexer) StoreIndexedOutputCoins(otaKey privacy.OTAKey, outputCoins []privacy.Coin, shardID byte) error
- type CoinMatcher
- type IndexParam
- type IndexerInitialConfig
- type JobStatus
Constants ¶
const ( // StatusNotSubmitted indicates that an OTAKey has not been submitted StatusNotSubmitted = iota // StatusIndexing indicates that either the OTAKey is in the queue or it is being indexed. // This status usually happens when the OTAKey has been submitted in the enhanced manner. StatusIndexing // StatusKeySubmittedUsual indicates that the OTAKey has been submitted using the regular method. StatusKeySubmittedUsual // StatusIndexingFinished indicates the OTAKey has been submitted using the enhanced method and the indexing // procedure has been finished. StatusIndexingFinished )
The following constants indicate the state of an OTAKey.
const ( NumWorkers = 100 DefaultAccessToken = "0c3d46946bbf99c8213dd7f6c640ed6433bdc056a5b68e7e80f5525311b0ca11" //nolint:gosec // DEV access token BatchWaitingTime = float64(30) IndexingBatchSize = 10 )
Variables ¶
This section is empty.
Functions ¶
func GetNextLowerHeight ¶
GetNextLowerHeight returns the next upper height in querying v2 coins given the current upper height and the base height.
func OTAKeyFromRaw ¶
func OTAKeyToRaw ¶
func QueryBatchDbCoinVer2 ¶
func QueryBatchDbCoinVer2(idxParams map[string]*IndexParam, shardID byte, tokenID *common.Hash, startHeight, destHeight uint64, db *statedb.StateDB, cachedCoins *sync.Map, filters ...CoinMatcher) (map[string][]privacy.Coin, error)
QueryBatchDbCoinVer2 queries the db to get v2 coins for `shardHeight` to `destHeight` and checks if the coins belong to any of the given IndexParam's using the given filters.
The wider the range [startHeight: desHeight] is, the longer time this function should take. nolint // TODO: consider using get coin by index to speed up the performance.
func QueryBatchDbCoinVer2ByIndices ¶
func QueryBatchDbCoinVer2ByIndices(idxParams map[string]*IndexParam, shardID byte, tokenID *common.Hash, fromIndex, toIndex uint64, db *statedb.StateDB, cachedCoins *sync.Map, filters ...CoinMatcher) (map[string][]privacy.Coin, error)
QueryBatchDbCoinVer2ByIndices queries the db to get v2 coins with indices in [fromIndex,toIndex] checks if the coins belong to any of the given IndexParam's using the given filters.
The wider the range [fromIndex: toIndex] is, the longer time this function should take.
func QueryDbCoinVer1 ¶
func QueryDbCoinVer1(pubKey []byte, tokenID *common.Hash, db *statedb.StateDB) ([]privacy.Coin, error)
QueryDbCoinVer1 returns all v1 output coins of a public key on the given tokenID.
func QueryDbCoinVer2 ¶
func QueryDbCoinVer2(otaKey privacy.OTAKey, tokenID *common.Hash, startHeight, destHeight uint64, db *statedb.StateDB, filters ...CoinMatcher) ([]privacy.Coin, error)
QueryDbCoinVer2 returns all v2 output coins of a public key on the given tokenID for heights from `startHeight` to `destHeight` using the given list of filters. nolint // TODO: consider using get coin by index to speed up the performance.
Types ¶
type CoinIndexer ¶
type CoinIndexer struct { IdxChan chan *IndexParam // contains filtered or unexported fields }
CoinIndexer implements a UTXO cache for v2 output coins for faster retrieval.
func NewOutCoinIndexer ¶
func NewOutCoinIndexer(numWorkers int64, db incdb.Database, accessToken string, allTokens map[common.Hash]interface{}) (*CoinIndexer, error)
NewOutCoinIndexer creates CoinIndexer instance.
func (*CoinIndexer) AddOTAKey ¶
func (ci *CoinIndexer) AddOTAKey(otaKey privacy.OTAKey, state int) error
AddOTAKey adds a new OTAKey to the cache list.
func (*CoinIndexer) AddTokenID ¶
func (ci *CoinIndexer) AddTokenID(tokenID common.Hash)
AddTokenID add a new TokenID to the cache layer.
func (*CoinIndexer) CacheCoinPublicKey ¶
func (ci *CoinIndexer) CacheCoinPublicKey(coinPublicKey *privacy.Point) error
CacheCoinPublicKey stores the public key of a cached output coin to mark an output coin as cached.
func (*CoinIndexer) GetAllTokenIDs ¶
func (ci *CoinIndexer) GetAllTokenIDs() map[common.Hash]interface{}
GetAllTokenIDs returns all tokenIDs from the cache layer.
func (*CoinIndexer) GetIndexedOutCoin ¶
func (ci *CoinIndexer) GetIndexedOutCoin(otaKey privacy.OTAKey, tokenID *common.Hash, txDb *statedb.StateDB, shardID byte) ([]privacy.Coin, int, error)
GetIndexedOutCoin returns the indexed (i.e, cached) output coins of an otaKey w.r.t a tokenID. It returns an error if the otaKey hasn't been submitted (state = 0) or the indexing is in progress (state = 1).
func (*CoinIndexer) GetManagedOTAKeys ¶
func (ci *CoinIndexer) GetManagedOTAKeys() *sync.Map
GetManagedOTAKeys returns the ci.managedOTAKeys.
func (*CoinIndexer) HasOTAKey ¶
func (ci *CoinIndexer) HasOTAKey(k [64]byte) (bool, int)
HasOTAKey checks if an OTAKey has been added to the indexer and returns the state of OTAKey. The returned state could be:
- StatusNotSubmitted
- StatusIndexing
- StatusKeySubmittedUsual
- StatusIndexingFinished
func (*CoinIndexer) IsAuthorizedRunning ¶
func (ci *CoinIndexer) IsAuthorizedRunning() bool
IsAuthorizedRunning checks if the current cache supports the enhanced mode.
func (*CoinIndexer) IsQueueFull ¶
func (ci *CoinIndexer) IsQueueFull(shardID byte) bool
IsQueueFull checks if the current indexing queue is full.
The idxQueue size for each shard is as large as the number of workers.
func (*CoinIndexer) IsValidAccessToken ¶
func (ci *CoinIndexer) IsValidAccessToken(accessToken string) bool
IsValidAccessToken checks if a user is authorized to use the enhanced cache.
An access token is said to be valid if it is a hex-string of length 64.
func (*CoinIndexer) ReIndexOutCoin ¶
func (ci *CoinIndexer) ReIndexOutCoin(idxParams *IndexParam)
ReIndexOutCoin re-scans all output coins from idxParams.FromHeight to idxParams.ToHeight and adds them to the cache if the belongs to idxParams.OTAKey.
func (*CoinIndexer) ReIndexOutCoinBatch ¶
func (ci *CoinIndexer) ReIndexOutCoinBatch(idxParams []*IndexParam, txDb *statedb.StateDB, id string)
ReIndexOutCoinBatch re-scans all output coins for a list of indexing params of the same shardID.
Callers must manage to make sure all indexing params belong to the same shard.
func (*CoinIndexer) ReIndexOutCoinBatchByIndices ¶
func (ci *CoinIndexer) ReIndexOutCoinBatchByIndices(idxParams []*IndexParam, txDb *statedb.StateDB, id string)
ReIndexOutCoinBatchByIndices re-scans all output coins for a list of indexing params of the same shardID.
Callers must manage to make sure all indexing params belong to the same shard.
func (*CoinIndexer) RemoveOTAKey ¶
func (ci *CoinIndexer) RemoveOTAKey(otaKey privacy.OTAKey) error
RemoveOTAKey removes an OTAKey from the cached database.
nolint // TODO: remove cached output coins, access token.
func (*CoinIndexer) Start ¶
func (ci *CoinIndexer) Start(cfg *IndexerInitialConfig)
Start starts the CoinIndexer in case the authorized cache is employed. It is a hub to
- record key submission from users;
- record the indexing status of keys;
- collect keys into batches and index them all together in a batching way.
func (*CoinIndexer) StoreIndexedOutputCoins ¶
func (ci *CoinIndexer) StoreIndexedOutputCoins(otaKey privacy.OTAKey, outputCoins []privacy.Coin, shardID byte) error
StoreIndexedOutputCoins stores output coins that have been indexed into the cache db. It also keep tracks of each output coin hash to boost up the retrieval process.
type CoinMatcher ¶
CoinMatcher is an interface for matching a v2 coin and given parameter(s).
func GetCoinFilterByOTAKeyAndToken ¶
func GetCoinFilterByOTAKeyAndToken() CoinMatcher
GetCoinFilterByOTAKeyAndToken returns a functions that filters if an output coin is of a specific token and belongs to an OTAKey. If the given token is the generic `common.ConfidentialAssetID` and the retrieved coin has an asset tag field, it immediately returns true after the belonging check.
type IndexParam ¶
type IndexerInitialConfig ¶
IndexerInitialConfig is the initial config provided when starting the CoinIndexer.