static.go 1.91 KB
Newer Older
1 2 3 4
package depset

import (
	"context"
5 6 7
	"sort"

	"golang.org/x/exp/maps"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
)

type StaticConfigDependency struct {
	// ActivationTime is when the chain becomes part of the dependency set.
	// This is the minimum timestamp of the inclusion of an executing message.
	ActivationTime uint64 `json:"activationTime"`

	// HistoryMinTime is what the lower bound of data is to store.
	// This is the minimum timestamp of an initiating message to be accessible to others.
	// This is set to 0 when all data since genesis is executable.
	HistoryMinTime uint64 `json:"historyMinTime"`
}

// StaticConfigDependencySet statically declares a DependencySet.
// It can be used as a DependencySetSource itself, by simply returning the itself when loading the set.
type StaticConfigDependencySet struct {
	Dependencies map[types.ChainID]*StaticConfigDependency `json:"dependencies"`
}

var _ DependencySetSource = (*StaticConfigDependencySet)(nil)

var _ DependencySet = (*StaticConfigDependencySet)(nil)

func (ds *StaticConfigDependencySet) LoadDependencySet(ctx context.Context) (DependencySet, error) {
	return ds, nil
}

func (ds *StaticConfigDependencySet) CanExecuteAt(chainID types.ChainID, execTimestamp uint64) (bool, error) {
	dep, ok := ds.Dependencies[chainID]
	if !ok {
		return false, nil
	}
	return execTimestamp >= dep.ActivationTime, nil
}

func (ds *StaticConfigDependencySet) CanInitiateAt(chainID types.ChainID, initTimestamp uint64) (bool, error) {
	dep, ok := ds.Dependencies[chainID]
	if !ok {
		return false, nil
	}
	return initTimestamp >= dep.HistoryMinTime, nil
}
52 53 54 55 56 57 58 59 60 61 62 63 64

func (ds *StaticConfigDependencySet) Chains() []types.ChainID {
	out := maps.Keys(ds.Dependencies)
	sort.Slice(out, func(i, j int) bool {
		return out[i].Cmp(out[j]) < 0
	})
	return out
}

func (ds *StaticConfigDependencySet) HasChain(chainID types.ChainID) bool {
	_, ok := ds.Dependencies[chainID]
	return ok
}