github.com/koko1123/flow-go-1@v0.29.6/state/protocol/badger/params.go (about) 1 package badger 2 3 import ( 4 "fmt" 5 6 "github.com/koko1123/flow-go-1/model/flow" 7 "github.com/koko1123/flow-go-1/storage/badger/operation" 8 ) 9 10 type Params struct { 11 state *State 12 } 13 14 func (p *Params) ChainID() (flow.ChainID, error) { 15 16 // retrieve root header 17 root, err := p.Root() 18 if err != nil { 19 return "", fmt.Errorf("could not get root: %w", err) 20 } 21 22 return root.ChainID, nil 23 } 24 25 func (p *Params) SporkID() (flow.Identifier, error) { 26 27 var sporkID flow.Identifier 28 err := p.state.db.View(operation.RetrieveSporkID(&sporkID)) 29 if err != nil { 30 return flow.ZeroID, fmt.Errorf("could not get spork id: %w", err) 31 } 32 33 return sporkID, nil 34 } 35 36 func (p *Params) ProtocolVersion() (uint, error) { 37 38 var version uint 39 err := p.state.db.View(operation.RetrieveProtocolVersion(&version)) 40 if err != nil { 41 return 0, fmt.Errorf("could not get protocol version: %w", err) 42 } 43 44 return version, nil 45 } 46 47 func (p *Params) Root() (*flow.Header, error) { 48 49 // retrieve the root height 50 var height uint64 51 err := p.state.db.View(operation.RetrieveRootHeight(&height)) 52 if err != nil { 53 return nil, fmt.Errorf("could not retrieve root height: %w", err) 54 } 55 56 // look up root header 57 var rootID flow.Identifier 58 err = p.state.db.View(operation.LookupBlockHeight(height, &rootID)) 59 if err != nil { 60 return nil, fmt.Errorf("could not look up root header: %w", err) 61 } 62 63 // retrieve root header 64 header, err := p.state.headers.ByBlockID(rootID) 65 if err != nil { 66 return nil, fmt.Errorf("could not retrieve root header: %w", err) 67 } 68 69 return header, nil 70 } 71 72 func (p *Params) Seal() (*flow.Seal, error) { 73 74 // retrieve the root height 75 var height uint64 76 err := p.state.db.View(operation.RetrieveRootHeight(&height)) 77 if err != nil { 78 return nil, fmt.Errorf("could not retrieve root height: %w", err) 79 } 80 81 // look up root header 82 var rootID flow.Identifier 83 err = p.state.db.View(operation.LookupBlockHeight(height, &rootID)) 84 if err != nil { 85 return nil, fmt.Errorf("could not look up root header: %w", err) 86 } 87 88 // retrieve the root seal 89 seal, err := p.state.seals.HighestInFork(rootID) 90 if err != nil { 91 return nil, fmt.Errorf("could not retrieve root seal: %w", err) 92 } 93 94 return seal, nil 95 }