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.

97 lines
2.2 KiB

  1. package blockchain
  2. import "sync"
  3. import "time"
  4. import "sync/atomic"
  5. import log "github.com/sirupsen/logrus"
  6. import "github.com/deroproject/derosuite/globals"
  7. import "github.com/deroproject/derosuite/crypto"
  8. // at this point in time, this is an ultrafast written mempool,
  9. // it will not scale for more than 10000 transactions but is good enough for now
  10. // we can always come back and rewrite it
  11. type Mempool struct {
  12. txs map[crypto.Hash]mempool_object
  13. // global variable , but don't see it utilisation here except fot tx verification
  14. chain *Blockchain
  15. sync.Mutex
  16. }
  17. type mempool_object struct {
  18. Tx *Transaction
  19. Added uint64
  20. Reason int // why is the tx in the mempool
  21. }
  22. var loggerpool *log.Entry
  23. func Init_Mempool(params map[string]interface{}) (*Mempool, error) {
  24. var mempool Mempool
  25. //mempool.chain = params["chain"].(*Blockchain)
  26. loggerpool = globals.Logger.WithFields(log.Fields{"com": "POOL"}) // all components must use this logger
  27. loggerpool.Infof("Mempool started")
  28. atomic.AddUint32(&globals.Subsystem_Active, 1) // increment subsystem
  29. //TODO load any trasactions saved at previous exit
  30. return &mempool, nil
  31. }
  32. func (pool *Mempool) Shutdown() {
  33. //TODO save mempool tx somewhere
  34. loggerpool.Infof("Mempool stopped")
  35. atomic.AddUint32(&globals.Subsystem_Active, ^uint32(0)) // this decrement 1 fom subsystem
  36. }
  37. func (pool *Mempool) Mempool_Add_TX(tx *Transaction, Reason int) {
  38. pool.Lock()
  39. defer pool.Unlock()
  40. var object mempool_object
  41. hash := crypto.Hash(tx.GetHash())
  42. // check if tx already exists, skip it
  43. if _, ok := pool.txs[hash]; ok {
  44. loggerpool.Infof("Pool already contains %x, skipping \n", hash)
  45. return
  46. }
  47. object.Tx = tx
  48. object.Reason = Reason
  49. object.Added = uint64(time.Now().Unix())
  50. pool.txs[hash] = object
  51. }
  52. // delete specific tx from pool
  53. func (pool *Mempool) Mempool_Delete_TX(crypto.Hash) {
  54. }
  55. // get specific tx from mem pool
  56. func (pool *Mempool) Mempool_Get_TX(txid crypto.Hash) ([]byte, error) {
  57. return nil, nil
  58. }
  59. // return list of all txs in pool
  60. func (pool *Mempool) Mempool_List_TX() []crypto.Hash {
  61. pool.Lock()
  62. defer pool.Unlock()
  63. var list []crypto.Hash
  64. for k, _ := range pool.txs {
  65. list = append(list, k)
  66. }
  67. return list
  68. }