kvstore

package
v0.0.0-...-ab07e92 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

README

KVStore

There are two app's here: the KVStoreApplication and the PersistentKVStoreApplication.

KVStoreApplication

The KVStoreApplication is a simple merkle key-value store. Transactions of the form key=value are stored as key-value pairs in the tree. Transactions without an = sign set the value to the key. The app has no replay protection (other than what the mempool provides).

PersistentKVStoreApplication

The PersistentKVStoreApplication wraps the KVStoreApplication and provides two additional features:

  1. persistence of state across app restarts (using Tendermint's ABCI-Handshake mechanism)
  2. validator set changes

The state is persisted in leveldb along with the last block committed, and the Handshake allows any necessary blocks to be replayed. Validator set changes are effected using the following transaction format:

"val:pubkey1!power1,pubkey2!power2,pubkey3!power3"

where pubkeyN is a base64-encoded 32-byte ed25519 key and powerN is a new voting power for the validator with pubkeyN (possibly a new one). To remove a validator from the validator set, set power to 0. There is no sybil protection against new validators joining.

Documentation

Index

Constants

View Source
const (
	Stale uint8 = iota
	Ready
	Busy
)
View Source
const (
	Registered uint8 = 0 // Indicates an initial state or condition
	Processing uint8 = 1 // Indicates a state where processing is underway
)

Additional constants for specific application states

View Source
const (
	ValidatorSetChangePrefix string = "val:"
)

Variables

View Source
var (
	ProtocolVersion uint64 = 0x1
)

Functions

func AddMinerToServiceTypeMapping

func AddMinerToServiceTypeMapping(db db.DB, serviceType uint64, minerAddr string) error

AddMinerToServiceTypeMapping adds a miner's address to the service type mapping in the database.

func AddOrUpdateMinerStatus

func AddOrUpdateMinerStatus(db db.DB, address string, status uint8) error

AddOrUpdateMinerStatus adds a new miner or updates an existing miner's status.

func AddServiceRequest

func AddServiceRequest(db db.DB, serviceID, minerID string, height int64) error

func BuildKeyForClientRegistration

func BuildKeyForClientRegistration(ethereumAddress string) []byte

BuildKey generates a database key for a given Ethereum address.

func BuildKeyForMinerJob

func BuildKeyForMinerJob(minerID string) []byte

BuildKeyForMinerJob generates a database key for a given miner's job.

func BuildKeyForMinerRating

func BuildKeyForMinerRating(minerAddress string) []byte

BuildKeyForMinerRating generates a database key for a given miner's address.

func BuildKeyForMinerRegistration

func BuildKeyForMinerRegistration(ethereumAddress string) []byte

BuildKeyForMinerRegistration generates a database key for a given Ethereum address.

func BuildServiceTypeKey

func BuildServiceTypeKey(serviceType uint64) []byte

BuildServiceTypeKey generates a database key for service type mappings.

func GenerateHashForServiceInfo

func GenerateHashForServiceInfo(clientAddress string, metadata []byte, blockHeight int64) string

GenerateHashForMinerInfo creates a unique hash for given miner information which includes the miner's address, any relevant metadata, and the block height.

func GetClientRating

func GetClientRating(db db.DB, minerAddress string) (map[string]uint8, error)

GetClientRating retrieves the map of client ratings from the database using the miner's address.

func GetMinerStatus

func GetMinerStatus(db db.DB, address string) (uint8, error)

GetMinerStatus queries a single miner's status.

func GetMinersForServiceType

func GetMinersForServiceType(db db.DB, serviceType uint64) ([]string, error)

GetMinersForServiceType retrieves the list of miners for a given service type from the database.

func InitKVStore

func InitKVStore(app *PersistentKVStoreApplication)

InitKVStore initializes the kvstore app with some data, which allows tests to pass and is fine as long as you don't make any tx that modify the validator state

func MakeValSetChangeTx

func MakeValSetChangeTx(pubkey pc.PublicKey, power int64) []byte

func RandVal

func RandVal(i int) types.ValidatorUpdate

RandVal creates one random validator, with a key derived from the input value

func RandVals

func RandVals(cnt int) []types.ValidatorUpdate

RandVals returns a list of cnt validators for initializing the application. Note that the keys are deterministically derived from the index in the array, while the power is random (Change this if not desired)

func RegisterMiner

func RegisterMiner(db db.DB, miner MinerInfo, minerAddress string) error

RegisterMiner registers a new miner and updates the service type mappings in the database.

func RemoveJobInfo

func RemoveJobInfo(db db.DB, minerID string, serviceID string) error

RemoveJobInfo removes a JobInfo from the list stored in the database under the miner's ID based on the ServiceID.

func RemoveMinerFromServiceTypeMapping

func RemoveMinerFromServiceTypeMapping(db db.DB, serviceType uint64, minerAddr string) error

RemoveMinerFromServiceTypeMapping removes a miner's address from the service type mapping in the database.

func RemoveMinerStatus

func RemoveMinerStatus(db db.DB, address string) error

RemoveMinerStatus removes a miner's status from the map.

func RemoveServiceRequest

func RemoveServiceRequest(db db.DB, serviceID string) error

func RetainServiceRequestsAboveHeight

func RetainServiceRequestsAboveHeight(db db.DB, retainHeight int64) error

func SaveMinerStatuses

func SaveMinerStatuses(db db.DB, statuses MinerStatuses) error

SaveMinerStatuses helper to store the entire map in the database.

func SaveServiceRequests

func SaveServiceRequests(db db.DB, requests ServiceRequests) error

func StoreClientInfo

func StoreClientInfo(db db.DB, ethereumAddress string, info ClientInfo) error

StoreClientInfo stores ClientInfo in the database under the key derived from the Ethereum address.

func StoreClientRating

func StoreClientRating(db db.DB, minerAddress string, ratings map[string]uint8) error

StoreClientRating stores the map of client ratings in the database under the key derived from the miner's address.

func StoreJobInfo

func StoreJobInfo(db db.DB, minerID string, newJob JobInfo) error

StoreJobInfo updates or appends a JobInfo in the list stored in the database under the miner's ID.

func StoreMinerInfo

func StoreMinerInfo(db db.DB, ethereumAddress string, info MinerInfo) error

StoreMinerInfo stores MinerInfo in the database under the key derived from the Ethereum address.

func StoreMinersForServiceType

func StoreMinersForServiceType(db db.DB, serviceType uint64, miners []string) error

StoreMinersForServiceType stores the list of miners for a given service type in the database.

Types

type Application

type Application struct {
	types.BaseApplication

	RetainBlocks int64 // blocks to retain after commit (via ResponseCommit.RetainHeight)
	// validator set
	ValUpdates []types.ValidatorUpdate
	// contains filtered or unexported fields
}

func NewApplication

func NewApplication(dbDir string) *Application

func (*Application) ApplySnapshotChunk

func (*Application) BeginBlock

Track the block hash and header information

func (*Application) CheckTx

func (*Application) Commit

func (app *Application) Commit() types.ResponseCommit

func (*Application) DeliverTx

tx is either "key=value" or just arbitrary bytes

func (*Application) EndBlock

Update the validator set

func (*Application) Info

func (app *Application) Info(req types.RequestInfo) (resInfo types.ResponseInfo)

func (*Application) ListSnapshots

func (*Application) LoadSnapshotChunk

func (*Application) OfferSnapshot

func (*Application) Query

func (app *Application) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery)

