From 041d6b2d2f2c647ce1c44b5825d0b12feb7a4203 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 2 Sep 2021 20:34:41 +0300 Subject: [PATCH] Code cleanup --- blockchain/types/block.go | 2 ++ config/config.go | 20 ++----------- consensus/consensus_handler.go | 53 +++++++++++++--------------------- lib/net.go | 8 ----- node/node_dep_providers.go | 3 +- 5 files changed, 25 insertions(+), 61 deletions(-) delete mode 100644 lib/net.go diff --git a/blockchain/types/block.go b/blockchain/types/block.go index f85b3e2..edafada 100644 --- a/blockchain/types/block.go +++ b/blockchain/types/block.go @@ -54,6 +54,8 @@ func CreateBlock(lastBlockHeader *BlockHeader, txs []*Transaction, minerEth comm merkleHashes = append(merkleHashes, tx.Hash) } merkleHashes = append(merkleHashes, lastBlockHeader.Hash) + + // we use timestamp as salt for block hash, because salt doesn't work in this merkle tree library for some reason timestampBytes := make([]byte, 8) binary.LittleEndian.PutUint64(timestampBytes, uint64(timestamp)) merkleHashes = append(merkleHashes, timestampBytes) diff --git a/config/config.go b/config/config.go index 5921e84..c8fcf69 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,6 @@ package config import ( - "fmt" - "github.com/spf13/viper" ) @@ -23,7 +21,6 @@ type Config struct { Ethereum EthereumConfig `mapstructure:"ethereum"` Filecoin FilecoinConfig `mapstructure:"filecoin"` PubSub PubSubConfig `mapstructure:"pubsub"` - Store StoreConfig `mapstructure:"store"` ConsensusMinApprovals int `mapstructure:"consensus_min_approvals"` Redis RedisConfig `mapstructure:"redis"` CacheType string `mapstructure:"cache_type"` @@ -51,11 +48,6 @@ type FilecoinConfig struct { type PubSubConfig struct { ServiceTopicName string `mapstructure:"service_topic_name"` } - -type StoreConfig struct { - DatabaseURL string `mapstructure:"database_url"` -} - type RedisConfig struct { Addr string `mapstructure:"redis_addr"` Password string `mapstructure:"redis_password"` @@ -71,11 +63,6 @@ type BlockchainConfig struct { // NewConfig creates a new config based on default values or provided .env file func NewConfig(configPath string) (*Config, error) { - dbName := "dione" - username := "user" - password := "password" - dbURL := fmt.Sprintf("host=localhost user=%s password=%s dbname=%s sslmode=disable", username, password, dbName) - cfg := &Config{ ListenAddr: "localhost", ListenPort: 8000, @@ -87,15 +74,12 @@ func NewConfig(configPath string) (*Config, error) { PubSub: PubSubConfig{ ServiceTopicName: "dione", }, - Store: StoreConfig{ - DatabaseURL: dbURL, - }, Redis: RedisConfig{ - Addr: "redisDB:6379", + Addr: "localhost:6379", Password: "", DB: 0, }, - CacheType: "in-memory", + CacheType: "memory", } viper.SetConfigFile(configPath) diff --git a/consensus/consensus_handler.go b/consensus/consensus_handler.go index a9daa1b..4df4036 100644 --- a/consensus/consensus_handler.go +++ b/consensus/consensus_handler.go @@ -41,17 +41,16 @@ var ( ) type ConsensusHandler struct { - bus EventBus.Bus - psb *pubsub.PubSubRouter - privKey crypto.PrivKey - validator *ConsensusValidator - ethereumClient ethclient.EthereumSideAPI - miner *blockchain.Miner - consensus *ConsensusManager - mempool *pool.Mempool - blockchain *blockchain.BlockChain - address peer.ID - stateChangeChannels map[string]map[State][]chan bool + bus EventBus.Bus + psb *pubsub.PubSubRouter + privKey crypto.PrivKey + validator *ConsensusValidator + ethereumClient ethclient.EthereumSideAPI + miner *blockchain.Miner + consensus *ConsensusManager + mempool *pool.Mempool + blockchain *blockchain.BlockChain + address peer.ID } func NewConsensusHandler( @@ -67,17 +66,16 @@ func NewConsensusHandler( h host.Host, ) *ConsensusHandler { pcm := &ConsensusHandler{ - psb: psb, - miner: miner, - validator: NewConsensusValidator(miner, bc, db), - privKey: privKey, - ethereumClient: ethereumClient, - bus: bus, - consensus: bp, - mempool: mempool, - blockchain: bc, - address: h.ID(), - stateChangeChannels: map[string]map[State][]chan bool{}, + psb: psb, + miner: miner, + validator: NewConsensusValidator(miner, bc, db), + privKey: privKey, + ethereumClient: ethereumClient, + bus: bus, + consensus: bp, + mempool: mempool, + blockchain: bc, + address: h.ID(), } pcm.psb.Hook(pubsub.PrePrepareMessageType, pcm.handlePrePrepare) @@ -301,17 +299,6 @@ func (pcm *ConsensusHandler) onNewBeaconEntry(entry types2.BeaconEntry) { } } - for k, v := range pcm.stateChangeChannels { - for k1, j := range v { - for _, ch := range j { - ch <- true - close(ch) - } - delete(v, k1) - } - delete(pcm.stateChangeChannels, k) - } - minedBlock, err := pcm.miner.MineBlock(entry.Data, entry.Round) if err != nil { if errors.Is(err, blockchain.ErrNoTxForBlock) { diff --git a/lib/net.go b/lib/net.go deleted file mode 100644 index 8ac0656..0000000 --- a/lib/net.go +++ /dev/null @@ -1,8 +0,0 @@ -package lib - -import "encoding/base64" - -func BasicAuth(username, password string) string { - auth := username + ":" + password - return base64.StdEncoding.EncodeToString([]byte(auth)) -} diff --git a/node/node_dep_providers.go b/node/node_dep_providers.go index 288b4c6..7ec1be2 100644 --- a/node/node_dep_providers.go +++ b/node/node_dep_providers.go @@ -193,7 +193,6 @@ func provideAppFlags() *AppFlags { func provideConfig(flags *AppFlags) *config.Config { if flags.ConfigPath == "" { logrus.Fatal("no config path provided") - } cfg, err := config.NewConfig(flags.ConfigPath) @@ -252,7 +251,7 @@ func providePrivateKey(cfg *config.Config) crypto.PrivKey { func generatePrivateKey() (crypto.PrivKey, error) { r := rand.Reader - // Creates a new RSA key pair for this host. + // Creates a new Ed25519 key pair for this host. prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.Ed25519, 2048, r) if err != nil { return nil, err