@@ -13,7 +13,6 @@ import (
1313 flowGo "github.com/onflow/flow-go/model/flow"
1414 "github.com/rs/zerolog"
1515 "github.com/sethvargo/go-retry"
16- "golang.org/x/sync/errgroup"
1716
1817 "github.com/onflow/flow-evm-gateway/config"
1918 "github.com/onflow/flow-evm-gateway/metrics"
@@ -93,15 +92,15 @@ func (t *SingleTxPool) Add(
9392 return err
9493 }
9594
96- latestBlock , account , err := t .fetchFlowLatestBlockAndCOA (ctx )
95+ latestBlock , err := t .client . GetLatestBlock (ctx , true )
9796 if err != nil {
9897 return err
9998 }
10099
101100 script := replaceAddresses (runTxScript , t .config .FlowNetworkID )
102101 flowTx , err := t .buildTransaction (
102+ ctx ,
103103 latestBlock ,
104- account ,
105104 script ,
106105 cadence .NewArray ([]cadence.Value {hexEncodedTx }),
107106 coinbaseAddress ,
@@ -157,8 +156,8 @@ func (t *SingleTxPool) Add(
157156// buildTransaction creates a Cadence transaction from the provided script,
158157// with the given arguments and signs it with the configured COA account.
159158func (t * SingleTxPool ) buildTransaction (
159+ ctx context.Context ,
160160 latestBlock * flow.Block ,
161- account * flow.Account ,
162161 script []byte ,
163162 args ... cadence.Value ,
164163) (* flow.Transaction , error ) {
@@ -177,53 +176,35 @@ func (t *SingleTxPool) buildTransaction(
177176 }
178177 }
179178
180- // building and signing transactions should be blocking,
181- // so we don't have keys conflict
182- t . mux . Lock ()
183- defer t . mux . Unlock ()
179+ accKey , err := t . fetchSigningAccountKey ()
180+ if err != nil {
181+ return nil , err
182+ }
184183
185- accKey , err := t .keystore .Take ()
184+ coaAddress := t .config .COAAddress
185+ accountKey , err := t .client .GetAccountKeyAtLatestBlock (ctx , coaAddress , accKey .Index )
186186 if err != nil {
187+ accKey .Done ()
187188 return nil , err
188189 }
189190
190- if err := accKey .SetProposerPayerAndSign (flowTx , account ); err != nil {
191+ if err := accKey .SetProposerPayerAndSign (flowTx , coaAddress , accountKey ); err != nil {
191192 accKey .Done ()
192193 return nil , err
193194 }
194195
195196 // now that the transaction is prepared, store the transaction's metadata
196197 accKey .SetLockMetadata (flowTx .ID (), latestBlock .Height )
197198
198- t .collector .OperatorBalance (account )
199-
200199 return flowTx , nil
201200}
202201
203- func (t * SingleTxPool ) fetchFlowLatestBlockAndCOA (ctx context.Context ) (
204- * flow.Block ,
205- * flow.Account ,
206- error ,
207- ) {
208- var (
209- g = errgroup.Group {}
210- err1 , err2 error
211- latestBlock * flow.Block
212- account * flow.Account
213- )
214-
215- // execute concurrently so we can speed up all the information we need for tx
216- g .Go (func () error {
217- latestBlock , err1 = t .client .GetLatestBlock (ctx , true )
218- return err1
219- })
220- g .Go (func () error {
221- account , err2 = t .client .GetAccount (ctx , t .config .COAAddress )
222- return err2
223- })
224- if err := g .Wait (); err != nil {
225- return nil , nil , err
226- }
202+ func (t * SingleTxPool ) fetchSigningAccountKey () (* keystore.AccountKey , error ) {
203+ // getting an account key from the `KeyStore` for signing transactions,
204+ // should be lock-protected, so that we don't sign any two Flow
205+ // transactions with the same account key
206+ t .mux .Lock ()
207+ defer t .mux .Unlock ()
227208
228- return latestBlock , account , nil
209+ return t . keystore . Take ()
229210}
0 commit comments