Returns an associated value or nil if missing.

func (*Application) SetLogger

func (app *Application) SetLogger(l log.Logger)

func (*Application) SetOption

func (*Application) Validators

func (app *Application) Validators() (validators []types.ValidatorUpdate)

type BlockServices

type BlockServices struct {
	BlockHeight   int64           `json:"block_height"`
	MinerServices []MinerServices `json:"miner_services"`
}

BlockServices holds the block height and a list of miner services provided at that height.

type ClientInfo

type ClientInfo struct {
	Name  string
	Power uint64
}

func GetClientInfo

func GetClientInfo(db db.DB, ethereumAddress string) (ClientInfo, error)

GetClientInfo retrieves ClientInfo from the database using the Ethereum address.

type JobInfo

type JobInfo struct {
	ServiceID    string `json:"service_id"`    // The unique identifier for the service
	ClientID     string `json:"client_id"`     // The identifier of the client requesting the service
	ServiceType  uint64 `json:"service_type"`  // The numeric identifier of the type of service
	JobStatus    uint8  `json:"job_status"`    // The status of the job
	TimeoutBlock int64  `json:"timeout_block"` // The block index when the job will expire
}

JobInfo represents information about a specific job or task associated with a service.

func GetJobInfoByServiceID

func GetJobInfoByServiceID(db db.DB, minerID string, serviceID string) (JobInfo, error)

