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

Execution module #107

Open
wants to merge 53 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a78f469
Updated
18aaddy Sep 19, 2024
5687685
Revert "Updated"
18aaddy Sep 24, 2024
cf677bd
Modified go.mod and go.sum
18aaddy Oct 29, 2024
42e5329
Updated
18aaddy Sep 19, 2024
00e2ec9
Revert "Updated"
18aaddy Sep 24, 2024
030dd05
Initial Commit
18aaddy Oct 23, 2024
cb8a486
Updated
18aaddy Oct 29, 2024
b7c0a1d
Update
18aaddy Oct 29, 2024
52120eb
Update
18aaddy Oct 29, 2024
2f4733a
Tests working locally
18aaddy Oct 29, 2024
24997e7
Added test file
18aaddy Oct 29, 2024
ff6cb44
Update
18aaddy Oct 30, 2024
3ea205b
Commented CreateAccessListTest
18aaddy Oct 30, 2024
903d4c0
Update
18aaddy Oct 30, 2024
b2cb789
fix for #96
ABD-AZE Nov 1, 2024
bc86ed5
json error resolved from createaccess list
ABD-AZE Nov 1, 2024
a142e0c
resolved warnings
ABD-AZE Nov 1, 2024
01a2b1b
tests passing
ABD-AZE Nov 1, 2024
d83884f
final warnings resolved
ABD-AZE Nov 1, 2024
7c1995c
edit common/types.go
ABD-AZE Nov 2, 2024
9a356b2
Running workflow again
ABD-AZE Nov 4, 2024
cfd2242
Tests passing
ABD-AZE Nov 4, 2024
3d89967
type consistency maintained
ABD-AZE Nov 4, 2024
eb84b9f
refactoring
ABD-AZE Nov 4, 2024
37d2643
.gitignore
ABD-AZE Nov 4, 2024
0056c23
Update consensus.go
DarkLord017 Oct 15, 2024
eef957e
Update nimbus_rpc.go
DarkLord017 Oct 15, 2024
a0838d7
Refactor consensus/rpc/consensus_rpc.go
DarkLord017 Sep 27, 2024
f6041b0
Refactor nimbus_rpc.go
DarkLord017 Sep 27, 2024
91a10b3
Update consensus_core.go to remove import cycle errors after refactor…
DarkLord017 Oct 1, 2024
728596d
Resolved builder errors
Nov 4, 2024
f8db9f7
Update consensus.go to correctly use rlp unmarsahalling and handle le…
DarkLord017 Nov 4, 2024
a7a2584
Updated
18aaddy Sep 19, 2024
7b6db66
Revert "Updated"
18aaddy Sep 24, 2024
eb56f21
Modified go.mod and go.sum
18aaddy Oct 29, 2024
e3686c5
Updated
18aaddy Sep 19, 2024
51ac0b6
Revert "Updated"
18aaddy Sep 24, 2024
e003163
Update
18aaddy Oct 29, 2024
b390057
Update consensus.go
DarkLord017 Oct 15, 2024
b55ece5
Update nimbus_rpc.go
DarkLord017 Oct 15, 2024
910ff62
Update go.mod
DarkLord017 Oct 15, 2024
b138b82
Update go.sum
DarkLord017 Oct 15, 2024
bb1bf17
Refactor nimbus_rpc.go
DarkLord017 Sep 27, 2024
4ac1a44
Refactor mock_rpc_test.go
DarkLord017 Sep 27, 2024
701edda
Refactor RPC module
DarkLord017 Sep 27, 2024
08ee418
Update mock_rpc_test.go
DarkLord017 Sep 27, 2024
4adfa33
Update consensus_rpc.go
DarkLord017 Nov 4, 2024
0c63cec
Refactor consensus.go
DarkLord017 Oct 1, 2024
21de06d
Update consensus_core.go to remove import cycle errors after refactor…
DarkLord017 Oct 1, 2024
60b7878
Resolved builder errors
Nov 4, 2024
e75a574
Update consensus.go to correctly use rlp unmarsahalling and handle le…
DarkLord017 Nov 4, 2024
a232f31
resolve errors
ABD-AZE Nov 4, 2024
ca8aa83
errors resolved
ABD-AZE Nov 4, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.mod
go.sum
45 changes: 23 additions & 22 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package common
import (
"encoding/json"
"fmt"
"math/big"
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -45,27 +45,28 @@ type Transactions struct {
Hashes [][32]byte
Full []Transaction // transaction needs to be defined
}

// Updated as earlier, txn data fetched from rpc was not able to unmarshal
// into the struct
type Transaction struct {
AccessList types.AccessList
Hash common.Hash
Nonce uint64
BlockHash [32]byte
BlockNumber *uint64
TransactionIndex uint64
From string
To *common.Address
Value *big.Int
GasPrice *big.Int
Gas uint64
Input []byte
ChainID *big.Int
TransactionType uint8
Signature *Signature
MaxFeePerGas *big.Int
MaxPriorityFeePerGas *big.Int
MaxFeePerBlobGas *big.Int
BlobVersionedHashes []common.Hash
AccessList types.AccessList `json:"accessList"`
Hash common.Hash `json:"hash"`
Nonce hexutil.Uint64 `json:"nonce"`
BlockHash common.Hash `json:"blockHash"` // Pointer because it's nullable
BlockNumber hexutil.Uint64 `json:"blockNumber"` // Pointer because it's nullable
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
From *common.Address `json:"from"`
To *common.Address `json:"to"` // Pointer because 'to' can be null for contract creation
Value hexutil.Big `json:"value"`
GasPrice hexutil.Big `json:"gasPrice"`
Gas hexutil.Uint64 `json:"gas"`
Input hexutil.Bytes `json:"input"`
ChainID hexutil.Big `json:"chainId"`
TransactionType hexutil.Uint `json:"type"`
Signature *Signature `json:"signature"`
MaxFeePerGas hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerBlobGas hexutil.Big `json:"maxFeePerBlobGas"`
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes"`
}

type Signature struct {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (b BlockTag) String() string {
if b.Finalized {
return "finalized"
}
return fmt.Sprintf("%d", b.Number)
return fmt.Sprintf("0x%x", b.Number)
}

func (b *BlockTag) UnmarshalJSON(data []byte) error {
Expand Down
72 changes: 41 additions & 31 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ type GenericUpdate struct {
FinalizedHeader consensus_core.Header
FinalityBranch []consensus_core.Bytes32
}

// The consensus client fields are updated because state takes chan *common.Block,
// not common.Block
type ConsensusClient struct {
BlockRecv *common.Block
FinalizedBlockRecv *common.Block
BlockRecv chan *common.Block
FinalizedBlockRecv chan *common.Block
CheckpointRecv *[]byte
genesisTime uint64
db Database
Expand All @@ -73,7 +74,7 @@ type Inner struct {
RPC rpc.ConsensusRpc
Store LightClientStore
lastCheckpoint *[]byte
blockSend chan common.Block
blockSend chan *common.Block
finalizedBlockSend chan *common.Block
checkpointSend chan *[]byte
Config *config.Config
Expand All @@ -88,22 +89,33 @@ type LightClientStore struct {
}

func (con ConsensusClient) New(rpc *string, config config.Config) ConsensusClient {
blockSend := make(chan common.Block, 256)
blockSend := make(chan *common.Block, 256)
finalizedBlockSend := make(chan *common.Block)
checkpointSend := make(chan *[]byte)

db, err := con.db.New(&config)
// Here the initialisation of database was wrong as it was pointing to an interface,
// not a types that implements the interface. This resulted in an invalid address error
_db, err := (&ConfigDB{}).New(&config)
if err != nil {
panic(err)
}

// Doing this makes the db variable a concrete type (ConfigDB here)
// It can also be switched to FileDB if needed
db, ok := _db.(*ConfigDB)
if !ok {
panic(errors.New("Expected ConfigDB instance"))
}
var initialCheckpoint [32]byte

// There was a problem in this assignment initially as it was setting the checkpoint
// to db.LoadCheckpoint() when the user inputed a checkpoint. This updated one is
// according to Helios.
if config.Checkpoint != nil {
initialCheckpoint = *config.Checkpoint
} else {
initialNewCheckpoint, errorWhileLoadingCheckpoint := db.LoadCheckpoint()
copy(initialCheckpoint[:], initialNewCheckpoint)
if errorWhileLoadingCheckpoint != nil {
log.Printf("error while loading checkpoint: %v", errorWhileLoadingCheckpoint)
initialCheckpoint = config.DefaultCheckpoint
}
}
if initialCheckpoint == [32]byte{} {
Expand Down Expand Up @@ -152,13 +164,11 @@ func (con ConsensusClient) New(rpc *string, config config.Config) ConsensusClien
}
}()

blocksReceived := <-blockSend
finalizedBlocksReceived := <-finalizedBlockSend
checkpointsReceived := <-checkpointSend

return ConsensusClient{
BlockRecv: &blocksReceived,
FinalizedBlockRecv: finalizedBlocksReceived,
BlockRecv: blockSend,
FinalizedBlockRecv: finalizedBlockSend,
CheckpointRecv: checkpointsReceived,
genesisTime: config.Chain.GenesisTime,
db: db,
Expand Down Expand Up @@ -251,7 +261,7 @@ func sync_all_fallback(inner *Inner, chainID uint64) error {
return <-errorChan
}

func (in *Inner) New(rpcURL string, blockSend chan common.Block, finalizedBlockSend chan *common.Block, checkpointSend chan *[]byte, config *config.Config) *Inner {
func (in *Inner) New(rpcURL string, blockSend chan *common.Block, finalizedBlockSend chan *common.Block, checkpointSend chan *[]byte, config *config.Config) *Inner {
rpcClient := rpc.NewConsensusRpc(rpcURL)

return &Inner{
Expand Down Expand Up @@ -524,7 +534,7 @@ func (in *Inner) send_blocks() error {
log.Printf("Error converting payload to block: %v", err)
return
}
in.blockSend <- *block
in.blockSend <- block
}()

go func() {
Expand Down Expand Up @@ -1036,17 +1046,17 @@ func processTransaction(txBytes *[]byte, blockHash consensus_core.Bytes32, block

tx := common.Transaction{
Hash: txEnvelope.Hash(),
Nonce: txEnvelope.Nonce(),
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: index,
Nonce: hexutil.Uint64(txEnvelope.Nonce()),
BlockHash: geth.BytesToHash(blockHash[:]),
BlockNumber: hexutil.Uint64(*blockNumber),
TransactionIndex: hexutil.Uint64(index),
To: txEnvelope.To(),
Value: txEnvelope.Value(),
GasPrice: txEnvelope.GasPrice(),
Gas: txEnvelope.Gas(),
Value: hexutil.Big(*txEnvelope.Value()),
GasPrice: hexutil.Big(*txEnvelope.GasPrice()),
Gas: hexutil.Uint64(txEnvelope.Gas()),
Input: txEnvelope.Data(),
ChainID: txEnvelope.ChainId(),
TransactionType: txEnvelope.Type(),
ChainID: hexutil.Big(*txEnvelope.ChainId()),
TransactionType: hexutil.Uint(txEnvelope.Type()),
}

// Handle signature and transaction type logic
Expand All @@ -1055,7 +1065,7 @@ func processTransaction(txBytes *[]byte, blockHash consensus_core.Bytes32, block
if err != nil {
return common.Transaction{}, fmt.Errorf("failed to recover sender: %v", err)
}
tx.From = from.Hex()
tx.From = &from

// Extract signature components
r, s, v := txEnvelope.RawSignatureValues()
Expand All @@ -1070,12 +1080,12 @@ func processTransaction(txBytes *[]byte, blockHash consensus_core.Bytes32, block
case types.AccessListTxType:
tx.AccessList = txEnvelope.AccessList()
case types.DynamicFeeTxType:
tx.MaxFeePerGas = new(big.Int).Set(txEnvelope.GasFeeCap())
tx.MaxPriorityFeePerGas = new(big.Int).Set(txEnvelope.GasTipCap())
tx.MaxFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasFeeCap()))
tx.MaxPriorityFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasTipCap()))
case types.BlobTxType:
tx.MaxFeePerGas = new(big.Int).Set(txEnvelope.GasFeeCap())
tx.MaxPriorityFeePerGas = new(big.Int).Set(txEnvelope.GasTipCap())
tx.MaxFeePerBlobGas = new(big.Int).Set(txEnvelope.BlobGasFeeCap())
tx.MaxFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasFeeCap()))
tx.MaxPriorityFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasTipCap()))
tx.MaxFeePerBlobGas = hexutil.Big(*new(big.Int).Set(txEnvelope.BlobGasFeeCap()))
tx.BlobVersionedHashes = txEnvelope.BlobHashes()
case types.LegacyTxType:
// No additional fields to set
Expand Down Expand Up @@ -1169,4 +1179,4 @@ func toGethSyncCommittee(committee *consensus_core.SyncCommittee) *beacon.Serial
}
copy(s[512*48:], jsoncommittee.Aggregate[:])
return &s
}
}
2 changes: 1 addition & 1 deletion consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetClient(strictCheckpointAge bool, sync bool) (*Inner, error) {
log.Fatalf("failed to decode checkpoint: %v", err)
}

blockSend := make(chan common.Block, 256)
blockSend := make(chan *common.Block, 256)
finalizedBlockSend := make(chan *common.Block)
channelSend := make(chan *[]byte)

Expand Down
2 changes: 1 addition & 1 deletion consensus/rpc/consensus_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rpc

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

)

