Skip to content
Open
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
54ea182
docs(giga-storage): propose staking, distribution and slashing storage
shemnon Nov 2, 2026
701b205
docs(giga-storage): keep only the staking structures an on-chain path…
codchen Nov 3, 2026
dcce426
docs(giga-storage): say where each kind of struct can grow
codchen Nov 5, 2026
269733b
docs(giga-storage): drop Validator's reserved slots
codchen Nov 5, 2026
ea164b7
docs(giga-storage): let an array element grow into a gap of its own
codchen Nov 5, 2026
2c812ee
docs(giga-storage): cover deletes, top-level values, scalar mapping v…
codchen Nov 5, 2026
6994fac
docs(giga-storage): give ValidatorSlashEvent a __gap
codchen Nov 5, 2026
717798f
docs(giga-storage): drop signingInfoSet
codchen Nov 9, 2026
81c182a
feat(params): add the staking subspace
codchen Nov 3, 2026
f30ab46
feat(staking): read and write the staking precompile's storage
codchen Nov 4, 2026
edf0390
refactor(params): take MaxBondedValidators from autobahn's committee cap
codchen Nov 9, 2026
8bafe18
feat(staking): store a validator in 15 slots
codchen Nov 5, 2026
ebe97e0
feat(staking): delegate, redelegate and undelegate in a precompile
codchen Nov 4, 2026
310f32a
feat(staking): never jail a validator
codchen Nov 9, 2026
2117c93
test(staking): check delegations and EndBlock against x/staking
codchen Nov 4, 2026
60ada4b
docs(staking): state the unbonding queue's real EndBlock throughput
codchen Nov 9, 2026
4c53cb4
test(staking): undo x/staking's jailing in the parity tests
codchen Nov 9, 2026
7982369
fix(staking): take calls only once an EndBlock has bonded validators
codchen Nov 9, 2026
ade0e82
test(staking): scope the parity coverage check and the invalid-reques…
codchen Nov 9, 2026
690a174
feat(evmonly): serve staking's bonded set as the validator set
codchen Nov 5, 2026
bf37f04
refactor(evmonlyapp): stop handling an ed25519 key error that cannot …
codchen Nov 5, 2026
1324fee
fix(evmonlyapp): serve no validators after a failed block commit
codchen Nov 9, 2026
4c7f65b
test(params): build the storage harness with solc 0.8.30
codchen Nov 12, 2026
d2cdc61
Merge branch 'claude/giga-staking-params-subspace' into claude/giga-s…
codchen Nov 12, 2026
9626d69
test(staking): build the storage harness with solc 0.8.30
codchen Nov 12, 2026
67974b9
Merge branch 'claude/giga-staking-storage-layer' into claude/giga-sta…
codchen Nov 12, 2026
288e5c1
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 12, 2026
1ef21c9
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 12, 2026
2665668
refactor(evmonly): move 1e18 decimal math to a shared dec package
codchen Nov 12, 2026
01fac38
Merge branch 'claude/giga-staking-storage-layer' into claude/giga-sta…
codchen Nov 12, 2026
c874455
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 12, 2026
30fa8b4
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 12, 2026
035bb86
fix(staking): refuse genesis validators worth no consensus power
codchen Nov 12, 2026
a0f9511
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 12, 2026
4e32d71
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 12, 2026
d830d0f
docs(giga-storage): propose staking, distribution and slashing storage
shemnon Nov 2, 2026
ca72590
docs(giga-storage): keep only the staking structures an on-chain path…
codchen Nov 3, 2026
a9bf44a
docs(giga-storage): say where each kind of struct can grow
codchen Nov 5, 2026
23de979
docs(giga-storage): drop Validator's reserved slots
codchen Nov 5, 2026
a576e3e
docs(giga-storage): let an array element grow into a gap of its own
codchen Nov 5, 2026
eb2760f
docs(giga-storage): cover deletes, top-level values, scalar mapping v…
codchen Nov 5, 2026
c2f60cd
docs(giga-storage): give ValidatorSlashEvent a __gap
codchen Nov 5, 2026
01eba02
docs(giga-storage): drop signingInfoSet
codchen Nov 9, 2026
66c7450
Merge branch 'claude/giga-staking-docs-simplify' into claude/giga-sta…
codchen Nov 16, 2026
16e8636
Merge branch 'claude/giga-staking-params-subspace' into claude/giga-s…
codchen Nov 16, 2026
a1f5106
Merge branch 'claude/giga-staking-storage-layer' into claude/giga-sta…
codchen Nov 16, 2026
8d4fb35
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 16, 2026
df4c62b
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 16, 2026
0ca730f
docs(giga-storage): keep delegations by validator
codchen Nov 16, 2026
3bf68ef
Merge branch 'claude/giga-staking-docs-simplify' into claude/giga-sta…
codchen Nov 16, 2026
12c5ad7
Merge branch 'claude/giga-staking-params-subspace' into claude/giga-s…
codchen Nov 16, 2026
c77cd41
feat(staking): keep each validator's delegators
codchen Nov 16, 2026
4358d77
Merge branch 'claude/giga-staking-storage-layer' into claude/giga-sta…
codchen Nov 16, 2026
2d7fcc3
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 16, 2026
22f564f
test(staking): compare each validator's delegators with V2
codchen Nov 16, 2026
a78b084
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 16, 2026
3eecf14
fix(evmonlyapp): serve validators without waiting for the commit in f…
codchen Nov 16, 2026
2c46720
feat(autobahn): tell the app which block closes an epoch
codchen Nov 16, 2026
6285f35
feat(staking): snapshot the bonded set when an epoch closes
codchen Nov 16, 2026
5240b62
Merge remote-tracking branch 'origin/main' into claude/giga-staking-s…
codchen Nov 16, 2026
9876bd8
Merge branch 'claude/giga-staking-storage-layer' into claude/giga-sta…
codchen Nov 16, 2026
2822d53
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 16, 2026
f43c1f4
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 16, 2026
ce583aa
docs(staking): say a redelegation that empties an unbonded source waits
codchen Nov 16, 2026
1617a68
Merge branch 'claude/giga-staking-precompile' into claude/giga-stakin…
codchen Nov 16, 2026
3790e4b
test(staking): compare redelegating an unbonded validator's last shar…
codchen Nov 16, 2026
4715836
Merge branch 'claude/giga-staking-parity-tests' into claude/giga-evmo…
codchen Nov 16, 2026
8a6aa88
fix(staking): keep each member's consensus key in the epoch snapshot
codchen Nov 16, 2026
6dc432b
Merge remote-tracking branch 'origin/main' into claude/giga-evmonly-g…
codchen Nov 16, 2026
24acfa7
feat(evmonly): serve staking's current bonded set as the validator set
codchen Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/giga-storage/StakingStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ contract StakingStorage {
uint256 sharesDst;
}

