Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Dec 7, 2023
1 parent b74b087 commit 7665f74
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
11 changes: 6 additions & 5 deletions chains/evm/contracts/spectre.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ func (c *Spectre) Step(stepInput message.SyncStepInput, stepProof []byte, opts t
}

func (c *Spectre) Rotate(rotateInput message.RotateInput, rotateProof []byte, stepInput message.SyncStepInput, stepProof []byte, opts transactor.TransactOptions) (*common.Hash, error) {
type ContractRotateInput struct {
type RotateInput struct {
SyncCommitteeSSZ [32]byte
SyncCommitteePoseidon [32]byte
}
rInput := RotateInput{
SyncCommitteeSSZ: rotateInput.SyncCommitteeSSZ,
SyncCommitteePoseidon: rotateInput.SyncCommitteePoseidon,
}
return c.ExecuteTransaction(
"rotate",
opts,
ContractRotateInput{
SyncCommitteeSSZ: rotateInput.SyncCommitteeSSZ,
SyncCommitteePoseidon: rotateInput.SyncCommitteePoseidon,
}, rotateProof, stepInput, stepProof, rotateInput.Accumulator,
rInput, rotateProof, stepInput, stepProof, rotateInput.Accumulator,
)
}
29 changes: 12 additions & 17 deletions chains/evm/listener/events/handlers/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/rs/zerolog/log"
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message"
"github.com/sygmaprotocol/sygma-core/relayer/message"
consensus "github.com/umbracle/go-eth-consensus"
)

type SyncCommitteeFetcher interface {
Expand Down Expand Up @@ -43,34 +42,30 @@ func NewRotateHandler(msgChan chan []*message.Message, syncCommitteeFetcher Sync
// HandleEvents checks if there is a new sync committee and sends a rotate message
// if there is
func (h *RotateHandler) HandleEvents(checkpoint *apiv1.Finality) error {
syncCommittee, err := h.syncCommitteeFetcher.SyncCommittee(context.Background(), &api.SyncCommitteeOpts{
State: "justified",
})
sArgs, err := h.prover.StepArgs()
if err != nil {
return err
}
if syncCommittee.Data.String() == h.currentSyncCommittee.String() {
return nil
}

log.Info().Uint8("domainID", h.domainID).Msgf("Rotating committee")

rArgs, err := h.prover.RotateArgs(uint64(checkpoint.Justified.Epoch))
if err != nil {
return err
}
sArgs, err := h.prover.StepArgs()

if sArgs.Update.FinalizedHeader.Header.Slot != rArgs.Update.AttestedHeader.Header.Slot {
return nil
}
syncCommittee, err := h.syncCommitteeFetcher.SyncCommittee(context.Background(), &api.SyncCommitteeOpts{
State: "justified",
})
if err != nil {
return err
}
sArgs.Update = &consensus.LightClientFinalityUpdateCapella{
AttestedHeader: rArgs.Update.AttestedHeader,
FinalizedHeader: rArgs.Update.FinalizedHeader,
FinalityBranch: rArgs.Update.FinalityBranch,
SyncAggregate: rArgs.Update.SyncAggregate,
SignatureSlot: rArgs.Update.SignatureSlot,
if syncCommittee.Data.String() == h.currentSyncCommittee.String() {
return nil
}

log.Info().Uint8("domainID", h.domainID).Msgf("Rotating committee")

rotateProof, err := h.prover.RotateProof(rArgs)
if err != nil {
return err
Expand Down
35 changes: 17 additions & 18 deletions chains/evm/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"strings"

"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec/phase0"
Expand Down Expand Up @@ -35,14 +36,6 @@ type ProverResponse struct {
Commitment string `json:"committee_poseidon"`
}

type CommitmentResponse struct {
Commitment [32]byte `json:"commitment"`
}

type CommitmentArgs struct {
Pubkeys [512][48]byte `json:"pubkeys"`
}

type EvmProof[T any] struct {
Proof []byte
Input T
Expand Down Expand Up @@ -126,11 +119,6 @@ func (p *Prover) StepProof(args *StepArgs) (*EvmProof[message.SyncStepInput], er
if err != nil {
return nil, err
}
log.Info().Msgf("Attested Header Slot: %d", args.Update.AttestedHeader.Header.Slot)
log.Info().Msgf("Finalized Header Slot: %d", args.Update.FinalizedHeader.Header.Slot)
log.Info().Msgf("Sync Count: %d", uint64(CountSetBits(args.Update.SyncAggregate.SyncCommiteeBits)))
log.Info().Msgf("FINALIZED HEADER ROOT: %s", hex.EncodeToString(finalizedHeaderRoot[:]))
log.Info().Msgf("Execution Root %s", hex.EncodeToString(executionRoot[:]))
proof := &EvmProof[message.SyncStepInput]{
Proof: U16ArrayToByteArray(resp.Proof),
Input: message.SyncStepInput{
Expand Down Expand Up @@ -171,7 +159,7 @@ func (p *Prover) RotateProof(args *RotateArgs) (*EvmProof[message.RotateInput],

log.Info().Msgf("Generated rotate proof")

comm, _ := hex.DecodeString(resp.Commitment[2:])
comm, err := hex.DecodeString(padHexString(resp.Commitment[2:], 64))
if err != nil {
return nil, err
}
Expand All @@ -181,10 +169,6 @@ func (p *Prover) RotateProof(args *RotateArgs) (*EvmProof[message.RotateInput],
accumulator[i], _ = new(big.Int).SetString(value[2:], 16)
}

log.Info().Msgf("Sync CommitteeRoot Bytes: %b", syncCommiteeRoot)
log.Info().Msg(hex.EncodeToString(syncCommiteeRoot[:]))
log.Info().Msg("POSEIDON")
log.Info().Msg(hex.EncodeToString(comm))
proof := &EvmProof[message.RotateInput]{
Proof: U16ArrayToByteArray(resp.Proof),
Input: message.RotateInput{
Expand Down Expand Up @@ -259,3 +243,18 @@ func (p *Prover) pubkeysRoot(pubkeys [512][48]byte) ([32]byte, error) {
h.Merkleize(subIndx)
return h.HashRoot()
}

func padHexString(hexString string, desiredLength int) string {
currentLength := len(hexString)
if currentLength >= desiredLength {
return hexString
}

// Calculate the number of leading zeroes needed
leadingZeroes := strings.Repeat("0", desiredLength-currentLength)

// Concatenate the leading zeroes with the original hex string
paddedHexString := leadingZeroes + hexString

return paddedHexString
}

0 comments on commit 7665f74

Please sign in to comment.