RPC/ws: add SlotsUpdatesSubscribe

This commit is contained in:
Slavomir 2021-07-20 16:13:07 +02:00
parent 2c8e89bf11
commit 947ac81bd3
2 changed files with 72 additions and 5 deletions

View File

@ -350,8 +350,8 @@ func (sw *RootSubscription) Unsubscribe() {
sw.sub.Unsubscribe()
}
// VoteSubscribe (UNSTABLE) subscribes to receive notification anytime
// a new vote is observed in gossip.
// VoteSubscribe (UNSTABLE, disabled by default) subscribes
// to receive notification anytime a new vote is observed in gossip.
// These votes are pre-consensus therefore there is
// no guarantee these votes will enter the ledger.
//
@ -394,3 +394,45 @@ func (sw *VoteSubscription) Recv() (*VoteResult, error) {
func (sw *VoteSubscription) Unsubscribe() {
sw.sub.Unsubscribe()
}
// SlotsUpdatesSubscribe (UNSTABLE) subscribes to receive a notification
// from the validator on a variety of updates on every slot.
//
// This subscription is unstable; the format of this subscription
// may change in the future and it may not always be supported.
func (cl *Client) SlotsUpdatesSubscribe() (*SlotsUpdatesSubscription, error) {
genSub, err := cl.subscribe(
nil,
nil,
"slotsUpdatesSubscribe",
"slotsUpdatesUnsubscribe",
func(msg []byte) (interface{}, error) {
var res SlotsUpdatesResult
err := decodeResponseFromMessage(msg, &res)
return &res, err
},
)
if err != nil {
return nil, err
}
return &SlotsUpdatesSubscription{
sub: genSub,
}, nil
}
type SlotsUpdatesSubscription struct {
sub *Subscription
}
func (sw *SlotsUpdatesSubscription) Recv() (*SlotsUpdatesResult, error) {
select {
case d := <-sw.sub.stream:
return d.(*SlotsUpdatesResult), nil
case err := <-sw.sub.err:
return nil, err
}
}
func (sw *SlotsUpdatesSubscription) Unsubscribe() {
sw.sub.Unsubscribe()
}

View File

@ -100,8 +100,33 @@ type SlotResult struct {
type RootResult bin.Uint64
type VoteResult struct {
Hash solana.Hash `json:"hash"`
// The vote hash.
Hash solana.Hash `json:"hash"`
// The slots covered by the vote.
Slots []bin.Uint64 `json:"slots"`
// TODO:
// Timestamp interface{} `json:"timestamp"`
// The timestamp of the vote.
Timestamp *rpc.UnixTimeSeconds `json:"timestamp,omitempty"`
}
type SlotsUpdatesResult struct {
// The parent slot.
Parent uint64 `json:"parent"`
// The newly updated slot.
Slot uint64 `json:"slot"`
// The Unix timestamp of the update.
Timestamp *rpc.UnixTimeSeconds `json:"timestamp"`
// The update type.
Type SlotsUpdatesType `json:"type"`
}
type SlotsUpdatesType string
const (
SlotsUpdatesFirstShredReceived SlotsUpdatesType = "firstShredReceived"
SlotsUpdatesCompleted SlotsUpdatesType = "completed"
SlotsUpdatesCreatedBank SlotsUpdatesType = "createdBank"
SlotsUpdatesFrozen SlotsUpdatesType = "frozen"
SlotsUpdatesDead SlotsUpdatesType = "dead"
SlotsUpdatesOptimisticConfirmation SlotsUpdatesType = "optimisticConfirmation"
SlotsUpdatesRoot SlotsUpdatesType = "root"
)