Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created utils.go #41

254 changes: 140 additions & 114 deletions consensus/consensus_core/consensus_core.go
Original file line number Diff line number Diff line change
@@ -1,169 +1,195 @@
// go implementation of consensus_core/src/types/mod.rs
// structs not defined yet:
// LightClientStore,genericUpdate
package consensus_core

import "github.com/BlocSoc-iitr/selene/consensus/types"

type BeaconBlock struct {
slot uint64
proposer_index uint64
parent_root [32]byte
state_root [32]byte
body BeaconBlockBody
Slot uint64
ProposerIndex uint64
ParentRoot [32]byte
StateRoot [32]byte
Body BeaconBlockBody
}

type Bytes32 [32]byte
type BLSPubKey [48]byte
type Address [20]byte
type LogsBloom = [256]byte
type SignatureBytes [96]byte

type Eth1Data struct {
deposit_root Bytes32
deposit_count uint64
block_hash Bytes32
DepositRoot Bytes32
DepositCount uint64
BlockHash Bytes32
}

type ProposerSlashing struct {
signed_header_1 SignedBeaconBlockHeader
signed_header_2 SignedBeaconBlockHeader
SignedHeader1 SignedBeaconBlockHeader
SignedHeader2 SignedBeaconBlockHeader
}

type SignedBeaconBlockHeader struct {
message BeaconBlockHeader
signature SignatureBytes
Message BeaconBlockHeader
Signature SignatureBytes
}

type BeaconBlockHeader struct {
slot uint64
proposer_index uint64
parent_root Bytes32
state_root Bytes32
body_root Bytes32
Slot uint64
ProposerIndex uint64
ParentRoot Bytes32
StateRoot Bytes32
BodyRoot Bytes32
}

type AttesterSlashing struct {
attestation_1 IndexedAttestation
attestation_2 IndexedAttestation
Attestation1 IndexedAttestation
Attestation2 IndexedAttestation
}

type IndexedAttestation struct {
attesting_indices []uint64 //max length 2048 to be ensured
data AttestationData
signature SignatureBytes
AttestingIndices [2048]uint64
Data AttestationData
Signature SignatureBytes
}

type AttestationData struct {
slot uint64
index uint64
beacon_block_root Bytes32
source Checkpoint
target Checkpoint
Slot uint64
Index uint64
BeaconBlockRoot Bytes32
Source Checkpoint
Target Checkpoint
}

type Checkpoint struct {
epoch uint64
root Bytes32
Epoch uint64
Root Bytes32
}

type Bitlist []bool

type Attestation struct {
aggregation_bits Bitlist `ssz-max:"2048"`
data AttestationData
signature SignatureBytes
AggregationBits Bitlist `ssz-max:"2048"`
Data AttestationData
Signature SignatureBytes
}

type Deposit struct {
proof [33]Bytes32 //fixed size array
data DepositData
Proof [33]Bytes32 //fixed size array
Data DepositData
}

type DepositData struct {
pubkey [48]byte
withdrawal_credentials Bytes32
amount uint64
signature SignatureBytes
Pubkey [48]byte
WithdrawalCredentials Bytes32
Amount uint64
Signature SignatureBytes
}

type SignedVoluntaryExit struct {
message VoluntaryExit
signature SignatureBytes
Message VoluntaryExit
Signature SignatureBytes
}

type VoluntaryExit struct {
epoch uint64
validator_index uint64
Epoch uint64
ValidatorIndex uint64
}

type SyncAggregate struct {
sync_committee_bits [64]byte
sync_committee_signature SignatureBytes
SyncCommitteeBits [64]byte
SyncCommitteeSignature SignatureBytes
}

type Withdrawal struct {
index uint64
validator_index uint64
address Address
amount uint64
}
type ExecutionPayload struct { //not implemented
parent_hash Bytes32
fee_recipient Address
state_root Bytes32
receipts_root Bytes32
logs_bloom LogsBloom
prev_randao Bytes32
block_number uint64
gas_limit uint64
gas_used uint64
timestamp uint64
extra_data [32]byte
base_fee_per_gas uint64
block_hash Bytes32
transactions []byte //max length 1073741824 to be implemented
withdrawals []Withdrawal //max length 16 to be implemented
blob_gas_used uint64
excess_blob_gas uint64
Index uint64
ValidatorIndex uint64
Address Address
Amount uint64
}

type ExecutionPayload struct {
ParentHash Bytes32
FeeRecipient Address
StateRoot Bytes32
ReceiptsRoot Bytes32
LogsBloom LogsBloom
PrevRandao Bytes32
BlockNumber uint64
GasLimit uint64
GasUsed uint64
Timestamp uint64
ExtraData [32]byte
BaseFeePerGas uint64
BlockHash Bytes32
Transactions []types.Transaction `ssz-max:"1048576"`
Withdrawals []types.Withdrawal `ssz-max:"16"`
BlobGasUsed *uint64 // Deneb-specific field, use pointer for optionality
ExcessBlobGas *uint64 // Deneb-specific field, use pointer for optionality
}

type SignedBlsToExecutionChange struct {
message BlsToExecutionChange
signature SignatureBytes
Message BlsToExecutionChange
Signature SignatureBytes
}

type BlsToExecutionChange struct {
validator_index uint64
from_bls_pubkey [48]byte
}
type BeaconBlockBody struct { //not implemented
randao_reveal SignatureBytes
eth1_data Eth1Data
graffiti Bytes32
proposer_slashings []ProposerSlashing //max length 16 to be insured how?
attester_slashings []AttesterSlashing //max length 2 to be ensured
attestations []Attestation //max length 128 to be ensured
deposits []Deposit //max length 16 to be ensured
voluntary_exits SignedVoluntaryExit
sync_aggregate SyncAggregate
execution_payload ExecutionPayload
bls_to_execution_changes []SignedBlsToExecutionChange //max length 16 to be ensured
blob_kzg_commitments [][48]byte //max length 4096 to be ensured
ValidatorIndex uint64
FromBlsPubkey [48]byte
}

// BeaconBlockBody represents the body of a beacon block.
type BeaconBlockBody struct {
RandaoReveal SignatureBytes
Eth1Data Eth1Data
Graffiti Bytes32
ProposerSlashings []ProposerSlashing `ssz-max:"16"`
AttesterSlashings []AttesterSlashing `ssz-max:"2"`
Attestations []Attestation `ssz-max:"128"`
Deposits []Deposit `ssz-max:"16"`
VoluntaryExits []SignedVoluntaryExit `ssz-max:"16"`
SyncAggregate SyncAggregate
BlsToExecutionChanges []SignedBlsToExecutionChange `ssz-max:"16"`
BlobKzgCommitments [][48]byte `ssz-max:"4096"`
}

