Skip to content

Instantly share code, notes, and snippets.

@dshulyak
Created September 3, 2018 06:26
Show Gist options
  • Save dshulyak/a1ceda9dcf7d491381eaeaf66d27841d to your computer and use it in GitHub Desktop.
Save dshulyak/a1ceda9dcf7d491381eaeaf66d27841d to your computer and use it in GitHub Desktop.
test generate hash
pragma solidity ^0.4.21;
contract TestHash {
bytes32 public result;
constructor() public {}
function generate(bytes input) public {
result = keccak256(input);
}
}
package main
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
first, _ := crypto.GenerateKey()
faddr := crypto.PubkeyToAddress(first.PublicKey)
genesis := core.GenesisAlloc{
faddr: {Balance: big.NewInt(10000000000000)},
}
b := backends.NewSimulatedBackend(genesis)
b.Commit()
fopts := bind.NewKeyedTransactor(first)
_, _, c, err := DeployTestHash(fopts, b)
must(err)
b.Commit()
input := make([]byte, 50*1024)
_, err = rand.Read(input)
must(err)
tx, err := c.Generate(fopts, input)
must(err)
b.Commit()
receipt, err := bind.WaitMined(context.TODO(), b, tx)
must(err)
hash, err := c.Result(nil)
must(err)
fmt.Printf("Hash %x costs %d\n", hash, receipt.GasUsed)
}
func must(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment