code.vegaprotocol.io/vega@v0.79.0/core/idgeneration/generator.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package idgeneration 17 18 import ( 19 "encoding/hex" 20 21 "code.vegaprotocol.io/vega/libs/crypto" 22 ) 23 24 // IDGenerator no mutex required, markets work deterministically, and sequentially. 25 type IDGenerator struct { 26 nextIDBytes []byte 27 } 28 29 // New returns an idGenerator, and is used to abstract this type. 30 func New(rootID string) *IDGenerator { //revive:disable:unexported-return 31 nextIDBytes, err := hex.DecodeString(rootID) 32 if err != nil { 33 panic("failed to create new deterministic id generator: " + err.Error()) 34 } 35 36 return &IDGenerator{ 37 nextIDBytes: nextIDBytes, 38 } 39 } 40 41 func (i *IDGenerator) NextID() string { 42 if i == nil { 43 panic("id generator instance is not initialised") 44 } 45 46 nextID := hex.EncodeToString(i.nextIDBytes) 47 i.nextIDBytes = crypto.Hash(i.nextIDBytes) 48 return nextID 49 }