type Header struct {
slot uint64
proposer_index uint64
parent_root Bytes32
state_root Bytes32
body_root Bytes32
Slot uint64
ProposerIndex uint64
ParentRoot Bytes32
StateRoot Bytes32
BodyRoot Bytes32
}
type SyncComittee struct {
pubkeys [512]BLSPubKey
aggregate_pubkey BLSPubKey

type SyncCommittee struct {
Pubkeys [512]BLSPubKey
AggregatePubkey BLSPubKey
}

type Update struct {
attested_header Header
next_sync_committee SyncComittee
next_sync_committee_branch []Bytes32
finalized_header Header
finality_branch []Bytes32
sync_aggregate SyncAggregate
signature_slot uint64
AttestedHeader Header
NextSyncCommittee SyncCommittee
NextSyncCommitteeBranch []Bytes32
FinalizedHeader Header
FinalityBranch []Bytes32
SyncAggregate SyncAggregate
SignatureSlot uint64
}

type FinalityUpdate struct {
attested_header Header
finalized_header Header
finality_branch []Bytes32
sync_aggregate SyncAggregate
signature_slot uint64
AttestedHeader Header
FinalizedHeader Header
FinalityBranch []Bytes32
SyncAggregate SyncAggregate
SignatureSlot uint64
}

type OptimisticUpdate struct {
attested_header Header
sync_aggregate SyncAggregate
signature_slot uint64
AttestedHeader Header
SyncAggregate SyncAggregate
SignatureSlot uint64
}

type Bootstrap struct {
header Header
current_sync_committee SyncComittee
current_sync_committee_branch []Bytes32
Header Header
CurrentSyncCommittee SyncCommittee
CurrentSyncCommitteeBranch []Bytes32
}
4 changes: 2 additions & 2 deletions consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

// return types not mention and oarameters as well
type ConsensusRpc interface {
GetBootstrap(block_root []byte) (consensus_core.Bootstrap, error)
GetBootstrap(block_root [32]byte) (consensus_core.Bootstrap, error)
GetUpdates(period uint64, count uint8) ([]consensus_core.Update, error)
GetFinalityUpdate() (consensus_core.FinalityUpdate, error)
GetOptimisticUpdate() (consensus_core.OptimisticUpdate, error)
GetBlock(slot uint64) (consensus_core.BeaconBlock, error)
GetBlock(slot uint64) (*consensus_core.BeaconBlock, error)
ChainId() (uint64, error)
}

Expand Down
16 changes: 10 additions & 6 deletions consensus/rpc/nimbus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package rpc
import (
"encoding/json"
"fmt"
"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"io"
"net/http"
"strconv"
"time"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
)

// uses types package
Expand Down Expand Up @@ -43,15 +44,17 @@ func min(a uint8, b uint8) uint8 {
}
return b
}

type NimbusRpc struct {
//ConsensusRpc
rpc string
}

func NewNimbusRpc(rpc string) *NimbusRpc {
return &NimbusRpc{
rpc: rpc}
}
func (n *NimbusRpc) GetBootstrap(block_root []byte) (consensus_core.Bootstrap, error) {
func (n *NimbusRpc) GetBootstrap(block_root [32]byte) (consensus_core.Bootstrap, error) {
root_hex := fmt.Sprintf("%x", block_root)
req := fmt.Sprintf("%s/eth/v1/beacon/light_client/bootstrap/0x%s", n.rpc, root_hex)
var res BootstrapResponse
Expand Down Expand Up @@ -93,14 +96,15 @@ func (n *NimbusRpc) GetOptimisticUpdate() (consensus_core.OptimisticUpdate, erro
}
return res.data, nil
}
func (n *NimbusRpc) GetBlock(slot uint64) (consensus_core.BeaconBlock, error) {
func (n *NimbusRpc) GetBlock(slot uint64) (*consensus_core.BeaconBlock, error) {
req := fmt.Sprintf("%s/eth/v2/beacon/blocks/%s", n.rpc, strconv.FormatUint(slot, 10))
var res BeaconBlockResponse
err := get(req, &res)
if err != nil {
return consensus_core.BeaconBlock{}, fmt.Errorf("block error: %w", err)
return nil, fmt.Errorf("block error: %w", err)
}
return res.data.message, nil

return &res.data.message, nil
}
func (n *NimbusRpc) ChainId() (uint64, error) {
req := fmt.Sprintf("%s/eth/v1/config/spec", n.rpc)
Expand All @@ -111,6 +115,7 @@ func (n *NimbusRpc) ChainId() (uint64, error) {
}
return res.data.chain_id, nil
}

// BeaconBlock, Update,FinalityUpdate ,OptimisticUpdate,Bootstrap yet to be defined in consensus-core/src/types/mod.go
// For now defined in consensus/consensus_core.go
type BeaconBlockResponse struct {
Expand Down Expand Up @@ -138,4 +143,3 @@ type Spec struct {
type BootstrapResponse struct {
data consensus_core.Bootstrap
}

Loading
Loading