You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.1 KiB

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