Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/ethereum/estimate_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ func (c *ethConnector) gasEstimate(ctx context.Context, tx *ethsigner.Transactio
// Multiply the gas estimate by the configured factor
fGasEstimate := new(big.Float).SetInt(gasEstimate.BigInt())
_ = fGasEstimate.Mul(fGasEstimate, c.gasEstimationFactor)
_, _ = fGasEstimate.Int(gasEstimate.BigInt())
return &gasEstimate, "", nil
scaledLimit, _ := fGasEstimate.Int(nil)

// If the gas estimate is larger than the maximum gas limit on the block, then we fail at this
// early stage. Because otherwise we'll accept the transaction in, but fail to actually submit
// it to the mempool of the blockchain. It's odd that the default configuration of chains (Besu inc.)
// allow the gas estimate max to be so much higher than the block max - but they do.
if c.blockListener != nil {
gasLimit := c.blockListener.GetBlockGasLimit()
if gasLimit != nil && gasLimit.BigInt().Cmp(scaledLimit) < 0 {
return nil, "", i18n.NewError(ctx, msgs.MsgTransactionEstimateTooLargeForBlock,
scaledLimit.String(), c.gasEstimationFactor, gasEstimate.BigInt().String(), gasLimit.BigInt().String())
}
}

return (*ethtypes.HexInteger)(scaledLimit), "", nil
}
30 changes: 29 additions & 1 deletion internal/ethereum/estimate_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-evmconnect/mocks/ethblocklistenermocks"
"github.com/hyperledger/firefly-signer/pkg/abi"
"github.com/hyperledger/firefly-signer/pkg/ethsigner"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
Expand Down Expand Up @@ -74,6 +75,34 @@ func TestGasEstimateOK(t *testing.T) {

}

func TestGasEstimateAboveBlockLimit(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
defer done()

mbl := ethblocklistenermocks.NewBlockListener(t)
mbl.On("GetBlockGasLimit").Return(ethtypes.NewHexInteger64(1000))
mbl.On("WaitClosed").Return()
c.blockListener = mbl

mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas",
mock.MatchedBy(func(tx *ethsigner.Transaction) bool {
return true
})).
Return(nil).
Run(func(args mock.Arguments) {
args[1].(*ethtypes.HexInteger).BigInt().SetString("12345", 10)
})

var req ffcapi.TransactionInput
err := json.Unmarshal([]byte(sampleGasEstimate), &req)
assert.NoError(t, err)
_, reason, err := c.GasEstimate(ctx, &req)
assert.Regexp(t, "FF23071", err)
assert.Empty(t, reason)

}

