tree

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AccountTreeHeight = 32
	AssetTreeHeight   = 16
	NftTreeHeight     = 40
)
View Source
const (
	NFTPrefix          = "nft:"
	AccountPrefix      = "account:"
	AccountAssetPrefix = "account_asset:"
)

Variables

View Source
var (
	NilHash                 = merkleTree.NilHash
	NilStateRoot            []byte
	NilAccountRoot          []byte
	NilNftRoot              []byte
	NilAccountAssetRoot     []byte
	NilAccountNodeHash      []byte
	NilNftNodeHash          []byte
	NilAccountAssetNodeHash []byte
)
View Source
var (
	ErrUnsupportedDriver = errors.New("unsupported db driver")
)

Functions

func AccountToNode

func AccountToNode(
	accountNameHash string,
	publicKey string,
	nonce int64,
	collectionNonce int64,
	assetRoot []byte,
) (hashVal []byte, err error)

func AssetToNode

func AssetToNode(balance string, offerCanceledOrFinalized string) (hashVal []byte, err error)

func CommitTrees

func CommitTrees(
	version uint64,
	accountTree bsmt.SparseMerkleTree,
	assetTrees *AssetTreeCache,
	nftTree bsmt.SparseMerkleTree) error

func ComputeAccountAssetLeafHash

func ComputeAccountAssetLeafHash(
	balance string,
	offerCanceledOrFinalized string,
) (hashVal []byte, err error)

func ComputeAccountLeafHash

func ComputeAccountLeafHash(
	accountNameHash string,
	pk string,
	nonce int64,
	collectionNonce int64,
	assetRoot []byte,
) (hashVal []byte, err error)

func ComputeNftAssetLeafHash

func ComputeNftAssetLeafHash(
	creatorAccountIndex int64,
	ownerAccountIndex int64,
	nftContentHash string,
	nftL1Address string,
	nftL1TokenId string,
	creatorTreasuryRate int64,
	collectionId int64,
) (hashVal []byte, err error)

func ComputeStateRootHash

func ComputeStateRootHash(
	accountRoot []byte,
	nftRoot []byte,
) []byte

func EmptyAccountAssetNodeHash

func EmptyAccountAssetNodeHash() []byte

func EmptyAccountNodeHash

func EmptyAccountNodeHash() []byte

func EmptyNftNodeHash

func EmptyNftNodeHash() []byte

func InitNftTree

func InitNftTree(
	nftHistoryModel nft.L2NftHistoryModel,
	blockHeight int64,
	ctx *Context,
) (
	nftTree bsmt.SparseMerkleTree, err error,
)

func NewMemAccountAssetTree

func NewMemAccountAssetTree() (tree bsmt.SparseMerkleTree, err error)

func NftAssetToNode

func NftAssetToNode(nftAsset *nft.L2NftHistory) (hashVal []byte, err error)

func RollBackTrees

func RollBackTrees(
	version uint64,
	accountTree bsmt.SparseMerkleTree,
	assetTrees *AssetTreeCache,
	nftTree bsmt.SparseMerkleTree) error

func SetNamespace

func SetNamespace(
	context *Context,
	namespace string,
) database.TreeDB

func SetupTreeDB

func SetupTreeDB(
	context *Context,
) error

Types

type AssetTreeCache added in v0.0.4

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

Lazy init cache for asset trees

func InitAccountTree

func InitAccountTree(
	accountModel account.AccountModel,
	accountHistoryModel account.AccountHistoryModel,
	blockHeight int64,
	ctx *Context,
	assetCacheSize int,
) (
	accountTree bsmt.SparseMerkleTree, accountAssetTrees *AssetTreeCache, err error,
)

func NewLazyTreeCache added in v0.0.4

func NewLazyTreeCache(maxSize int, accountNumber int64, blockNumber int64, f func(index, block int64) bsmt.SparseMerkleTree) *AssetTreeCache

Creates new AssetTreeCache maxSize defines the maximum size of currently initialized trees accountNumber defines the number of accounts to create/or next index for new account

func (*AssetTreeCache) CleanChanges added in v0.0.4

func (c *AssetTreeCache) CleanChanges()

Cleans all saved tree changes in the cache

func (*AssetTreeCache) Get added in v0.0.4

func (c *AssetTreeCache) Get(i int64) (tree bsmt.SparseMerkleTree)

Returns asset tree based on account index

func (*AssetTreeCache) GetChanges added in v0.0.4

func (c *AssetTreeCache) GetChanges() []int64

Returns slice of indexes of asset trees that were changned

func (*AssetTreeCache) GetNextAccountIndex added in v0.0.4

func (c *AssetTreeCache) GetNextAccountIndex() int64

Returns index of next account

func (*AssetTreeCache) UpdateCache added in v0.0.4

func (c *AssetTreeCache) UpdateCache(accountNumber, latestBlock int64)

Updates current cache with new block number and with latest account index

type Context

