arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/l2db/l2db.go
l2db/l2db.goRaw
  1package l2db
  2
  3import (
  4	"time"
  5
  6	eth "github.com/ethereum/go-ethereum/common"
  7	"github.com/hermeznetwork/hermez-node/common"
  8	"github.com/jinzhu/gorm"
  9)
 10
 11// L2DB stores L2 txs and authorization registers received by the coordinator and keeps them until they are no longer relevant
 12// due to them being forged or invalid after a safety period
 13type L2DB struct {
 14	db           *gorm.DB
 15	safetyPeriod uint16
 16	ttl          time.Duration
 17	maxTxs       uint32
 18}
 19
 20// NewL2DB creates a L2DB.
 21// More info on how to set dbDialect and dbArgs here: http://gorm.io/docs/connecting_to_the_database.html
 22// safetyPeriod is the ammount of blockchain blocks that must be waited before deleting anything (to avoid reorg problems).
 23// maxTxs indicates the desired maximum amount of txs stored on the L2DB.
 24// TTL indicates the maximum amount of time that a tx can be in the L2DB
 25// (to prevent tx that won't ever be forged to stay there, will be used if maxTxs is exceeded).
 26// autoPurgePeriod will be used as delay between calls to Purge. If the value is 0, it will be disabled.
 27func NewL2DB(
 28	dbDialect, dbArgs string,
 29	safetyPeriod uint16,
 30	maxTxs uint32,
 31	TTL time.Duration,
 32) (*L2DB, error) {
 33	// Stablish DB connection
 34	db, err := gorm.Open(dbDialect, dbArgs)
 35	if err != nil {
 36		return nil, err
 37	}
 38
 39	// Create or update SQL schemas
 40	// WARNING: AutoMigrate will ONLY create tables, missing columns and missing indexes,
 41	// and WON’T change existing column’s type or delete unused columns to protect your data.
 42	// more info: http://gorm.io/docs/migration.html
 43	db.AutoMigrate(&common.PoolL2Tx{})
 44	// TODO: db.AutoMigrate(&common.RegisterAuthorization{})
 45
 46	return &L2DB{
 47		db:           db,
 48		safetyPeriod: safetyPeriod,
 49		ttl:          TTL,
 50		maxTxs:       maxTxs,
 51	}, nil
 52}
 53
 54// AddTx inserts a tx into the L2DB
 55func (l2db *L2DB) AddTx(tx *common.PoolL2Tx) error {
 56	return nil
 57}
 58
 59// AddAccountCreationAuth inserts an account creation authorization into the DB
 60func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error { // TODO: AddRegisterAuthorization(auth &common.RegisterAuthorization)
 61	return nil
 62}
 63
 64// GetTx return the specified Tx
 65func (l2db *L2DB) GetTx(txID common.TxID) (*common.PoolL2Tx, error) {
 66	return nil, nil
 67}
 68
 69// GetPendingTxs return all the pending txs of the L2DB
 70func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
 71	return nil, nil
 72}
 73
 74// GetAccountCreationAuth return the authorization to make registers of an Ethereum address
 75func (l2db *L2DB) GetAccountCreationAuth(ethAddr eth.Address) (*common.AccountCreationAuth, error) {
 76	return nil, nil
 77}
 78
 79// StartForging updates the state of the transactions that will begin the forging process.
 80// The state of the txs referenced by txIDs will be changed from Pending -> Forging
 81func (l2db *L2DB) StartForging(txIDs []common.TxID) error {
 82	return nil
 83}
 84
 85// DoneForging updates the state of the transactions that have been forged
 86// so the state of the txs referenced by txIDs will be changed from Forging -> Forged
 87func (l2db *L2DB) DoneForging(txIDs []common.TxID) error {
 88	return nil
 89}
 90
 91// InvalidateTxs updates the state of the transactions that are invalid.
 92// The state of the txs referenced by txIDs will be changed from * -> Invalid
 93func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID) error {
 94	return nil
 95}
 96
 97// CheckNonces invalidate txs with nonces that are smaller than their respective accounts nonces.
 98// The state of the affected txs will be changed from Pending -> Invalid
 99func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account) error {
100	return nil
101}
102
103// Reorg updates the state of txs that were updated in a batch that has been discarted due to a blockchian reorg.
104// The state of the affected txs can change form Forged -> Pending or from Invalid -> Pending
105func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
106	return nil
107}
108
109// Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
110// it also deletes txs that has been in the L2DB for longer than the ttl if maxTxs has been exceeded
111func (l2db *L2DB) Purge() error {
112	return nil
113}
114
115// Close frees the resources used by the L2DB
116func (l2db *L2DB) Close() error {
117	return l2db.db.Close()
118}