Code cleanup
This commit is contained in:
parent
4c11425d18
commit
041d6b2d2f
@ -54,6 +54,8 @@ func CreateBlock(lastBlockHeader *BlockHeader, txs []*Transaction, minerEth comm
|
|||||||
merkleHashes = append(merkleHashes, tx.Hash)
|
merkleHashes = append(merkleHashes, tx.Hash)
|
||||||
}
|
}
|
||||||
merkleHashes = append(merkleHashes, lastBlockHeader.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)
|
timestampBytes := make([]byte, 8)
|
||||||
binary.LittleEndian.PutUint64(timestampBytes, uint64(timestamp))
|
binary.LittleEndian.PutUint64(timestampBytes, uint64(timestamp))
|
||||||
merkleHashes = append(merkleHashes, timestampBytes)
|
merkleHashes = append(merkleHashes, timestampBytes)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,7 +21,6 @@ type Config struct {
|
|||||||
Ethereum EthereumConfig `mapstructure:"ethereum"`
|
Ethereum EthereumConfig `mapstructure:"ethereum"`
|
||||||
Filecoin FilecoinConfig `mapstructure:"filecoin"`
|
Filecoin FilecoinConfig `mapstructure:"filecoin"`
|
||||||
PubSub PubSubConfig `mapstructure:"pubsub"`
|
PubSub PubSubConfig `mapstructure:"pubsub"`
|
||||||
Store StoreConfig `mapstructure:"store"`
|
|
||||||
ConsensusMinApprovals int `mapstructure:"consensus_min_approvals"`
|
ConsensusMinApprovals int `mapstructure:"consensus_min_approvals"`
|
||||||
Redis RedisConfig `mapstructure:"redis"`
|
Redis RedisConfig `mapstructure:"redis"`
|
||||||
CacheType string `mapstructure:"cache_type"`
|
CacheType string `mapstructure:"cache_type"`
|
||||||
@ -51,11 +48,6 @@ type FilecoinConfig struct {
|
|||||||
type PubSubConfig struct {
|
type PubSubConfig struct {
|
||||||
ServiceTopicName string `mapstructure:"service_topic_name"`
|
ServiceTopicName string `mapstructure:"service_topic_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StoreConfig struct {
|
|
||||||
DatabaseURL string `mapstructure:"database_url"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RedisConfig struct {
|
type RedisConfig struct {
|
||||||
Addr string `mapstructure:"redis_addr"`
|
Addr string `mapstructure:"redis_addr"`
|
||||||
Password string `mapstructure:"redis_password"`
|
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
|
// NewConfig creates a new config based on default values or provided .env file
|
||||||
func NewConfig(configPath string) (*Config, error) {
|
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{
|
cfg := &Config{
|
||||||
ListenAddr: "localhost",
|
ListenAddr: "localhost",
|
||||||
ListenPort: 8000,
|
ListenPort: 8000,
|
||||||
@ -87,15 +74,12 @@ func NewConfig(configPath string) (*Config, error) {
|
|||||||
PubSub: PubSubConfig{
|
PubSub: PubSubConfig{
|
||||||
ServiceTopicName: "dione",
|
ServiceTopicName: "dione",
|
||||||
},
|
},
|
||||||
Store: StoreConfig{
|
|
||||||
DatabaseURL: dbURL,
|
|
||||||
},
|
|
||||||
Redis: RedisConfig{
|
Redis: RedisConfig{
|
||||||
Addr: "redisDB:6379",
|
Addr: "localhost:6379",
|
||||||
Password: "",
|
Password: "",
|
||||||
DB: 0,
|
DB: 0,
|
||||||
},
|
},
|
||||||
CacheType: "in-memory",
|
CacheType: "memory",
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.SetConfigFile(configPath)
|
viper.SetConfigFile(configPath)
|
||||||
|
@ -41,17 +41,16 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ConsensusHandler struct {
|
type ConsensusHandler struct {
|
||||||
bus EventBus.Bus
|
bus EventBus.Bus
|
||||||
psb *pubsub.PubSubRouter
|
psb *pubsub.PubSubRouter
|
||||||
privKey crypto.PrivKey
|
privKey crypto.PrivKey
|
||||||
validator *ConsensusValidator
|
validator *ConsensusValidator
|
||||||
ethereumClient ethclient.EthereumSideAPI
|
ethereumClient ethclient.EthereumSideAPI
|
||||||
miner *blockchain.Miner
|
miner *blockchain.Miner
|
||||||
consensus *ConsensusManager
|
consensus *ConsensusManager
|
||||||
mempool *pool.Mempool
|
mempool *pool.Mempool
|
||||||
blockchain *blockchain.BlockChain
|
blockchain *blockchain.BlockChain
|
||||||
address peer.ID
|
address peer.ID
|
||||||
stateChangeChannels map[string]map[State][]chan bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsensusHandler(
|
func NewConsensusHandler(
|
||||||
@ -67,17 +66,16 @@ func NewConsensusHandler(
|
|||||||
h host.Host,
|
h host.Host,
|
||||||
) *ConsensusHandler {
|
) *ConsensusHandler {
|
||||||
pcm := &ConsensusHandler{
|
pcm := &ConsensusHandler{
|
||||||
psb: psb,
|
psb: psb,
|
||||||
miner: miner,
|
miner: miner,
|
||||||
validator: NewConsensusValidator(miner, bc, db),
|
validator: NewConsensusValidator(miner, bc, db),
|
||||||
privKey: privKey,
|
privKey: privKey,
|
||||||
ethereumClient: ethereumClient,
|
ethereumClient: ethereumClient,
|
||||||
bus: bus,
|
bus: bus,
|
||||||
consensus: bp,
|
consensus: bp,
|
||||||
mempool: mempool,
|
mempool: mempool,
|
||||||
blockchain: bc,
|
blockchain: bc,
|
||||||
address: h.ID(),
|
address: h.ID(),
|
||||||
stateChangeChannels: map[string]map[State][]chan bool{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pcm.psb.Hook(pubsub.PrePrepareMessageType, pcm.handlePrePrepare)
|
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)
|
minedBlock, err := pcm.miner.MineBlock(entry.Data, entry.Round)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, blockchain.ErrNoTxForBlock) {
|
if errors.Is(err, blockchain.ErrNoTxForBlock) {
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
package lib
|
|
||||||
|
|
||||||
import "encoding/base64"
|
|
||||||
|
|
||||||
func BasicAuth(username, password string) string {
|
|
||||||
auth := username + ":" + password
|
|
||||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
|
||||||
}
|
|
@ -193,7 +193,6 @@ func provideAppFlags() *AppFlags {
|
|||||||
func provideConfig(flags *AppFlags) *config.Config {
|
func provideConfig(flags *AppFlags) *config.Config {
|
||||||
if flags.ConfigPath == "" {
|
if flags.ConfigPath == "" {
|
||||||
logrus.Fatal("no config path provided")
|
logrus.Fatal("no config path provided")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := config.NewConfig(flags.ConfigPath)
|
cfg, err := config.NewConfig(flags.ConfigPath)
|
||||||
@ -252,7 +251,7 @@ func providePrivateKey(cfg *config.Config) crypto.PrivKey {
|
|||||||
|
|
||||||
func generatePrivateKey() (crypto.PrivKey, error) {
|
func generatePrivateKey() (crypto.PrivKey, error) {
|
||||||
r := rand.Reader
|
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)
|
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.Ed25519, 2048, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user