type Context struct {
	Name          string
	Driver        Driver
	LevelDBOption *LevelDBOption
	RedisDBOption *RedisDBOption

	TreeDB database.TreeDB
	// contains filtered or unexported fields
}

func NewContext added in v0.0.5

func NewContext(
	name string, driver Driver,
	reload bool, routineSize int,
	levelDBOption *LevelDBOption,
	redisDBOption *RedisDBOption) (*Context, error)

func (*Context) BatchReloadSize

func (ctx *Context) BatchReloadSize() int

func (*Context) Hasher added in v0.0.5

func (ctx *Context) Hasher() *bsmt.Hasher

func (*Context) IsLoad

func (ctx *Context) IsLoad() bool

func (*Context) Options

func (ctx *Context) Options(blockHeight int64) []bsmt.Option

func (*Context) RoutinePool added in v0.0.5

func (ctx *Context) RoutinePool() *ants.Pool

func (*Context) SetBatchReloadSize

func (ctx *Context) SetBatchReloadSize(size int)

func (*Context) SetOptions

func (ctx *Context) SetOptions(opts ...bsmt.Option)

type Driver

type Driver string
const (
	MemoryDB Driver = "memorydb"
	LevelDB  Driver = "leveldb"
	RedisDB  Driver = "redis"
)

type LevelDBOption

type LevelDBOption struct {
	File string
	//nolint:staticcheck
	Cache int `json:",optional"`
	//nolint:staticcheck
	Handles int `json:",optional"`
}

type RedisDBOption

type RedisDBOption struct {
	//nolint:staticcheck
	ClusterAddr []string `json:",optional"`
	//nolint:staticcheck
	Addr string `json:",optional"`
	// Use the specified Username to authenticate the current connection
	// with one of the connections defined in the ACL list when connecting
	// to a Redis 6.0 instance, or greater, that is using the Redis ACL system.
	//nolint:staticcheck
	Username string `json:",optional"`
	// Optional password. Must match the password specified in the
	// requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower),
	// or the User Password when connecting to a Redis 6.0 instance, or greater,
	// that is using the Redis ACL system.
	//nolint:staticcheck
	Password string `json:",optional"`

	// The maximum number of retries before giving up. Command is retried
	// on network errors and MOVED/ASK redirects.
	// Default is 3 retries.
	//nolint:staticcheck
	MaxRedirects int `json:",optional"`

	// Enables read-only commands on slave nodes.
	//nolint:staticcheck
	ReadOnly bool `json:",optional"`
	// Allows routing read-only commands to the closest master or slave node.
	// It automatically enables ReadOnly.
	//nolint:staticcheck
	RouteByLatency bool `json:",optional"`
	// Allows routing read-only commands to the random master or slave node.
	// It automatically enables ReadOnly.
	//nolint:staticcheck
	RouteRandomly bool `json:",optional"`

	// Maximum number of retries before giving up.
	// Default is 3 retries; -1 (not 0) disables retries.
	//nolint:staticcheck
	MaxRetries int `json:",optional"`
	// Minimum backoff between each retry.
	// Default is 8 milliseconds; -1 disables backoff.
	//nolint:staticcheck
	MinRetryBackoff time.Duration `json:",optional"`
	// Maximum backoff between each retry.
	// Default is 512 milliseconds; -1 disables backoff.
	//nolint:staticcheck
	MaxRetryBackoff time.Duration `json:",optional"`

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	//nolint:staticcheck
	DialTimeout time.Duration `json:",optional"`
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
	// Default is 3 seconds.
	//nolint:staticcheck
	ReadTimeout time.Duration `json:",optional"`
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	//nolint:staticcheck
	WriteTimeout time.Duration `json:",optional"`

	// Type of connection pool.
	// true for FIFO pool, false for LIFO pool.
	// Note that fifo has higher overhead compared to lifo.
	//nolint:staticcheck
	PoolFIFO bool `json:",optional"`
	// Maximum number of socket connections.
	// Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
	//nolint:staticcheck
	PoolSize int `json:",optional"`
	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	//nolint:staticcheck
	MinIdleConns int `json:",optional"`
	// Connection age at which client retires (closes) the connection.
	// Default is to not close aged connections.
	//nolint:staticcheck
	MaxConnAge time.Duration `json:",optional"`
	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	//nolint:staticcheck
	PoolTimeout time.Duration `json:",optional"`
	// Amount of time after which client closes idle connections.
	// Should be less than server's timeout.
	// Default is 5 minutes. -1 disables idle timeout check.
	//nolint:staticcheck
	IdleTimeout time.Duration `json:",optional"`
	// Frequency of idle checks made by idle connection reaper.
	// Default is 1 minute. -1 disables idle connection reaper,
	// but idle connections are still discarded by the client
	// if IdleTimeout is set.
	//nolint:staticcheck
	IdleCheckFrequency time.Duration `json:",optional"`
}

Jump to

Keyboard shortcuts

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