本文整理匯總了Golang中github.com/ethereum/go-ethereum/common.HexToHash函數的典型用法代碼示例。如果您正苦於以下問題:Golang HexToHash函數的具體用法?Golang HexToHash怎麽用?Golang HexToHash使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HexToHash函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: processShare
func (m *Miner) processShare(s *ProxyServer, t *BlockTemplate, diff string, params []string) bool {
paramsOrig := params[:]
hashNoNonce := params[1]
nonce, err := strconv.ParseUint(strings.Replace(params[0], "0x", "", -1), 16, 64)
if err != nil {
log.Printf("Malformed nonce: %v", err)
return false
}
mixDigest := params[2]
minerDifficulty, err := strconv.ParseFloat(diff, 64)
if err != nil {
log.Println("Malformed difficulty: " + diff)
minerDifficulty = 5
}
minerAdjustedDifficulty := int64(minerDifficulty * 1000000 * 100)
share := Block{
number: t.Height,
hashNoNonce: common.HexToHash(hashNoNonce),
difficulty: big.NewInt(minerAdjustedDifficulty),
nonce: nonce,
mixDigest: common.HexToHash(mixDigest),
}
block := Block{
number: t.Height,
hashNoNonce: common.HexToHash(hashNoNonce),
difficulty: t.Difficulty,
nonce: nonce,
mixDigest: common.HexToHash(mixDigest),
}
if hasher.Verify(share) {
m.heartbeat()
m.storeShare(minerAdjustedDifficulty)
atomic.AddUint64(&m.validShares, 1)
log.Printf("Valid share from %[email protected]%s at difficulty %v", m.Id, m.IP, minerDifficulty)
} else {
atomic.AddUint64(&m.invalidShares, 1)
log.Printf("Invalid share from %[email protected]%s", m.Id, m.IP)
return false
}
if hasher.Verify(block) {
_, err = s.rpc().SubmitBlock(paramsOrig)
if err != nil {
atomic.AddUint64(&m.invalidBlocks, 1)
atomic.AddUint64(&s.invalidBlocks, 1)
log.Printf("Upstream share submission failure on height: %v for %v: %v", t.Height, t.Header, err)
} else {
s.fetchBlockTemplate()
atomic.AddUint64(&m.validBlocks, 1)
atomic.AddUint64(&s.validBlocks, 1)
log.Printf("Upstream share found by miner %[email protected]%v at height: %d", m.Id, m.IP, t.Height)
}
}
return true
}
示例2: TestInsert
func TestInsert(t *testing.T) {
trie := newEmpty()
updateString(trie, "doe", "reindeer")
updateString(trie, "dog", "puppy")
updateString(trie, "dogglesworth", "cat")
exp := common.HexToHash("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3")
root := trie.Hash()
if root != exp {
t.Errorf("exp %x got %x", exp, root)
}
trie = newEmpty()
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
root, err := trie.Commit()
if err != nil {
t.Fatalf("commit error: %v", err)
}
if root != exp {
t.Errorf("exp %x got %x", exp, root)
}
}
示例3: makeBlock
func makeBlock() *types.Block {
parentHash := common.HexToHash("0x01")
coinbase := common.HexToAddress("0x01")
root := common.HexToHash("0x01")
difficulty := common.Big1
nonce := uint64(1)
block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
txto := common.HexToAddress("0x02")
txamount := big.NewInt(1)
txgasAmount := big.NewInt(1)
txgasPrice := big.NewInt(1)
txdata := []byte{1, 2, 3}
tx := types.NewTransactionMessage(txto, txamount, txgasAmount, txgasPrice, txdata)
txs := make([]*types.Transaction, 1)
txs[0] = tx
block.SetTransactions(txs)
uncles := make([]*types.Header, 1)
uncles[0] = makeHeader()
block.SetUncles(uncles)
return block
}
示例4: TestTransactionSigHash
func TestTransactionSigHash(t *testing.T) {
if emptyTx.SigHash() != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
}
if rightvrsTx.SigHash() != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
}
}
示例5: makeStateLog
func makeStateLog(num int) *state.Log {
address := common.HexToAddress("0x0")
data := []byte{1, 2, 3}
number := uint64(num)
topics := make([]common.Hash, 3)
topics = append(topics, common.HexToHash("0x00"))
topics = append(topics, common.HexToHash("0x10"))
topics = append(topics, common.HexToHash("0x20"))
log := state.NewLog(address, topics, data, number)
return log
}
示例6: InsertPreState
// InsertPreState populates the given database with the genesis
// accounts defined by the test.
func (t *BlockTest) InsertPreState(db ethdb.Database, am *accounts.Manager) (*state.StateDB, error) {
statedb, err := state.New(common.Hash{}, db)
if err != nil {
return nil, err
}
for addrString, acct := range t.preAccounts {
addr, err := hex.DecodeString(addrString)
if err != nil {
return nil, err
}
code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
if err != nil {
return nil, err
}
balance, ok := new(big.Int).SetString(acct.Balance, 0)
if !ok {
return nil, err
}
nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
if err != nil {
return nil, err
}
if acct.PrivateKey != "" {
privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
err = crypto.ImportBlockTestKey(privkey)
err = am.TimedUnlock(common.BytesToAddress(addr), "", 999999*time.Second)
if err != nil {
return nil, err
}
}
obj := statedb.CreateAccount(common.HexToAddress(addrString))
obj.SetCode(code)
obj.SetBalance(balance)
obj.SetNonce(nonce)
for k, v := range acct.Storage {
statedb.SetState(common.HexToAddress(addrString), common.HexToHash(k), common.HexToHash(v))
}
}
root, err := statedb.Commit()
if err != nil {
return nil, fmt.Errorf("error writing state: %v", err)
}
if t.Genesis.Root() != root {
return nil, fmt.Errorf("computed state root does not match genesis block: genesis=%x computed=%x", t.Genesis.Root().Bytes()[:4], root.Bytes()[:4])
}
return statedb, nil
}
示例7: benchStateTest
func benchStateTest(ruleSet RuleSet, test VmTest, env map[string]string, b *testing.B) {
b.StopTimer()
db, _ := ethdb.NewMemDatabase()
statedb, _ := state.New(common.Hash{}, db)
for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account)
statedb.SetStateObject(obj)
for a, v := range account.Storage {
obj.SetState(common.HexToHash(a), common.HexToHash(v))
}
}
b.StartTimer()
RunState(ruleSet, statedb, env, test.Exec)
}
示例8: InsertPreState
// InsertPreState populates the given database with the genesis
// accounts defined by the test.
func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) {
db := ethereum.StateDb()
statedb := state.New(common.Hash{}, db)
for addrString, acct := range t.preAccounts {
addr, err := hex.DecodeString(addrString)
if err != nil {
return nil, err
}
code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
if err != nil {
return nil, err
}
balance, ok := new(big.Int).SetString(acct.Balance, 0)
if !ok {
return nil, err
}
nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
if err != nil {
return nil, err
}
if acct.PrivateKey != "" {
privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
err = crypto.ImportBlockTestKey(privkey)
err = ethereum.AccountManager().TimedUnlock(common.BytesToAddress(addr), "", 999999*time.Second)
if err != nil {
return nil, err
}
}
obj := statedb.CreateAccount(common.HexToAddress(addrString))
obj.SetCode(code)
obj.SetBalance(balance)
obj.SetNonce(nonce)
for k, v := range acct.Storage {
statedb.SetState(common.HexToAddress(addrString), common.HexToHash(k), common.HexToHash(v))
}
}
// sync objects to trie
statedb.SyncObjects()
// sync trie to disk
statedb.Sync()
if !bytes.Equal(t.Genesis.Root().Bytes(), statedb.Root().Bytes()) {
return nil, fmt.Errorf("computed state root does not match genesis block %x %x", t.Genesis.Root().Bytes()[:4], statedb.Root().Bytes()[:4])
}
return statedb, nil
}
示例9: dumpBlock
func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
var block *types.Block
if len(call.ArgumentList) > 0 {
if call.Argument(0).IsNumber() {
num, _ := call.Argument(0).ToInteger()
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
} else if call.Argument(0).IsString() {
hash, _ := call.Argument(0).ToString()
block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
} else {
fmt.Println("invalid argument for dump. Either hex string or number")
}
} else {
block = js.ethereum.ChainManager().CurrentBlock()
}
if block == nil {
fmt.Println("block not found")
return otto.UndefinedValue()
}
statedb := state.New(block.Root(), js.ethereum.StateDb())
dump := statedb.RawDump()
return js.re.ToVal(dump)
}
示例10: DumpState
func (self *Gui) DumpState(hash, path string) {
var stateDump []byte
if len(hash) == 0 {
stateDump = self.eth.ChainManager().State().Dump()
} else {
var block *types.Block
if hash[0] == '#' {
i, _ := strconv.Atoi(hash[1:])
block = self.eth.ChainManager().GetBlockByNumber(uint64(i))
} else {
block = self.eth.ChainManager().GetBlock(common.HexToHash(hash))
}
if block == nil {
guilogger.Infof("block err: not found %s\n", hash)
return
}
stateDump = state.New(block.Root(), self.eth.StateDb()).Dump()
}
file, err := os.OpenFile(path[7:], os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
guilogger.Infoln("dump err: ", err)
return
}
defer file.Close()
guilogger.Infof("dumped state (%s) to %s\n", hash, path)
file.Write(stateDump)
}
示例11: TestDelete
func TestDelete(t *testing.T) {
trie := newEmpty()
vals := []struct{ k, v string }{
{"do", "verb"},
{"ether", "wookiedoo"},
{"horse", "stallion"},
{"shaman", "horse"},
{"doge", "coin"},
{"ether", ""},
{"dog", "puppy"},
{"shaman", ""},
}
for _, val := range vals {
if val.v != "" {
updateString(trie, val.k, val.v)
} else {
deleteString(trie, val.k)
}
}
hash := trie.Hash()
exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
if hash != exp {
t.Errorf("expected %x got %x", exp, hash)
}
}
示例12: TestSecureDelete
func TestSecureDelete(t *testing.T) {
trie := newEmptySecure()
vals := []struct{ k, v string }{
{"do", "verb"},
{"ether", "wookiedoo"},
{"horse", "stallion"},
{"shaman", "horse"},
{"doge", "coin"},
{"ether", ""},
{"dog", "puppy"},
{"shaman", ""},
}
for _, val := range vals {
if val.v != "" {
trie.Update([]byte(val.k), []byte(val.v))
} else {
trie.Delete([]byte(val.k))
}
}
hash := trie.Hash()
exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
if hash != exp {
t.Errorf("expected %x got %x", exp, hash)
}
}
示例13: checkLogs
func checkLogs(tlog []Log, logs state.Logs) error {
if len(tlog) != len(logs) {
return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs))
} else {
for i, log := range tlog {
if common.HexToAddress(log.AddressF) != logs[i].Address {
return fmt.Errorf("log address expected %v got %x", log.AddressF, logs[i].Address)
}
if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) {
return fmt.Errorf("log data expected %v got %x", log.DataF, logs[i].Data)
}
if len(log.TopicsF) != len(logs[i].Topics) {
return fmt.Errorf("log topics length expected %d got %d", len(log.TopicsF), logs[i].Topics)
} else {
for j, topic := range log.TopicsF {
if common.HexToHash(topic) != logs[i].Topics[j] {
return fmt.Errorf("log topic[%d] expected %v got %x", j, topic, logs[i].Topics[j])
}
}
}
genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
return fmt.Errorf("bloom mismatch")
}
}
}
return nil
}
示例14: EthTransactionByHash
func (self *XEth) EthTransactionByHash(hash string) (tx *types.Transaction, blhash common.Hash, blnum *big.Int, txi uint64) {
data, _ := self.backend.ExtraDb().Get(common.FromHex(hash))
if len(data) != 0 {
tx = types.NewTransactionFromBytes(data)
} else { // check pending transactions
tx = self.backend.TxPool().GetTransaction(common.HexToHash(hash))
}
// meta
var txExtra struct {
BlockHash common.Hash
BlockIndex uint64
Index uint64
}
v, _ := self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0001))
r := bytes.NewReader(v)
err := rlp.Decode(r, &txExtra)
if err == nil {
blhash = txExtra.BlockHash
blnum = big.NewInt(int64(txExtra.BlockIndex))
txi = txExtra.Index
} else {
glog.V(logger.Error).Infoln(err)
}
return
}
示例15: SubmitWork
func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) {
args := new(SubmitWorkArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, shared.NewDecodeParamError(err.Error())
}
return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil
}