/// @dev completionTime and unbondingHeight are the operator's validator's
/// unbondingTime and unbondingHeight when it began unbonding.
struct ValidatorQueueEntry {
uint64 completionTime;
uint64 completionHeight;
uint64 unbondingHeight;
address operator;
}

Expand All @@ -95,6 +97,11 @@ contract StakingStorage {

ValidatorPower[] internal lastValidators; // 0x11: the bonded set, as last sent to consensus
uint256 internal lastTotalPower; // 0x12
/// @dev lastValidators at the end of each epoch's last block. Consensus
/// derives an epoch's committee from the set two epochs before it, so the
/// latest three are kept.
mapping(uint64 => ValidatorPower[]) internal epochValidators; // epoch =>
uint64 internal nextSnapshotEpoch; // the epoch after the latest in epochValidators, 0 before any

mapping(address => Validator) internal validators; // 0x21: operator =>
AddressSet internal validatorSet; // every operator in validators
Expand Down
23 changes: 22 additions & 1 deletion docs/giga-storage/staking-design-rationale.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Staking:
|---|---|---|
| `0x11` | last validator power | `lastValidators` (array, one slot per bonded validator) |
| `0x12` | last total power | `lastTotalPower` |
| none | the bonded set at each epoch end | `epochValidators` (see *The epoch snapshot*) |
| `0x21` | validators | `validators` (+ set) |
| `0x22` | validator by consensus address | `validatorsByConsensusPubkey`, keyed by the key itself |
| `0x23` | validators by power | `validatorsByPower` (sorted array) |
Expand Down Expand Up @@ -289,6 +290,25 @@ that cost is small.
every node, follow tx reverts and snapshots, and be rebuilt after state sync.
- **Ordered flatkv keys**, V2's approach exactly. See *Prefix walks*.

### The epoch snapshot

V2 hands CometBFT a new validator set every block. Autobahn changes its
committee once per epoch, and derives epoch E+2's committee from the set it
reads at the end of epoch E's last block. The node reads the set outside block
execution, at whatever height the app has reached by then, so the set it gets
could differ from node to node.

So at the end of an epoch's last block, EndBlock copies `lastValidators` into
`epochValidators[epoch]`, and the app serves consensus the latest snapshot. The
snapshot is committed state, so every node serves the same one for an epoch,
and a node that state-syncs gets it too. Consensus marks the block that closes
an epoch, so the app never repeats the epoch length.

The snapshot is also what rewards read: the committee that served a block in
epoch E is the snapshot of epoch E−2. Three are kept, E−2 to E, and EndBlock
deletes the one before them when it takes a new one: about 100 slot writes once
an epoch.

### Where the structures don't carry over

Each of these is a place where slot storage, or a deliberate simplification,
Expand Down Expand Up @@ -501,7 +521,8 @@ of a million state writes per epoch.
- The fee pot is for the rewards design to define. Giga's coinbase and base fee
are zero today.
- Minting belongs to the rewards design. pacific-1's release schedule is empty.
- Power and the bonded set are read from staking, not copied.
- Power and the bonded set are read from staking, not copied: the committee
that served a block from staking's epoch snapshot.
- `communityTax` and the proposer reward params stay in distribution's params
(all zero on pacific-1).

Expand Down
7 changes: 7 additions & 0 deletions giga/evmonly/custom_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/holiman/uint256"

"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
)

