Skip to content

Commit

Permalink
todo - fix duplicate rollups in validate chain map
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Aug 14, 2024
1 parent 10b4673 commit 236afda
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type L1BlockHandler interface {
HandleBlock(block *types.Block)
}

// L1Publisher provides an interface for the host to interact with Obscuro data (management contract etc.) on L1
// L1Publisher provides an interface for the host to interact with Ten data (management contract etc.) on L1
type L1Publisher interface {
// InitializeSecret will send a management contract transaction to initialize the network with the generated secret
InitializeSecret(attestation *common.AttestationReport, encSecret common.EncryptedSharedEnclaveSecret) error
Expand Down
4 changes: 1 addition & 3 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,11 @@ func NewEnclave(
gasOracle := gas.NewGasOracle()
blockProcessor := components.NewBlockProcessor(storage, crossChainProcessors, gasOracle, logger)
registry := components.NewBatchRegistry(storage, logger)
httpClient := new(http.Client)
//FIXME put in config
baseURL := "http://localhost:3500"
//baseURL := "http://localhost:16550"
//baseURL := "https://sepolia-beacon.chainstacklabs.com"
beaconClient := ethadapter.NewL1BeaconClient(ethadapter.NewBeaconHTTPClient(httpClient, baseURL))
blobResolver := components.NewBeaconBlobResolver(beaconClient)
blobResolver := components.NewBeaconBlobResolver(ethadapter.NewL1BeaconClient(ethadapter.NewBeaconHTTPClient(new(http.Client), baseURL)))
batchExecutor := components.NewBatchExecutor(storage, registry, *config, gethEncodingService, crossChainProcessors, genesis, gasOracle, chainConfig, config.GasBatchExecutionLimit, logger)
sigVerifier, err := components.NewSignatureValidator(storage)
rProducer := components.NewRollupProducer(enclaveKey.EnclaveID(), storage, registry, logger)
Expand Down
2 changes: 1 addition & 1 deletion go/enclave/nodetype/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ExportCrossChainData(ctx context.Context, storage storage.Storage, fromSeqN
return nil, errutil.ErrCrossChainBundleNoBatches
}

// todo - siliev - all those fetches need to be atomic
//todo - siliev - all those fetches need to be atomic
header, err := storage.FetchHeadBatchHeader(ctx)
if err != nil {
return nil, err
Expand Down
25 changes: 25 additions & 0 deletions go/ethadapter/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package ethadapter
import (
"bytes"
"fmt"

Check failure on line 5 in go/ethadapter/blob.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ten-protocol/go-ten/go/common"
)

// The number of bits in a BLS scalar that aren't part of a whole byte.
Expand Down Expand Up @@ -569,6 +571,16 @@ const excessBlobBits = 6 // = math.floor(math.log2(BLS_MODULUS)) % 8
// Hash common.Hash // hash of the blob, used for consistency checks
//}

// ToIndexedBlobHashes is needed as the beacon API has an optional indices parameter that allows us to specify which blob
// index to retrieve from a given block
func ToIndexedBlobHashes(hs ...gethcommon.Hash) []IndexedBlobHash {
hashes := make([]IndexedBlobHash, 0, len(hs))
for i, hash := range hs {
hashes = append(hashes, IndexedBlobHash{Index: uint64(i), Hash: hash})
}
return hashes
}

// EncodeBlobs takes converts bytes into blobs used for KZG commitment EIP-4844
// transactions on Ethereum.
func EncodeBlobs(data []byte) ([]kzg4844.Blob, error) {
Expand Down Expand Up @@ -650,3 +662,16 @@ func DecodeBlobs(blobs []*kzg4844.Blob) ([]byte, error) {
err := rlp.Decode(bytes.NewReader(rlpData), &outputData)
return outputData, err
}

// ReconstructRollup decodes and returns the ExtRollup in the blob
func ReconstructRollup(blobs []*kzg4844.Blob) (*common.ExtRollup, error) {
data, err := DecodeBlobs(blobs)
if err != nil {
fmt.Println("Error decoding rollup blob:", err)
}
var rollup common.ExtRollup
if err := rlp.DecodeBytes(data, &rollup); err != nil {
return nil, fmt.Errorf("could not decode rollup. Cause: %w", err)
}
return &rollup, nil
}
13 changes: 1 addition & 12 deletions go/ethadapter/mgmtcontractlib/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ func (c *contractLibImpl) DecodeTx(tx *types.Transaction) ethadapter.L1Transacti
switch method.Name {
case AddRollupMethod:
if tx.Type() == types.BlobTxType {
blobHashes := toIndexedBlobHashes(tx.BlobHashes()...)
blobHashes := ethadapter.ToIndexedBlobHashes(tx.BlobHashes()...)

return &ethadapter.L1RollupHashes{
BlobHashes: blobHashes,
}
} else {
println("NONE BLOB ROLLUP FOUND !!")
return nil
}
case RespondSecretMethod:
Expand Down Expand Up @@ -577,13 +576,3 @@ func makeSidecar(blobs []kzg4844.Blob) (*types.BlobTxSidecar, []gethcommon.Hash,
}
return sidecar, blobHashes, nil
}

// toIndexedBlobHashes is needed as the beacon API has an optional indices parameter that allows us to specify which blob
// index to retrieve from a given block
func toIndexedBlobHashes(hs ...gethcommon.Hash) []ethadapter.IndexedBlobHash {
hashes := make([]ethadapter.IndexedBlobHash, 0, len(hs))
for i, hash := range hs {
hashes = append(hashes, ethadapter.IndexedBlobHash{Index: uint64(i), Hash: hash})
}
return hashes
}
8 changes: 8 additions & 0 deletions go/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/ten-protocol/go-ten/go/enclave/components"
"net/http"

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

Expand Down Expand Up @@ -96,6 +98,11 @@ func NewHost(config *config.HostConfig, hostServices *ServicesRegistry, p2p host
enclService := enclave.NewService(hostIdentity, hostServices, enclGuardians, logger)
l2Repo := l2.NewBatchRepository(config, hostServices, hostStorage, logger)
subsService := events.NewLogEventManager(hostServices, logger)
//FIXME from config
baseURL := "http://localhost:3500"
//baseURL := "http://localhost:16550"
//baseURL := "https://sepolia-beacon.chainstacklabs.com"
blobResolver := components.NewBeaconBlobResolver(ethadapter.NewL1BeaconClient(ethadapter.NewBeaconHTTPClient(new(http.Client), baseURL)))

l2Repo.SubscribeValidatedBatches(batchListener{newHeads: host.newHeads})
hostServices.RegisterService(hostcommon.P2PName, p2p)
Expand All @@ -108,6 +115,7 @@ func NewHost(config *config.HostConfig, hostServices *ServicesRegistry, p2p host
ethClient,
mgmtContractLib,
l1Repo,
blobResolver,
host.stopControl,
logger,
maxWaitForL1Receipt,
Expand Down
29 changes: 24 additions & 5 deletions go/host/l1/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/ten-protocol/go-ten/go/enclave/components"
"math/big"
"strings"
"sync"
Expand Down Expand Up @@ -34,6 +35,7 @@ type Publisher struct {
ethClient ethadapter.EthClient
mgmtContractLib mgmtcontractlib.MgmtContractLib // Library to handle Management Contract lib operations
storage storage.Storage
blobResolver components.BlobResolver

// cached map of important contract addresses (updated when we see a SetImportantContractsTx)
importantContractAddresses map[string]gethcommon.Address
Expand Down Expand Up @@ -61,6 +63,7 @@ func NewL1Publisher(
client ethadapter.EthClient,
mgmtContract mgmtcontractlib.MgmtContractLib,
repository host.L1BlockRepository,
blobResolver components.BlobResolver,
hostStopper *stopcontrol.StopControl,
logger gethlog.Logger,
maxWaitForL1Receipt time.Duration,
Expand All @@ -74,6 +77,7 @@ func NewL1Publisher(
ethClient: client,
mgmtContractLib: mgmtContract,
repository: repository,
blobResolver: blobResolver,
hostStopper: hostStopper,
logger: logger,
maxWaitForL1Receipt: maxWaitForL1Receipt,
Expand Down Expand Up @@ -222,7 +226,7 @@ func (p *Publisher) PublishSecretResponse(secretResponse *common.ProducedSecretR
return nil
}

// ExtractObscuroRelevantTransactions will extract any transactions from the block that are relevant to obscuro
// ExtractObscuroRelevantTransactions will extract any transactions from the block that are relevant to Ten
// todo (#2495) we should monitor for relevant L1 events instead of scanning every transaction in the block
func (p *Publisher) ExtractObscuroRelevantTransactions(block *types.Block) ([]*ethadapter.L1RespondSecretTx, []*ethadapter.L1RollupTx, []*ethadapter.L1SetImportantContractsTx) {
var secretRespTxs []*ethadapter.L1RespondSecretTx
Expand All @@ -237,14 +241,29 @@ func (p *Publisher) ExtractObscuroRelevantTransactions(block *types.Block) ([]*e
secretRespTxs = append(secretRespTxs, scrtTx)
continue
}
if rollupTx, ok := t.(*ethadapter.L1RollupTx); ok {
rollupTxs = append(rollupTxs, rollupTx)
continue
}

if contractAddressTx, ok := t.(*ethadapter.L1SetImportantContractsTx); ok {
contractAddressTxs = append(contractAddressTxs, contractAddressTx)
continue
}
rollupHashes, ok := t.(*ethadapter.L1RollupHashes)
if !ok {
continue
}

blobs, err := p.blobResolver.FetchBlobs(p.sendingContext, block.Header(), rollupHashes.BlobHashes)
if err != nil {
p.logger.Crit("could not fetch blobs.", log.ErrKey, err)
return nil, nil, nil
}
encodedRlp, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
p.logger.Crit("could not decode blobs.", log.ErrKey, err)
}
rlp := &ethadapter.L1RollupTx{
Rollup: encodedRlp,
}
rollupTxs = append(rollupTxs, rlp)
}
return secretRespTxs, rollupTxs, contractAddressTxs
}
Expand Down
5 changes: 0 additions & 5 deletions integration/ethereummock/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func (m *mockContractLib) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa
if err != nil {
return nil, fmt.Errorf("failed to convert rollup to blobs: %w", err)
}
//maxBlobSize := 128 * 1024 // 128KB in bytes TODO move to config
//base64ChunkSize := int(math.Floor(float64(maxBlobSize) * 4 / 3))
//base64ChunkSize = base64ChunkSize - (base64ChunkSize % 4) - 4 //metadata size
//
//blobs, _ := chunkRollup(encRollupData, base64ChunkSize)

var blobHashes []gethcommon.Hash
var sidecar *types.BlobTxSidecar
Expand Down
26 changes: 24 additions & 2 deletions integration/simulation/validate_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package simulation
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ten-protocol/go-ten/go/enclave/components"
"math/big"
"net/http"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -279,8 +282,8 @@ func ExtractDataFromEthereumChain(
deposits = append(deposits, tx.Hash())
totalDeposited.Add(totalDeposited, l1tx.Amount)
successfulDeposits++
case *ethadapter.L1RollupTx:
r, err := common.DecodeRollup(l1tx.Rollup)
case *ethadapter.L1RollupHashes:
r, err := getRollupFromBlobHashes(s.ctx, block, l1tx.BlobHashes)
if err != nil {
testlog.Logger().Crit("could not decode rollup. ", log.ErrKey, err)
}
Expand Down Expand Up @@ -848,3 +851,22 @@ func checkBatchFromTxs(t *testing.T, client rpc.Client, txHash gethcommon.Hash,
t.Errorf("node %d: retrieved batch by hash, but hash was incorrect", nodeIdx)
}
}

func getRollupFromBlobHashes(ctx context.Context, block *types.Block, blobHashes []ethadapter.IndexedBlobHash) (*common.ExtRollup, error) {
//FIXME from config
baseURL := "http://localhost:3500"
blobResolver := components.NewBeaconBlobResolver(ethadapter.NewL1BeaconClient(ethadapter.NewBeaconHTTPClient(new(http.Client), baseURL)))
blobs, err := blobResolver.FetchBlobs(ctx, block.Header(), blobHashes)
if err != nil {
return nil, fmt.Errorf("could not fetch blobs from hashes during chain validation")
}
data, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
fmt.Println("Error decoding rollup blob:", err)
}
var rollup common.ExtRollup
if err := rlp.DecodeBytes(data, &rollup); err != nil {
return nil, fmt.Errorf("could not decode rollup. Cause: %w", err)
}
return &rollup, nil
}

0 comments on commit 236afda

Please sign in to comment.