func TestGasEstimateFail(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
Expand Down Expand Up @@ -285,4 +314,3 @@ func TestGasEstimateFailCustomErrorCannotParse(t *testing.T) {
assert.Nil(t, res)

}

1 change: 1 addition & 0 deletions internal/msgs/en_error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ var (
MsgReturnedBlockHashMismatch = ffe("FF23068", "Returned block %d hash %s does not match requested hash %s")
MsgInvalidChainTrackingMode = ffe("FF23069", "Invalid chain tracking mode '%s': must be 'light' or 'full'")
MsgTransactionNotIncludedInChainHead = ffe("FF23070", "Transaction '%s' cannot be reconciled because chain head %d is before receipt block %s")
MsgTransactionEstimateTooLargeForBlock = ffe("FF23071", "Gas estimate %s (scaled at %.2f from estimate %s) too large for the current block gas limit %s")
)
20 changes: 20 additions & 0 deletions mocks/ethblocklistenermocks/block_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions pkg/ethblocklistener/blocklistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type BlockListener interface {
AddConsumer(ctx context.Context, c *BlockUpdateConsumer)
RemoveConsumer(ctx context.Context, id *fftypes.UUID)
GetHighestBlock(ctx context.Context) (uint64, bool)
GetBlockGasLimit() *ethtypes.HexInteger // nil if unknown
GetBlockInfoByNumber(ctx context.Context, blockNumber uint64, allowCache bool, expectedParentHashStr string, expectedBlockHashStr string) (*ethrpc.BlockInfoJSONRPC, error)
GetBlockInfoByHash(ctx context.Context, hash0xString string) (*ethrpc.BlockInfoJSONRPC, error)
GetEVMBlockWithTxHashesByHash(ctx context.Context, hash0xString string) (b *ethrpc.EVMBlockWithTxHashesJSONRPC, err error)
Expand Down Expand Up @@ -124,10 +125,11 @@ type blockListener struct {
BlockListenerConfig

// canonical chain
canonicalChainLock sync.RWMutex // covers highestBlock and canonicalChain
canonicalChain *list.List
highestBlockSet bool
highestBlock uint64
canonicalChainLock sync.RWMutex // covers highestBlock and canonicalChain
canonicalChain *list.List
highestBlockSet bool
highestBlock uint64
highestBlockGasLimit *ethtypes.HexInteger

// headBlockNumber mode: last head value sent on the block listener channel (only written from listenLoop)
currentChainHead uint64
Expand Down Expand Up @@ -424,7 +426,7 @@ func (bl *blockListener) reconcileCanonicalChain(bi *ethrpc.BlockInfoJSONRPC) *l
bl.canonicalChainLock.Lock()
defer bl.canonicalChainLock.Unlock()

bl.checkAndSetHighestBlock(bi.Number.Uint64())
bl.checkAndSetHighestBlock(bi.Number.Uint64(), bi.GasLimit)

// Find the position of this block in the block sequence
pos := bl.canonicalChain.Back()
Expand Down Expand Up @@ -553,7 +555,7 @@ func (bl *blockListener) rebuildCanonicalChain() *list.Element {
notifyPos = newElem
}

bl.checkAndSetHighestBlock(bi.Number.Uint64())
bl.checkAndSetHighestBlock(bi.Number.Uint64(), bi.GasLimit)

}
return notifyPos
Expand Down Expand Up @@ -665,6 +667,13 @@ func (bl *blockListener) GetHighestBlock(ctx context.Context) (uint64, bool) {
return highestBlock, true
}

// Gives a non-nil value only if the block listener is tracking the head and has access to the full block
func (bl *blockListener) GetBlockGasLimit() *ethtypes.HexInteger {
bl.canonicalChainLock.RLock()
defer bl.canonicalChainLock.RUnlock()
return bl.highestBlockGasLimit
}

func (bl *blockListener) GetHeadBlockNumber(_ context.Context) uint64 {
return bl.currentChainHead
}
Expand All @@ -677,10 +686,13 @@ func (bl *blockListener) setHighestBlock(block uint64) {
}

// Caller MUST hold the canonicalChain WRITE LOCK
func (bl *blockListener) checkAndSetHighestBlock(block uint64) {
func (bl *blockListener) checkAndSetHighestBlock(block uint64, blockGasLimit *ethtypes.HexInteger) {
if block > bl.highestBlock {
bl.highestBlock = block
bl.highestBlockSet = true
if blockGasLimit != nil && blockGasLimit.BigInt().Sign() > 0 {
bl.highestBlockGasLimit = blockGasLimit
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/ethblocklistener/blocklistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestBlockListenerOKSequential(t *testing.T) {
Number: 1003,
Hash: block1003Hash,
ParentHash: block1002Hash,
GasLimit: ethtypes.NewHexInteger64(10000),
}}
})
})
Expand Down Expand Up @@ -253,6 +254,8 @@ func TestBlockListenerOKSequential(t *testing.T) {

assert.Len(t, bl.SnapshotMonitoredHeadChain(), bl.MonitoredHeadLength)

require.Equal(t, int64(10000), bl.GetBlockGasLimit().Int64())

}

func TestBlockListenerWSShoulderTap(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type BlockInfoJSONRPC struct {
Hash ethtypes.HexBytes0xPrefix `json:"hash" ffstruct:"BlockInfoJSONRPC"`
ParentHash ethtypes.HexBytes0xPrefix `json:"parentHash" ffstruct:"BlockInfoJSONRPC"`
Timestamp ethtypes.HexUint64 `json:"timestamp" ffstruct:"BlockInfoJSONRPC"`
GasLimit *ethtypes.HexInteger `json:"gasLimit" ffstruct:"BlockInfoJSONRPC"`
LogsBloom ethtypes.HexBytes0xPrefix `json:"logsBloom" ffstruct:"BlockInfoJSONRPC"`
Transactions []ethtypes.HexBytes0xPrefix `json:"transactions" ffstruct:"BlockInfoJSONRPC"`
}
Expand Down Expand Up @@ -277,6 +278,7 @@ func (b *BlockHeaderJSONRPC) ToBlockInfo(includeLogsBloom bool) *BlockInfoJSONRP
Hash: b.Hash,
ParentHash: b.ParentHash,
Timestamp: b.Timestamp,
GasLimit: b.GasLimit,
}
if includeLogsBloom {
bi.LogsBloom = b.LogsBloom
Expand Down
Loading