code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/store/config.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 store 17 18 import ( 19 "bytes" 20 "crypto/sha256" 21 "encoding/base64" 22 "time" 23 24 "code.vegaprotocol.io/vega/datanode/config/encoding" 25 "code.vegaprotocol.io/vega/logging" 26 27 "github.com/ipfs/kubo/config" 28 "github.com/libp2p/go-libp2p/core/crypto" 29 "github.com/libp2p/go-libp2p/core/peer" 30 uuid "github.com/satori/go.uuid" 31 ) 32 33 type Config struct { 34 // Mandatory Setting, must be set 35 PeerID string `description:"the ipfs peer id of this node" long:"peer-id"` 36 PrivKey string `description:"the ipfs priv key of this node" long:"priv-key"` 37 38 // Optional Settings 39 BootstrapPeers []string `description:"a list of the multiaddress of bootstrap peers, will be used in addition to the ipfs default peers if enabled" long:"bootstrap-peers"` 40 SwarmPort int `description:"ipfs swarm port" long:"swarm-port"` 41 42 // Without this there would be no way to isolate an environment if needed and process a given chains data (e.g. for dev) 43 SwarmKeyOverride string `description:"optional swarm key override, the default behaviour is to use the datanode's chain id'" long:"swarm-key-override"` 44 45 HistoryRetentionBlockSpan int64 `description:"the block span of history, from the most recent history segment, that should be retained" long:"history-retention-block-span"` 46 GarbageCollectionInterval encoding.Duration `description:"the interval at which garbage collection should be run" long:"garbage-collection-interval"` 47 } 48 49 func NewDefaultConfig() Config { 50 seed := uuid.NewV4().String() 51 identity, err := GenerateIdentityFromSeed([]byte(seed)) 52 if err != nil { 53 panic("failed to generate default ipfs identity") 54 } 55 56 return Config{ 57 PeerID: identity.PeerID, 58 PrivKey: identity.PrivKey, 59 60 BootstrapPeers: []string{}, 61 62 SwarmPort: 4001, 63 64 HistoryRetentionBlockSpan: 604800, // One week of history at 1s per block 65 GarbageCollectionInterval: encoding.Duration{Duration: 24 * time.Hour}, 66 } 67 } 68 69 func (c Config) GetSwarmKeySeed(log *logging.Logger, chainID string) string { 70 swarmKeySeed := chainID 71 if len(c.SwarmKeyOverride) > 0 { 72 swarmKeySeed = c.SwarmKeyOverride 73 log.Info("Using swarm key override as the swarm key seed", logging.String("swarm key seed", c.SwarmKeyOverride)) 74 } else { 75 log.Info("Using chain id as the swarm key seed", logging.String("swarm key seed", c.SwarmKeyOverride)) 76 } 77 return swarmKeySeed 78 } 79 80 func GenerateIdentityFromSeed(seed []byte) (config.Identity, error) { 81 ident := config.Identity{} 82 83 var sk crypto.PrivKey 84 var pk crypto.PubKey 85 86 // Everything > 32 bytes is ignored in GenerateEd25519Key so do a little pre hashing 87 seedHash := sha256.Sum256(seed) 88 89 priv, pub, err := crypto.GenerateEd25519Key(bytes.NewReader(seedHash[:])) 90 if err != nil { 91 return ident, err 92 } 93 94 sk = priv 95 pk = pub 96 97 skbytes, err := crypto.MarshalPrivateKey(sk) 98 if err != nil { 99 return ident, err 100 } 101 ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes) 102 103 id, err := peer.IDFromPublicKey(pk) 104 if err != nil { 105 return ident, err 106 } 107 ident.PeerID = id.Pretty() 108 return ident, nil 109 }