Back to godoc.org
github.com/smartcontractkit/chainlink/core/store

package store

v0.8.8
Latest Go to latest
Published: Jun 29, 2020 | License: MIT | Module: github.com/smartcontractkit/chainlink

Overview

Package store is used to keep application events in sync between the database on the node and the blockchain.

Config

Config contains the local configuration options that the application will adhere to.

CallerSubscriberClient

This makes use of Go-Ethereum's functions to interact with the blockchain. The underlying functions can be viewed here:

go-ethereum/rpc/client.go

KeyStore

KeyStore also utilizes Go-Ethereum's functions to store encrypted keys on the local file system. The underlying functions can be viewed here:

go-ethereum/accounts/keystore/keystore.go

Store

The Store is the persistence layer for the application. It saves the the application state and most interaction with the node needs to occur through the store.

Tx Manager

The transaction manager is used to syncronize interactions on the Ethereum blockchain with the application and database.

Index

Constants

const (
	// AutoMigrate is a flag that automatically migrates the DB when passed to initializeORM
	AutoMigrate = "auto_migrate"
)
const EthereumMessageHashPrefix = "\x19Ethereum Signed Message:\n32"

EthereumMessageHashPrefix is a Geth-originating message prefix that seeks to prevent arbitrary message data to be representable as a valid Ethereum transaction For more information, see: https://github.com/ethereum/go-ethereum/issues/3731

Variables

var AttemptToDeleteNonExistentKeyFromDB = errors.New("key is not present in DB")

AttemptToDeleteNonExistentKeyFromDB is returned when Delete is asked to delete a key it can't find in the DB.

var (
	// ErrPendingConnection is the error returned if TxManager is not connected.
	ErrPendingConnection = errors.New("Cannot talk to chain, pending connection")
)
var MatchingVRFKeyError = errors.New(
	`key with matching public key already stored in DB`)

MatchingVRFKeyError is returned when Import attempts to import key with a PublicKey matching one already in the database

var Sha = "unset"

Sha string "unset"

var Version = "unset"

Version the version of application

func BumpGas

func BumpGas(config orm.ConfigReader, originalGasPrice *big.Int) *big.Int

BumpGas returns a new gas price increased by the largest of: - A configured percentage bump (ETH_GAS_BUMP_PERCENT) - A configured fixed amount of Wei (ETH_GAS_PRICE_WEI) - The configured default base gas price (ETH_GAS_PRICE_DEFAULT)

type AttemptState

type AttemptState int

AttemptState enumerates the possible states of a transaction attempt as it gets accepted and confirmed by the blockchain

const (
	// Unknown is returned when the state of a transaction could not be
	// determined because of an error
	Unknown AttemptState = iota
	// Unconfirmed means that a transaction has had no confirmations at all
	Unconfirmed
	// Confirmed means that a transaction has had at least one confirmation, but
	// not enough to satisfy the minimum number of confirmations configuration
	// option
	Confirmed
	// Safe has the required number of confirmations or more
	Safe
)

func (AttemptState) String

func (a AttemptState) String() string

String conforms to the Stringer interface for AttemptState

type Dialer

type Dialer interface {
	Dial(string) (eth.CallerSubscriber, error)
}

Dialer implements Dial which is a function that creates a client for that url

type EthDialer

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

EthDialer is Dialer which accesses rpc urls

func NewEthDialer

func NewEthDialer(rateLimit uint64) *EthDialer

NewEthDialer returns an eth dialer with the specified rate limit

func (*EthDialer) Dial

func (ed *EthDialer) Dial(urlString string) (eth.CallerSubscriber, error)

Dial will dial the given url and return a CallerSubscriber

type EthTxManager

type EthTxManager struct {
	eth.Client
	// contains filtered or unexported fields
}

EthTxManager contains fields for the Ethereum client, the KeyStore, the local Config for the application, and the database.

func NewEthTxManager

func NewEthTxManager(client eth.Client, config orm.ConfigReader, keyStore KeyStoreInterface, orm *orm.ORM) *EthTxManager

NewEthTxManager constructs an EthTxManager using the passed variables and initializing internal variables.

func (*EthTxManager) BumpGasByIncrement

func (txm *EthTxManager) BumpGasByIncrement(originalGasPrice *big.Int) *big.Int

func (*EthTxManager) BumpGasUntilSafe

func (txm *EthTxManager) BumpGasUntilSafe(hash common.Hash) (*models.TxReceipt, AttemptState, error)

BumpGasUntilSafe process a collection of related TxAttempts, trying to get at least one TxAttempt into a safe state, bumping gas if needed

func (*EthTxManager) CheckAttempt

