Skip to content

Commit

Permalink
Merge pull request #21 from x-senpai-x/Implement/consensus/rpc-#16
Browse files Browse the repository at this point in the history
Implement/consensus/rpc #16
  • Loading branch information
gerceboss authored Aug 31, 2024
2 parents 24c826f + 5307472 commit dbdc513
Show file tree
Hide file tree
Showing 11 changed files with 8,674 additions and 102 deletions.
147 changes: 74 additions & 73 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,109 @@ package common

//other option is to import a package of go-ethereum but that was weird
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
"encoding/json"
"strconv"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"strconv"
)

// need to confirm how such primitive types will be imported,
//
//Transaction https://docs.rs/alloy/latest/alloy/rpc/types/struct.Transaction.html
// Transaction https://docs.rs/alloy/latest/alloy/rpc/types/struct.Transaction.html
// address: 20 bytes
// B256: 32 bytes https://bluealloy.github.io/revm/docs/revm/precompile/primitives/type.B256.html
type Address struct{
addr [20]byte
type Address struct {
addr [20]byte
}

type Block struct {
number uint64
base_fee_per_gas uint256.Int
difficulty uint256.Int
extra_data []byte
gas_limit uint64
gas_used uint64
hash [32]byte
logs_bloom []byte
miner Address
mix_hash [32]byte
nonce string
parent_hash [32]byte
receipts_root [32]byte
sha3_uncles [32]byte
size uint64
state_root [32]byte
timestamp uint64
total_difficulty uint64
transactions Transactions
transactions_root [32]byte
uncles [][32]byte
number uint64
base_fee_per_gas uint256.Int
difficulty uint256.Int
extra_data []byte
gas_limit uint64
gas_used uint64
hash [32]byte
logs_bloom []byte
miner Address
mix_hash [32]byte
nonce string
parent_hash [32]byte
receipts_root [32]byte
sha3_uncles [32]byte
size uint64
state_root [32]byte
timestamp uint64
total_difficulty uint64
transactions Transactions
transactions_root [32]byte
uncles [][32]byte
}

// an enum having 2 types- how to implement??
type Transactions struct {
Hashes [][32]byte
Full []types.Transaction //transaction needs to be defined
Hashes [][32]byte
Full []types.Transaction //transaction needs to be defined
}
func Default() *Transactions{
return &Transactions{
Full: []types.Transaction{},
}

func Default() *Transactions {
return &Transactions{
Full: []types.Transaction{},
}
}
func (t *Transactions) hashes() [][32]byte{
if len(t.Hashes)>0{//if Transactions struct contains hashes then return them directly
return t.Hashes
}
hashes := make([][32]byte, len(t.Full))
func (t *Transactions) hashes() [][32]byte {
if len(t.Hashes) > 0 { //if Transactions struct contains hashes then return them directly
return t.Hashes
}
hashes := make([][32]byte, len(t.Full))
for i, tx := range t.Full {
hashes[i] = tx.Hash()
}
return hashes
}
func (t Transactions) MarshalJSON()([]byte,error){
if len(t.Hashes)>0{
return json.Marshal(t.Hashes)
}
return json.Marshal(t.Full)
func (t Transactions) MarshalJSON() ([]byte, error) {
if len(t.Hashes) > 0 {
return json.Marshal(t.Hashes)
}
return json.Marshal(t.Full)
}

// can be an enum
type BlockTag struct {
Latest bool
Finalized bool
Number uint64
Latest bool
Finalized bool
Number uint64
}

func (b BlockTag) String() string {
if b.Latest{
return "latest"
}
if b.Finalized{
return "finalized"
}
return fmt.Sprintf("%d", b.Number)
if b.Latest {
return "latest"
}
if b.Finalized {
return "finalized"
}
return fmt.Sprintf("%d", b.Number)
}

func (b *BlockTag)UnmarshalJSON(data []byte) error{
var block string
if err := json.Unmarshal(data, &block); err != nil {
return err
}
switch block {
case "latest":
b.Latest = true
case "finalized":
b.Finalized = true
default:
var err error
b.Number, err = parseBlockNumber(block)
if err != nil {
return err
}
}
return nil
func (b *BlockTag) UnmarshalJSON(data []byte) error {
var block string
if err := json.Unmarshal(data, &block); err != nil {
return err
}
switch block {
case "latest":
b.Latest = true
case "finalized":
b.Finalized = true
default:
var err error
b.Number, err = parseBlockNumber(block)
if err != nil {
return err
}
}
return nil
}
func parseBlockNumber(block string) (uint64, error) {
if len(block) > 2 && block[:2] == "0x" {
Expand All @@ -117,6 +119,5 @@ func parseDecimalUint64(decStr string) (uint64, error) {
return strconv.ParseUint(decStr, 10, 64)
}


// need some error structs and enums as well
// Example BlockNotFoundError
169 changes: 169 additions & 0 deletions consensus/consensus_core/consensus_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// go implementation of consensus_core/src/types/mod.rs
// structs not defined yet:
// LightClientStore,genericUpdate
package consensus_core

type BeaconBlock struct {
slot uint64
proposer_index uint64
parent_root [32]byte
state_root [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
}
type ProposerSlashing struct {
signed_header_1 SignedBeaconBlockHeader
signed_header_2 SignedBeaconBlockHeader
}
type SignedBeaconBlockHeader struct {
message BeaconBlockHeader
signature SignatureBytes
}
type BeaconBlockHeader struct {
slot uint64
proposer_index uint64
parent_root Bytes32
state_root Bytes32
body_root Bytes32
}
type AttesterSlashing struct {
attestation_1 IndexedAttestation
attestation_2 IndexedAttestation
}
type IndexedAttestation struct {
attesting_indices []uint64 //max length 2048 to be ensured
data AttestationData
signature SignatureBytes
}
type AttestationData struct {
slot uint64
index uint64
beacon_block_root Bytes32
source Checkpoint
target Checkpoint
}
type Checkpoint struct {
epoch uint64
root Bytes32
}
type Bitlist []bool
type Attestation struct {
aggregation_bits Bitlist `ssz-max:"2048"`
data AttestationData
signature SignatureBytes
}
type Deposit struct {
proof [33]Bytes32 //fixed size array
data DepositData
}
type DepositData struct {
pubkey [48]byte
withdrawal_credentials Bytes32
amount uint64
signature SignatureBytes
}
type SignedVoluntaryExit struct {
message VoluntaryExit
signature SignatureBytes
}
type VoluntaryExit struct {
epoch uint64
validator_index uint64
}
type SyncAggregate struct {
sync_committee_bits [64]byte
sync_committee_signature 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
}
type SignedBlsToExecutionChange struct {
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
}
type Header struct {
slot uint64
proposer_index uint64
parent_root Bytes32
state_root Bytes32
body_root Bytes32
}
type SyncComittee struct {
pubkeys [512]BLSPubKey
aggregate_pubkey 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
}
type FinalityUpdate struct {
attested_header Header
finalized_header Header
finality_branch []Bytes32
sync_aggregate SyncAggregate
signature_slot uint64
}
type OptimisticUpdate struct {
attested_header Header
sync_aggregate SyncAggregate
signature_slot uint64
}
type Bootstrap struct {
header Header
current_sync_committee SyncComittee
current_sync_committee_branch []Bytes32
}
25 changes: 16 additions & 9 deletions consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package rpc

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

// return types not mention and oarameters as well
type ConsensusRpc interface{
new()
get_bootstrap()
get_updates()
get_finality_update();
get_optimistic_update();
get_block()
chain_id()
}
type ConsensusRpc interface {
GetBootstrap(block_root []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)
ChainId() (uint64, error)
}

func NewConsensusRpc(rpc string) ConsensusRpc {
return NewNimbusRpc(rpc)
}
Loading

0 comments on commit dbdc513

Please sign in to comment.