Skip to content

Commit

Permalink
Add base cost for getVerifiedWarpMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald committed Jul 25, 2023
1 parent 9507e18 commit 1ad4ea6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions x/warp/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
)

const (
GetBlockchainIDGasCost uint64 = 2 // Based on GasQuickStep used in existing EVM instructions
AddWarpMessageGasCost uint64 = 20_000 // Cost of producing and serving a BLS Signature
GetVerifiedWarpMessageBaseCost uint64 = 2 // Base cost of entering getVerifiedWarpMessage
GetBlockchainIDGasCost uint64 = 2 // Based on GasQuickStep used in existing EVM instructions
AddWarpMessageGasCost uint64 = 20_000 // Cost of producing and serving a BLS Signature
// Sum of base log gas cost, cost of producing 4 topics, and producing + serving a BLS Signature (sign + trie write)
// Note: using trie write for the gas cost results in a conservative overestimate since the message is stored in a
// flat database that can be cleaned up after a period of time instead of the EVM trie.
Expand Down Expand Up @@ -117,9 +118,10 @@ func PackGetVerifiedWarpMessageOutput(outputStruct GetVerifiedWarpMessageOutput)
// getVerifiedWarpMessage retrieves the pre-verified warp message from the predicate storage slots and returns
// the expected ABI encoding of the message to the caller.
func getVerifiedWarpMessage(accessibleState contract.AccessibleState, caller common.Address, addr common.Address, _ []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
remainingGas = suppliedGas
// XXX Note: there is no base cost for retrieving a verified warp message. Instead, we charge for each piece of gas,
// prior to each execution step.
remainingGas, err = contract.DeductGas(suppliedGas, GetVerifiedWarpMessageBaseCost)
if err != nil {
return nil, remainingGas, err
}
// Ignore input since there are no arguments
predicateBytes, exists := accessibleState.GetStateDB().GetPredicateStorageSlots(ContractAddress)
// If there is no such value, return false to the caller.
Expand Down
16 changes: 8 additions & 8 deletions x/warp/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
state.SetPredicateStorageSlots(ContractAddress, warpMessagePredicateBytes)
},
SuppliedGas: GasCostPerWarpMessageBytes * uint64(len(warpMessagePredicateBytes)),
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)),
ReadOnly: false,
ExpectedRes: func() []byte {
res, err := PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{
Expand All @@ -202,7 +202,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
"get non-existent message": {
Caller: callerAddr,
InputFn: func(t testing.TB) []byte { return getVerifiedWarpMsg },
SuppliedGas: 0,
SuppliedGas: GetVerifiedWarpMessageBaseCost,
ReadOnly: false,
ExpectedRes: func() []byte {
res, err := PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{Exists: false})
Expand All @@ -218,7 +218,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
state.SetPredicateStorageSlots(ContractAddress, warpMessagePredicateBytes)
},
SuppliedGas: GasCostPerWarpMessageBytes * uint64(len(warpMessagePredicateBytes)),
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)),
ReadOnly: true,
ExpectedRes: func() []byte {
res, err := PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{
Expand All @@ -240,7 +240,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
"get non-existent message readOnly": {
Caller: callerAddr,
InputFn: func(t testing.TB) []byte { return getVerifiedWarpMsg },
SuppliedGas: 0,
SuppliedGas: GetVerifiedWarpMessageBaseCost,
ReadOnly: true,
ExpectedRes: func() []byte {
res, err := PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{Exists: false})
Expand All @@ -256,7 +256,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
state.SetPredicateStorageSlots(ContractAddress, warpMessagePredicateBytes)
},
SuppliedGas: GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessagePredicateBytes)) - 1,
ReadOnly: false,
ExpectedErr: vmerrs.ErrOutOfGas.Error(),
},
Expand All @@ -266,7 +266,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
state.SetPredicateStorageSlots(ContractAddress, warpMessage.Bytes())
},
SuppliedGas: GasCostPerWarpMessageBytes * uint64(len(warpMessage.Bytes())),
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(len(warpMessage.Bytes())),
ReadOnly: false,
ExpectedErr: errInvalidPredicateBytes.Error(),
},
Expand All @@ -276,7 +276,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {
BeforeHook: func(t testing.TB, state contract.StateDB) {
state.SetPredicateStorageSlots(ContractAddress, predicateutils.PackPredicate([]byte{1, 2, 3}))
},
SuppliedGas: GasCostPerWarpMessageBytes * uint64(32),
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(32),
ReadOnly: false,
ExpectedErr: errInvalidWarpMsg.Error(),
},
Expand All @@ -291,7 +291,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) {

state.SetPredicateStorageSlots(ContractAddress, predicateutils.PackPredicate(warpMessage.Bytes()))
},
SuppliedGas: GasCostPerWarpMessageBytes * uint64(192),
SuppliedGas: GetVerifiedWarpMessageBaseCost + GasCostPerWarpMessageBytes*uint64(192),
ReadOnly: false,
ExpectedErr: errInvalidAddressedPayload.Error(),
},
Expand Down

0 comments on commit 1ad4ea6

Please sign in to comment.