From 419cf85433fb9b12883eefb7a9073d6e409daac2 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 21 Aug 2018 07:13:34 -0700 Subject: [PATCH] Merge PR #2046: auth: Make gas cost take into account cryptosystem * auth: Make gas cost take into account cryptosystem This will make including the multisig easier. The ratio for ed25519 vs secp256k1 are based on the ratio of their verification times, using the benchmarks in tendermint/crypto. --- PENDING.md | 4 ++-- x/auth/ante.go | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/PENDING.md b/PENDING.md index f120b8ab8..c455419c9 100644 --- a/PENDING.md +++ b/PENDING.md @@ -20,7 +20,6 @@ BREAKING CHANGES * Tendermint - FEATURES * Gaia REST API (`gaiacli advanced rest-server`) @@ -42,7 +41,8 @@ IMPROVEMENTS * Gaia CLI (`gaiacli`) * Gaia - * [x/stake] [#2023](https://github.com/cosmos/cosmos-sdk/pull/2023) Terminate iteration loop in `UpdateBondedValidators` and `UpdateBondedValidatorsFull` when the first revoked validator is encountered and perform a sanity check. + * [x/stake] [#2023](https://github.com/cosmos/cosmos-sdk/pull/2023) Terminate iteration loop in `UpdateBondedValidators` and `UpdateBondedValidatorsFull` when the first revoked validator is encountered and perform a sanity check. + * [x/auth] Signature verification's gas cost now accounts for pubkey type. [#2046](https://github.com/tendermint/tendermint/pull/2046) * SDK * [tools] Make get_vendor_deps deletes `.vendor-new` directories, in case scratch files are present. diff --git a/x/auth/ante.go b/x/auth/ante.go index 60aea8fc7..078f376e2 100644 --- a/x/auth/ante.go +++ b/x/auth/ante.go @@ -5,13 +5,17 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" ) const ( - deductFeesCost sdk.Gas = 10 - memoCostPerByte sdk.Gas = 1 - verifyCost = 100 - maxMemoCharacters = 100 + deductFeesCost sdk.Gas = 10 + memoCostPerByte sdk.Gas = 1 + ed25519VerifyCost = 59 + secp256k1VerifyCost = 100 + maxMemoCharacters = 100 ) // NewAnteHandler returns an AnteHandler that checks @@ -187,7 +191,7 @@ func processSig( } // Check sig. - ctx.GasMeter().ConsumeGas(verifyCost, "ante verify") + consumeSignatureVerificationGas(ctx.GasMeter(), pubKey) if !pubKey.VerifyBytes(signBytes, sig.Signature) { return nil, sdk.ErrUnauthorized("signature verification failed").Result() } @@ -195,6 +199,17 @@ func processSig( return } +func consumeSignatureVerificationGas(meter sdk.GasMeter, pubkey crypto.PubKey) { + switch pubkey.(type) { + case ed25519.PubKeyEd25519: + meter.ConsumeGas(ed25519VerifyCost, "ante verify: ed25519") + case secp256k1.PubKeySecp256k1: + meter.ConsumeGas(secp256k1VerifyCost, "ante verify: secp256k1") + default: + panic("Unrecognized signature type") + } +} + // Deduct the fee from the account. // We could use the CoinKeeper (in addition to the AccountMapper, // because the CoinKeeper doesn't give us accounts), but it seems easier to do this.