var (
Expand Down Expand Up @@ -167,6 +168,12 @@ var (
_ precompiles.LogSink = (*precompileState)(nil)
)

// ReadOnlyState returns view as a custom precompile's State under a static
// call: a write is dropped and fails the state, and nothing is charged.
func ReadOnlyState(view gigatypes.EVMStateView) precompiles.State {
return &precompileState{db: newNativeStateDB(gigaSnapshotStateReader{snapshot: view}), readOnly: true}
}

func (s *precompileState) GetBalance(addr common.Address) *big.Int {
return s.db.GetBalance(addr).ToBig()
}
Expand Down
16 changes: 16 additions & 0 deletions giga/evmonly/custom_precompile_execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,19 @@ func TestPrecompileStateMetersLogs(t *testing.T) {
require.ErrorIs(t, s.err, vm.ErrOutOfGas)
require.Empty(t, db.logs, "the log is dropped")
}

func TestReadOnlyStateReadsTheViewAndFailsOnAWrite(t *testing.T) {
slot, value := testHash(0x01), testHash(0x02)
source := NewMemoryState()
source.SetState(registeredPrecompileAddress, slot, value)
view := NewMemoryStore(source).OpenView()
defer view.Close()

state := ReadOnlyState(view)
require.Equal(t, value, state.GetState(registeredPrecompileAddress, slot))
require.False(t, state.Failed())

state.SetState(registeredPrecompileAddress, slot, common.Hash{})
require.True(t, state.Failed())
require.Equal(t, value, view.GetStorage(registeredPrecompileAddress, slot), "the view is unchanged")
}
15 changes: 8 additions & 7 deletions giga/evmonly/end_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ func truncateUTF8(s string, n int) string {

func endBlockContext(block BlockContext, chainID *big.Int) precompiles.BlockContext {
return precompiles.BlockContext{
Number: block.Number,
UnixNano: unixNano(block.Time),
ChainID: cloneOptionalBig(chainID),
BaseFee: cloneOptionalBig(block.BaseFee),
BlobBaseFee: cloneOptionalBig(block.BlobBaseFee),
Coinbase: block.Coinbase,
PrevRandao: block.PrevRandao,
Number: block.Number,
UnixNano: unixNano(block.Time),
ChainID: cloneOptionalBig(chainID),
BaseFee: cloneOptionalBig(block.BaseFee),
BlobBaseFee: cloneOptionalBig(block.BlobBaseFee),
Coinbase: block.Coinbase,
PrevRandao: block.PrevRandao,
ClosingEpoch: block.ClosingEpoch,
}
}

Expand Down
47 changes: 47 additions & 0 deletions giga/evmonly/giga_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common"

"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
gigametrics "github.com/sei-protocol/sei-chain/giga/metrics"
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
)
Expand Down Expand Up @@ -168,6 +169,52 @@ func (e *Executor) AwaitCommits() error {
return e.awaitPipelineCommit()
}

// ReadLatest calls read with read-only state holding every block this executor has run, without
// waiting for the commit in flight. It fails once a commit is known to have failed.
func (e *Executor) ReadLatest(read func(precompiles.State) error) error {
if e.stateStore == nil {
return errMissingStateStore
}
// Held so that no block starts its commit between taking the overlay and opening the view,
// which would pair the view with an overlay older than it.
e.storeMu.Lock()
defer e.storeMu.Unlock()
// Taken before the view, never after, as in executePreparedBlockWithStore.
pending := e.pipelinePending()
snapshot := e.stateStore.OpenView()
if snapshot == nil {
return errors.New("giga store returned a nil snapshot")
}
defer snapshot.Close()
source := pending.overlay(gigaSnapshotStateReader{snapshot: snapshot, missingState: e.missingState})
if err := read(&precompileState{db: newNativeStateDB(source), readOnly: true}); err != nil {
return err
}
// Checked after the read: a failed commit retired before the overlay was taken leaves the view
// short of that block and nothing to supply it.
return e.commitFailure()
}

// commitFailure reports a commit known to have failed, without waiting for the one in flight.
func (e *Executor) commitFailure() error {
e.pipelineMu.Lock()
defer e.pipelineMu.Unlock()
if e.pipelineFailure != nil {
return e.pipelineFailure
}
if e.pipelineDone == nil {
return nil
}
select {
case <-e.pipelineDone:
if e.pipelineErr != nil {
return fmt.Errorf("commit state changes: %w", e.pipelineErr)
}
default:
}
return nil
}

// pipelinePending returns the indexed changes of a block whose commit has not been waited on yet, or
// nil when the store is caught up.
func (e *Executor) pipelinePending() *pendingIndex {
Expand Down
70 changes: 70 additions & 0 deletions giga/evmonly/pipeline_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package evmonly
import (
"errors"
"math/big"
"sync"
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
)

Expand Down Expand Up @@ -307,3 +309,71 @@ func executePipelinedBlock(t *testing.T, executor *Executor, chainID *big.Int, n
require.NoError(t, err)
return result
}

// heldCommitStore holds every commit until release is closed.
type heldCommitStore struct {
*recordingGigaStore
release chan struct{}
}

func (s *heldCommitStore) CommitStateChanges(blockNum int64, changes *gigatypes.StateChangeSet) error {
<-s.release
return s.recordingGigaStore.CommitStateChanges(blockNum, changes)
}

// ReadLatest serves the state a block produced while that block's commit is still running.
func TestReadLatestServesTheBlockStillCommitting(t *testing.T) {
chainID := big.NewInt(testChainID)
key, err := crypto.GenerateKey()
require.NoError(t, err)
sender := crypto.PubkeyToAddress(key.PublicKey)
recipient := testAddress(0xa9)

snapshot := newMemoryGigaSnapshot(40)
snapshot.setBalance(sender, big.NewInt(testFundedBalanceWei))
store := &heldCommitStore{recordingGigaStore: &recordingGigaStore{snapshot: snapshot}, release: make(chan struct{})}
executor := NewExecutor(Config{}, withTestStores(store, NewMemoryReceiptStore()))
defer executor.Close()
var released sync.Once
release := func() { released.Do(func() { close(store.release) }) }
defer release()

executePipelinedBlock(t, executor, chainID, 41,
signLegacyTx(t, key, chainID, 0, &recipient, big.NewInt(7), nil))

var balance *big.Int
require.NoError(t, executor.ReadLatest(func(state precompiles.State) error {
balance = state.GetBalance(recipient)
return nil
}))
require.Equal(t, big.NewInt(7), balance, "block 41's payment is served while its commit is held")

release()
require.NoError(t, executor.AwaitCommits())
require.Equal(t, []int64{41}, store.commitBlock)
}

// ReadLatest refuses once a commit has failed, whether or not anything has waited on it, since the
// store then predates that block.
func TestReadLatestFailsOnceACommitHasFailed(t *testing.T) {
chainID := big.NewInt(testChainID)
key, err := crypto.GenerateKey()
require.NoError(t, err)
sender := crypto.PubkeyToAddress(key.PublicKey)
recipient := testAddress(0xa9)

snapshot := newMemoryGigaSnapshot(40)
snapshot.setBalance(sender, big.NewInt(testFundedBalanceWei))
store := &recordingGigaStore{snapshot: snapshot, commitErr: errTestCommitFailed}
executor := NewExecutor(Config{}, withTestStores(store, NewMemoryReceiptStore()))
defer executor.Close()

executePipelinedBlock(t, executor, chainID, 41,
signLegacyTx(t, key, chainID, 0, &recipient, big.NewInt(7), nil))

read := func(precompiles.State) error { return nil }
require.Eventually(t, func() bool { return errors.Is(executor.ReadLatest(read), errTestCommitFailed) },
5*time.Second, time.Millisecond, "a failed commit is reported before anything waits on it")
require.ErrorIs(t, executor.AwaitCommits(), errTestCommitFailed)
require.ErrorIs(t, executor.ReadLatest(read), errTestCommitFailed, "and still once it has been waited on")
}
5 changes: 5 additions & 0 deletions giga/evmonly/precompiles/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"

"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
)

var ErrCustomPrecompilesOpen = errors.New("evm-only custom precompiles are not implemented")
Expand Down Expand Up @@ -82,6 +84,9 @@ type BlockContext struct {
// Replay is set when the block runs only to rebuild its state, as for a
// trace, rather than to execute it.
Replay bool
// ClosingEpoch is the consensus epoch this block is the last block of, if
// it is one.
ClosingEpoch utils.Option[uint64]
}

// State is the precompile-facing state API. Implementations must make these
Expand Down
48 changes: 48 additions & 0 deletions giga/evmonly/precompiles/dec/dec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Package dec is arithmetic on decimals held as big.Int values scaled by 1e18,
// rounding as the Cosmos SDK's sdk.Dec does.
package dec

import (
"math/big"
)

// one is 1 as a decimal.
var one = big.NewInt(1_000_000_000_000_000_000)

// One returns 1 as a decimal.
func One() *big.Int {
return new(big.Int).Set(one)
}

// FromInt returns integer i as a decimal.
func FromInt(i *big.Int) *big.Int {
return new(big.Int).Mul(i, one)
}

// Truncate returns non-negative decimal d with its fraction dropped.
func Truncate(d *big.Int) *big.Int {
return new(big.Int).Quo(d, one)
}

// Quo divides decimal a by decimal b, both non-negative, as sdk.Dec's Quo
// does: truncated to 36 places, then rounded half to even to 18.
func Quo(a, b *big.Int) *big.Int {
q := new(big.Int).Mul(a, one)
q.Mul(q, one)
return round(q.Quo(q, b))
}

// round divides non-negative x by 1e18, rounding half to even, as sdk.Dec's
// chopPrecisionAndRound does.
func round(x *big.Int) *big.Int {
q, r := new(big.Int).QuoRem(x, one, new(big.Int))
switch r.Lsh(r, 1).Cmp(one) {
case 1:
q.Add(q, big.NewInt(1))
case 0:
if q.Bit(0) == 1 {
q.Add(q, big.NewInt(1))
}
}
return q
}
21 changes: 21 additions & 0 deletions giga/evmonly/precompiles/dec/dec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dec

import (
"math/big"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
)

func TestQuoMatchesSDK(t *testing.T) {
rng := rand.New(rand.NewSource(11)) //nolint:gosec // deterministic test data
for range 2_000 {
total := big.NewInt(rng.Int63n(1<<40) + 1)
power := new(big.Int).Rand(rng, total)
want := sdk.NewIntFromBigInt(power).ToDec().Quo(sdk.NewIntFromBigInt(total).ToDec())
require.Equal(t, want.BigInt().String(), Quo(FromInt(power), FromInt(total)).String(), "%s / %s", power, total)
}
}
9 changes: 9 additions & 0 deletions giga/evmonly/precompiles/solstorage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func (s Store) SetUint8(slot common.Hash, offset int, v uint8) {
s.setPacked(slot, offset, []byte{v})
}

func (s Store) AddressAt(slot common.Hash, offset int) common.Address {
w := s.Word(slot)
return common.BytesToAddress(w[32-offset-common.AddressLength : 32-offset])
}

func (s Store) SetAddressAt(slot common.Hash, offset int, a common.Address) {
s.setPacked(slot, offset, a[:])
}

func (s Store) Bool(slot common.Hash, offset int) bool {
w := s.Word(slot)
return w[31-offset] != 0
Expand Down
Loading
Loading