Skip to content

Commit

Permalink
Merge pull request #104 from DarkLord017/BlockTests
Browse files Browse the repository at this point in the history
Block tests
  • Loading branch information
star-gazer111 authored Nov 4, 2024
2 parents c97b104 + b355170 commit 753eaee
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 68 deletions.
34 changes: 14 additions & 20 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package consensus


///NOTE: only these imports are required others are in package already
// uses rpc
// uses config for networks
// uses common for datatypes

import (
"bytes"
"encoding/hex"
Expand Down Expand Up @@ -302,7 +300,7 @@ func (in *Inner) get_execution_payload(slot *uint64) (*consensus_core.ExecutionP
}

block := <-blockChan
Gethblock, err := beacon.BlockFromJSON("capella", block.Body.Hash)
Gethblock, err := beacon.BlockFromJSON("capella", block.Hash)
if err != nil {
return nil, err
}
Expand All @@ -311,7 +309,6 @@ func (in *Inner) get_execution_payload(slot *uint64) (*consensus_core.ExecutionP
finalizedSlot := in.Store.FinalizedHeader.Slot

var verifiedBlockHash geth.Hash

if *slot == latestSlot {
verifiedBlockHash = toGethHeader(&in.Store.OptimisticHeader).Hash()
} else if *slot == finalizedSlot {
Expand Down Expand Up @@ -377,13 +374,15 @@ func (in *Inner) Get_payloads(startSlot, endSlot uint64) ([]interface{}, error)
select {
case payload, ok := <-payloadsChan:
if !ok {
return payloads, nil
return nil, errors.New("payloads channel closed unexpectedly")
}
payloads = append(payloads, payload)
return payloads, nil
case err := <-errorChan:
return nil, err
}
}

}
func (in *Inner) advance() error {
ErrorChan := make(chan error, 1)
Expand Down Expand Up @@ -460,6 +459,7 @@ func (in *Inner) sync(checkpoint [32]byte) error {
// Apply updates
for _, update := range updates {
if err := in.verify_update(&update); err != nil {

errorChan <- err
return
}
Expand Down Expand Up @@ -562,6 +562,7 @@ func (in *Inner) bootstrap(checkpoint [32]byte) {
bootstrapChan := make(chan consensus_core.Bootstrap, 1)
go func() {
bootstrap, errInBootstrap := in.RPC.GetBootstrap(checkpoint)

if errInBootstrap != nil {
log.Printf("failed to fetch bootstrap: %v", errInBootstrap)
errorChan <- errInBootstrap
Expand Down Expand Up @@ -930,10 +931,8 @@ func verifySyncCommitteeSignature(
return false
}



return utils.FastAggregateVerify(collectedPks, signingRoot[:], &sig)

}

func ComputeCommitteeSignRoot(header *beacon.Header, fork consensus_core.Bytes32) consensus_core.Bytes32 {
Expand Down Expand Up @@ -1027,7 +1026,7 @@ func PayloadToBlock(value *consensus_core.ExecutionPayload) (*common.Block, erro
}, nil
}

func processTransaction(txBytes *[1073741824]byte, blockHash consensus_core.Bytes32, blockNumber *uint64, index uint64) (common.Transaction, error) {
func processTransaction(txBytes *[]byte, blockHash consensus_core.Bytes32, blockNumber *uint64, index uint64) (common.Transaction, error) {
// Decode the transaction envelope (RLP-encoded)

txEnvelope, err := DecodeTxEnvelope(txBytes)
Expand Down Expand Up @@ -1078,8 +1077,10 @@ func processTransaction(txBytes *[1073741824]byte, blockHash consensus_core.Byte
tx.MaxPriorityFeePerGas = new(big.Int).Set(txEnvelope.GasTipCap())
tx.MaxFeePerBlobGas = new(big.Int).Set(txEnvelope.BlobGasFeeCap())
tx.BlobVersionedHashes = txEnvelope.BlobHashes()
case types.LegacyTxType:
// No additional fields to set
default:
fmt.Println("Unhandled transaction type")
fmt.Print("Unhandled transaction type")
}

return tx, nil
Expand All @@ -1105,18 +1106,11 @@ func popCount(b byte) int {
}

// DecodeTxEnvelope takes the transaction bytes and decodes them into a transaction envelope (Ethereum transaction)
func DecodeTxEnvelope(txBytes *[1073741824]byte) (*types.Transaction, error) {
func DecodeTxEnvelope(txBytes *[]byte) (*types.Transaction, error) {
// Create an empty transaction object
var tx types.Transaction

var txBytesForUnmarshal []byte
for _, b := range txBytes {
if b == 0 {
break
}
txBytesForUnmarshal = append(txBytesForUnmarshal, b)
}

var txBytesForUnmarshal []byte = *txBytes
// Unmarshal the RLP-encoded transaction bytes into the transaction object
err := tx.UnmarshalBinary(txBytesForUnmarshal)
if err != nil {
Expand Down Expand Up @@ -1173,6 +1167,6 @@ func toGethSyncCommittee(committee *consensus_core.SyncCommittee) *beacon.Serial
for i, key := range jsoncommittee.Pubkeys {
copy(s[i*48:], key[:])
}
copy(s[512*48:], jsoncommittee.Aggregate[:])
copy(s[512*48:], jsoncommittee.Aggregate[:])
return &s
}
4 changes: 2 additions & 2 deletions consensus/consensus_core/consensus_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BeaconBlock struct {
ParentRoot Bytes32 `json:"parent_root"`
StateRoot Bytes32 `json:"state_root"`
Body BeaconBlockBody `json:"body"`
Hash []byte
}

type Eth1Data struct {
Expand Down Expand Up @@ -119,7 +120,7 @@ type ExecutionPayload struct {
ExtraData []byte `json:"extra_data"`
BaseFeePerGas uint64 `json:"base_fee_per_gas"`
BlockHash Bytes32 `json:"block_hash"`
Transactions []Transaction `json:"transactions"`
Transactions [][]byte `json:"transactions"`
Withdrawals *[]Withdrawal `json:"withdrawals"` //Only capella and deneb
BlobGasUsed *uint64 `json:"blob_gas_used"` // Only deneb
ExcessBlobGas *uint64 `json:"excess_blob_gas"` // Only deneb
Expand Down Expand Up @@ -149,7 +150,6 @@ type BeaconBlockBody struct {
ExecutionPayload ExecutionPayload `json:"execution_payload"`
BlsToExecutionChanges *[]SignedBlsToExecutionChange `json:"bls_to_execution_changes"` //Only capella and deneb
BlobKzgCommitments *[][]byte `json:"blob_kzg_commitments"` // Dynamic slice
Hash []byte
}

type Header struct {
Expand Down
Loading

0 comments on commit 753eaee

Please sign in to comment.