chain

package
v0.0.0-...-a5060f3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CommitteeSize  = 15      //5,10,15,20
	NumTxs         = 50      // the maximum number of transactions that contained in the pool
	BaseNode       = ":9000" // the initial node in the blockchain network
	DefaultStake   = 1
	NodeNum        = 100 //100, 200, 500, 1000
	SybilRatio     = 0.0 //the fraction of sybils in blockchain nodes
	MaxVoteStep    = 5
	BlockTimeout   = 240 * time.Second
	VotingTimeout  = 200 * time.Second
	CommiteeSleep  = 20 * time.Second
	CandidateSleep = 20 * time.Second
)

Functions

func BlockToByteWithSig

func BlockToByteWithSig(block Block) []byte

func BlockToByteWithoutSig

func BlockToByteWithoutSig(block Block) []byte

func ComputeHashForBlock

func ComputeHashForBlock(blockBytes []byte) []byte

func DownloadIPFS

func DownloadIPFS(address string) []byte

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func LocalTxHash

func LocalTxHash(tx LocalTransaction) string

func LocalTxToByte

func LocalTxToByte(tx LocalTransaction) []byte

func Random

func Random(low int, high int) int

func RandomArray

func RandomArray(length int, low int, high int) []int

func SaveBlock

func SaveBlock(node_id int, block Block)

func TxToByte

func TxToByte(tx Transaction) []byte

func TxToByteWithoutDeltas

func TxToByteWithoutDeltas(tx Transaction) []byte

func UploadIPFS

func UploadIPFS(data []byte) string

func ValidCandidateBlock

func ValidCandidateBlock(lastBlock Block, candidateBlock Block, globalParam [][]float64, momentum [][]float64, modelSize [][]int, compSize []int, PartNum int, rank int, beta float64, slr float64, unlabel *python3.PyObject, model *python3.PyObject) bool

func ValidChain

func ValidChain(localChain []Block, peerChain []Block) bool

func ValidLocalTx

func ValidLocalTx(tx LocalTransaction, modelSize []int) bool

func ValidVerifiedBlock

func ValidVerifiedBlock(lastBlock Block, verifiedBlock Block) bool

Types

type Block

type Block struct {
	Index         int64         `json:"index"`        // the index of current block
	TimeStamp     int64         `json:"timeStamp"`    // the creation timestamp
	PrevBlockHash string        `json:"prevHash"`     // the previous block hash
	Transactions  []Transaction `json:"transactions"` // transactions contained in the block
	Signatures    []Signature   `json:"signatures"`
}

func CreateBlock

func CreateBlock(lastBlock Block, txs []Transaction) Block

func Genesis

func Genesis() Block

Custom Genesis Block

type Blockchain

type Blockchain struct {
	Chain        []Block         `json:"chain"`        // linear chain structure
	Transactions TransactionPool `json:"transactions"` // the transactions
	Nodes        PeerSet         `json:"nodes"`        // the participated nodes
	Committee    MemberSet       `json:"committee"`    // the committee members
}

func NewBlockchain

func NewBlockchain() *Blockchain

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block Block, id int)

Add block to the chain

func (*Blockchain) AddTransaction

func (bc *Blockchain) AddTransaction(tx LocalTransaction)

Receive transaction and put it into the local transaction pool

func (*Blockchain) CommitteeClear

func (bc *Blockchain) CommitteeClear()

Clear the committee group, called when a new block is generated and wait for the next round committee construction

func (*Blockchain) CommitteeUpdate

func (bc *Blockchain) CommitteeUpdate(member Member) bool

Update the committee group, called when the proof of role is valid

func (*Blockchain) GetLength

func (bc *Blockchain) GetLength() int

Get the length of the chain Returns: -{int} length

func (*Blockchain) LastBlock

func (bc *Blockchain) LastBlock() Block

type Global

type Global struct {
	GlobalModel [][]float64 `json:"roundModel"`
	Momentum    [][]float64 `json:"momentum"`
}

type LocalTransaction

type LocalTransaction struct {
	ClientID    int           `json:"clientID"`
	PublicKey   rsa.PublicKey `json:"publicKey"`
	Round       int           `json:"round"`
	ModelUpdate [][]float64   `json:"modelUpdate"`
	Signature   []byte        `json:"signature"`
}

type Member

type Member struct {
	ID       int
	Address  string
	Pk       vrf.PublicKey
	VrfValue []byte
	VrfProof []byte
}

type MemberSet

type MemberSet struct {
	Set     map[int]bool
	Members map[int]Member
}

func NewMemberSet

func NewMemberSet() MemberSet

func (*MemberSet) Add

func (set *MemberSet) Add(id int, member Member) bool

func (*MemberSet) Keys

func (set *MemberSet) Keys() []int

type Peer

type Peer struct {
	Address string
	Pk      vrf.PublicKey
}

type PeerSet

type PeerSet struct {
	Set   map[int]bool
	Peers map[int]Peer
}

func NewPeerSet

func NewPeerSet() PeerSet

func (*PeerSet) Add

func (set *PeerSet) Add(id int, peer Peer) bool

---Input: string - the key(address) of node ---Ouput: bool - true, the node has been successfully added

  • false, the node already existed

func (*PeerSet) Keys

func (set *PeerSet) Keys() []int

type Signature

type Signature struct {
	Sig []byte
	Pk  ecdsa.PublicKey
}

type Transaction

type Transaction struct {
	Task           string             `json:"task"`
	Round          int                `json:"round"`
	GlobalModel    string             `json:"globalModel"`      //the IPFS address of the global model
	GlobalModelSig Signature          `json:"SenderSignauture"` //the signature of uploaded global
	Deltas         []LocalTransaction `json:"modelUpdates"`     //the deltas will be removed when the confirmed block generated
}

Custom transaction

func CreateTx

func CreateTx(priKey *ecdsa.PrivateKey, task string, round int, txs []LocalTransaction, globalParam [][]float64, momentum [][]float64, beta float64, slr float64, rank int, model_size [][]int, unlabel *python3.PyObject, model *python3.PyObject) Transaction

func CreateTx_Malicious

func CreateTx_Malicious(priKey *ecdsa.PrivateKey, task string, round int, txs []LocalTransaction, globalParam [][]float64, momentum [][]float64, beta float64, slr float64, rank int, model_size [][]int, unlabel *python3.PyObject, model *python3.PyObject) Transaction

type TransactionPool

type TransactionPool struct {
	Transactions map[string]LocalTransaction
	// contains filtered or unexported fields
}

func NewTxPool

func NewTxPool() TransactionPool

func (*TransactionPool) Add

func (txp *TransactionPool) Add(tx LocalTransaction) bool

func (*TransactionPool) Keys

func (txp *TransactionPool) Keys() []string

Jump to

Keyboard shortcuts

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