|
| 1 | +package synchandler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | + "github.com/jackc/pgx/v4" |
| 12 | + "github.com/jackc/pgx/v4/pgxpool" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/rs/zerolog/log" |
| 15 | + bindings "github.com/shutter-network/gnosh-contracts/gnoshcontracts/sequencer" |
| 16 | + |
| 17 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/database" |
| 18 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/metrics" |
| 19 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/chainsync/syncer" |
| 20 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/shdb" |
| 21 | +) |
| 22 | + |
| 23 | +func init() { |
| 24 | + var err error |
| 25 | + SequencerContractABI, err = bindings.SequencerMetaData.GetAbi() |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +var SequencerContractABI *abi.ABI |
| 32 | + |
| 33 | +func NewSequencerTransactionSubmitted(dbPool *pgxpool.Pool, address common.Address) (syncer.ContractEventHandler, error) { |
| 34 | + return syncer.WrapHandler( |
| 35 | + &SequencerTransactionSubmitted{ |
| 36 | + evABI: SequencerContractABI, |
| 37 | + address: address, |
| 38 | + dbPool: dbPool, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +type SequencerTransactionSubmitted struct { |
| 43 | + evABI *abi.ABI |
| 44 | + address common.Address |
| 45 | + |
| 46 | + dbPool *pgxpool.Pool |
| 47 | +} |
| 48 | + |
| 49 | +func (sts *SequencerTransactionSubmitted) Address() common.Address { |
| 50 | + return sts.address |
| 51 | +} |
| 52 | + |
| 53 | +func (*SequencerTransactionSubmitted) Event() string { |
| 54 | + return "TransactionSubmitted" |
| 55 | +} |
| 56 | + |
| 57 | +func (sts *SequencerTransactionSubmitted) ABI() abi.ABI { |
| 58 | + return *sts.evABI |
| 59 | +} |
| 60 | + |
| 61 | +func (sts *SequencerTransactionSubmitted) Accept( |
| 62 | + _ context.Context, |
| 63 | + _ types.Header, |
| 64 | + _ bindings.SequencerTransactionSubmitted, |
| 65 | +) (bool, error) { |
| 66 | + return true, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (sts *SequencerTransactionSubmitted) Handle( |
| 70 | + ctx context.Context, |
| 71 | + update syncer.ChainUpdateContext, |
| 72 | + events []bindings.SequencerTransactionSubmitted, |
| 73 | +) error { |
| 74 | + numInsertedEvents := 0 |
| 75 | + numDiscardedEvents := 0 |
| 76 | + err := sts.dbPool.BeginFunc(ctx, func(tx pgx.Tx) error { |
| 77 | + db := database.New(tx) |
| 78 | + if update.Remove != nil { |
| 79 | + for _, header := range update.Remove.Get() { |
| 80 | + if err := db.DeleteTransactionSubmittedEventsFromBlockHash(ctx, header.Hash().Bytes()); err != nil { |
| 81 | + return errors.Wrap(err, "failed to delete transaction submitted events from db") |
| 82 | + } |
| 83 | + } |
| 84 | + log.Info(). |
| 85 | + Int("depth", update.Remove.Len()). |
| 86 | + Int64("previous-synced-until", update.Remove.Latest().Number.Int64()). |
| 87 | + Int64("new-synced-until", update.Append.Latest().Number.Int64()). |
| 88 | + Msg("sync status reset due to reorg") |
| 89 | + } |
| 90 | + filteredEvents := sts.filterEvents(events) |
| 91 | + numDiscardedEvents = len(events) - len(filteredEvents) |
| 92 | + for _, event := range filteredEvents { |
| 93 | + _, err := db.InsertTransactionSubmittedEvent(ctx, database.InsertTransactionSubmittedEventParams{ |
| 94 | + Index: int64(event.TxIndex), |
| 95 | + BlockNumber: int64(event.Raw.BlockNumber), |
| 96 | + BlockHash: event.Raw.BlockHash[:], |
| 97 | + TxIndex: int64(event.Raw.TxIndex), |
| 98 | + LogIndex: int64(event.Raw.Index), |
| 99 | + Eon: int64(event.Eon), |
| 100 | + IdentityPrefix: event.IdentityPrefix[:], |
| 101 | + Sender: shdb.EncodeAddress(event.Sender), |
| 102 | + GasLimit: event.GasLimit.Int64(), |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + return errors.Wrap(err, "failed to insert transaction submitted event into db") |
| 106 | + } |
| 107 | + numInsertedEvents++ |
| 108 | + metrics.LatestTxSubmittedEventIndex.WithLabelValues(fmt.Sprint(event.Eon)).Set(float64(event.TxIndex)) |
| 109 | + log.Debug(). |
| 110 | + Uint64("index", event.TxIndex). |
| 111 | + Uint64("block", event.Raw.BlockNumber). |
| 112 | + Uint64("eon", event.Eon). |
| 113 | + Hex("identityPrefix", event.IdentityPrefix[:]). |
| 114 | + Hex("sender", event.Sender.Bytes()). |
| 115 | + Uint64("gasLimit", event.GasLimit.Uint64()). |
| 116 | + Msg("synced new transaction submitted event") |
| 117 | + } |
| 118 | + return nil |
| 119 | + }) |
| 120 | + log.Info(). |
| 121 | + Uint64("start-block", update.Append.Earliest().Number.Uint64()). |
| 122 | + Uint64("end-block", update.Append.Latest().Number.Uint64()). |
| 123 | + Int("num-inserted-events", numInsertedEvents). |
| 124 | + Int("num-discarded-events", numDiscardedEvents). |
| 125 | + Msg("synced sequencer contract") |
| 126 | + metrics.TxSubmittedEventsSyncedUntil.Set(float64(update.Append.Latest().Number.Uint64())) |
| 127 | + return err |
| 128 | +} |
| 129 | + |
| 130 | +func (sts *SequencerTransactionSubmitted) filterEvents( |
| 131 | + events []bindings.SequencerTransactionSubmitted, |
| 132 | +) []bindings.SequencerTransactionSubmitted { |
| 133 | + filteredEvents := []bindings.SequencerTransactionSubmitted{} |
| 134 | + // TODO: before the refactoring, a mux'ed field was read here |
| 135 | + // in order to filter out Eons that are before "our" first |
| 136 | + // keyper-sets eon. |
| 137 | + // Do this by direcly reading "our" keyper-sets from the DB |
| 138 | + // without manually iterating over all keyper-sets / addresses. |
| 139 | + // This requires persisting this information upon insertion |
| 140 | + // in the KeyperSetAdded event handler and thus a database migration. |
| 141 | + // |
| 142 | + // For now, insert all events into the DB. |
| 143 | + // We could clean up this from time to time as a temporary measure |
| 144 | + // to not make the DB grow unnecessarily. |
| 145 | + for _, event := range events { |
| 146 | + if event.Eon > math.MaxInt64 || |
| 147 | + !event.GasLimit.IsInt64() { |
| 148 | + log.Debug(). |
| 149 | + Uint64("eon", event.Eon). |
| 150 | + Uint64("block-number", event.Raw.BlockNumber). |
| 151 | + Str("block-hash", event.Raw.BlockHash.Hex()). |
| 152 | + Uint("tx-index", event.Raw.TxIndex). |
| 153 | + Uint("log-index", event.Raw.Index). |
| 154 | + Msg("ignoring transaction submitted event") |
| 155 | + continue |
| 156 | + } |
| 157 | + filteredEvents = append(filteredEvents, event) |
| 158 | + } |
| 159 | + return filteredEvents |
| 160 | +} |
0 commit comments