Skip to content

Commit

Permalink
introduce lock
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 13, 2024
1 parent f714dff commit 99f7684
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
14 changes: 14 additions & 0 deletions jsonrpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type JSONRPCBackend struct {
// fee cache
feeDenom string
feeDecimals uint8
feeMutex sync.RWMutex

mut sync.Mutex // mutex for accMuts
accMuts map[string]*AccMut
Expand Down Expand Up @@ -126,6 +127,17 @@ func NewJSONRPCBackend(
return b, nil

Check warning on line 127 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L122-L127

Added lines #L122 - L127 were not covered by tests
}

func (b *JSONRPCBackend) feeInfo() (string, uint8, error) {
b.feeMutex.RLock()
defer b.feeMutex.RUnlock()

if b.feeDenom == "" {
return "", 0, NewInternalError("jsonrpc is not ready")
}

Check warning on line 136 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L130-L136

Added lines #L130 - L136 were not covered by tests

return b.feeDenom, b.feeDecimals, nil

Check warning on line 138 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L138

Added line #L138 was not covered by tests
}

func (b *JSONRPCBackend) feeFetcher() {
fetcher := func() (err error) {
defer func() {
Expand All @@ -150,8 +162,10 @@ func (b *JSONRPCBackend) feeFetcher() {
return err
}

Check warning on line 163 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L159-L163

Added lines #L159 - L163 were not covered by tests

b.feeMutex.Lock()
b.feeDenom = feeDenom
b.feeDecimals = decimals
b.feeMutex.Unlock()

return nil

Check warning on line 170 in jsonrpc/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/backend.go#L165-L170

Added lines #L165 - L170 were not covered by tests
}
Expand Down
9 changes: 5 additions & 4 deletions jsonrpc/backend/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ func (b *JSONRPCBackend) GetBalance(address common.Address, blockNrOrHash rpc.Bl
}

// jsonrpc is not ready for querying
if b.feeDenom == "" {
return nil, errors.New("jsonrpc is not ready")
feeDenom, feeDecimals, err := b.feeInfo()

Check warning on line 29 in jsonrpc/backend/eth.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/eth.go#L29

Added line #L29 was not covered by tests
if err != nil {
return nil, err
}

balance, err := b.app.EVMKeeper.ERC20Keeper().GetBalance(queryCtx, sdk.AccAddress(address[:]), b.feeDenom)
balance, err := b.app.EVMKeeper.ERC20Keeper().GetBalance(queryCtx, sdk.AccAddress(address[:]), feeDenom)
if err != nil {
return nil, err
}

return (*hexutil.Big)(types.ToEthersUint(b.feeDecimals, balance.BigInt())), nil
return (*hexutil.Big)(types.ToEthersUint(feeDecimals, balance.BigInt())), nil

Check warning on line 39 in jsonrpc/backend/eth.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/eth.go#L39

Added line #L39 was not covered by tests
}

func (b *JSONRPCBackend) Call(args rpctypes.TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash, overrides *rpctypes.StateOverride, blockOverrides *rpctypes.BlockOverrides) (hexutil.Bytes, error) {
Expand Down
29 changes: 20 additions & 9 deletions jsonrpc/backend/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ func (b *JSONRPCBackend) EstimateGas(args rpctypes.TransactionArgs, blockNrOrHas
}

// jsonrpc is not ready for querying
if b.feeDenom == "" {
return hexutil.Uint64(0), NewInternalError("jsonrpc is not ready")
_, feeDecimals, err := b.feeInfo()

Check warning on line 43 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L43

Added line #L43 was not covered by tests
if err != nil {
return hexutil.Uint64(0), err
}

sdkMsgs := []sdk.Msg{}
if args.To == nil {
sdkMsgs = append(sdkMsgs, &types.MsgCreate{
Sender: sender,
Code: hexutil.Encode(args.GetData()),
Value: math.NewIntFromBigInt(types.FromEthersUnit(b.feeDecimals, args.Value.ToInt())),
Value: math.NewIntFromBigInt(types.FromEthersUnit(feeDecimals, args.Value.ToInt())),

Check warning on line 53 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L53

Added line #L53 was not covered by tests
})
} else {
sdkMsgs = append(sdkMsgs, &types.MsgCall{
Sender: sender,
ContractAddr: args.To.Hex(),
Input: hexutil.Encode(args.GetData()),
Value: math.NewIntFromBigInt(types.FromEthersUnit(b.feeDecimals, args.Value.ToInt())),
Value: math.NewIntFromBigInt(types.FromEthersUnit(feeDecimals, args.Value.ToInt())),

Check warning on line 60 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L60

Added line #L60 was not covered by tests
})
}

Expand Down Expand Up @@ -102,16 +103,26 @@ func (b *JSONRPCBackend) GasPrice() (*hexutil.Big, error) {
}

// jsonrpc is not ready for querying
if b.feeDenom == "" {
return nil, NewInternalError("jsonrpc is not ready")
feeDenom, feeDecimals, err := b.feeInfo()

Check warning on line 106 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L106

Added line #L106 was not covered by tests
if err != nil {
return nil, err
}

// Multiply by 1e9 to maintain precision during conversion
// This adds 9 decimal places to prevent truncation errors
const precisionMultiplier = 1e9

Check warning on line 114 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L113-L114

Added lines #L113 - L114 were not covered by tests
// multiply by 1e9 to prevent decimal drops
gasPrice := params.MinGasPrices.AmountOf(b.feeDenom).
MulTruncate(math.LegacyNewDec(1e9)).
gasPrice := params.MinGasPrices.AmountOf(feeDenom).
MulTruncate(math.LegacyNewDec(precisionMultiplier)).

Check warning on line 117 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L117

Added line #L117 was not covered by tests
TruncateInt().BigInt()

return (*hexutil.Big)(types.ToEthersUint(b.feeDecimals+9, gasPrice)), nil
// Verify the result is within safe bounds
if gasPrice.BitLen() > 256 {
return nil, NewInternalError("gas price overflow")
}

Check warning on line 123 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L120-L123

Added lines #L120 - L123 were not covered by tests

return (*hexutil.Big)(types.ToEthersUint(feeDecimals+9, gasPrice)), nil

Check warning on line 125 in jsonrpc/backend/gas.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/gas.go#L125

Added line #L125 was not covered by tests
}

func (b *JSONRPCBackend) MaxPriorityFeePerGas() (*hexutil.Big, error) {
Expand Down

0 comments on commit 99f7684

Please sign in to comment.