blockchain sandbox: UTXO chain, p2p mining, attacker node

This commit is contained in:
2026-07-26 20:37:26 +03:00
commit 7ce39c8964
18 changed files with 2536 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package chain
import (
"crypto/ed25519"
"encoding/hex"
"maps"
"strconv"
)
type CoinID struct {
TxHash string
Index int
}
func (id CoinID) Key() string { return id.TxHash + ":" + strconv.Itoa(id.Index) }
type Coin struct {
Amount int
Owner ed25519.PublicKey
}
func (c Coin) OwnerHex() string { return hex.EncodeToString(c.Owner) }
type UnspentCoin struct {
Coin
CoinID CoinID
}
type CoinStore map[string]*UnspentCoin
func (s CoinStore) Add(txHash string, idx int, coin Coin) {
id := CoinID{TxHash: txHash, Index: idx}
s[id.Key()] = &UnspentCoin{Coin: coin, CoinID: id}
}
func (s CoinStore) Spend(id CoinID) { delete(s, id.Key()) }
func (s CoinStore) Get(id CoinID) *UnspentCoin { return s[id.Key()] }
func (s CoinStore) Clone() CoinStore {
c := make(CoinStore, len(s))
maps.Copy(c, s)
return c
}