database

package
v0.0.0-...-acf744c Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2018 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClassNameNodes                  = "nodes"
	ClassNameBlockchain             = "blockchain"
	ClassNameTransactions           = "transactions"
	ClassNameUnapprovedTransactions = "unapprovedtransactions"
	ClassNameUnspentOutputs         = "unspentoutputs"
)
View Source
const DBConfigError = "rownotfound"
View Source
const DBCursorBreak = "cursorbreak"
View Source
const DBHashEmptyError = "hashisemptyd"
View Source
const DBHashError = "hashemptyd"
View Source
const DBHashNotFoundError = "hashnotfound"
View Source
const DBRowNotFoundError = "rownotfound"
View Source
const TXVerifyErrorNoInput = "noinput"

Variables

This section is empty.

Functions

func GetDBManagerMock

func GetDBManagerMock() mockMySQLDBManager

func NewBucketNotFoundDBError

func NewBucketNotFoundDBError() error

func NewConfigDBError

func NewConfigDBError(err string) error

func NewDBCursorStopError

func NewDBCursorStopError() error

func NewDBError

func NewDBError(err string, kind string) error

func NewDBIsNotReadyError

func NewDBIsNotReadyError() error

func NewHashDBError

func NewHashDBError(err string) error

func NewHashEmptyDBError

func NewHashEmptyDBError() error

func NewHashNotFoundDBError

func NewHashNotFoundDBError(err string) error

func NewNotFoundDBError

func NewNotFoundDBError(kind string) error

func NewRowNotFoundDBError

func NewRowNotFoundDBError(err string) error

func Quote

func Quote(s string) string

Types

type Blockchain

type Blockchain struct {
	DB *MySQLDB
	// contains filtered or unexported fields
}

func (*Blockchain) AddToChain

func (bc *Blockchain) AddToChain(hash, prevHash []byte) error

add block to chain

func (*Blockchain) BlockInChain

func (bc *Blockchain) BlockInChain(hash []byte) (bool, error)

func (*Blockchain) CheckBlockExists

func (bc *Blockchain) CheckBlockExists(hash []byte) (bool, error)

Check if block exists by hash

func (*Blockchain) DeleteBlock

func (bc *Blockchain) DeleteBlock(hash []byte) error

Delete block record

func (*Blockchain) GetBlock

func (bc *Blockchain) GetBlock(hash []byte) ([]byte, error)

Get block data by hash. It returns just []byte and and must be deserialised on ther place

func (*Blockchain) GetFirstHash

func (bc *Blockchain) GetFirstHash() ([]byte, error)

func (*Blockchain) GetLocationInChain

func (bc *Blockchain) GetLocationInChain(hash []byte) (bool, []byte, []byte, error)

Finds if a hash is in the main chain, returns previous and next hash if it has

func (*Blockchain) GetTopBlock

func (bc *Blockchain) GetTopBlock() ([]byte, error)

Get block on the top of blockchain

func (*Blockchain) GetTopHash

func (bc *Blockchain) GetTopHash() ([]byte, error)

Get top level block hash

func (*Blockchain) InitDB

func (bc *Blockchain) InitDB() error

create bucket etc. DB is already inited

func (*Blockchain) PutBlock

func (bc *Blockchain) PutBlock(hash []byte, blockdata []byte) error

Add block record

func (*Blockchain) PutBlockOnTop

func (bc *Blockchain) PutBlockOnTop(hash []byte, blockdata []byte) error

Add block to the top of block chain

func (*Blockchain) RemoveFromChain

func (bc *Blockchain) RemoveFromChain(hash []byte) error

remove block from chain

func (*Blockchain) SaveFirstHash

func (bc *Blockchain) SaveFirstHash(hash []byte) error

Save first (or genesis) block hash. It should be called when blockchain is created

func (*Blockchain) SaveTopHash

func (bc *Blockchain) SaveTopHash(hash []byte) error

Save top level block hash

type BlockchainInterface

type BlockchainInterface interface {
	InitDB() error

	GetTopBlock() ([]byte, error)
	GetBlock(hash []byte) ([]byte, error)
	PutBlockOnTop(hash []byte, blockdata []byte) error
	PutBlock(hash []byte, blockdata []byte) error
	CheckBlockExists(hash []byte) (bool, error)
	DeleteBlock(hash []byte) error
	SaveTopHash(hash []byte) error
	GetTopHash() ([]byte, error)
	SaveFirstHash(hash []byte) error
	GetFirstHash() ([]byte, error)

	GetLocationInChain(hash []byte) (bool, []byte, []byte, error)
	BlockInChain(hash []byte) (bool, error)
	RemoveFromChain(hash []byte) error
	AddToChain(hash, prevHash []byte) error
}

type DBError

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

func (*DBError) Error

func (e *DBError) Error() string

func (*DBError) IsKind

func (e *DBError) IsKind(kind string) bool

func (*DBError) IsRowNotFound

func (e *DBError) IsRowNotFound() bool

func (*DBError) Kind

func (e *DBError) Kind() string

type DBManager

type DBManager interface {
	QM() DBQueryManager // get QueryManager object

	SetConfig(config DatabaseConfig) error
	SetLogger(logger *utils.LoggerMan) error
	GetLockerObject() DatabaseLocker
	SetLockerObject(lockerobj DatabaseLocker)

	InitDatabase() error
	CheckDBExists() (bool, error)

	CheckConnection() error
	OpenConnection() error
	CloseConnection() error
	IsConnectionOpen() bool

	GetBlockchainObject() (BlockchainInterface, error)
	GetTransactionsObject() (TranactionsInterface, error)
	GetUnapprovedTransactionsObject() (UnapprovedTransactionsInterface, error)
	GetUnspentOutputsObject() (UnspentOutputsInterface, error)
	GetNodesObject() (NodesInterface, error)
	GetDataReferencesObject() (DataReferencesaInterface, error)
}

type DBQueryManager

type DBQueryManager interface {
	Dump(file string) error
	Restore(file string) error
	ExecuteSQL(sql string) error
	ExecuteSQLExplain(sql string) (SQLExplainInfo, error)
	ExecuteSQLPrimaryKey(table string) (string, error)
	ExecuteSQLNextKeyValue(table string) (string, error)
	ExecuteSQLSelectRow(sqlcommand string) (data map[string]string, err error)
	ExecuteSQLSelectRows(sqlcommand string) (data []resultRow, err error)
	ExecuteSQLTableDump(table string, limit int, offset int) ([]string, error)
	ExecuteSQLCountInTable(table string) (int, error)
}

type DataReferencesaInterface

type DataReferencesaInterface interface {
	InitDB() error
	TruncateDB() error
	SetTXForRefID(RefID []byte, txID []byte) error
	GetTXForRefID(RefID []byte) ([]byte, error)
	DeleteRefID(RefID []byte) error
}

this is interface for DB of connects of SQL refernces to transactions It keeps last transaction in a chain where a DB table row was updated

type DatabaseConfig

type DatabaseConfig struct {
	MysqlHost    string
	MysqlPort    int
	MysqlSocket  string
	DatabaseName string
	DbUser       string
	DbPassword   string
	TablesPrefix string
}

func (*DatabaseConfig) GetMySQLConnString

func (dbc *DatabaseConfig) GetMySQLConnString() string

func (*DatabaseConfig) GetServerAddress

func (dbc *DatabaseConfig) GetServerAddress() string

func (*DatabaseConfig) HasMinimum

func (dbc *DatabaseConfig) HasMinimum() bool

type DatabaseConnection

type DatabaseConnection interface {
	Close() error
}

type DatabaseLocker

type DatabaseLocker interface {
}

locker interface. is empty for now. maybe in future we will have some methods

type ForEachKeyIteratorInterface

type ForEachKeyIteratorInterface func(key, value []byte) error

type MySQLDB

type MySQLDB struct {
	Logger *utils.LoggerMan
	// contains filtered or unexported fields
}

func (*MySQLDB) Close

func (bdb *MySQLDB) Close() error

close DB connection

func (*MySQLDB) CreateTable

func (bdb *MySQLDB) CreateTable(table string, keytype string, valuetype string) error

create key value table

func (*MySQLDB) Delete

func (bdb *MySQLDB) Delete(table string, k []byte) error

Delete record from DB

func (*MySQLDB) Get

func (bdb *MySQLDB) Get(table string, k []byte) ([]byte, error)

Get record from DB

func (*MySQLDB) GetAll

func (bdb *MySQLDB) GetAll(table string) ([][][]byte, error)

Get all records from DB. There is max limit maxPossibleRowsToReturn returns list of 2 dimensional slices with key and pair

func (*MySQLDB) Put

func (bdb *MySQLDB) Put(table string, k, v []byte) error

Put record in DB

func (*MySQLDB) Truncate

func (bdb *MySQLDB) Truncate(table string) error

truncate table

type MySQLDBManager

type MySQLDBManager struct {
	Logger *utils.LoggerMan
	Config DatabaseConfig

	SessID string
	// contains filtered or unexported fields
}

func (*MySQLDBManager) CheckConnection

func (bdm *MySQLDBManager) CheckConnection() error

try to set up a connection to DB. and close it then

func (*MySQLDBManager) CheckDBExists

func (bdm *MySQLDBManager) CheckDBExists() (bool, error)

Check if database was already inited

func (*MySQLDBManager) CloseConnection

func (bdm *MySQLDBManager) CloseConnection() error

func (*MySQLDBManager) Dump

func (bdm *MySQLDBManager) Dump(file string) error

func (MySQLDBManager) ExecuteSQL

func (bdm MySQLDBManager) ExecuteSQL(sql string) error

execute query.

func (MySQLDBManager) ExecuteSQLCountInTable

func (bdm MySQLDBManager) ExecuteSQLCountInTable(table string) (int, error)

Get count of rows in table

func (MySQLDBManager) ExecuteSQLExplain

func (bdm MySQLDBManager) ExecuteSQLExplain(sql string) (r SQLExplainInfo, err error)

execute EXPLAIN query to check if sql query is correct and what is type and which table it affects

func (MySQLDBManager) ExecuteSQLNextKeyValue

func (bdm MySQLDBManager) ExecuteSQLNextKeyValue(table string) (string, error)

Return next auto_increment before query executed

func (MySQLDBManager) ExecuteSQLPrimaryKey

func (bdm MySQLDBManager) ExecuteSQLPrimaryKey(table string) (column string, err error)

get primary key column name for a table

func (MySQLDBManager) ExecuteSQLRowByKey

func (bdm MySQLDBManager) ExecuteSQLRowByKey(table string, priKeyVal string) (data map[string]string, err error)

get row by table name and primary key value

func (MySQLDBManager) ExecuteSQLSelectRow

func (bdm MySQLDBManager) ExecuteSQLSelectRow(sqlcommand string) (data map[string]string, err error)

get single row as a map

func (MySQLDBManager) ExecuteSQLSelectRows

func (bdm MySQLDBManager) ExecuteSQLSelectRows(sqlcommand string) (data []resultRow, err error)

get all rows as array of maps

func (MySQLDBManager) ExecuteSQLTableDump

func (bdm MySQLDBManager) ExecuteSQLTableDump(table string, limit int, offset int) (list []string, err error)

Return list of SQL queries as part of dump If offset is 0 we retrn table create SQL comand too

func (*MySQLDBManager) GetBlockchainObject

func (bdm *MySQLDBManager) GetBlockchainObject() (BlockchainInterface, error)

returns BlockChain Database structure. does all init

func (*MySQLDBManager) GetDataReferencesObject

func (bdm *MySQLDBManager) GetDataReferencesObject() (DataReferencesaInterface, error)

func (*MySQLDBManager) GetLockerObject

func (bdm *MySQLDBManager) GetLockerObject() DatabaseLocker

func (*MySQLDBManager) GetNodesObject

func (bdm *MySQLDBManager) GetNodesObject() (NodesInterface, error)

returns Nodes Database structure. does al init

func (*MySQLDBManager) GetTransactionsObject

func (bdm *MySQLDBManager) GetTransactionsObject() (TranactionsInterface, error)

returns Transaction Index Database structure. does al init

func (*MySQLDBManager) GetUnapprovedTransactionsObject

func (bdm *MySQLDBManager) GetUnapprovedTransactionsObject() (UnapprovedTransactionsInterface, error)

returns Unapproved Transaction Database structure. does al init

func (*MySQLDBManager) GetUnspentOutputsObject

func (bdm *MySQLDBManager) GetUnspentOutputsObject() (UnspentOutputsInterface, error)

returns Unspent Transactions Database structure. does al init

func (*MySQLDBManager) InitDatabase

func (bdm *MySQLDBManager) InitDatabase() error

create empty database. must create all creates tables for BC

func (*MySQLDBManager) IsConnectionOpen

func (bdm *MySQLDBManager) IsConnectionOpen() bool

func (*MySQLDBManager) OpenConnection

func (bdm *MySQLDBManager) OpenConnection() error

set status of connection to open

func (*MySQLDBManager) QM

func (bdm *MySQLDBManager) QM() DBQueryManager

func (*MySQLDBManager) Restore

func (bdm *MySQLDBManager) Restore(file string) error

func (*MySQLDBManager) SetConfig

func (bdm *MySQLDBManager) SetConfig(config DatabaseConfig) error

func (*MySQLDBManager) SetLockerObject

func (bdm *MySQLDBManager) SetLockerObject(lockerobj DatabaseLocker)

func (*MySQLDBManager) SetLogger

func (bdm *MySQLDBManager) SetLogger(logger *utils.LoggerMan) error

type Nodes

type Nodes struct {
	DB *MySQLDB
	// contains filtered or unexported fields
}

func (*Nodes) DeleteNode

func (ns *Nodes) DeleteNode(nodeID []byte) error

func (*Nodes) ForEach

func (ns *Nodes) ForEach(callback ForEachKeyIteratorInterface) error

retrns nodes list iterator

func (*Nodes) GetCount

func (ns *Nodes) GetCount() (int, error)

get count of records in the table

func (*Nodes) InitDB

func (ns *Nodes) InitDB() error

Init new DB. Create table.

func (*Nodes) PutNode

func (ns *Nodes) PutNode(nodeID []byte, nodeData []byte) error

Save node info

type NodesInterface

type NodesInterface interface {
	InitDB() error
	ForEach(callback ForEachKeyIteratorInterface) error
	GetCount() (int, error)

	PutNode(nodeID []byte, nodeData []byte) error
	DeleteNode(nodeID []byte) error
}

type SQLExplainInfo

type SQLExplainInfo struct {
	Id           string
	SelectType   string
	Table        string
	Partitions   string
	Type         string
	PossibleKeys string
	Key          string
	KeyLen       int
	Ref          string
	Rows         int
	Filtered     string
	Extra        string
}

type Tranactions

type Tranactions struct {
	DB *MySQLDB
	// contains filtered or unexported fields
}

func (*Tranactions) DeleteTXSpentData

func (txs *Tranactions) DeleteTXSpentData(txID []byte) error

Delete info about spent outputs for TX

func (txs *Tranactions) DeleteTXToBlockLink(txID []byte) error

Delete link between TX and a block hash

func (*Tranactions) GetBlockHashForTX

func (txs *Tranactions) GetBlockHashForTX(txID []byte) ([]byte, error)

Get block hash for TX

func (*Tranactions) GetTXSpentOutputs

func (txs *Tranactions) GetTXSpentOutputs(txID []byte) ([]byte, error)

Get spent outputs for TX , seialised to bytes

func (*Tranactions) InitDB

func (txs *Tranactions) InitDB() error

Init database

func (*Tranactions) PutTXSpentOutputs

func (txs *Tranactions) PutTXSpentOutputs(txID []byte, outputs []byte) error

Save spent outputs for TX

func (txs *Tranactions) PutTXToBlockLink(txID []byte, blockHash []byte) error

Save link between TX and block hash

func (*Tranactions) TruncateDB

func (txs *Tranactions) TruncateDB() error

transacet tables

type TranactionsInterface

type TranactionsInterface interface {
	InitDB() error
	TruncateDB() error
	PutTXToBlockLink(txID []byte, blockHash []byte) error
	GetBlockHashForTX(txID []byte) ([]byte, error)
	DeleteTXToBlockLink(txID []byte) error
	PutTXSpentOutputs(txID []byte, outputs []byte) error
	GetTXSpentOutputs(txID []byte) ([]byte, error)
	DeleteTXSpentData(txID []byte) error
}

type UnapprovedTransactions

type UnapprovedTransactions struct {
	DB *MySQLDB
	// contains filtered or unexported fields
}

func (*UnapprovedTransactions) DeleteTransaction

func (uts *UnapprovedTransactions) DeleteTransaction(txID []byte) error

delete transation from DB

func (*UnapprovedTransactions) ForEach

execute functon for each key/value in the bucket

func (*UnapprovedTransactions) GetAll

func (uts *UnapprovedTransactions) GetAll() ([][][]byte, error)

Get all records as single request

func (*UnapprovedTransactions) GetCount

func (uts *UnapprovedTransactions) GetCount() (int, error)

get count of records in the table

func (*UnapprovedTransactions) GetTransaction

func (uts *UnapprovedTransactions) GetTransaction(txID []byte) ([]byte, error)

returns transaction by ID if it exists

func (*UnapprovedTransactions) InitDB

func (uts *UnapprovedTransactions) InitDB() error

Init DB. create table

func (*UnapprovedTransactions) PutTransaction

func (uts *UnapprovedTransactions) PutTransaction(txID []byte, txdata []byte) error

Add transaction record

func (*UnapprovedTransactions) TruncateDB

func (uts *UnapprovedTransactions) TruncateDB() error

type UnapprovedTransactionsInterface

type UnapprovedTransactionsInterface interface {
	InitDB() error
	TruncateDB() error
	ForEach(callback ForEachKeyIteratorInterface) error
	GetCount() (int, error)
	GetAll() ([][][]byte, error)

	GetTransaction(txID []byte) ([]byte, error)
	PutTransaction(txID []byte, txdata []byte) error
	DeleteTransaction(txID []byte) error
}

type UnspentOutputs

type UnspentOutputs struct {
	DB *MySQLDB
	// contains filtered or unexported fields
}

func (*UnspentOutputs) DeleteDataForTransaction

func (uos *UnspentOutputs) DeleteDataForTransaction(txID []byte) error

func (*UnspentOutputs) ForEach

func (uos *UnspentOutputs) ForEach(callback ForEachKeyIteratorInterface) error

execute functon for each key/value in the bucket

func (*UnspentOutputs) GetCount

func (uos *UnspentOutputs) GetCount() (int, error)

get count of records in the table

func (*UnspentOutputs) GetDataForTransaction

func (uos *UnspentOutputs) GetDataForTransaction(txID []byte) ([]byte, error)

func (*UnspentOutputs) InitDB

func (uos *UnspentOutputs) InitDB() error

Init DB. create table

func (*UnspentOutputs) PutDataForTransaction

func (uos *UnspentOutputs) PutDataForTransaction(txID []byte, txData []byte) error

func (*UnspentOutputs) TruncateDB

func (uos *UnspentOutputs) TruncateDB() error

type UnspentOutputsInterface

type UnspentOutputsInterface interface {
	InitDB() error
	TruncateDB() error
	ForEach(callback ForEachKeyIteratorInterface) error
	GetCount() (int, error)

	GetDataForTransaction(txID []byte) ([]byte, error)
	DeleteDataForTransaction(txID []byte) error
	PutDataForTransaction(txID []byte, txData []byte) error
}

Jump to

Keyboard shortcuts

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