Skip to content

Commit

Permalink
ağ haberleşmesinde ufak bir sorun var
Browse files Browse the repository at this point in the history
  • Loading branch information
SadikSunbul committed Jun 23, 2024
1 parent c813b54 commit e64ed9e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ func openDB(dir string, opts badger.Options) (*badger.DB, error) {
if db, err := badger.Open(opts); err != nil {
if strings.Contains(err.Error(), "LOCK") {
if db, err := retry(dir, opts); err == nil {
log.Println("database unlocked, value log truncated")
log.Println("veritabanının kilidi açıldı, değer günlüğü kesildi")
return db, nil
}
log.Println("could not unlock database:", err)
log.Println("veritabanının kilidi açılamadı:", err)
}
return nil, err
} else {
Expand Down
30 changes: 30 additions & 0 deletions blockchain/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,33 @@ func (u *UTXOSet) DeleteByPrefix(prefix []byte) {
return nil
})
}

func (u UTXOSet) FindUnspentTransactions(pubKeyHash []byte) []TxOutput {
var UTXOs []TxOutput

db := u.Blockchain.Database

err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions

it := txn.NewIterator(opts)
defer it.Close()

for it.Seek(utxoPrefix); it.ValidForPrefix(utxoPrefix); it.Next() {
item := it.Item()
v, err := item.ValueCopy(nil)
Handle(err)
outs := DeserializeOutputs(v)
for _, out := range outs.Outputs {
if out.IsLockedWithKey(pubKeyHash) {
UTXOs = append(UTXOs, out)
}
}

}
return nil
})
Handle(err)

return UTXOs
}
16 changes: 9 additions & 7 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func (cli *CommandLine) getBalance(address, nodeID string) {
defer chain.Database.Close() // blok zincirini kapat

balance := 0
pubKeyHash := wallet.Base58Decode([]byte(address)) // adresin base58 kodunu okur
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4] // adresin ilk 4 karakterini kaldırır
UTXOs := UTXOSet.FindUTXO(pubKeyHash) // adresin bakiyesini bulur
pubKeyHash := wallet.Base58Decode([]byte(address)) // adresin base58 kodunu okur
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4] // adresin ilk 4 karakterini kaldırır
UTXOs := UTXOSet.FindUnspentTransactions(pubKeyHash) // adresin bakiyesini bulur

for _, out := range UTXOs { // bakiye döngüsü
balance += out.Value // bakiyeyi arttırır
Expand Down Expand Up @@ -182,11 +182,13 @@ func (cli *CommandLine) Run() { // komut satırı işlemleri

nodeID := os.Getenv("NODE_ID") // Set-Item -Path Env:NODE_ID -Value "3000" | set NODE_ID=3000
/*
Set-Item -Path Env:NODE_ID -Value "3000"
Set-Item -Path Env:NODE_ID -Value "4000"
Set-Item -Path Env:NODE_ID -Value "5000"
Set-Item -Path Env:NODE_ID -Value "3000"
Set-Item -Path Env:NODE_ID -Value "4000"
Set-Item -Path Env:NODE_ID -Value "5000"
xcopy blocks_3000 blocks_5000
xcopy blocks_3000 blocks_5000
xcopy blocks_3000 blocks_4000
xcopy blocks_3000 blocks_gen
*/
if nodeID == "" {
Expand Down

0 comments on commit e64ed9e

Please sign in to comment.