github.com/koko1123/flow-go-1@v0.29.6/state/cluster/badger/translator.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/state/protocol" 8 "github.com/koko1123/flow-go-1/storage" 9 ) 10 11 // Translator is a translation layer that determines the reference block on 12 // the main chain for a given cluster block, using the reference block from 13 // the cluster block's payload. 14 type Translator struct { 15 payloads storage.ClusterPayloads 16 state protocol.State 17 } 18 19 // NewTranslator returns a new block ID translator. 20 func NewTranslator(payloads storage.ClusterPayloads, state protocol.State) *Translator { 21 translator := &Translator{ 22 payloads: payloads, 23 state: state, 24 } 25 return translator 26 } 27 28 // Translate retrieves the reference main-chain block ID for the given cluster 29 // block ID. 30 func (t *Translator) Translate(blockID flow.Identifier) (flow.Identifier, error) { 31 32 payload, err := t.payloads.ByBlockID(blockID) 33 if err != nil { 34 return flow.ZeroID, fmt.Errorf("could not retrieve reference block payload: %w", err) 35 } 36 37 // if a reference block is specified, use that 38 if payload.ReferenceBlockID != flow.ZeroID { 39 return payload.ReferenceBlockID, nil 40 } 41 42 // otherwise, we are dealing with a root block, and must retrieve the 43 // reference block by epoch number 44 //TODO this returns the latest block in the epoch, thus will take slashing 45 // into account. We don't slash yet, so this is OK short-term. 46 // We should change the API boundaries a bit here, so this chain-aware 47 // translation changes to be f(blockID) -> IdentityList rather than 48 // f(blockID) -> blockID. 49 // REF: https://github.com/dapperlabs/flow-go/issues/4655 50 head, err := t.state.Final().Head() 51 if err != nil { 52 return flow.ZeroID, fmt.Errorf("could not retrieve block: %w", err) 53 } 54 return head.ID(), nil 55 }