Skip to content
Merged
3 changes: 2 additions & 1 deletion chain_capabilities/common/gasmeter/gasmeter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *Meter) Close() error {
// txHash (or tx signature) is the capability event id: one record per
// on-chain write, identical on every node of the DON that observes it. The
// log line is a contract consumed by the billing reconciler (fields:
// executionID, eventID, resourceType, value, orgID, txHash) and must stay
// workflowID, executionID, eventID, resourceType, value, orgID, txHash) and must stay
// stable. Fail-open: never affects the reply.
func (m *Meter) Emit(ctx context.Context, metadata capabilities.RequestMetadata, txHash string, fee *big.Int) {
if m == nil || fee == nil {
Expand All @@ -90,6 +90,7 @@ func (m *Meter) Emit(ctx context.Context, metadata capabilities.RequestMetadata,
OrgID: metadata.OrgID,
})
m.lggr.Infow("Emitted capability usage meter record",
"workflowID", metadata.WorkflowID,
Comment thread
DylanTinianov marked this conversation as resolved.
Resolved
"executionID", metadata.WorkflowExecutionID,
"eventID", txHash,
"resourceType", m.resourceType,
Expand Down
60 changes: 60 additions & 0 deletions chain_capabilities/common/gasmeter/gasmeter_fault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package gasmeter

import (
"fmt"
"math/big"
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/settings"
)

// faultSettings builds a getter with PerOrg.BillingFault* overrides for org.
func faultSettings(t *testing.T, org string, kv map[string]string) settings.Getter {
t.Helper()
cfg := fmt.Sprintf("[org.%s.PerOrg]\n", org)
for k, v := range kv {
cfg += fmt.Sprintf("%s = %q\n", k, v)
}
g, err := settings.NewTOMLGetter([]byte(cfg))
require.NoError(t, err)
return g
}

// Fault injection is read from CRE settings by the shared ResourceManager, so
// it reaches the gas meter with no code here.
func TestEmit_FaultInjection(t *testing.T) {
metadata := capabilities.RequestMetadata{WorkflowID: "wf-1", WorkflowExecutionID: "exec-1", OrgID: "org-1"}

t.Run("perturb applies delta to the emitted fee", func(t *testing.T) {
emitter := &recordingEmitter{}
cfg := enabledCfg(emitter)
cfg.Settings = faultSettings(t, "org-1", map[string]string{"BillingFaultPerturbRate": "1", "BillingFaultPerturbDelta": "7"})
m := New(logger.Test(t), cfg, 1, 7)
m.Emit(t.Context(), metadata, "0xabc", big.NewInt(100))
require.Len(t, emitter.records, 1)
require.Equal(t, "107", emitter.records[0].GetUtilizations()[0].GetValue(), "sign sample for 0xabc adds")
})

t.Run("drop emits nothing", func(t *testing.T) {
emitter := &recordingEmitter{}
cfg := enabledCfg(emitter)
cfg.Settings = faultSettings(t, "org-1", map[string]string{"BillingFaultDropRate": "1"})
m := New(logger.Test(t), cfg, 1, 7)
m.Emit(t.Context(), metadata, "0xabc", big.NewInt(100))
require.Empty(t, emitter.records)
})

t.Run("other org is untouched", func(t *testing.T) {
emitter := &recordingEmitter{}
cfg := enabledCfg(emitter)
cfg.Settings = faultSettings(t, "org-canary", map[string]string{"BillingFaultDropRate": "1"})
m := New(logger.Test(t), cfg, 1, 7)
m.Emit(t.Context(), metadata, "0xabc", big.NewInt(100))
require.Len(t, emitter.records, 1)
require.Equal(t, "100", emitter.records[0].GetUtilizations()[0].GetValue())
})
}
1 change: 1 addition & 0 deletions chain_capabilities/common/gasmeter/gasmeter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestEmit(t *testing.T) {
logs := obs.FilterMessage("Emitted capability usage meter record").All()
require.Len(t, logs, 1)
fields := logs[0].ContextMap()
require.Equal(t, "wf-1", fields["workflowID"])
require.Equal(t, "exec-1", fields["executionID"])
require.Equal(t, "0xabc", fields["eventID"])
require.Equal(t, "cre:workflow:gas:421614", fields["resourceType"])
Expand Down
4 changes: 2 additions & 2 deletions chain_capabilities/common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.27.3
require (
github.com/jpillora/backoff v1.0.0
github.com/smartcontractkit/capabilities/libs v0.0.0-20260714133332-db2a5f11cd64
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261006175240-29594528f464
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261029170057-b615a8de42d9
github.com/smartcontractkit/chainlink-protos/metering/go v0.0.0-20260710151514-27b5a126dabe
github.com/smartcontractkit/libocr v0.0.0-20260810200708-618b5bf7f342
github.com/stretchr/testify v1.12.1
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/shopspring/decimal v1.4.0 // indirect
github.com/smartcontractkit/chain-selectors v1.0.104 // indirect
github.com/smartcontractkit/chainlink-common/pkg/chipingress v0.0.11-0.20261005132539-b7d63d5fd549 // indirect
github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20260804191526-b7a850ae7648 // indirect
github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20261013212015-e622e8b83b6b // indirect
github.com/smartcontractkit/chainlink-protos/linking-service/go v0.0.0-20251002192024-d2ad9222409b // indirect
github.com/stretchr/objx v0.5.3 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
Expand Down
8 changes: 4 additions & 4 deletions chain_capabilities/common/go.sum

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

2 changes: 1 addition & 1 deletion chain_capabilities/evm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/smartcontractkit/capabilities/chain_capabilities/common v0.0.0-20261009182120-372f846f528c
github.com/smartcontractkit/capabilities/libs v0.0.0-20261001142216-b1dea8dbb961
github.com/smartcontractkit/chain-selectors v1.0.112
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261028220357-f9f63cb65fce
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261029170057-b615a8de42d9
github.com/smartcontractkit/chainlink-evm v0.3.4-0.20260917132551-2a2b38e8b744
github.com/smartcontractkit/chainlink-evm/gethwrappers v0.0.0-20260512150409-b4068bf735e6
github.com/smartcontractkit/chainlink-framework/multinode v0.0.0-20260521164805-26d78d5e1243
Expand Down
4 changes: 2 additions & 2 deletions chain_capabilities/evm/go.sum

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

4 changes: 2 additions & 2 deletions chain_capabilities/solana/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (
github.com/smartcontractkit/capabilities/chain_capabilities/common v0.0.0-20261009182120-372f846f528c
github.com/smartcontractkit/capabilities/libs v0.0.0-20261001142216-b1dea8dbb961
github.com/smartcontractkit/chain-selectors v1.0.104
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261006175240-29594528f464
github.com/smartcontractkit/chainlink-common v0.11.2-0.20261029170057-b615a8de42d9
github.com/smartcontractkit/chainlink-framework/multinode v0.0.0-20260521164805-26d78d5e1243
github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20260821155228-fa1d775e2138
github.com/smartcontractkit/chainlink-protos/cre/go v0.0.0-20261013212015-e622e8b83b6b
github.com/smartcontractkit/chainlink-solana v1.3.1-0.20260917175559-f7d4a86019d4
github.com/smartcontractkit/chainlink-solana/contracts v0.0.0-20260803170030-90043607d911
github.com/smartcontractkit/libocr v0.0.0-20260810200708-618b5bf7f342
Expand Down
8 changes: 4 additions & 4 deletions chain_capabilities/solana/go.sum

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

Loading