store_memory.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package blockchain
  16. import "sync"
  17. type MemoryStore struct {
  18. sync.Mutex
  19. block *Block
  20. }
  21. func (m *MemoryStore) Add(b Block) {
  22. m.Lock()
  23. m.block = &b
  24. m.Unlock()
  25. }
  26. func (m *MemoryStore) Len() int {
  27. m.Lock()
  28. defer m.Unlock()
  29. return m.block.Index
  30. }
  31. func (m *MemoryStore) Last() Block {
  32. m.Lock()
  33. defer m.Unlock()
  34. return *m.block
  35. }