Skip to content

Commit

Permalink
fix nil txs with failed receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Nov 29, 2024
1 parent 48ac699 commit d97469f
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 110 deletions.
6 changes: 3 additions & 3 deletions go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package host

import (
"context"
"github.com/ten-protocol/go-ten/go/common/l1"
"github.com/ten-protocol/go-ten/go/ethadapter"

Check failure on line 5 in go/common/host/services.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"math/big"

"github.com/ten-protocol/go-ten/go/responses"
Expand Down Expand Up @@ -104,9 +104,9 @@ type L1Publisher interface {
// RequestSecret will send a management contract transaction to request a secret from the enclave, returning the L1 head at time of sending
RequestSecret(report *common.AttestationReport) (gethcommon.Hash, error)
// ExtractRelevantTenTransactions will return all TEN relevant tx from an L1 block
ExtractRelevantTenTransactions(block *types.Block, receipts types.Receipts) ([]*common.TxAndReceiptAndBlobs, []*l1.L1RollupTx, []*l1.L1SetImportantContractsTx)
ExtractRelevantTenTransactions(block *types.Block, receipts types.Receipts) ([]*common.TxAndReceiptAndBlobs, []*ethadapter.L1RollupTx, []*ethadapter.L1SetImportantContractsTx)
// FindSecretResponseTx will return the secret response tx from an L1 block
FindSecretResponseTx(block *types.Block) []*l1.L1RespondSecretTx
FindSecretResponseTx(block *types.Block) []*ethadapter.L1RespondSecretTx
// PublishRollup will create and publish a rollup tx to the management contract - fire and forget we don't wait for receipt
// todo (#1624) - With a single sequencer, it is problematic if rollup publication fails; handle this case better
PublishRollup(producedRollup *common.ExtRollup)
Expand Down
41 changes: 39 additions & 2 deletions go/common/l1_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
// easily by TEN.
type TenTransaction interface{}

type L1TxType int
// L1TxType represents different types of L1 transactions
type L1TxType uint8 // Change to uint8 for RLP serialization

const (
RollupTx L1TxType = iota
Expand All @@ -21,10 +22,16 @@ const (
SetImportantContractsTx
)

// L1Event represents a single event type and its associated transactions
type L1Event struct {
Type uint8 // Change to uint8 for RLP serialization
Txs []*L1TxData
}

// ProcessedL1Data is submitted to the enclave by the guardian
type ProcessedL1Data struct {
BlockHeader *types.Header
Events map[L1TxType][]*L1TxData
Events []L1Event // Changed from map to slice of L1Event
}

// L1TxData represents an L1 transaction that's relevant to us
Expand All @@ -36,3 +43,33 @@ type L1TxData struct {
CrossChainMessages *CrossChainMessages // Only populated for xchain messages
ValueTransfers *ValueTransferEvents // Only populated for xchain transfers
}

// helper methods as we can't serialize a map
func (p *ProcessedL1Data) AddEvent(txType L1TxType, tx *L1TxData) {
for i := range p.Events {
if p.Events[i].Type == uint8(txType) {
p.Events[i].Txs = append(p.Events[i].Txs, tx)
return
}
}
p.Events = append(p.Events, L1Event{
Type: uint8(txType), // Convert to uint8 when storing
Txs: []*L1TxData{tx},
})
}

func (p *ProcessedL1Data) GetEvents(txType L1TxType) []*L1TxData {
if p == nil || len(p.Events) == 0 {
return nil
}

for _, event := range p.Events {
if event.Type == uint8(txType) {
if event.Txs == nil {
return nil
}
return event.Txs
}
}
return nil
}
30 changes: 0 additions & 30 deletions go/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package common

import (
"fmt"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ten-protocol/go-ten/go/common/l1"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -190,31 +188,3 @@ func (cf *ChainFork) String() string {
func MaskedSender(address L2Address) L2Address {
return common.BigToAddress(big.NewInt(0).Sub(address.Big(), big.NewInt(1)))
}

type L1TxType int

const (
RollupTx L1TxType = iota
SecretRequestTx
InitialiseSecretTx
CrossChainMessageTx
CrossChainValueTranserTx
SequencerAddedTx
SetImportantContractsTx
)

// ProcessedL1Data is submitted to the enclave by the guardian
type ProcessedL1Data struct {
BlockHeader *types.Header
Events map[L1TxType][]*L1TxData
}

// L1TxData represents an L1 transaction that's relevant to us
type L1TxData struct {
Type l1.L1Transaction
Transaction *types.Transaction
Receipt *types.Receipt
Blobs []*kzg4844.Blob // Only populated for blob transactions
CrossChainMessages *CrossChainMessages // Only populated for xchain messages
ValueTransfers *ValueTransferEvents // Only populated for xchain transfers
}
2 changes: 1 addition & 1 deletion go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (rc *rollupConsumerImpl) getSignedRollup(rollups []*common.ExtRollup) ([]*c
// If a transaction is not a rollup or fails verification, it's skipped
// The function only returns an error if there's a critical failure in rollup reconstruction
func (rc *rollupConsumerImpl) extractAndVerifyRollups(processed *common.ProcessedL1Data) ([]*common.ExtRollup, error) {
rollupTxs := processed.Events[common.RollupTx]
rollupTxs := processed.GetEvents(common.RollupTx)
rollups := make([]*common.ExtRollup, 0, len(rollupTxs))

blobs, blobHashes, err := rc.extractBlobsAndHashes(rollupTxs)
Expand Down
4 changes: 2 additions & 2 deletions go/enclave/components/shared_secret_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (ssp *SharedSecretProcessor) ProcessNetworkSecretMsgs(ctx context.Context,
block := processed.BlockHeader

// process secret requests
for _, txData := range processed.Events[common.SecretRequestTx] {
for _, txData := range processed.GetEvents(common.SecretRequestTx) {
scrtReqTx, ok := txData.Type.(*ethadapter.L1RequestSecretTx)
if !ok {
continue
Expand All @@ -58,7 +58,7 @@ func (ssp *SharedSecretProcessor) ProcessNetworkSecretMsgs(ctx context.Context,
}

// process initialize secret events
for _, txData := range processed.Events[common.InitialiseSecretTx] {
for _, txData := range processed.GetEvents(common.InitialiseSecretTx) {
initSecretTx, ok := txData.Type.(*ethadapter.L1InitializeSecretTx)
if !ok {
continue
Expand Down
4 changes: 2 additions & 2 deletions go/enclave/crosschain/block_message_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m *blockMessageExtractor) StoreCrossChainValueTransfers(ctx context.Contex

// collect all value transfer events from processed data
var transfers common.ValueTransferEvents
for _, txData := range processedData.Events[common.CrossChainValueTranserTx] {
for _, txData := range processedData.GetEvents(common.CrossChainValueTranserTx) {
if txData.ValueTransfers != nil {
transfers = append(transfers, *txData.ValueTransfers...)
}
Expand All @@ -70,7 +70,7 @@ func (m *blockMessageExtractor) StoreCrossChainMessages(ctx context.Context, blo
// collect all messages from the events
var xchain common.CrossChainMessages
var receipts types.Receipts
for _, txData := range processedData.Events[common.CrossChainMessageTx] {
for _, txData := range processedData.GetEvents(common.CrossChainMessageTx) {
if txData.CrossChainMessages != nil {
xchain = append(xchain, *txData.CrossChainMessages...)
receipts = append(receipts, txData.Receipt)
Expand Down
7 changes: 4 additions & 3 deletions go/enclave/enclave_admin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/params"
"github.com/ten-protocol/go-ten/go/enclave/txpool"
"github.com/ten-protocol/go-ten/go/ethadapter/mgmtcontractlib"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/params"
"github.com/ten-protocol/go-ten/go/enclave/txpool"
"github.com/ten-protocol/go-ten/go/ethadapter/mgmtcontractlib"

gethcommon "github.com/ethereum/go-ethereum/common"
enclaveconfig "github.com/ten-protocol/go-ten/go/enclave/config"

Expand Down
3 changes: 2 additions & 1 deletion go/ethadapter/erc20contractlib/erc20_contract_lib.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package erc20contractlib

import (
"github.com/ten-protocol/go-ten/go/common"
"math/big"
"strings"

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

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/ethadapter"
Expand Down
33 changes: 0 additions & 33 deletions go/ethadapter/l1_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

//// L1Transaction is an abstraction that transforms an Ethereum transaction into a format that can be consumed more
//// easily by TEN.
//type L1Transaction interface{}

type L1RollupTx struct {
Rollup common.EncodedRollup
}
Expand Down Expand Up @@ -76,32 +72,3 @@ type L1InitializeSecretTx struct {
InitialSecret []byte
Attestation common.EncodedAttestationReport
}

//
//type L1TxType int
//
//const (
// RollupTx L1TxType = iota
// SecretRequestTx
// InitialiseSecretTx
// CrossChainMessageTx
// CrossChainValueTranserTx
// SequencerAddedTx
// SetImportantContractsTx
//)
//
//// ProcessedL1Data is submitted to the enclave by the guardian
//type ProcessedL1Data struct {
// BlockHeader *types.Header
// Events map[L1TxType][]*L1TxData
//}
//
//// L1TxData represents an L1 transaction that's relevant to us
//type L1TxData struct {
// Type *L1Transaction
// Transaction *types.Transaction
// Receipt *types.Receipt
// Blobs []*kzg4844.Blob // Only populated for blob transactions
// CrossChainMessages *common.CrossChainMessages // Only populated for xchain messages
// ValueTransfers *common.ValueTransferEvents // Only populated for xchain transfers
//}
3 changes: 2 additions & 1 deletion go/host/container/host_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package container

import (
"fmt"
gethcommon "github.com/ethereum/go-ethereum/common"
"net/http"
"time"

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

"github.com/ten-protocol/go-ten/lib/gethfork/node"

"github.com/ten-protocol/go-ten/go/host/l1"
Expand Down
38 changes: 17 additions & 21 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (g *Guardian) submitL1Block(block *common.L1Block, isLatest bool) (bool, er
processedData, err := g.sl.L1Repo().ExtractTenTransactions(block)
if err != nil {
g.submitDataLock.Unlock() // lock must be released before returning
return false, fmt.Errorf("could not fetch obscuro receipts for block=%s - %w", block.Hash(), err)
return false, fmt.Errorf("could not extract ten transactions for block=%s - %w", block.Hash(), err)
}
rollupTxs, contractAddressTxs := g.getRollupsAndContractAddrTxs(*processedData)

Expand Down Expand Up @@ -759,30 +759,26 @@ func (g *Guardian) getRollupsAndContractAddrTxs(data common.ProcessedL1Data) ([]
rollupTxs := make([]*ethadapter.L1RollupTx, 0)
contractAddressTxs := make([]*ethadapter.L1SetImportantContractsTx, 0)

for !g.hostInterrupter.IsStopping() {
// Get rollup transactions
for _, event := range data.Events[common.RollupTx] {
encodedRlp, err := ethadapter.DecodeBlobs(event.Blobs)
if err != nil {
g.logger.Crit("could not decode blobs.", log.ErrKey, err)
continue
}

rlp := &ethadapter.L1RollupTx{
Rollup: encodedRlp,
}
rollupTxs = append(rollupTxs, rlp)
for _, event := range data.GetEvents(common.RollupTx) {
encodedRlp, err := ethadapter.DecodeBlobs(event.Blobs)
if err != nil {
g.logger.Crit("could not decode blobs.", log.ErrKey, err)
continue
}

// Get contract address transactions
for _, event := range data.Events[common.SetImportantContractsTx] {
if contractTx, ok := event.Type.(*ethadapter.L1SetImportantContractsTx); ok {
contractAddressTxs = append(contractAddressTxs, contractTx)
} else {
g.logger.Warn("Unexpected type for SetImportantContractsTx event", "type", fmt.Sprintf("%T", event.Type))
}
rlp := &ethadapter.L1RollupTx{
Rollup: encodedRlp,
}
rollupTxs = append(rollupTxs, rlp)
}

// Get contract address transactions
for _, event := range data.GetEvents(common.SetImportantContractsTx) {
if contractTx, ok := event.Type.(*ethadapter.L1SetImportantContractsTx); ok {
contractAddressTxs = append(contractAddressTxs, contractTx)
} else {
g.logger.Warn("Unexpected type for SetImportantContractsTx event", "type", fmt.Sprintf("%T", event.Type))
}
}
return rollupTxs, contractAddressTxs
}
27 changes: 17 additions & 10 deletions go/host/l1/blockrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,19 @@ func (r *Repository) FetchObscuroReceipts(block *common.L1Block) (types.Receipts
func (r *Repository) ExtractTenTransactions(block *common.L1Block) (*common.ProcessedL1Data, error) {
processed := &common.ProcessedL1Data{
BlockHeader: block.Header(),
Events: make(map[common.L1TxType][]*common.L1TxData),
Events: []common.L1Event{},
}
txsWithReceipts, err := r.getRelevantTxReceiptsAndBlobs(block)
if err != nil {
return nil, err
}

for _, txWithReceipt := range txsWithReceipts {
// Skip if the entire txWithReceipt is nil
if txWithReceipt == nil {
println("TX IS NIL WHY")
continue
}
messages, err := r.getCrossChainMessages(txWithReceipt.Receipt)
if err != nil {
r.logger.Error("Error encountered converting the extracted relevant logs to messages", log.ErrKey, err)
Expand All @@ -230,19 +235,19 @@ func (r *Repository) ExtractTenTransactions(block *common.L1Block) (*common.Proc
}

if len(*txData.CrossChainMessages) > 0 {
processed.Events[common.CrossChainMessageTx] = append(processed.Events[common.CrossChainMessageTx], txData)
processed.AddEvent(common.CrossChainMessageTx, txData)
}

if len(*txData.ValueTransfers) > 0 {
processed.Events[common.CrossChainValueTranserTx] = append(processed.Events[common.CrossChainValueTranserTx], txData)
processed.AddEvent(common.CrossChainValueTranserTx, txData)
}

if len(txData.Blobs) > 0 {
processed.Events[common.RollupTx] = append(processed.Events[common.RollupTx], txData)
processed.AddEvent(common.RollupTx, txData)
}

if len(sequencerLogs) > 0 {
processed.Events[common.SequencerAddedTx] = append(processed.Events[common.SequencerAddedTx], txData)
processed.AddEvent(common.SequencerAddedTx, txData)
}

decodedTx := r.mgmtContractLib.DecodeTx(txWithReceipt.Tx)
Expand All @@ -253,11 +258,11 @@ func (r *Repository) ExtractTenTransactions(block *common.L1Block) (*common.Proc

switch decodedTx.(type) {
case *ethadapter.L1RequestSecretTx:
processed.Events[common.SecretRequestTx] = append(processed.Events[common.SecretRequestTx], txData)
processed.AddEvent(common.SecretRequestTx, txData)
case *ethadapter.L1InitializeSecretTx:
processed.Events[common.InitialiseSecretTx] = append(processed.Events[common.InitialiseSecretTx], txData)
processed.AddEvent(common.InitialiseSecretTx, txData)
case *ethadapter.L1SetImportantContractsTx:
processed.Events[common.SetImportantContractsTx] = append(processed.Events[common.SetImportantContractsTx], txData)
processed.AddEvent(common.SetImportantContractsTx, txData)
}
}

Expand Down Expand Up @@ -332,7 +337,8 @@ func (r *Repository) isObscuroTransaction(transaction *types.Transaction) bool {
}

func (r *Repository) getRelevantTxReceiptsAndBlobs(block *common.L1Block) ([]*common.TxAndReceiptAndBlobs, error) {
txsWithReceipts := make([]*common.TxAndReceiptAndBlobs, len(block.Transactions()))
// Create a slice that will only contain valid transactions
var txsWithReceipts []*common.TxAndReceiptAndBlobs

receipts, err := r.FetchObscuroReceipts(block)
if err != nil {
Expand Down Expand Up @@ -364,7 +370,8 @@ func (r *Repository) getRelevantTxReceiptsAndBlobs(block *common.L1Block) ([]*co
txWithReceipt.Blobs = blobs
}

txsWithReceipts[i] = txWithReceipt
// Append only valid transactions
txsWithReceipts = append(txsWithReceipts, txWithReceipt)
}

return txsWithReceipts, nil
Expand Down
Loading

0 comments on commit d97469f

Please sign in to comment.