func (txm *EthTxManager) CheckAttempt(txAttempt *models.TxAttempt, blockHeight uint64) (*models.TxReceipt, AttemptState, error)

CheckAttempt retrieves a receipt for a TxAttempt, and check if it meets the minimum number of confirmations

func (*EthTxManager) Connect

func (txm *EthTxManager) Connect(bn *models.Head) error

Connect iterates over the available accounts to retrieve their nonce for client side management.

func (*EthTxManager) Connected

func (txm *EthTxManager) Connected() bool

Connected returns a bool indicating whether or not it is connected.

func (*EthTxManager) ContractLINKBalance

func (txm *EthTxManager) ContractLINKBalance(wr models.WithdrawalRequest) (assets.Link, error)

ContractLINKBalance returns the balance for the contract associated with this withdrawal request, or any errors

func (*EthTxManager) CreateTx

func (txm *EthTxManager) CreateTx(to common.Address, data []byte) (*models.Tx, error)

CreateTx signs and sends a transaction to the Ethereum blockchain.

func (*EthTxManager) CreateTxWithEth

func (txm *EthTxManager) CreateTxWithEth(from, to common.Address, value *assets.Eth) (*models.Tx, error)

CreateTxWithEth signs and sends a transaction with some ETH to transfer.

func (*EthTxManager) CreateTxWithGas

func (txm *EthTxManager) CreateTxWithGas(surrogateID null.String, to common.Address, data []byte, gasPriceWei *big.Int, gasLimit uint64) (*models.Tx, error)

CreateTxWithGas signs and sends a transaction to the Ethereum blockchain.

func (*EthTxManager) Disconnect

func (txm *EthTxManager) Disconnect()

Disconnect marks this instance as disconnected.

func (*EthTxManager) GetAvailableAccount

func (txm *EthTxManager) GetAvailableAccount(from common.Address) *ManagedAccount

GetAvailableAccount retrieves a managed account if it one matches the address given.

func (*EthTxManager) GetLINKBalance

func (txm *EthTxManager) GetLINKBalance(address common.Address) (*assets.Link, error)

GetLINKBalance returns the balance of LINK at the given address

func (*EthTxManager) NextActiveAccount

func (txm *EthTxManager) NextActiveAccount() *ManagedAccount

NextActiveAccount uses round robin to select a managed account from the list of available accounts as defined in Register(...)

func (*EthTxManager) OnNewLongestChain

func (txm *EthTxManager) OnNewLongestChain(head models.Head)

OnNewLongestChain does nothing; exists to comply with interface.

func (*EthTxManager) Register

func (txm *EthTxManager) Register(accts []accounts.Account)

Register activates accounts for outgoing transactions and client side nonce management.

func (*EthTxManager) SignedRawTxWithBumpedGas

func (txm *EthTxManager) SignedRawTxWithBumpedGas(originalTx models.Tx, gasLimit uint64, gasPrice big.Int) ([]byte, error)

SignedRawTxWithBumpedGas takes a transaction and generates a new signed TX from it with the provided params

func (txm *EthTxManager) WithdrawLINK(wr models.WithdrawalRequest) (common.Hash, error)

WithdrawLINK withdraws the given amount of LINK from the contract to the configured withdrawal address. If wr.ContractAddress is empty (zero address), funds are withdrawn from configured OracleContractAddress.

type GethClientWrapper

type GethClientWrapper interface {
	GethClient(func(gethClient eth.GethClient) error) error
}

type HeadTrackable

type HeadTrackable interface {
	Connect(*models.Head) error
	Disconnect()
	OnNewLongestChain(head models.Head)
}

HeadTrackable represents any object that wishes to respond to ethereum events, after being attached to HeadTracker. go:generate mockery -name HeadTrackable -output ../internal/mocks/ -case=underscore

type InMemoryKeyStore

type InMemoryKeyStore = map[vrfkey.PublicKey]vrfkey.PrivateKey

type KeyStore

type KeyStore struct {
	*keystore.KeyStore
}

KeyStore manages a key storage directory on disk.

func NewInsecureKeyStore

func NewInsecureKeyStore(keyDir string) *KeyStore

NewInsecureKeyStore creates an *INSECURE* keystore for the given directory. NOTE: Should only be used for testing!

func NewKeyStore

func NewKeyStore(keyDir string) *KeyStore

NewKeyStore creates a keystore for the given directory.

func (*KeyStore) GetAccountByAddress

func (ks *KeyStore) GetAccountByAddress(address common.Address) (accounts.Account, error)

GetAccountByAddress returns the account matching the address provided, or an error if it is missing

func (*KeyStore) GetAccounts

func (ks *KeyStore) GetAccounts() []accounts.Account

GetAccounts returns all accounts

func (*KeyStore) GetFirstAccount

