|
| 1 | +package babe |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + sc "github.com/LimeChain/goscale" |
| 7 | + "github.com/LimeChain/gosemble/constants/metadata" |
| 8 | + "github.com/LimeChain/gosemble/frame/babe" |
| 9 | + babetypes "github.com/LimeChain/gosemble/primitives/babe" |
| 10 | + "github.com/LimeChain/gosemble/primitives/hashing" |
| 11 | + "github.com/LimeChain/gosemble/primitives/log" |
| 12 | + primitives "github.com/LimeChain/gosemble/primitives/types" |
| 13 | + "github.com/LimeChain/gosemble/utils" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + ApiModuleName = "BabeApi" |
| 18 | + apiVersion = 2 |
| 19 | +) |
| 20 | + |
| 21 | +// Module implements the BabeApi Runtime API definition, necessary for block authorship. |
| 22 | +type Module struct { |
| 23 | + babe babe.Module |
| 24 | + memUtils utils.WasmMemoryTranslator |
| 25 | + logger log.Logger |
| 26 | +} |
| 27 | + |
| 28 | +func New(babe babe.Module, logger log.Logger) Module { |
| 29 | + return Module{ |
| 30 | + babe: babe, |
| 31 | + memUtils: utils.NewMemoryTranslator(), |
| 32 | + logger: logger, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Name returns the name of the api module. |
| 37 | +func (m Module) Name() string { |
| 38 | + return ApiModuleName |
| 39 | +} |
| 40 | + |
| 41 | +// Item returns the first 8 bytes of the Blake2b hash of the name and version of the api module. |
| 42 | +func (m Module) Item() primitives.ApiItem { |
| 43 | + hash := hashing.MustBlake2b8([]byte(ApiModuleName)) |
| 44 | + return primitives.NewApiItem(hash, apiVersion) |
| 45 | +} |
| 46 | + |
| 47 | +// Metadata returns the runtime api metadata of the module. |
| 48 | +func (m Module) Metadata() primitives.RuntimeApiMetadata { |
| 49 | + methods := sc.Sequence[primitives.RuntimeApiMethodMetadata]{ |
| 50 | + primitives.RuntimeApiMethodMetadata{ |
| 51 | + Name: "configuration", |
| 52 | + Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{}, |
| 53 | + Output: sc.ToCompact(metadata.TypesBabeConfiguration), |
| 54 | + Docs: sc.Sequence[sc.Str]{""}, |
| 55 | + }, |
| 56 | + primitives.RuntimeApiMethodMetadata{ |
| 57 | + Name: "current_epoch_start", |
| 58 | + Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{}, |
| 59 | + Output: sc.ToCompact(metadata.PrimitiveTypesU64), |
| 60 | + Docs: sc.Sequence[sc.Str]{""}, |
| 61 | + }, |
| 62 | + primitives.RuntimeApiMethodMetadata{ |
| 63 | + Name: "current_epoch", |
| 64 | + Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{}, |
| 65 | + Output: sc.ToCompact(metadata.TypesBabeEpoch), |
| 66 | + Docs: sc.Sequence[sc.Str]{""}, |
| 67 | + }, |
| 68 | + primitives.RuntimeApiMethodMetadata{ |
| 69 | + Name: "next_epoch", |
| 70 | + Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{}, |
| 71 | + Output: sc.ToCompact(metadata.TypesBabeEpoch), |
| 72 | + Docs: sc.Sequence[sc.Str]{""}, |
| 73 | + }, |
| 74 | + // TODO: add metadata for GenerateKeyOwnershipProof and SubmitReportEquivocationUnsignedExtrinsic |
| 75 | + } |
| 76 | + |
| 77 | + return primitives.RuntimeApiMetadata{ |
| 78 | + Name: ApiModuleName, |
| 79 | + Methods: methods, |
| 80 | + Docs: sc.Sequence[sc.Str]{"Babe consensus API module."}, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Returns configuration data used by the Babe consensus engine. |
| 85 | +func (m Module) Configuration() int64 { |
| 86 | + epochConfig, err := m.babe.StorageEpochConfig() |
| 87 | + if err != nil { |
| 88 | + m.logger.Critical(err.Error()) |
| 89 | + } |
| 90 | + |
| 91 | + if reflect.DeepEqual(epochConfig, babetypes.EpochConfiguration{}) { |
| 92 | + epochConfig = m.babe.EpochConfig() |
| 93 | + } |
| 94 | + |
| 95 | + authorities, err := m.babe.StorageAuthorities() |
| 96 | + if err != nil { |
| 97 | + m.logger.Critical(err.Error()) |
| 98 | + } |
| 99 | + |
| 100 | + randomness, err := m.babe.StorageRandomness() |
| 101 | + if err != nil { |
| 102 | + m.logger.Critical(err.Error()) |
| 103 | + } |
| 104 | + |
| 105 | + config := babetypes.Configuration{ |
| 106 | + // The slot duration in milliseconds. Currently, only the value provided by this |
| 107 | + // type at genesis will be used. Dynamic slot duration may be supported in the future. |
| 108 | + SlotDuration: m.babe.SlotDuration(), |
| 109 | + // The duration of epochs in slots. |
| 110 | + EpochLength: m.babe.EpochDuration(), |
| 111 | + // A constant value that is used in the threshold calculation formula as defined in |
| 112 | + // https://spec.polkadot.network/sect-block-production#defn-babe-constant |
| 113 | + C: epochConfig.C, |
| 114 | + // The authority list for the genesis epoch as defined in |
| 115 | + // https://spec.polkadot.network/chap-sync#defn-authority-list |
| 116 | + Authorities: authorities, |
| 117 | + // The randomness for the genesis epoch. |
| 118 | + Randomness: randomness, |
| 119 | + // Whether this chain should run with a round-robin-style secondary slot and |
| 120 | + // if this secondary slot requires the inclusion of an auxiliary VRF output. |
| 121 | + AllowedSlots: epochConfig.AllowedSlots, |
| 122 | + } |
| 123 | + |
| 124 | + return m.memUtils.BytesToOffsetAndSize(config.Bytes()) |
| 125 | +} |
| 126 | + |
| 127 | +// Returns the start slot of the current epoch. |
| 128 | +func (m Module) CurrentEpochStart() int64 { |
| 129 | + epochStart, err := m.babe.CurrentEpochStart() |
| 130 | + if err != nil { |
| 131 | + m.logger.Critical(err.Error()) |
| 132 | + } |
| 133 | + |
| 134 | + return m.memUtils.BytesToOffsetAndSize(epochStart.Bytes()) |
| 135 | +} |
| 136 | + |
| 137 | +// Returns information regarding the current epoch. |
| 138 | +func (m Module) CurrentEpoch() int64 { |
| 139 | + epoch, err := m.babe.CurrentEpoch() |
| 140 | + if err != nil { |
| 141 | + m.logger.Critical(err.Error()) |
| 142 | + } |
| 143 | + |
| 144 | + return m.memUtils.BytesToOffsetAndSize(epoch.Bytes()) |
| 145 | +} |
| 146 | + |
| 147 | +// Returns information regarding the next epoch (which was already |
| 148 | +// previously announced). |
| 149 | +func (m Module) NextEpoch() int64 { |
| 150 | + epoch, err := m.babe.NextEpoch() |
| 151 | + if err != nil { |
| 152 | + m.logger.Critical(err.Error()) |
| 153 | + } |
| 154 | + |
| 155 | + return m.memUtils.BytesToOffsetAndSize(epoch.Bytes()) |
| 156 | +} |
| 157 | + |
| 158 | +// Generates a proof of key ownership for the given authority in the |
| 159 | +// current epoch. An example usage of this module is coupled with the |
| 160 | +// session historical module to prove that a given authority key is |
| 161 | +// tied to a given staking identity during a specific session. Proofs |
| 162 | +// of key ownership are necessary for submitting equivocation reports. |
| 163 | +// NOTE: even though the API takes a `slot` as parameter the current |
| 164 | +// implementations ignores this parameter and instead relies on this |
| 165 | +// method being called at the correct block height, i.e. any point at |
| 166 | +// which the epoch for the given slot is live on-chain. Future |
| 167 | +// implementations will instead use indexed data through an offchain |
| 168 | +// worker, not requiring older states to be available. |
| 169 | +func (m Module) GenerateKeyOwnershipProof(dataPtr int32, dataLen int32) int64 { |
| 170 | + // TODO: Implement |
| 171 | + |
| 172 | + m.logger.Critical("GenerateKeyOwnershipProof is not implemented") |
| 173 | + |
| 174 | + return m.memUtils.BytesToOffsetAndSize(sc.NewOption[sc.Empty](nil).Bytes()) |
| 175 | +} |
| 176 | + |
| 177 | +// Submits an unsigned extrinsic to report an equivocation. The caller |
| 178 | +// must provide the equivocation proof and a key ownership proof |
| 179 | +// (should be obtained using `generate_key_ownership_proof`). The |
| 180 | +// extrinsic will be unsigned and should only be accepted for local |
| 181 | +// authorship (not to be broadcast to the network). This method returns |
| 182 | +// `None` when creation of the extrinsic fails, e.g. if equivocation |
| 183 | +// reporting is disabled for the given runtime (i.e. this method is |
| 184 | +// hardcoded to return `None`). Only useful in an offchain context. |
| 185 | +func (m Module) SubmitReportEquivocationUnsignedExtrinsic(dataPtr int32, dataLen int32) int64 { |
| 186 | + // TODO: Implement |
| 187 | + |
| 188 | + m.logger.Critical("SubmitReportEquivocationUnsignedExtrinsic is not implemented") |
| 189 | + |
| 190 | + return m.memUtils.BytesToOffsetAndSize(sc.NewOption[sc.Empty](nil).Bytes()) |
| 191 | +} |
0 commit comments