// return types not mention and oarameters as well
Expand All @@ -16,6 +15,7 @@ type ConsensusRpc interface {
}



func NewConsensusRpc(rpc string) ConsensusRpc {
if rpc == "testdata/" {
return NewMockRpc(rpc)
Expand Down
1 change: 1 addition & 0 deletions consensus/rpc/mock_rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rpc


import (
"encoding/json"
"os"
Expand Down
85 changes: 85 additions & 0 deletions execution/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package execution

import (
"errors"
"testing"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/consensus/types"
"github.com/stretchr/testify/assert"
)

func TestExecutionErrors(t *testing.T) {
// Test InvalidAccountProofError
address := types.Address{0x01, 0x02}
err := NewInvalidAccountProofError(address)
assert.EqualError(t, err, "invalid account proof for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test InvalidStorageProofError
slot := consensus_core.Bytes32{0x0a}
err = NewInvalidStorageProofError(address, slot)
assert.EqualError(t, err, "invalid storage proof for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], slot: [10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test CodeHashMismatchError
found := consensus_core.Bytes32{0x03}
expected := consensus_core.Bytes32{0x04}
err = NewCodeHashMismatchError(address, found, expected)
assert.EqualError(t, err, "code hash mismatch for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], found: [3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], expected: [4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test ReceiptRootMismatchError
tx := consensus_core.Bytes32{0x05}
err = NewReceiptRootMismatchError(tx)
assert.EqualError(t, err, "receipt root mismatch for tx: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test MissingTransactionError
err = NewMissingTransactionError(tx)
assert.EqualError(t, err, "missing transaction for tx: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test MissingLogError
err = NewMissingLogError(tx, 3)
assert.EqualError(t, err, "missing log for transaction: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], index: 3")

// Test TooManyLogsToProveError
err = NewTooManyLogsToProveError(5000, 1000)
assert.EqualError(t, err, "too many logs to prove: 5000, current limit is: 1000")

// Test InvalidBaseGasFeeError
err = NewInvalidBaseGasFeeError(1000, 2000, 123456)
assert.EqualError(t, err, "Invalid base gas fee selene 1000 vs rpc endpoint 2000 at block 123456")

// Test BlockNotFoundError
err = NewBlockNotFoundError(123456)
assert.EqualError(t, err, "Block 123456 not found")

// Test EmptyExecutionPayloadError
err = NewEmptyExecutionPayloadError()
assert.EqualError(t, err, "Selene Execution Payload is empty")
}

func TestEvmErrors(t *testing.T) {
// Test RevertError
data := []byte{0x08, 0xc3, 0x79, 0xa0}
err := NewRevertError(data)
assert.EqualError(t, err, "execution reverted: [8 195 121 160]")

// Test GenericError
err = NewGenericError("generic error")
assert.EqualError(t, err, "evm error: generic error")

// Test RpcError
rpcErr := errors.New("rpc connection failed")
err = NewRpcError(rpcErr)
assert.EqualError(t, err, "rpc error: rpc connection failed")
}

func TestDecodeRevertReason(t *testing.T) {
// Test successful revert reason decoding
reasonData := []byte{0x08, 0xc3, 0x79, 0xa0}
reason := DecodeRevertReason(reasonData)
assert.NotEmpty(t, reason, "Revert reason should be decoded")

// Test invalid revert data
invalidData := []byte{0x00}
reason = DecodeRevertReason(invalidData)
assert.Contains(t, reason, "invalid data for unpacking")
}
Loading
Loading