func (ks *KeyStore) GetFirstAccount() (accounts.Account, error)

GetFirstAccount returns the unlocked account in the KeyStore object. The client ensures that an account exists during authentication.

func (*KeyStore) HasAccounts

func (ks *KeyStore) HasAccounts() bool

HasAccounts returns true if there are accounts located at the keystore directory.

func (*KeyStore) NewAccount

func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error)

NewAccount adds an account to the keystore

func (*KeyStore) SignHash

func (ks *KeyStore) SignHash(hash common.Hash) (models.Signature, error)

SignHash signs a precomputed digest, using the first account's private key This method adds an ethereum message prefix to the message before signing it, invalidating any would-be valid Ethereum transactions

func (*KeyStore) SignTx

func (ks *KeyStore) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)

SignTx uses the unlocked account to sign the given transaction.

func (*KeyStore) Unlock

func (ks *KeyStore) Unlock(phrase string) error

Unlock uses the given password to try to unlock accounts located in the keystore directory.

type KeyStoreInterface

type KeyStoreInterface interface {
	Accounts() []accounts.Account
	Wallets() []accounts.Wallet
	GetFirstAccount() (accounts.Account, error)
	HasAccounts() bool
	Unlock(phrase string) error
	NewAccount(passphrase string) (accounts.Account, error)
	SignHash(hash common.Hash) (models.Signature, error)
	Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error)
	GetAccounts() []accounts.Account
	GetAccountByAddress(common.Address) (accounts.Account, error)

	SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
}

go:generate mockery -name KeyStoreInterface -output ../internal/mocks/ -case=underscore

type ManagedAccount

type ManagedAccount struct {
	accounts.Account
	// contains filtered or unexported fields
}

ManagedAccount holds the account information alongside a client managed nonce to coordinate outgoing transactions.

func NewManagedAccount

func NewManagedAccount(a accounts.Account, nonce uint64) *ManagedAccount

NewManagedAccount creates a managed account that handles nonce increments locally.

func (*ManagedAccount) GetAndIncrementNonce

func (a *ManagedAccount) GetAndIncrementNonce(callback func(uint64) error) error

GetAndIncrementNonce will Yield the current nonce to a callback function and increment it once the callback has finished executing

func (*ManagedAccount) Nonce

func (a *ManagedAccount) Nonce() uint64

Nonce returns the client side managed nonce.

func (*ManagedAccount) ReloadNonce

func (a *ManagedAccount) ReloadNonce(txm *EthTxManager) error

ReloadNonce fetch and update the current nonce via eth_getTransactionCount

type NotifyNewEthTx

type NotifyNewEthTx interface {
	Trigger()
}

NotifyNewEthTx allows to notify the ethBroadcaster of a new transaction go:generate mockery -name NotifyNewEthTx -output ../internal/mocks/ -case=underscore

type Store

type Store struct {
	*orm.ORM
	Config            *orm.Config
	Clock             utils.AfterNower
	KeyStore          KeyStoreInterface
	VRFKeyStore       *VRFKeyStore
	TxManager         TxManager
	GethClientWrapper GethClientWrapper
	NotifyNewEthTx    NotifyNewEthTx
	// contains filtered or unexported fields
}

Store contains fields for the database, Config, KeyStore, and TxManager for keeping the application state in sync with the database.

func NewInsecureStore

func NewInsecureStore(config *orm.Config, shutdownSignal gracefulpanic.Signal) *Store

NewInsecureStore creates a new store with the given config and dialer, using an insecure keystore. NOTE: Should only be used for testing!

func NewStore

func NewStore(config *orm.Config, shutdownSignal gracefulpanic.Signal) *Store

NewStore will create a new store using the Eth dialer

func (*Store) AuthorizedUserWithSession

func (s *Store) AuthorizedUserWithSession(sessionID string) (models.User, error)

AuthorizedUserWithSession will return the one API user if the Session ID exists and hasn't expired, and update session's LastUsed field.

func (*Store) Close

func (s *Store) Close() error

Close shuts down all of the working parts of the store.

func (*Store) Start

func (s *Store) Start() error

Start initiates all of Store's dependencies including the TxManager.

func (*Store) SyncDiskKeyStoreToDB

func (s *Store) SyncDiskKeyStoreToDB() error

SyncDiskKeyStoreToDB writes all keys in the keys directory to the underlying orm.

func (*Store) Unscoped

func (s *Store) Unscoped() *Store

Unscoped returns a shallow copy of the store, with an unscoped ORM allowing one to work with soft deleted records.

type TxManager

