From 5b14eb3f284b61c11d659bc051631e3522f5d8bf Mon Sep 17 00:00:00 2001 From: oxwa Date: Sun, 6 Oct 2024 16:33:49 +0300 Subject: [PATCH] Changes to be committed: new file: cmd/geth/consensus/hybrid.go new file: cmd/geth/core/blockchain.go modified: cmd/geth/main.go new file: cmd/geth/tests/hybrid_test.go new file: consensus/hybrid-consensus/consensus.go new file: consensus/hybrid-consensus/consensus_test.go new file: consensus/hybrid-consensus/go.mod new file: consensus/hybrid-consensus/hybridconsensus.go new file: consensus/hybrid-consensus/metrics.go new file: consensus/hybrid-consensus/validators.go --- cmd/geth/consensus/hybrid.go | 22 +++++++ cmd/geth/core/blockchain.go | 9 +++ cmd/geth/main.go | 20 ++++++ cmd/geth/tests/hybrid_test.go | 11 ++++ consensus/hybrid-consensus/consensus.go | 64 +++++++++++++++++++ consensus/hybrid-consensus/consensus_test.go | 24 +++++++ consensus/hybrid-consensus/go.mod | 4 ++ consensus/hybrid-consensus/hybridconsensus.go | 36 +++++++++++ consensus/hybrid-consensus/metrics.go | 16 +++++ consensus/hybrid-consensus/validators.go | 16 +++++ 10 files changed, 222 insertions(+) create mode 100644 cmd/geth/consensus/hybrid.go create mode 100644 cmd/geth/core/blockchain.go create mode 100644 cmd/geth/tests/hybrid_test.go create mode 100644 consensus/hybrid-consensus/consensus.go create mode 100644 consensus/hybrid-consensus/consensus_test.go create mode 100644 consensus/hybrid-consensus/go.mod create mode 100644 consensus/hybrid-consensus/hybridconsensus.go create mode 100644 consensus/hybrid-consensus/metrics.go create mode 100644 consensus/hybrid-consensus/validators.go diff --git a/cmd/geth/consensus/hybrid.go b/cmd/geth/consensus/hybrid.go new file mode 100644 index 0000000000..db369c5463 --- /dev/null +++ b/cmd/geth/consensus/hybrid.go @@ -0,0 +1,22 @@ +package consensus + +import "log" + +type HybridConsensus struct { + PoW *ProofOfWork + PoS *ProofOfStake +} + +type ProofOfWork struct { + // PoW ile ilgili alanlar +} + +type ProofOfStake struct { + // PoS ile ilgili alanlar +} + +func (h *HybridConsensus) Start() { + log.Println("Hybrid konsensüs başlatıldı.") + // Başlatma işlemleri +} + diff --git a/cmd/geth/core/blockchain.go b/cmd/geth/core/blockchain.go new file mode 100644 index 0000000000..12f5a6e11d --- /dev/null +++ b/cmd/geth/core/blockchain.go @@ -0,0 +1,9 @@ +package core + +import "log" + +func ProcessBlock() { + // Hybrid konsensüs ile blok işleme mantığı + log.Println("Blok işleniyor...") +} + diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 2675a61675..37b8913f76 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -45,6 +45,7 @@ import ( _ "github.com/ethereum/go-ethereum/eth/tracers/native" "github.com/urfave/cli/v2" + "github.com/oxwa/hybrid-consensus" // Hibrit konsensüs paketini içe aktarın ) const ( @@ -277,6 +278,25 @@ func init() { } func main() { + // Validator listesi (örnek) + validators := []hybridconsensus.Validator{ + {Address: common.HexToAddress("0xValidator1"), Stake: 100}, + {Address: common.HexToAddress("0xValidator2"), Stake: 200}, + } + + // Hibrit konsensüs algoritmasını başlat + hc := hybridconsensus.NewHybridConsensus(validators) + + // İlk bloğu oluştur + block, err := hc.CreateBlock(validators[0].Address) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to create block: %v\n", err) + os.Exit(1) + } + + fmt.Printf("Created block: %+v\n", block) + + // Uygulamanızı başlat if err := app.Run(os.Args); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/cmd/geth/tests/hybrid_test.go b/cmd/geth/tests/hybrid_test.go new file mode 100644 index 0000000000..c543b3d65c --- /dev/null +++ b/cmd/geth/tests/hybrid_test.go @@ -0,0 +1,11 @@ +package tests + +import ( + "testing" +) + +func TestHybridConsensus(t *testing.T) { + // Hybrid konsensüs testleri + // Örnek test kodları +} + diff --git a/consensus/hybrid-consensus/consensus.go b/consensus/hybrid-consensus/consensus.go new file mode 100644 index 0000000000..91210c37c9 --- /dev/null +++ b/consensus/hybrid-consensus/consensus.go @@ -0,0 +1,64 @@ +package hybridconsensus + +import ( + "errors" + "time" +) + +type Block struct { + Number uint64 + Validator string + Timestamp time.Time + PrevHash string +} + +type Validator struct { + Address string + Stake uint64 +} + +type HybridConsensus struct { + validators []Validator + blocks []Block +} + +func NewHybridConsensus(validators []Validator) *HybridConsensus { + return &HybridConsensus{ + validators: validators, + blocks: []Block{}, + } +} + +func (hc *HybridConsensus) CreateBlock(validator string) (*Block, error) { + if !hc.IsValidator(validator) { + return nil, errors.New("validator not authorized") + } + + newBlock := Block{ + Number: uint64(len(hc.blocks) + 1), + Validator: validator, + Timestamp: time.Now(), + PrevHash: hc.GetLatestBlockHash(), + } + + hc.blocks = append(hc.blocks, newBlock) + return &newBlock, nil +} + +func (hc *HybridConsensus) GetLatestBlockHash() string { + if len(hc.blocks) == 0 { + return "" + } + latestBlock := hc.blocks[len(hc.blocks)-1] + return string(latestBlock.Number) +} + +func (hc *HybridConsensus) IsValidator(address string) bool { + for _, validator := range hc.validators { + if validator.Address == address { + return true + } + } + return false +} + diff --git a/consensus/hybrid-consensus/consensus_test.go b/consensus/hybrid-consensus/consensus_test.go new file mode 100644 index 0000000000..70a0fdbd37 --- /dev/null +++ b/consensus/hybrid-consensus/consensus_test.go @@ -0,0 +1,24 @@ +package hybridconsensus + +import ( + "testing" +) + +func TestCreateBlock(t *testing.T) { + validators := []Validator{ + {Address: "0xValidator1", Stake: 100}, + {Address: "0xValidator2", Stake: 200}, + } + + hc := NewHybridConsensus(validators) + + block, err := hc.CreateBlock("0xValidator1") + if err != nil { + t.Errorf("expected no error, got %v", err) + } + + if block.Number != 1 { + t.Errorf("expected block number 1, got %d", block.Number) + } +} + diff --git a/consensus/hybrid-consensus/go.mod b/consensus/hybrid-consensus/go.mod new file mode 100644 index 0000000000..0da25d2254 --- /dev/null +++ b/consensus/hybrid-consensus/go.mod @@ -0,0 +1,4 @@ +module github.com/oxwa/hybrid-consensus + +go 1.23.2 + diff --git a/consensus/hybrid-consensus/hybridconsensus.go b/consensus/hybrid-consensus/hybridconsensus.go new file mode 100644 index 0000000000..cc51ca5f5c --- /dev/null +++ b/consensus/hybrid-consensus/hybridconsensus.go @@ -0,0 +1,36 @@ +package hybridconsensus + +import ( + "github.com/ethereum/go-ethereum/common" + "time" +) + +type Validator struct { + Address common.Address + Stake uint64 +} + +type Block struct { + Number uint64 + Time time.Time + Validators []common.Address +} + +type HybridConsensus struct { + validators []Validator +} + +func NewHybridConsensus(validators []Validator) *HybridConsensus { + return &HybridConsensus{validators: validators} +} + +func (hc *HybridConsensus) CreateBlock(validatorAddress common.Address) (*Block, error) { + // Burada blok oluşturma mantığını ekleyin + block := &Block{ + Number: 1, // Blok numarasını güncel bir mekanizma ile güncelleyin + Time: time.Now(), + Validators: []common.Address{validatorAddress}, + } + return block, nil +} + diff --git a/consensus/hybrid-consensus/metrics.go b/consensus/hybrid-consensus/metrics.go new file mode 100644 index 0000000000..53ad89f5a0 --- /dev/null +++ b/consensus/hybrid-consensus/metrics.go @@ -0,0 +1,16 @@ +package hybridconsensus + +import ( + "time" +) + +type Metrics struct { + BlockCreationTime time.Duration + TotalBlocks uint64 +} + +func (hc *HybridConsensus) RecordBlockCreationTime(start time.Time) { + hc.metrics.BlockCreationTime = time.Since(start) + hc.metrics.TotalBlocks++ +} + diff --git a/consensus/hybrid-consensus/validators.go b/consensus/hybrid-consensus/validators.go new file mode 100644 index 0000000000..73316371fe --- /dev/null +++ b/consensus/hybrid-consensus/validators.go @@ -0,0 +1,16 @@ +package hybridconsensus + +// Validator ekleme, çıkarma ve doğrulama işlevleri +func (hc *HybridConsensus) AddValidator(address string, stake uint64) { + hc.validators = append(hc.validators, Validator{Address: address, Stake: stake}) +} + +func (hc *HybridConsensus) RemoveValidator(address string) { + for i, validator := range hc.validators { + if validator.Address == address { + hc.validators = append(hc.validators[:i], hc.validators[i+1:]...) + return + } + } +} +