Skip to content

Commit

Permalink
Merge PR cosmos#2375: Add an example for the size of a transaction
Browse files Browse the repository at this point in the history
* Add an example for the size of a transaction

This shows that the size of a send tx from 1 input to 1 output is 173 bytes
This is good information to have, though we need to find the correct place
to put it.

* fix lint
  • Loading branch information
ValarDragon authored and rigelrozanski committed Sep 24, 2018
1 parent a65c6eb commit 8774adc
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/gaia/app/benchmarks/txsize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package app

import (
"fmt"

"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/tendermint/tendermint/crypto/secp256k1"
)

// This will fail half the time with the second output being 173
// This is due to secp256k1 signatures not being constant size.
// This will be resolved when updating to tendermint v0.24.0
// nolint: vet
func ExampleTxSendSize() {
cdc := app.MakeCodec()
priv1 := secp256k1.GenPrivKeySecp256k1([]byte{0})
addr1 := sdk.AccAddress(priv1.PubKey().Address())
priv2 := secp256k1.GenPrivKeySecp256k1([]byte{1})
addr2 := sdk.AccAddress(priv2.PubKey().Address())
coins := []sdk.Coin{sdk.NewCoin("denom", sdk.NewInt(10))}
msg1 := bank.MsgSend{
Inputs: []bank.Input{bank.NewInput(addr1, coins)},
Outputs: []bank.Output{bank.NewOutput(addr2, coins)},
}
sig, _ := priv1.Sign(msg1.GetSignBytes())
sigs := []auth.StdSignature{auth.StdSignature{nil, sig, 0, 0}}
tx := auth.NewStdTx([]sdk.Msg{msg1}, auth.NewStdFee(0, coins...), sigs, "")
fmt.Println(len(cdc.MustMarshalBinaryBare([]sdk.Msg{msg1})))
fmt.Println(len(cdc.MustMarshalBinaryBare(tx)))
// output: 80
// 173
}

0 comments on commit 8774adc

Please sign in to comment.