type TxManager interface {
	HeadTrackable
	Connected() bool
	Register(accounts []accounts.Account)

	CreateTx(to common.Address, data []byte) (*models.Tx, error)
	CreateTxWithGas(surrogateID null.String, to common.Address, data []byte, gasPriceWei *big.Int, gasLimit uint64) (*models.Tx, error)
	CreateTxWithEth(from, to common.Address, value *assets.Eth) (*models.Tx, error)
	CheckAttempt(txAttempt *models.TxAttempt, blockHeight uint64) (*models.TxReceipt, AttemptState, error)

	BumpGasUntilSafe(hash common.Hash) (*models.TxReceipt, AttemptState, error)

	ContractLINKBalance(wr models.WithdrawalRequest) (assets.Link, error)
	GetLINKBalance(address common.Address) (*assets.Link, error)
	NextActiveAccount() *ManagedAccount

	SignedRawTxWithBumpedGas(originalTx models.Tx, gasLimit uint64, gasPrice big.Int) ([]byte, error)

	eth.Client
}

TxManager represents an interface for interacting with the blockchain

type VRFKeyStore

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

VRFKeyStore tracks auxiliary VRF secret keys, and generates their VRF proofs

VRF proofs need access to the actual secret key, which geth does not expose. Similar to the way geth's KeyStore exposes signing capability, VRFKeyStore exposes VRF proof generation without the caller needing explicit knowledge of the secret key.

func NewVRFKeyStore

func NewVRFKeyStore(store *Store) *VRFKeyStore

NewVRFKeyStore returns an empty VRFKeyStore

func (*VRFKeyStore) CreateKey

func (ks *VRFKeyStore) CreateKey(phrase string, p ...vrfkey.ScryptParams,
) (*vrfkey.PublicKey, error)

CreateKey returns a public key which is immediately unlocked in memory, and saved in DB encrypted with phrase. If p is given, its parameters are used for key derivation from the phrase.

func (*VRFKeyStore) CreateWeakInMemoryEncryptedKeyXXXTestingOnly

func (ks *VRFKeyStore) CreateWeakInMemoryEncryptedKeyXXXTestingOnly(
	phrase string) (*vrfkey.EncryptedSecretKey, error)

CreateWeakInMemoryEncryptedKeyXXXTestingOnly is for testing only! It returns an encrypted key which is fast to unlock, but correspondingly easy to brute force. It is not persisted to the DB, because no one should be keeping such keys lying around.

func (*VRFKeyStore) Delete

func (ks *VRFKeyStore) Delete(key *vrfkey.PublicKey) (err error)

Delete removes keys with this public key from the keystore and the DB, if present.

func (*VRFKeyStore) Forget

func (ks *VRFKeyStore) Forget(k *vrfkey.PublicKey) error

func (*VRFKeyStore) GenerateProof

func (ks *VRFKeyStore) GenerateProof(k *vrfkey.PublicKey, seed *big.Int) (
	vrf.MarshaledProof, error)

GenerateProof(k, seed) is marshaled randomness proof given public key k and VRF input seed.

k must have already been unlocked in ks, as constructing the VRF proof requires the secret key.

func (*VRFKeyStore) Get

func (ks *VRFKeyStore) Get(k *vrfkey.PublicKey) ([]*vrfkey.EncryptedSecretKey, error)

Get retrieves all EncryptedSecretKey's associated with k, or all encrypted keys if k is nil, or errors

func (*VRFKeyStore) GetSpecificKey

func (ks *VRFKeyStore) GetSpecificKey(
	k *vrfkey.PublicKey) (*vrfkey.EncryptedSecretKey, error)

func (*VRFKeyStore) Import

func (ks *VRFKeyStore) Import(keyjson []byte, auth string) error

Import adds this encrypted key to the DB and unlocks it in in-memory store with passphrase auth, and returns any resulting errors

func (*VRFKeyStore) ListKeys

func (ks *VRFKeyStore) ListKeys() (publicKeys []*vrfkey.PublicKey, err error)

ListKey lists the public keys contained in the db

func (*VRFKeyStore) Store

func (ks *VRFKeyStore) Store(key *vrfkey.PrivateKey, phrase string,
	p ...vrfkey.ScryptParams) error

Store saves key to ks (in memory), and to the DB, encrypted with phrase

func (*VRFKeyStore) StoreInMemoryXXXTestingOnly

func (ks *VRFKeyStore) StoreInMemoryXXXTestingOnly(key *vrfkey.PrivateKey)

StoreInMemoryXXXTestingOnly memorizes key, only in in-memory store.

func (*VRFKeyStore) Unlock

func (ks *VRFKeyStore) Unlock(phrase string) (keysUnlocked []vrfkey.PublicKey,
	merr error)

Unlock tries to unlock each vrf key in the db, using the given pass phrase, and returns any keys it manages to unlock, and any errors which result.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier