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.

pending.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package atto
  2. import (
  3. "encoding/json"
  4. )
  5. // Pending represents a block that is waiting to be received.
  6. type Pending struct {
  7. Hash string
  8. Amount string
  9. Source string
  10. }
  11. type internalPending struct {
  12. Error string `json:"error"`
  13. Blocks pendingBlocks `json:"blocks"`
  14. }
  15. type pendingBlocks map[string]pendingBlock
  16. // UnmarshalJSON just unmarshals a list of strings, but
  17. // interprets an empty string as an empty list. This is
  18. // necessary due to a bug in the Nano node implementation. See
  19. // https://github.com/nanocurrency/nano-node/issues/3161.
  20. func (b *pendingBlocks) UnmarshalJSON(in []byte) error {
  21. if string(in) == `""` {
  22. return nil
  23. }
  24. var raw map[string]pendingBlock
  25. err := json.Unmarshal(in, &raw)
  26. *b = pendingBlocks(raw)
  27. return err
  28. }
  29. type pendingBlock struct {
  30. Amount string `json:"amount"`
  31. Source string `json:"source"`
  32. }
  33. func internalPendingToPending(internalPending internalPending) []Pending {
  34. pendings := make([]Pending, 0)
  35. for hash, source := range internalPending.Blocks {
  36. pending := Pending{hash, source.Amount, source.Source}
  37. pendings = append(pendings, pending)
  38. }
  39. return pendings
  40. }