Skip to content
Closed

WIP #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions blockchain_golang/life/execution_thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,27 @@ func TryToFinishCurrentEpoch(epochHandler *structures.EpochDataHandler) {

nextEpochIndex := epochIndex + 1

var nextEpochData *structures.EpochDataHandler
var nextEpochData *structures.NextEpochDataHandler

rawHandler, dbErr := globals.EPOCH_DATA.Get([]byte("EPOCH_DATA:"+strconv.Itoa(nextEpochIndex)), nil)

if dbErr == nil {

json.Unmarshal(rawHandler, &nextEpochData)

}

if nextEpochData != nil {

nextEpochDataTemplate := structures.EpochDataHandler{

Id: nextEpochIndex,

Hash: nextEpochData.Hash,
Hash: nextEpochData.NextEpochHash,

Quorum: nextEpochData.Quorum,
Quorum: nextEpochData.NextEpochQuorum,

LeadersSequence: nextEpochData.LeadersSequence,
LeadersSequence: nextEpochData.NextEpochLeadersSequence,
}

// Find the first blocks for epoch X+1
Expand Down Expand Up @@ -405,19 +413,16 @@ func SetupNextEpoch(epochHandler *structures.EpochDataHandler) {

// Prepare epoch handler for next epoch

var templateForNextEpoch *structures.EpochDataHandler

templateForNextEpoch.Id = nextEpochIndex

templateForNextEpoch.Hash = nextEpochData.NextEpochHash

templateForNextEpoch.PoolsRegistry = nextEpochData.NextEpochPoolsRegistry

templateForNextEpoch.StartTimestamp += uint64(globals.EXECUTION_THREAD_METADATA_HANDLER.Handler.NetworkParameters.EpochTime)

templateForNextEpoch.Quorum = nextEpochData.NextEpochQuorum
templateForNextEpoch := &structures.EpochDataHandler{
Id: nextEpochIndex,
Hash: nextEpochData.NextEpochHash,
PoolsRegistry: nextEpochData.NextEpochPoolsRegistry,
StartTimestamp: epochHandler.StartTimestamp + uint64(globals.EXECUTION_THREAD_METADATA_HANDLER.Handler.NetworkParameters.EpochTime),
Quorum: nextEpochData.NextEpochQuorum,
LeadersSequence: nextEpochData.NextEpochLeadersSequence,
}

templateForNextEpoch.LeadersSequence = nextEpochData.NextEpochLeadersSequence
globals.EXECUTION_THREAD_METADATA_HANDLER.Handler.EpochDataHandler = *templateForNextEpoch

// Nullify values for the upcoming epoch

Expand Down
27 changes: 27 additions & 0 deletions blockchain_golang/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,33 @@ func OpenDb(dbName string) *leveldb.DB {
return db
}

func OpenWebsocketConnectionWithPool(poolPubkey string) (*websocket.Conn, error) {

// Retrieve pool metadata from LevelDB
raw, err := globals.APPROVEMENT_THREAD_METADATA.Get([]byte(poolPubkey+"(POOL)_STORAGE_POOL"), nil)
if err != nil {
return nil, fmt.Errorf("failed to get pool metadata: %w", err)
}

var pool structures.PoolStorage
if err := json.Unmarshal(raw, &pool); err != nil {
return nil, fmt.Errorf("failed to unmarshal pool metadata: %w", err)
}

// Check if WebSocket URL is present
if pool.WssPoolUrl == "" {
return nil, fmt.Errorf("pool %s has empty WssPoolUrl", poolPubkey)
}

// Attempt to establish WebSocket connection
conn, _, err := websocket.DefaultDialer.Dial(pool.WssPoolUrl, nil)
if err != nil {
return nil, fmt.Errorf("failed to dial websocket for pool %s: %w", poolPubkey, err)
}

return conn, nil
}

func OpenWebsocketConnectionsWithQuorum(quorum []string, wsConnMap map[string]*websocket.Conn) {

// Close and remove any existing connections
Expand Down