Skip to content

Commit

Permalink
More flexible gas estimate provider interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Aug 7, 2024
1 parent 5b6b191 commit 3e98152
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
10 changes: 9 additions & 1 deletion execute/internal/gas/evm/gas_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package evm

import (
"math"

"github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

const (
Expand Down Expand Up @@ -45,7 +47,13 @@ func bytesForMsgTokens(numTokens int) int {
}

// CalculateMessageMaxGas computes the maximum gas overhead for a message.
func (gp EstimateProvider) CalculateMessageMaxGas(dataLength, numTokens int) uint64 {
func (gp EstimateProvider) CalculateMessageMaxGas(msg ccipocr3.Message) uint64 {
numTokens := len(msg.TokenAmounts)
var data []byte = msg.Data
dataLength := len(data)

// TODO: parse max gas from ExtraArgs.

messageBytes := ConstantMessagePartBytes +
bytesForMsgTokens(numTokens) +
dataLength
Expand Down
15 changes: 13 additions & 2 deletions execute/internal/gas/evm/gas_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

func Test_calculateMessageMaxGas(t *testing.T) {
Expand Down Expand Up @@ -41,8 +43,12 @@ func Test_calculateMessageMaxGas(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := ccipocr3.Message{
Data: make([]byte, tt.args.dataLen),
TokenAmounts: make([]ccipocr3.RampTokenAmount, tt.args.numTokens),
}
ep := EstimateProvider{}
got := ep.CalculateMessageMaxGas(tt.args.dataLen, tt.args.numTokens)
got := ep.CalculateMessageMaxGas(msg)
fmt.Println(got)
assert.Equalf(t, tt.want, got, "calculateMessageMaxGas(%v, %v)", tt.args.dataLen, tt.args.numTokens)
})
Expand Down Expand Up @@ -77,9 +83,14 @@ func TestCalculateMaxGas(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := ccipocr3.Message{
Data: make([]byte, tt.dataLength),
TokenAmounts: make([]ccipocr3.RampTokenAmount, tt.numberOfTokens),
}
ep := EstimateProvider{}

gotTree := ep.CalculateMerkleTreeGas(tt.numRequests)
gotMsg := ep.CalculateMessageMaxGas(tt.dataLength, tt.numberOfTokens)
gotMsg := ep.CalculateMessageMaxGas(msg)
fmt.Println("want", tt.want, "got", gotTree+gotMsg)
assert.Equal(t, tt.want, gotTree+gotMsg)
})
Expand Down
4 changes: 3 additions & 1 deletion execute/internal/gas/gas_estimate_provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gas

import "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"

type EstimateProvider interface {
CalculateMerkleTreeGas(numRequests int) uint64
CalculateMessageMaxGas(dataLength, numTokens int) uint64
CalculateMessageMaxGas(msg ccipocr3.Message) uint64
}
4 changes: 1 addition & 3 deletions execute/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,9 @@ func (b *execReportBuilder) verifyReport(
}
gasSum := uint64(0)
for _, msg := range execReport.Messages {
var data []byte = msg.Data
gasSum += b.estimateProvider.CalculateMessageMaxGas(len(data), len(msg.TokenAmounts))
gasSum += b.estimateProvider.CalculateMessageMaxGas(msg)
}
merkleTreeGas := b.estimateProvider.CalculateMerkleTreeGas(len(execReport.Messages))
// TODO: Add in message max gas (unless it can be added to CalculateMessageMaxGas).
totalGas := gasSum + merkleTreeGas

maxGas := b.maxGas - b.accumulated.gas
Expand Down

0 comments on commit 3e98152

Please sign in to comment.