-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add beacon state and decorated event publisher
- Loading branch information
Showing
17 changed files
with
676 additions
and
130 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
package state | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/attestantio/go-eth2-client/spec" | ||
) | ||
|
||
type TimedBlock struct { | ||
Block *spec.VersionedSignedBeaconBlock | ||
SeenAt time.Time | ||
} |
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,51 @@ | ||
package state | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
v1 "github.com/attestantio/go-eth2-client/api/v1" | ||
"github.com/attestantio/go-eth2-client/spec" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
) | ||
|
||
type Epoch struct { | ||
ProposerDuties ProposerDuties | ||
Blocks MapOfSlotToBlock | ||
} | ||
|
||
func NewEpoch(slotsPerEpoch phase0.Slot) Epoch { | ||
e := Epoch{ | ||
ProposerDuties: make(ProposerDuties), | ||
Blocks: NewMapOfSlotToBlock(), | ||
} | ||
|
||
e.Blocks.InitializeSlots(slotsPerEpoch) | ||
|
||
return e | ||
} | ||
|
||
func (e *Epoch) SetProposerDuties(duties []*v1.ProposerDuty) { | ||
for _, duty := range duties { | ||
e.ProposerDuties[duty.Slot] = duty | ||
} | ||
} | ||
|
||
func (e *Epoch) AddBlock(block *spec.VersionedSignedBeaconBlock) error { | ||
if block == nil { | ||
return errors.New("block is nil") | ||
} | ||
|
||
return e.Blocks.AddBlock(&TimedBlock{ | ||
Block: block, | ||
SeenAt: time.Now(), | ||
}) | ||
} | ||
|
||
func (e *Epoch) GetProserDutyAtSlot(slot phase0.Slot) (*v1.ProposerDuty, error) { | ||
if e.ProposerDuties[slot] == nil { | ||
return nil, errors.New("no proposer duty at slot") | ||
} | ||
|
||
return e.ProposerDuties[slot], nil | ||
} |
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,75 @@ | ||
package state | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
) | ||
|
||
type Epochs struct { | ||
spec *Spec | ||
state map[phase0.Epoch]*Epoch | ||
|
||
mu *sync.Mutex | ||
} | ||
|
||
func NewEpochs(spec *Spec) Epochs { | ||
return Epochs{ | ||
spec: spec, | ||
state: make(map[phase0.Epoch]*Epoch), | ||
|
||
mu: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (e *Epochs) GetEpoch(epoch phase0.Epoch) (*Epoch, error) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
if e.state[epoch] == nil { | ||
return nil, errors.New("epoch not found") | ||
} | ||
|
||
return e.state[epoch], nil | ||
} | ||
|
||
func (e *Epochs) Exists(number phase0.Epoch) bool { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
_, ok := e.state[number] | ||
|
||
return ok | ||
} | ||
|
||
func (e *Epochs) NewInitializedEpoch(number phase0.Epoch) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
epoch := NewEpoch(e.spec.SlotsPerEpoch) | ||
|
||
return e.AddEpoch(number, &epoch) | ||
} | ||
|
||
func (e *Epochs) AddEpoch(number phase0.Epoch, epoch *Epoch) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
if epoch == nil { | ||
return errors.New("epoch is nil") | ||
} | ||
|
||
e.state[number] = epoch | ||
|
||
return nil | ||
} | ||
|
||
func (e *Epochs) RemoveEpoch(number phase0.Epoch) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
delete(e.state, number) | ||
|
||
return nil | ||
} |
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,20 @@ | ||
package state | ||
|
||
import ( | ||
v1 "github.com/attestantio/go-eth2-client/api/v1" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
) | ||
|
||
type ProposerDuties = map[phase0.Slot]*v1.ProposerDuty | ||
|
||
func NewProposerDuties(duties []*v1.ProposerDuty) ProposerDuties { | ||
out := make(ProposerDuties) | ||
|
||
for _, duty := range duties { | ||
out[duty.Slot] = duty | ||
} | ||
|
||
return out | ||
} | ||
|
||
type ProposerDutiesForEpoch = map[phase0.Epoch]ProposerDuties |
Oops, something went wrong.