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

Add a boolean field to tx receipt object indicating if the tx was timeboosted #369

Merged
merged 11 commits into from
Jan 29, 2025
Merged
5 changes: 5 additions & 0 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type SyncProgressBackend interface {
SyncProgressMap() map[string]interface{}
SafeBlockNumber(ctx context.Context) (uint64, error)
FinalizedBlockNumber(ctx context.Context) (uint64, error)
BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error)
}

func createRegisterAPIBackend(backend *Backend, filterConfig filters.Config, fallbackClientUrl string, fallbackClientTimeout time.Duration) (*filters.FilterSystem, error) {
Expand Down Expand Up @@ -460,6 +461,10 @@ func (a *APIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.
return nil, errors.New("invalid arguments; neither block nor hash specified")
}

func (a *APIBackend) BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error) {
return a.sync.BlockMetadataByNumber(blockNum)
}

func StateAndHeaderFromHeader(ctx context.Context, chainDb ethdb.Database, bc *core.BlockChain, maxRecreateStateDepth int64, header *types.Header, err error) (*state.StateDB, *types.Header, error) {
if err != nil {
return nil, header, err
Expand Down
21 changes: 21 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,24 @@ func (b PrettyBytes) TerminalString() string {
}
return fmt.Sprintf("%#x...%x (%dB)", b[:3], b[len(b)-3:], len(b))
}

type BlockMetadata []byte

// IsTxTimeboosted given a tx's index in the block returns whether the tx was timeboosted
// or not. The first byte of blockMetadata byte array is reserved to indicate the version,
// starting from the second byte, (N)th bit would represent if (N)th tx is timeboosted or not, 1 means yes and 0 means no
// blockMetadata[index / 8 + 1] & (1 << (index % 8)) != 0; where index = (N - 1), implies whether (N)th tx in a block is timeboosted
// note that number of txs in a block will always lag behind (len(blockMetadata) - 1) * 8 but it wont lag more than a value of 7
func (b BlockMetadata) IsTxTimeboosted(txIndex int) (bool, error) {
if len(b) == 0 {
return false, errors.New("blockMetadata is not set")
}
if txIndex < 0 {
return false, fmt.Errorf("invalid transaction index- %d, should be positive", txIndex)
}
maxTxCount := (len(b) - 1) * 8
if txIndex >= maxTxCount {
return false, nil
}
return b[1+(txIndex/8)]&(1<<(txIndex%8)) != 0, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs a comment.
8 seems like a magical number and it isn't clear how the bitwise & is checking if the transaction was timeboosted or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Currently the explanation for this exists on the sequencer side where blockMetadata originates https://github.com/OffchainLabs/nitro/blob/071aa54de1a8c2f510b5d11835bad9124eb38f37/execution/gethexec/executionengine.go#L615-L619 but having it here as well is a good idea

}
4 changes: 4 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
return nil, errors.New("invalid arguments; neither block nor hash specified")
}

func (b *EthAPIBackend) BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error) {
return nil, nil
}

func (b *EthAPIBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
return b.eth.miner.Pending()
}
Expand Down
11 changes: 11 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,17 @@ func marshalReceipt(ctx context.Context, receipt *types.Receipt, blockHash commo
fields["l1BlockNumber"] = hexutil.Uint64(arbTx.L1BlockNumber)
}
}

blockMetadata, err := backend.BlockMetadataByNumber(blockNumber)
if err != nil {
return nil, err
}
if blockMetadata != nil {
fields["timeboosted"], err = blockMetadata.IsTxTimeboosted(txIndex)
if err != nil {
log.Error("Error checking if a tx was timeboosted", "txIndex", txIndex, "txHash", tx.Hash(), "err", err)
}
}
}
return fields, nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ func (b testBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.
}
panic("unknown type rpc.BlockNumberOrHash")
}
func (b testBackend) BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error) {
return nil, nil
}
func (b testBackend) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
return b.chain.GetBlock(hash, uint64(number.Int64())).Body(), nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Backend interface {
BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error)
StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
Pending() (*types.Block, types.Receipts, *state.StateDB)
Expand Down
3 changes: 3 additions & 0 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types
func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
return nil, nil
}
func (b *backendMock) BlockMetadataByNumber(blockNum uint64) (common.BlockMetadata, error) {
return nil, nil
}
func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
return nil, nil
}
Expand Down
Loading