mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package e2e
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/luxfi/consensus/utils/ids"
|
|
)
|
|
|
|
// GoNode implements NodeRunner for Go implementation
|
|
type GoNode struct {
|
|
t *testing.T
|
|
blocks map[ids.ID]*Block
|
|
decisions map[ids.ID]bool
|
|
mu sync.RWMutex
|
|
healthy bool
|
|
}
|
|
|
|
// NewGoNode creates a new Go consensus node
|
|
func NewGoNode(t *testing.T) *GoNode {
|
|
return &GoNode{
|
|
t: t,
|
|
blocks: make(map[ids.ID]*Block),
|
|
decisions: make(map[ids.ID]bool),
|
|
}
|
|
}
|
|
|
|
// Start initializes and starts the Go consensus engine
|
|
func (n *GoNode) Start(ctx context.Context, port int) error {
|
|
n.t.Logf("Starting Go node on port %d", port)
|
|
|
|
// Simulated node for E2E cross-language consensus tests.
|
|
n.healthy = true
|
|
n.t.Log("Go node started successfully (simulated)")
|
|
return nil
|
|
}
|
|
|
|
// Stop shuts down the Go consensus engine
|
|
func (n *GoNode) Stop() error {
|
|
n.t.Log("Stopping Go node")
|
|
n.healthy = false
|
|
return nil
|
|
}
|
|
|
|
// ProposeBlock submits a block to the Go consensus engine
|
|
func (n *GoNode) ProposeBlock(testBlock *Block) error {
|
|
n.mu.Lock()
|
|
defer n.mu.Unlock()
|
|
|
|
n.t.Logf("Go node: proposing block %s (height %d)", testBlock.ID, testBlock.Height)
|
|
|
|
// Store block
|
|
n.blocks[testBlock.ID] = testBlock
|
|
|
|
// Simulated consensus: accept immediately for E2E cross-language test.
|
|
n.decisions[testBlock.ID] = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetDecision returns whether a block was accepted
|
|
func (n *GoNode) GetDecision(blockID ids.ID) (bool, error) {
|
|
n.mu.RLock()
|
|
defer n.mu.RUnlock()
|
|
|
|
decision, exists := n.decisions[blockID]
|
|
if !exists {
|
|
return false, fmt.Errorf("no decision for block %s", blockID)
|
|
}
|
|
|
|
return decision, nil
|
|
}
|
|
|
|
// IsHealthy returns whether the Go node is healthy
|
|
func (n *GoNode) IsHealthy() bool {
|
|
n.mu.RLock()
|
|
defer n.mu.RUnlock()
|
|
return n.healthy
|
|
}
|