github.com/mavryk-network/mvgo@v1.19.9/rpc/mempool.go (about) 1 // Copyright (c) 2020-2022 Blockwatch Data Inc. 2 // Author: alex@blockwatch.cc 3 4 package rpc 5 6 import ( 7 "context" 8 "encoding/json" 9 ) 10 11 // Mempool represents mempool operations 12 type Mempool struct { 13 Applied []*Operation `json:"applied"` 14 Refused []*Operation `json:"refused"` 15 Outdated []*Operation `json:"outdated"` // v012+ 16 BranchRefused []*Operation `json:"branch_refused"` 17 BranchDelayed []*Operation `json:"branch_delayed"` 18 Unprocessed []*Operation `json:"unprocessed"` 19 } 20 21 // GetMempool returns mempool pending operations 22 func (c *Client) GetMempool(ctx context.Context) (*Mempool, error) { 23 var mem Mempool 24 if err := c.Get(ctx, "chains/main/mempool/pending_operations", &mem); err != nil { 25 return nil, err 26 } 27 return &mem, nil 28 } 29 30 type PendingOperation Operation 31 32 func (o *PendingOperation) UnmarshalJSON(data []byte) error { 33 return unmarshalMultiTypeJSONArray(data, &o.Hash, (*Operation)(o)) 34 } 35 36 func (o PendingOperation) MarshalJSON() ([]byte, error) { 37 return marshalMultiTypeJSONArray(o.Hash, (Operation)(o)) 38 } 39 40 func (m *Mempool) UnmarshalJSON(data []byte) error { 41 type mempool struct { 42 Applied []*Operation `json:"applied"` 43 Refused []*PendingOperation `json:"refused"` 44 Outdated []*PendingOperation `json:"outdated"` 45 BranchRefused []*PendingOperation `json:"branch_refused"` 46 BranchDelayed []*PendingOperation `json:"branch_delayed"` 47 Unprocessed []*PendingOperation `json:"unprocessed"` 48 } 49 mp := mempool{} 50 if err := json.Unmarshal(data, &mp); err != nil { 51 return err 52 } 53 // applied is the correct type 54 m.Applied = mp.Applied 55 mp.Applied = mp.Applied[:0] 56 57 // type-convert the rest 58 type convert struct { 59 A *[]*PendingOperation 60 B *[]*Operation 61 } 62 for _, v := range []convert{ 63 {&mp.Refused, &m.Refused}, 64 {&mp.Outdated, &m.Outdated}, 65 {&mp.BranchRefused, &m.BranchRefused}, 66 {&mp.BranchDelayed, &m.BranchDelayed}, 67 {&mp.Unprocessed, &m.Unprocessed}, 68 } { 69 if l := len(*v.A); l > 0 { 70 *v.B = make([]*Operation, l) 71 for i := range *v.A { 72 (*v.B)[i] = (*Operation)((*v.A)[i]) 73 } 74 *v.A = (*v.A)[:0] 75 } 76 } 77 return nil 78 }