Skip to content

Commit

Permalink
very wip and very messy
Browse files Browse the repository at this point in the history
need to remove blockchain.RegisterTypesToEncoder from
recalculateBloommFilters migration since we're changing the core
receipt type. Migrations should probably avoid using abstractions higher
than raw txns and iterators since types will change later on. Ideally we
never "refactor" migrations.
  • Loading branch information
joshklop committed Jul 9, 2023
1 parent 0209b96 commit b47227a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
12 changes: 12 additions & 0 deletions adapters/feeder2core/feeder2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,25 @@ func AdaptTransactionReceipt(response *feeder.TransactionReceipt) *core.Transact
l2ToL1Messages[i] = AdaptL2ToL1Message(msg)
}

var es core.ExecutionStatus
switch response.ExecutionStatus {
case feeder.TxnSucceeded:
es = core.TxnSuccess
case feeder.TxnReverted:
es = core.TxnFailure
default:
// Should not happen.
panic("unknown execution status")
}

return &core.TransactionReceipt{
Fee: response.ActualFee,
TransactionHash: response.TransactionHash,
Events: events,
ExecutionResources: AdaptExecutionResources(response.ExecutionResources),
L1ToL2Message: AdaptL1ToL2Message(response.L1ToL2Message),
L2ToL1Message: l2ToL1Messages,
ExecutionStatus: es,
}
}

Expand Down
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func BlockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {
return nil, err
}

block.Receipts, err = receiptsByBlockNumber(txn, number)
block.Receipts, err = ReceiptsByBlockNumber(txn, number)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -479,7 +479,7 @@ func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transa
return txs, nil
}

func receiptsByBlockNumber(txn db.Transaction, number uint64) ([]*core.TransactionReceipt, error) {
func ReceiptsByBlockNumber(txn db.Transaction, number uint64) ([]*core.TransactionReceipt, error) {
iterator, err := txn.NewIterator()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion blockchain/event_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (e *EventFilter) Events(cToken *ContinuationToken, chunkSize uint64) ([]*Fi

var receipts []*core.TransactionReceipt
if curBlock != latest+1 {
receipts, err = receiptsByBlockNumber(e.txn, header.Number)
receipts, err = ReceiptsByBlockNumber(e.txn, header.Number)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"github.com/ethereum/go-ethereum/common"
)

type ExecutionStatus uint8

const (
TxnSuccess ExecutionStatus = iota + 1
TxnFailure
)

type Event struct {
Data []*felt.Felt
From *felt.Felt
Expand Down Expand Up @@ -57,6 +64,7 @@ type TransactionReceipt struct {
L1ToL2Message *L1ToL2Message
L2ToL1Message []*L2ToL1Message
TransactionHash *felt.Felt
ExecutionStatus ExecutionStatus
}

type Transaction interface {
Expand Down
66 changes: 66 additions & 0 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var revisions = []revision{
relocateContractStorageRootKeys,
recalculateBloomFilters,
changeTrieNodeEncoding,
addExecutionStatusSucceeded,
}

var ErrCallWithNewTransaction = errors.New("call with new transaction")
Expand Down Expand Up @@ -266,3 +267,68 @@ func changeTrieNodeEncoding(txn db.Transaction) error {
}
return iterator.Close()
}

// addExecutionStatusSuccess adds the ExecutionStatus field to transaction receipts, which
// was added in Starknet v0.12.1. No failed transactions were included before 0.12.1.
func addExecutionStatusSucceeded(txn db.Transaction) error {
type oldReceipt struct {
Fee *felt.Felt
Events []*core.Event
ExecutionResources *core.ExecutionResources
L1ToL2Message *core.L1ToL2Message
L2ToL1Message []*core.L2ToL1Message
TransactionHash *felt.Felt
ExecutionStatus core.ExecutionStatus
}

blockchain.RegisterCoreTypesToEncoder()
it, err := txn.NewIterator()
if err != nil {
return err
}

for blockNumber := uint64(0); ; blockNumber++ {
receipts, err := blockchain.ReceiptsByBlockNumber(txn, blockNumber)

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / lint

receipts declared and not used) (typecheck)

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / lint

receipts declared and not used (typecheck)

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / lint

receipts declared and not used) (typecheck)

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.20.3, ubuntu-latest)

receipts declared and not used

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / run_smoke_tests

receipts declared and not used

Check failure on line 291 in migration/migration.go

View workflow job for this annotation

GitHub Actions / Run Tests (1.20.3, macOS-latest)

receipts declared and not used
if err != nil {
if errors.Is(err, db.ErrKeyNotFound) {
return nil
}
return err
}
}

prefix := db.ReceiptsByBlockNumberAndIndex.Key()
var receipt *core.TransactionReceipt
for it.Seek(prefix); it.Valid(); it.Next() {
// Obtain pre-0.12.1 receipt from DB

key := it.Key()
if !bytes.HasPrefix(key, prefix) {
break
}

val, err := it.Value()
if err != nil {
return db.CloseAndWrapOnError(it.Close, err)
}

// Set the execution status and store in DB

if err := encoder.Unmarshal(val, &receipt); err != nil {
return db.CloseAndWrapOnError(it.Close, err)
}

receipt.ExecutionStatus = core.TxnSuccess

receiptBytes, err := encoder.Marshal(receipt)
if err != nil {
return db.CloseAndWrapOnError(it.Close, err)
}

if err := txn.Set(key, receiptBytes); err != nil {
return db.CloseAndWrapOnError(it.Close, err)
}
}

return nil
}
34 changes: 34 additions & 0 deletions migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,37 @@ func TestChangeTrieNodeEncoding(t *testing.T) {
return nil
}))
}

func TestAddExecutionStatusSucceeded(t *testing.T) {
testdb := pebble.NewMemTest()
t.Cleanup(func() {
require.NoError(t, testdb.Close())
})

type transactionReceipt struct {
Fee *felt.Felt
Events []*core.Event
ExecutionResources *core.ExecutionResources
L1ToL2Message *core.L1ToL2Message
L2ToL1Message []*core.L2ToL1Message
TransactionHash *felt.Felt
}

tx := testdb.NewTransaction(true)
receipt := new(transactionReceipt)
receiptBytes, err := encoder.Marshal(receipt)
require.NoError(t, err)
key := db.ReceiptsByBlockNumberAndIndex.Key([]byte{0}, []byte{0})
err = tx.Set(key, receiptBytes)
require.NoError(t, err)

require.NoError(t, addExecutionStatusSucceeded(tx))

tx.Get(key, func(val []byte) error {
var newReceipt *core.TransactionReceipt
err = encoder.Unmarshal(val, &newReceipt)
require.NoError(t, err)
assert.Equal(t, newReceipt.ExecutionStatus, core.TxnSuccess)
return nil
})
}

0 comments on commit b47227a

Please sign in to comment.