GetJobInfoByServiceID retrieves a single JobInfo by ServiceID from a specified miner's list of jobs.

func GetJobInfos

func GetJobInfos(db db.DB, minerID string) ([]JobInfo, error)

GetJobInfos retrieves a list of JobInfo from the database using the miner's ID.

type MinerInfo

type MinerInfo struct {
	Name          string   // The name of the miner
	Power         uint64   // The computational power of the miner, possibly in hashes per second
	ServiceTypes  []uint64 // An array of service type identifiers that the miner provides
	IP            string   // The IP address of the miner for network connections
	InitialStatus uint8
}

func GetMinerInfo

func GetMinerInfo(db db.DB, ethereumAddress string) (MinerInfo, error)

GetMinerInfo retrieves MinerInfo from the database using the Ethereum address.

type MinerServices

type MinerServices struct {
	MinerID      string           `json:"miner_id"`
	ServiceTypes ServiceTypeCount `json:"service_types"`
}

MinerServices contains a miner ID and a map of service types with their respective counts.

type MinerStatuses

type MinerStatuses map[string]uint8 // Map from address to status

func LoadMinerStatuses

func LoadMinerStatuses(db db.DB) (MinerStatuses, error)

LoadMinerStatuses helper to retrieve the entire map from the database.

type MinerWorkRecords

type MinerWorkRecords []BlockServices

MinerWorkRecords manages a collection of BlockServices, tracking services provided by miners over time.

func (*MinerWorkRecords) AddOrUpdateRecord

func (mwr *MinerWorkRecords) AddOrUpdateRecord(newBlock BlockServices) error

func (MinerWorkRecords) FindByHeight

func (mwr MinerWorkRecords) FindByHeight(height int64) (int, bool)

func (*MinerWorkRecords) IncrementServiceType

func (mwr *MinerWorkRecords) IncrementServiceType(height int64, minerID string, serviceType uint64) error

func (*MinerWorkRecords) UpdateMinerServices

func (mwr *MinerWorkRecords) UpdateMinerServices(height int64, minerServices MinerServices) error

type PersistentKVStoreApplication

type PersistentKVStoreApplication struct {

	// validator set
	ValUpdates []types.ValidatorUpdate
	// contains filtered or unexported fields
}

func NewPersistentKVStoreApplication

func NewPersistentKVStoreApplication(dbDir string) *PersistentKVStoreApplication

func (*PersistentKVStoreApplication) ApplySnapshotChunk

func (*PersistentKVStoreApplication) BeginBlock

Track the block hash and header information

func (*PersistentKVStoreApplication) CheckTx

func (*PersistentKVStoreApplication) Commit

Commit will panic if InitChain was not called

func (*PersistentKVStoreApplication) DeliverTx

tx is either "val:pubkey!power" or "key=value" or just arbitrary bytes

func (*PersistentKVStoreApplication) EndBlock

Update the validator set

func (*PersistentKVStoreApplication) Info

func (*PersistentKVStoreApplication) InitChain

Save the validators in the merkle tree

func (*PersistentKVStoreApplication) ListSnapshots

func (*PersistentKVStoreApplication) LoadSnapshotChunk

func (*PersistentKVStoreApplication) OfferSnapshot

func (*PersistentKVStoreApplication) Query

func (app *PersistentKVStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery)

When path=/val and data={validator address}, returns the validator update (types.ValidatorUpdate) varint encoded. For any other path, returns an associated value or nil if missing.

func (*PersistentKVStoreApplication) SetLogger

func (app *PersistentKVStoreApplication) SetLogger(l log.Logger)

func (*PersistentKVStoreApplication) SetOption

func (*PersistentKVStoreApplication) Validators

func (app *PersistentKVStoreApplication) Validators() (validators []types.ValidatorUpdate)

type ServiceRequest

type ServiceRequest struct {
	ServiceID string
	MinerID   string
	Height    int64
}

type ServiceRequests

type ServiceRequests []ServiceRequest

func LoadServiceRequests

func LoadServiceRequests(db db.DB) (ServiceRequests, error)

type ServiceTypeCount

type ServiceTypeCount map[uint64]int

ServiceTypeCount maps a service type to the number of times a miner has served that type.

type State

type State struct {
	Size                 int64            `json:"size"`
	Height               int64            `json:"height"`
	AppHash              []byte           `json:"app_hash"`
	MinerActivityRecords MinerWorkRecords `json:"miner_activity_records"`
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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