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

remove l1 block, close resources #2242

Merged
merged 8 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
22 changes: 0 additions & 22 deletions go/common/encoding.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
package common

import (
"fmt"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)

// EncodedL1Block the encoded version of an L1 block.
type EncodedL1Block []byte

func EncodeBlock(b *types.Block) (EncodedL1Block, error) {
encoded, err := rlp.EncodeToBytes(b)
if err != nil {
return nil, fmt.Errorf("could not encode block to bytes. Cause: %w", err)
}
return encoded, nil
}

func (eb EncodedL1Block) DecodeBlock() (*types.Block, error) {
b := types.Block{}
if err := rlp.DecodeBytes(eb, &b); err != nil {
return nil, fmt.Errorf("could not decode block from bytes. Cause: %w", err)
}
return &b, nil
}

func EncodeRollup(r *ExtRollup) (EncodedRollup, error) {
return rlp.EncodeToBytes(r)
}
Expand Down
4 changes: 3 additions & 1 deletion go/common/gethencoding/geth_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"
"unsafe"

"github.com/ten-protocol/go-ten/go/common/gethutil"

gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/enclave/storage"
Expand Down Expand Up @@ -288,7 +290,7 @@ func (enc *gethEncodingServiceImpl) CreateEthHeaderForBatch(ctx context.Context,

gethHeader := types.Header{
ParentHash: convertedParentHash,
UncleHash: gethcommon.Hash{},
UncleHash: gethutil.EmptyHash,
Root: h.Root,
TxHash: h.TxHash,
ReceiptHash: h.ReceiptHash,
Expand Down
12 changes: 7 additions & 5 deletions go/common/gethutil/gethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"context"
"fmt"

"github.com/ten-protocol/go-ten/go/enclave/storage"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/common"
)

// Utilities for working with geth structures

type BlockResolver interface {
FetchBlock(ctx context.Context, blockHash common.L1BlockHash) (*types.Header, error)
}

// EmptyHash is useful for comparisons to check if hash has been set
var EmptyHash = gethcommon.Hash{}

// LCA - returns the latest common ancestor of the 2 blocks or an error if no common ancestor is found
// it also returns the blocks that became canonical, and the once that are now the fork
func LCA(ctx context.Context, newCanonical *types.Header, oldCanonical *types.Header, resolver storage.BlockResolver) (*common.ChainFork, error) {
func LCA(ctx context.Context, newCanonical *types.Header, oldCanonical *types.Header, resolver BlockResolver) (*common.ChainFork, error) {
b, cp, ncp, err := internalLCA(ctx, newCanonical, oldCanonical, resolver, []common.L1BlockHash{}, []common.L1BlockHash{})
return &common.ChainFork{
NewCanonical: newCanonical,
Expand All @@ -29,9 +31,9 @@ func LCA(ctx context.Context, newCanonical *types.Header, oldCanonical *types.He
}, err
}

func internalLCA(ctx context.Context, newCanonical *types.Header, oldCanonical *types.Header, resolver storage.BlockResolver, canonicalPath []common.L1BlockHash, nonCanonicalPath []common.L1BlockHash) (*types.Header, []common.L1BlockHash, []common.L1BlockHash, error) {
func internalLCA(ctx context.Context, newCanonical *types.Header, oldCanonical *types.Header, resolver BlockResolver, canonicalPath []common.L1BlockHash, nonCanonicalPath []common.L1BlockHash) (*types.Header, []common.L1BlockHash, []common.L1BlockHash, error) {
if newCanonical.Number.Uint64() == common.L1GenesisHeight || oldCanonical.Number.Uint64() == common.L1GenesisHeight {
return newCanonical, canonicalPath, nonCanonicalPath, nil
return oldCanonical, canonicalPath, nonCanonicalPath, nil
}
if newCanonical.Hash() == oldCanonical.Hash() {
// this is where we reach the common ancestor, which we add to the canonical path
Expand Down
6 changes: 0 additions & 6 deletions go/common/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package host
import (
"context"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/common"
hostconfig "github.com/ten-protocol/go-ten/go/host/config"
"github.com/ten-protocol/go-ten/go/host/storage"
Expand Down Expand Up @@ -38,11 +37,6 @@ type Host interface {
NewHeadsChan() chan *common.BatchHeader
}

type BlockStream struct {
Stream <-chan *types.Block // the channel which will receive the consecutive, canonical blocks
Stop func() // function to permanently stop the stream and clean up any associated processes/resources
}

type BatchMsg struct {
Batches []*common.ExtBatch // The batches being sent.
IsLive bool // true if these batches are being sent as new, false if in response to a p2p request
Expand Down
8 changes: 4 additions & 4 deletions go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ type L1DataService interface {
// Subscribe will register a block handler to receive new blocks as they arrive, returns unsubscribe func
Subscribe(handler L1BlockHandler) func()

FetchBlockByHeight(height *big.Int) (*types.Block, error)
FetchBlockByHeight(height *big.Int) (*types.Header, error)
// FetchNextBlock returns the next canonical block after a given block hash
// It returns the new block, a bool which is true if the block is the current L1 head and a bool if the block is on a different fork to prevBlock
FetchNextBlock(prevBlock gethcommon.Hash) (*types.Block, bool, error)
FetchNextBlock(prevBlock gethcommon.Hash) (*types.Header, bool, error)
// GetTenRelevantTransactions returns the events and transactions relevant to Ten
GetTenRelevantTransactions(block *common.L1Block) (*common.ProcessedL1Data, error)
GetTenRelevantTransactions(block *types.Header) (*common.ProcessedL1Data, error)
}

// L1BlockHandler is an interface for receiving new blocks from the repository as they arrive
type L1BlockHandler interface {
// HandleBlock will be called in a new goroutine for each new block as it arrives
HandleBlock(block *types.Block)
HandleBlock(block *types.Header)
}

// L1Publisher provides an interface for the host to interact with Ten data (management contract etc.) on L1
Expand Down
3 changes: 1 addition & 2 deletions go/common/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
BatchHeightKey = "batch_height"
BatchSeqNoKey = "batch_seq_num"
RollupHashKey = "rollup"
CmpKey = "component"
CmpKey = "cmp"
NodeIDKey = "node_id"
EnclaveIDKey = "enclave_id"
NetworkIDKey = "network_id"
Expand All @@ -34,7 +34,6 @@ const (
HostCmp = "host"
HostRPCCmp = "host_rpc"
TxInjectCmp = "tx_inject"
TestLogCmp = "test_log"
P2PCmp = "p2p"
RPCClientCmp = "rpc_client"
DeployerCmp = "deployer"
Expand Down
5 changes: 3 additions & 2 deletions go/common/subscription/new_heads_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"sync/atomic"
"time"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ten-protocol/go-ten/go/common/gethutil"

"github.com/ethereum/go-ethereum/core/types"

gethlog "github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (nhs *NewHeadsService) HealthStatus(context.Context) host.HealthStatus {
func ConvertBatchHeader(head *common.BatchHeader) *types.Header {
return &types.Header{
ParentHash: head.ParentHash,
UncleHash: gethcommon.Hash{},
UncleHash: gethutil.EmptyHash,
Coinbase: head.Coinbase,
Root: head.Root,
TxHash: head.TxHash,
Expand Down
1 change: 0 additions & 1 deletion go/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type (
// MainNet aliases
L1Address = common.Address
L1BlockHash = common.Hash
L1Block = types.Block
L1Transaction = types.Transaction
L1Receipt = types.Receipt
L1Receipts = types.Receipts
Expand Down
22 changes: 0 additions & 22 deletions go/common/utils.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
package common

import (
"math/big"
"math/rand"
"time"

"github.com/ethereum/go-ethereum/core/types"

gethcommon "github.com/ethereum/go-ethereum/common"
)

type (
Latency func() time.Duration
)

func MaxInt(x, y uint32) uint32 {
if x < y {
return y
}
return x
}

// ShortHash converts the hash to a shorter uint64 for printing.
func ShortHash(hash gethcommon.Hash) uint64 {
return hash.Big().Uint64()
}

// ShortAddress converts the address to a shorter uint64 for printing.
func ShortAddress(address gethcommon.Address) uint64 {
return address.Big().Uint64()
}

// ShortNonce converts the nonce to a shorter uint64 for printing.
func ShortNonce(nonce types.BlockNonce) uint64 {
return new(big.Int).SetBytes(nonce[4:]).Uint64()
}

// ExtractPotentialAddress - given a 32 byte hash , it checks whether it can be an address and extracts that
func ExtractPotentialAddress(hash gethcommon.Hash) *gethcommon.Address {
bitlen := hash.Big().BitLen()
Expand Down
4 changes: 3 additions & 1 deletion go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"math/big"
"sync"

"github.com/ten-protocol/go-ten/go/common/gethutil"

"github.com/ten-protocol/go-ten/go/common/compression"

gethcore "github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -515,7 +517,7 @@ func (executor *batchExecutor) execResult(ec *BatchExecutionContext) (*ComputedB
defer executor.stateDBMutex.Unlock()
h, err := ec.stateDB.Commit(batch.Number().Uint64(), deleteEmptyObjects)
if err != nil {
return gethcommon.Hash{}, fmt.Errorf("commit failure for batch %d. Cause: %w", ec.currentBatch.SeqNo(), err)
return gethutil.EmptyHash, fmt.Errorf("commit failure for batch %d. Cause: %w", ec.currentBatch.SeqNo(), err)
}
trieDB := executor.storage.TrieDB()
err = trieDB.Commit(h, false)
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/enclave_admin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (e *enclaveAdminService) getNodeType(ctx context.Context) common.NodeType {
id := e.enclaveKeyService.EnclaveID()
attestedEnclave, err := e.storage.GetEnclavePubKey(ctx, id)
if err != nil {
e.logger.Warn("could not read enclave pub key. Defaulting to validator type", log.ErrKey, err)
e.logger.Info("could not read enclave pub key. Defaulting to validator type", log.ErrKey, err)
return common.Validator
}
return attestedEnclave.Type
Expand Down
6 changes: 4 additions & 2 deletions go/enclave/rpc/GetTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/big"

"github.com/ten-protocol/go-ten/go/common/gethutil"

"github.com/ten-protocol/go-ten/go/enclave/storage"

"github.com/ten-protocol/go-ten/go/common/log"
Expand Down Expand Up @@ -121,7 +123,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash gethcommon.Hash, blockNu
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
}
if blockHash != (gethcommon.Hash{}) {
if blockHash != gethutil.EmptyHash {
result.BlockHash = &blockHash
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
result.TransactionIndex = (*hexutil.Uint64)(&index)
Expand All @@ -138,7 +140,7 @@ func newRPCTransaction(tx *types.Transaction, blockHash gethcommon.Hash, blockNu
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
// if the transaction has been mined, compute the effective gas price
if baseFee != nil && blockHash != (gethcommon.Hash{}) {
if baseFee != nil && blockHash != gethutil.EmptyHash {
// price = min(tip, gasFeeCap - baseFee) + baseFee
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
result.GasPrice = (*hexutil.Big)(price)
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (s *storageImpl) GetEnclavePubKey(ctx context.Context, enclaveId common.Enc
return s.cachingService.ReadEnclavePubKey(ctx, enclaveId, func() (*AttestedEnclave, error) {
key, nodeType, err := enclavedb.FetchAttestation(ctx, s.db.GetSQLDB(), enclaveId)
if err != nil {
return nil, fmt.Errorf("could not retrieve attestation key for address %s. Cause: %w", enclaveId, err)
return nil, fmt.Errorf("could not retrieve attestation key for enclave %s. Cause: %w", enclaveId, err)
}

publicKey, err := gethcrypto.DecompressPubkey(key)
Expand Down
Loading
Loading