-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
643b672
commit f709d78
Showing
16 changed files
with
812 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package dispute | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ChainSafe/gossamer/dot/parachain/dispute/overseer" | ||
"github.com/ChainSafe/gossamer/dot/parachain/dispute/types" | ||
parachainTypes "github.com/ChainSafe/gossamer/dot/parachain/types" | ||
"time" | ||
) | ||
|
||
const timeout = 10 * time.Second | ||
|
||
func getBlockNumber(overseerChannel chan<- any, receipt parachainTypes.CandidateReceipt) (uint32, error) { | ||
respCh := make(chan any, 1) | ||
relayParent, err := receipt.Hash() | ||
if err != nil { | ||
return 0, fmt.Errorf("get hash: %w", err) | ||
} | ||
|
||
message := overseer.ChainAPIMessage[overseer.BlockNumberRequest]{ | ||
Message: overseer.BlockNumberRequest{Hash: relayParent}, | ||
ResponseChannel: respCh, | ||
} | ||
result, err := call(overseerChannel, message, message.ResponseChannel) | ||
if err != nil { | ||
return 0, fmt.Errorf("send message: %w", err) | ||
} | ||
|
||
blockNumber, ok := result.(uint32) | ||
if !ok { | ||
return 0, fmt.Errorf("unexpected response type: %T", result) | ||
} | ||
return blockNumber, nil | ||
} | ||
|
||
func sendMessage(channel chan<- any, message any) error { | ||
// Send with timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
select { | ||
case channel <- message: | ||
return nil | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
|
||
func call(channel chan<- any, message any, responseChan chan any) (any, error) { | ||
// Send with timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
select { | ||
case channel <- message: | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
} | ||
|
||
select { | ||
case response := <-responseChan: | ||
return response, nil | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
} | ||
} | ||
|
||
func sendResult(overseerChannel chan<- any, request ParticipationRequest, outcome types.ParticipationOutcomeType) { | ||
participationOutcome, err := types.NewCustomParticipationOutcomeVDT(outcome) | ||
if err != nil { | ||
logger.Errorf( | ||
"failed to create participation outcome: %s, error: %s", | ||
outcome, | ||
err, | ||
) | ||
return | ||
} | ||
|
||
statement := ParticipationStatement{ | ||
Session: request.session, | ||
CandidateHash: request.candidateHash, | ||
CandidateReceipt: request.candidateReceipt, | ||
Outcome: participationOutcome, | ||
} | ||
if err := sendMessage(overseerChannel, statement); err != nil { | ||
logger.Errorf( | ||
"failed to send participation statement: %s, error: %s", | ||
statement, | ||
err, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.