core

package
v0.0.0-...-79e88c3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NatsBrokerInstance *nats.Conn

Functions

func CloseNATS

func CloseNATS()

func DecodeJSON

func DecodeJSON(data []byte, v interface{}) error

func EncodeJSON

func EncodeJSON(v interface{}) []byte

func EncodeTx

func EncodeTx(tx Transaction) ([]byte, error)

EncodeTx converts a transaction to bytes

func GenerateKeyPair

func GenerateKeyPair() (*ecdsa.PrivateKey, error)

GenerateKeyPair creates a new key pair for signing transactions

func GetLatestBlockHash

func GetLatestBlockHash() string

func GetLatestBlockHeight

func GetLatestBlockHeight() int

func InitBlockchain

func InitBlockchain(chainID string, mp MempoolInterface)

Initialize blockchain

func SetLatestBlock

func SetLatestBlock(block Block)

func SetupNATS

func SetupNATS(natsURL string)

func ValidateTx

func ValidateTx(tx Transaction) error

ValidateTx performs basic validation of a transaction

Types

type Agent

type Agent struct {
	ID               string                 `json:"id"`
	Name             string                 `json:"name"`
	Role             string                 `json:"role"` // "producer" or "validator"
	ValidatorAddress string                 `json:"validator_address,omitempty"`
	IsValidator      bool                   `json:"is_validator"`
	Metadata         map[string]interface{} `json:"metadata"` // Flexible metadata for external agents
}

Agent represents a generic AI-powered entity in ChaosChain (Producer or Validator)

type AgentPersonality

type AgentPersonality struct {
	Name            string   `json:"name"`
	Traits          []string `json:"traits"`
	Style           string   `json:"style"`
	MemePreferences []string `json:"meme_preferences"`
}

AgentPersonality defines an AI validator's personality and decision-making style

type Block

type Block struct {
	Height    int           `json:"height"`
	PrevHash  string        `json:"prev_hash"`
	Txs       []Transaction `json:"transactions"`
	Timestamp int64         `json:"timestamp"`
	Signature string        `json:"signature"`
	Proposer  string        `json:"proposer"`
	ChainID   string        `json:"chain_id"`
}

Block represents a basic block structure

func GetBlockByHeight

func GetBlockByHeight(height int) (Block, bool)

GetBlockByHeight retrieves a block at a specific height

func (*Block) Hash

func (b *Block) Hash() string

Hash returns the block's hash

func (*Block) SignBlock

func (b *Block) SignBlock(privateKey string) error

SignBlock signs a block using the validator's private key

func (*Block) VerifyBlock

func (b *Block) VerifyBlock(publicKey string) bool

VerifyBlock verifies the authenticity of a block

type Blockchain

type Blockchain struct {
	Blocks  []Block
	Mempool MempoolInterface
	ChainID string
	Nodes   map[string]*p2p.Node
	NodesMu sync.RWMutex
}

Blockchain represents a sequence of validated blocks

func GetBlockchain

func GetBlockchain() *Blockchain

GetBlockchain returns the default blockchain instance

func GetChain

func GetChain(chainID string) *Blockchain

Add GetChain helper

func NewBlockchain

func NewBlockchain(chainID string, mp MempoolInterface) *Blockchain

NewBlockchain initializes a blockchain with a genesis block

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(newBlock Block) error

AddBlock appends a new block to the chain

func (*Blockchain) CreateBlock

func (bc *Blockchain) CreateBlock() (*Block, error)

CreateBlock creates a new block proposal (doesn't add to chain)

func (*Blockchain) ProcessTransaction

func (bc *Blockchain) ProcessTransaction(tx Transaction, mp MempoolInterface) error

ProcessTransaction validates and adds a transaction to the mempool

func (*Blockchain) RegisterNode

func (bc *Blockchain) RegisterNode(addr string, node *p2p.Node)

RegisterNode adds a node to the chain's network

func (*Blockchain) ValidateBlock

func (bc *Blockchain) ValidateBlock(block Block) bool

ValidateBlock checks whether a given block follows chain rules

type ChainInfo

type ChainInfo struct {
	ChainID string `json:"chain_id"`
	Name    string `json:"name"`
	Agents  int    `json:"agents"`
	Blocks  int    `json:"blocks"`
}

func GetAllChains

func GetAllChains() []ChainInfo

GetAllChains returns a list of all chain IDs

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database represents a simple in-memory database using string buffer

func NewDatabase

func NewDatabase() *Database

NewDatabase creates a new in-memory database

func (*Database) Delete

func (db *Database) Delete(key string)

Delete removes a key from the database

func (*Database) Get

func (db *Database) Get(key string, value interface{}) error

Get retrieves a value from the database

func (*Database) Set

func (db *Database) Set(key string, value interface{}) error

Set stores a value in the database

type MempoolInterface

type MempoolInterface interface {
	AddTransaction(tx interface{}) bool
	GetPendingTransactions() []Transaction
	RemoveTransaction(txID string)
	CleanupExpiredTransactions()
	Size() int
}

MempoolInterface defines the required functionality for a mempool

type NATSBroker

type NATSBroker struct {
	Conn *nats.Conn
}

NATSBroker encapsulates a NATS connection.

func NewNATSBroker

func NewNATSBroker(url string) (*NATSBroker, error)

NewNATSBroker creates a new NATSBroker connected to the provided URL.

func (*NATSBroker) Close

func (b *NATSBroker) Close()

Close gracefully closes the connection.

func (*NATSBroker) Publish

func (b *NATSBroker) Publish(subject string, data []byte) error

Publish sends data on the provided subject.

func (*NATSBroker) Subscribe

func (b *NATSBroker) Subscribe(subject string, cb nats.MsgHandler) error

Subscribe registers a callback for a specific subject.

type StateRoot

type StateRoot struct {
	StateID string            `json:"state_id"`
	Changes map[string]string `json:"changes"` // Stores arbitrary AI-generated changes
}

StateRoot represents the blockchain's state at a given block height

func (*StateRoot) ToJSON

func (sr *StateRoot) ToJSON() string

ToJSON converts the state root to JSON

type Transaction

type Transaction struct {
	Type      string  `json:"type" amino:"bytes"` // Transaction type (e.g., "register_validator")
	From      string  `json:"from" amino:"bytes"`
	To        string  `json:"to" amino:"bytes"`
	Amount    float64 `json:"amount" amino:"fixed64"`
	Fee       uint64  `json:"fee" amino:"varint"`
	Content   string  `json:"content" amino:"bytes"`
	Timestamp int64   `json:"timestamp" amino:"varint"`
	Signature string  `json:"signature" amino:"bytes"`
	PublicKey string  `json:"publicKey" amino:"bytes"`
	ChainID   string  `json:"chainID" amino:"bytes"`
	Hash      []byte  `json:"hash" amino:"bytes"` // Transaction hash
	Data      []byte  `json:"data" amino:"bytes"`
}

Transaction represents a basic transaction structure

func DecodeTx

func DecodeTx(data []byte) (Transaction, error)

DecodeTx converts bytes to a transaction

func (*Transaction) GetHash

func (tx *Transaction) GetHash() []byte

func (*Transaction) Marshal

func (tx *Transaction) Marshal() ([]byte, error)

func (*Transaction) SignTransaction

func (tx *Transaction) SignTransaction(privateKey *ecdsa.PrivateKey) error

SignTransaction signs a transaction with the given private key

func (*Transaction) VerifyTransaction

func (tx *Transaction) VerifyTransaction(from string) bool

VerifyTransaction verifies the transaction signature

type TxChecker

type TxChecker struct {
	// contains filtered or unexported fields
}

TxChecker implements mempool.TxChecker for CometBFT mempool

func NewTxChecker

func NewTxChecker(app abci.Application) *TxChecker

func (*TxChecker) CheckTx

func (tc *TxChecker) CheckTx(ctx context.Context, tx []byte) (*abci.ResponseCheckTx, error)

CheckTx implements mempool.TxChecker

type ValidationResult

type ValidationResult struct {
	BlockHash string `json:"block_hash"`
	Valid     bool   `json:"valid"`
	Reason    string `json:"reason"`
	Meme      string `json:"meme"`
}

ValidationResult represents the outcome of block validation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL