Skip to content

Commit

Permalink
Fix dependency nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Sep 6, 2024
1 parent 2f73208 commit ee20071
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 45 deletions.
20 changes: 11 additions & 9 deletions internal/eigenState/avsOperators/avsOperators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"database/sql"
"fmt"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState"
"github.com/Layr-Labs/sidecar/internal/eigenState/base"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/eigenState/types"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/utils"
"github.com/wealdtech/go-merkletree/v2"
Expand Down Expand Up @@ -42,8 +44,8 @@ type AvsOperatorChange struct {

// EigenState model for AVS operators that implements IEigenStateModel
type AvsOperators struct {
eigenState.BaseEigenState
StateTransitions eigenState.StateTransitions[AvsOperatorChange]
base.BaseEigenState
StateTransitions types.StateTransitions[AvsOperatorChange]
Db *gorm.DB
Network config.Network
Environment config.Environment
Expand All @@ -60,15 +62,15 @@ type RegisteredAvsOperatorDiff struct {

// Create new instance of AvsOperators state model
func NewAvsOperators(
esm *eigenState.EigenStateManager,
esm *stateManager.EigenStateManager,
grm *gorm.DB,
Network config.Network,
Environment config.Environment,
logger *zap.Logger,
globalConfig *config.Config,
) (*AvsOperators, error) {
s := &AvsOperators{
BaseEigenState: eigenState.BaseEigenState{
BaseEigenState: base.BaseEigenState{
Logger: logger,
},
Db: grm,
Expand All @@ -92,8 +94,8 @@ func (a *AvsOperators) GetModelName() string {
//
// Returns the map and a reverse sorted list of block numbers that can be traversed when
// processing a log to determine which state change to apply.
func (a *AvsOperators) GetStateTransitions() (eigenState.StateTransitions[AvsOperatorChange], []uint64) {
stateChanges := make(eigenState.StateTransitions[AvsOperatorChange])
func (a *AvsOperators) GetStateTransitions() (types.StateTransitions[AvsOperatorChange], []uint64) {
stateChanges := make(types.StateTransitions[AvsOperatorChange])

// TODO(seanmcgary): make this not a closure so this function doesnt get big an messy...
stateChanges[0] = func(log *storage.TransactionLog) (*AvsOperatorChange, error) {
Expand Down Expand Up @@ -326,7 +328,7 @@ func (a *AvsOperators) getDifferenceInStates(blockNumber uint64) ([]RegisteredAv
// 3. Create a merkle tree for each AVS, with the operator:block_number pairs as leaves
// 4. Create a merkle tree for all AVS trees
// 5. Return the root of the full tree
func (a *AvsOperators) GenerateStateRoot(blockNumber uint64) (eigenState.StateRoot, error) {
func (a *AvsOperators) GenerateStateRoot(blockNumber uint64) (types.StateRoot, error) {
results, err := a.getDifferenceInStates(blockNumber)
if err != nil {
return "", err
Expand All @@ -336,7 +338,7 @@ func (a *AvsOperators) GenerateStateRoot(blockNumber uint64) (eigenState.StateRo
if err != nil {
return "", err
}
return eigenState.StateRoot(utils.ConvertBytesToString(fullTree.Root())), nil
return types.StateRoot(utils.ConvertBytesToString(fullTree.Root())), nil
}

func (a *AvsOperators) merkelizeState(blockNumber uint64, avsOperators []RegisteredAvsOperatorDiff) (*merkletree.MerkleTree, error) {
Expand Down
10 changes: 5 additions & 5 deletions internal/eigenState/avsOperators/avsOperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package avsOperators
import (
"database/sql"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/tests"
Expand Down Expand Up @@ -41,13 +41,13 @@ func Test_AvsOperatorState(t *testing.T) {
}

t.Run("Should create a new AvsOperatorState", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
avsOperatorState, err := NewAvsOperators(esm, grm, cfg.Network, cfg.Environment, l, cfg)
assert.Nil(t, err)
assert.NotNil(t, avsOperatorState)
})
t.Run("Should register AvsOperatorState", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
blockNumber := uint64(200)
log := storage.TransactionLog{
TransactionHash: "some hash",
Expand Down Expand Up @@ -75,7 +75,7 @@ func Test_AvsOperatorState(t *testing.T) {
teardown(avsOperatorState)
})
t.Run("Should register AvsOperatorState and generate the table for the block", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
blockNumber := uint64(200)

log := storage.TransactionLog{
Expand Down Expand Up @@ -123,7 +123,7 @@ func Test_AvsOperatorState(t *testing.T) {
teardown(avsOperatorState)
})
t.Run("Should correctly generate state across multiple blocks", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
blocks := []uint64{
300,
301,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eigenState
package base

import (
"encoding/json"
Expand Down
68 changes: 68 additions & 0 deletions internal/eigenState/eigenstate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package eigenState

import (
"fmt"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState/avsOperators"
"github.com/Layr-Labs/sidecar/internal/eigenState/operatorShares"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
"testing"
)

func setup() (
*config.Config,
*gorm.DB,
*zap.Logger,
error,
) {
cfg := tests.GetConfig()
l, _ := logger.NewLogger(&logger.LoggerConfig{Debug: cfg.Debug})

_, grm, err := tests.GetDatabaseConnection(cfg)

return cfg, grm, l, err
}

func teardown(grm *gorm.DB) {
grm.Exec("truncate table avs_operator_changes cascade")
grm.Exec("truncate table registered_avs_operators cascade")
}

func Test_EigenStateManager(t *testing.T) {
cfg, grm, l, err := setup()

if err != nil {
t.Fatal(err)
}

t.Run("Should create a new EigenStateManager", func(t *testing.T) {
esm := stateManager.NewEigenStateManager(l)
assert.NotNil(t, esm)
})
t.Run("Should create a state root with states from models", func(t *testing.T) {
esm := stateManager.NewEigenStateManager(l)
avsOperatorsModel, err := avsOperators.NewAvsOperators(esm, grm, cfg.Network, cfg.Environment, l, cfg)
assert.Nil(t, err)
assert.NotNil(t, avsOperatorsModel)

operatorSharesModel, err := operatorShares.NewOperatorSharesModel(esm, grm, cfg.Network, cfg.Environment, l, cfg)
assert.Nil(t, err)
assert.NotNil(t, operatorSharesModel)

indexes := esm.GetSortedModelIndexes()
assert.Equal(t, 2, len(indexes))
assert.Equal(t, 0, indexes[0])
assert.Equal(t, 1, indexes[1])

root, err := esm.GenerateStateRoot(200)
assert.Nil(t, err)
assert.True(t, len(root) > 0)
fmt.Printf("Root: %+v\n", root)
})
teardown(grm)
}
32 changes: 18 additions & 14 deletions internal/eigenState/operatorShares/operatorShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState"
"github.com/Layr-Labs/sidecar/internal/eigenState/base"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/eigenState/types"
"github.com/Layr-Labs/sidecar/internal/parser"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/utils"
Expand Down Expand Up @@ -46,8 +48,8 @@ type OperatorShares struct {

// Implements IEigenStateModel
type OperatorSharesModel struct {
eigenState.BaseEigenState
StateTransitions eigenState.StateTransitions[OperatorShareChange]
base.BaseEigenState
StateTransitions types.StateTransitions[OperatorShareChange]
Db *gorm.DB
Network config.Network
Environment config.Environment
Expand All @@ -56,20 +58,22 @@ type OperatorSharesModel struct {
}

func NewOperatorSharesModel(
esm *eigenState.EigenStateManager,
esm *stateManager.EigenStateManager,
grm *gorm.DB,
Network config.Network,
Environment config.Environment,
logger *zap.Logger,
globalConfig *config.Config,
) (*OperatorSharesModel, error) {
model := &OperatorSharesModel{
BaseEigenState: eigenState.BaseEigenState{},
Db: grm,
Network: Network,
Environment: Environment,
logger: logger,
globalConfig: globalConfig,
BaseEigenState: base.BaseEigenState{
Logger: logger,
},
Db: grm,
Network: Network,
Environment: Environment,
logger: logger,
globalConfig: globalConfig,
}

esm.RegisterState(model, 1)
Expand All @@ -80,8 +84,8 @@ func (osm *OperatorSharesModel) GetModelName() string {
return "OperatorSharesModel"
}

func (osm *OperatorSharesModel) GetStateTransitions() (eigenState.StateTransitions[OperatorShareChange], []uint64) {
stateChanges := make(eigenState.StateTransitions[OperatorShareChange])
func (osm *OperatorSharesModel) GetStateTransitions() (types.StateTransitions[OperatorShareChange], []uint64) {
stateChanges := make(types.StateTransitions[OperatorShareChange])

stateChanges[0] = func(log *storage.TransactionLog) (*OperatorShareChange, error) {
arguments := make([]parser.Argument, 0)
Expand Down Expand Up @@ -283,7 +287,7 @@ func (osm *OperatorSharesModel) getDifferencesInStates(currentBlock uint64) ([]O
return diffs, nil
}

func (osm *OperatorSharesModel) GenerateStateRoot(blockNumber uint64) (eigenState.StateRoot, error) {
func (osm *OperatorSharesModel) GenerateStateRoot(blockNumber uint64) (types.StateRoot, error) {
diffs, err := osm.getDifferencesInStates(blockNumber)
if err != nil {
return "", err
Expand All @@ -293,7 +297,7 @@ func (osm *OperatorSharesModel) GenerateStateRoot(blockNumber uint64) (eigenStat
if err != nil {
return "", err
}
return eigenState.StateRoot(utils.ConvertBytesToString(fullTree.Root())), nil
return types.StateRoot(utils.ConvertBytesToString(fullTree.Root())), nil
}

func (osm *OperatorSharesModel) merkelizeState(blockNumber uint64, diffs []OperatorShares) (*merkletree.MerkleTree, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/eigenState/operatorShares/operatorShares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"
"fmt"
"github.com/Layr-Labs/sidecar/internal/config"
"github.com/Layr-Labs/sidecar/internal/eigenState"
"github.com/Layr-Labs/sidecar/internal/eigenState/stateManager"
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/tests"
Expand Down Expand Up @@ -43,13 +43,13 @@ func Test_OperatorSharesState(t *testing.T) {
}

t.Run("Should create a new OperatorSharesState", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
model, err := NewOperatorSharesModel(esm, grm, cfg.Network, cfg.Environment, l, cfg)
assert.Nil(t, err)
assert.NotNil(t, model)
})
t.Run("Should register OperatorSharesState", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
blockNumber := uint64(200)
log := storage.TransactionLog{
TransactionHash: "some hash",
Expand All @@ -75,7 +75,7 @@ func Test_OperatorSharesState(t *testing.T) {
teardown(model)
})
t.Run("Should register AvsOperatorState and generate the table for the block", func(t *testing.T) {
esm := eigenState.NewEigenStateManager(l)
esm := stateManager.NewEigenStateManager(l)
blockNumber := uint64(200)
log := storage.TransactionLog{
TransactionHash: "some hash",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package eigenState
package stateManager

import (
"fmt"
"github.com/Layr-Labs/sidecar/internal/eigenState/types"
"github.com/Layr-Labs/sidecar/internal/storage"
"github.com/Layr-Labs/sidecar/internal/utils"
"github.com/wealdtech/go-merkletree/v2"
Expand All @@ -11,19 +12,19 @@ import (
)

type EigenStateManager struct {
StateModels map[int]IEigenStateModel
StateModels map[int]types.IEigenStateModel
logger *zap.Logger
}

func NewEigenStateManager(logger *zap.Logger) *EigenStateManager {
return &EigenStateManager{
StateModels: make(map[int]IEigenStateModel),
StateModels: make(map[int]types.IEigenStateModel),
logger: logger,
}
}

// Allows a model to register itself with the state manager
func (e *EigenStateManager) RegisterState(model IEigenStateModel, index int) {
func (e *EigenStateManager) RegisterState(model types.IEigenStateModel, index int) {
if m, ok := e.StateModels[index]; ok {
e.logger.Sugar().Fatalf("Registering model model at index %d which already exists and belongs to %s", index, m.GetModelName())
}
Expand All @@ -32,7 +33,7 @@ func (e *EigenStateManager) RegisterState(model IEigenStateModel, index int) {

// Given a log, allow each state model to determine if/how to process it
func (e *EigenStateManager) HandleLogStateChange(log *storage.TransactionLog) error {
for _, index := range e.getSortedModelIndexes() {
for _, index := range e.GetSortedModelIndexes() {
state := e.StateModels[index]
if state.IsInterestingLog(log) {
_, err := state.HandleStateChange(log)
Expand All @@ -46,7 +47,7 @@ func (e *EigenStateManager) HandleLogStateChange(log *storage.TransactionLog) er

// With all transactions/logs processed for a block, commit the final state to the table
func (e *EigenStateManager) CommitFinalState(blockNumber uint64) error {
for _, index := range e.getSortedModelIndexes() {
for _, index := range e.GetSortedModelIndexes() {
state := e.StateModels[index]
err := state.WriteFinalState(blockNumber)
if err != nil {
Expand All @@ -56,8 +57,8 @@ func (e *EigenStateManager) CommitFinalState(blockNumber uint64) error {
return nil
}

func (e *EigenStateManager) GenerateStateRoot(blockNumber uint64) (StateRoot, error) {
sortedIndexes := e.getSortedModelIndexes()
func (e *EigenStateManager) GenerateStateRoot(blockNumber uint64) (types.StateRoot, error) {
sortedIndexes := e.GetSortedModelIndexes()
roots := [][]byte{
[]byte(fmt.Sprintf("%d", blockNumber)),
}
Expand All @@ -79,18 +80,18 @@ func (e *EigenStateManager) GenerateStateRoot(blockNumber uint64) (StateRoot, er
return "", err
}

return StateRoot(utils.ConvertBytesToString(tree.Root())), nil
return types.StateRoot(utils.ConvertBytesToString(tree.Root())), nil
}

func (e *EigenStateManager) encodeModelLeaf(model IEigenStateModel, blockNumber uint64) ([]byte, error) {
func (e *EigenStateManager) encodeModelLeaf(model types.IEigenStateModel, blockNumber uint64) ([]byte, error) {
root, err := model.GenerateStateRoot(blockNumber)
if err != nil {
return nil, err
}
return append([]byte(model.GetModelName()), []byte(root)[:]...), nil
}

func (e *EigenStateManager) getSortedModelIndexes() []int {
func (e *EigenStateManager) GetSortedModelIndexes() []int {
indexes := make([]int, 0, len(e.StateModels))
for i := range e.StateModels {
indexes = append(indexes, i)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eigenState
package types

import (
"github.com/Layr-Labs/sidecar/internal/storage"
Expand Down

0 comments on commit ee20071

Please sign in to comment.