How to use Hash method of diff Package

Best Got code snippet using diff.Hash

difflayer_test.go

Source:difflayer_test.go Github

copy

Full Screen

...22	"github.com/ethereum/go-ethereum/common"23	"github.com/ethereum/go-ethereum/crypto"24	"github.com/ethereum/go-ethereum/ethdb/memorydb"25)26func copyDestructs(destructs map[common.Hash]struct{}) map[common.Hash]struct{} {27	copy := make(map[common.Hash]struct{})28	for hash := range destructs {29		copy[hash] = struct{}{}30	}31	return copy32}33func copyAccounts(accounts map[common.Hash][]byte) map[common.Hash][]byte {34	copy := make(map[common.Hash][]byte)35	for hash, blob := range accounts {36		copy[hash] = blob37	}38	return copy39}40func copyStorage(storage map[common.Hash]map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {41	copy := make(map[common.Hash]map[common.Hash][]byte)42	for accHash, slots := range storage {43		copy[accHash] = make(map[common.Hash][]byte)44		for slotHash, blob := range slots {45			copy[accHash][slotHash] = blob46		}47	}48	return copy49}50// TestMergeBasics tests some simple merges51func TestMergeBasics(t *testing.T) {52	var (53		destructs = make(map[common.Hash]struct{})54		accounts  = make(map[common.Hash][]byte)55		storage   = make(map[common.Hash]map[common.Hash][]byte)56	)57	// Fill up a parent58	for i := 0; i < 100; i++ {59		h := randomHash()60		data := randomAccount()61		accounts[h] = data62		if rand.Intn(4) == 0 {63			destructs[h] = struct{}{}64		}65		if rand.Intn(2) == 0 {66			accStorage := make(map[common.Hash][]byte)67			value := make([]byte, 32)68			rand.Read(value)69			accStorage[randomHash()] = value70			storage[h] = accStorage71		}72	}73	// Add some (identical) layers on top74	parent := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))75	child := newDiffLayer(parent, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))76	child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))77	child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))78	child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))79	// And flatten80	merged := (child.flatten()).(*diffLayer)81	{ // Check account lists82		if have, want := len(merged.accountList), 0; have != want {83			t.Errorf("accountList wrong: have %v, want %v", have, want)84		}85		if have, want := len(merged.AccountList()), len(accounts); have != want {86			t.Errorf("AccountList() wrong: have %v, want %v", have, want)87		}88		if have, want := len(merged.accountList), len(accounts); have != want {89			t.Errorf("accountList [2] wrong: have %v, want %v", have, want)90		}91	}92	{ // Check account drops93		if have, want := len(merged.destructSet), len(destructs); have != want {94			t.Errorf("accountDrop wrong: have %v, want %v", have, want)95		}96	}97	{ // Check storage lists98		i := 099		for aHash, sMap := range storage {100			if have, want := len(merged.storageList), i; have != want {101				t.Errorf("[1] storageList wrong: have %v, want %v", have, want)102			}103			list, _ := merged.StorageList(aHash)104			if have, want := len(list), len(sMap); have != want {105				t.Errorf("[2] StorageList() wrong: have %v, want %v", have, want)106			}107			if have, want := len(merged.storageList[aHash]), len(sMap); have != want {108				t.Errorf("storageList wrong: have %v, want %v", have, want)109			}110			i++111		}112	}113}114// TestMergeDelete tests some deletion115func TestMergeDelete(t *testing.T) {116	var (117		storage = make(map[common.Hash]map[common.Hash][]byte)118	)119	// Fill up a parent120	h1 := common.HexToHash("0x01")121	h2 := common.HexToHash("0x02")122	flipDrops := func() map[common.Hash]struct{} {123		return map[common.Hash]struct{}{124			h2: {},125		}126	}127	flipAccs := func() map[common.Hash][]byte {128		return map[common.Hash][]byte{129			h1: randomAccount(),130		}131	}132	flopDrops := func() map[common.Hash]struct{} {133		return map[common.Hash]struct{}{134			h1: {},135		}136	}137	flopAccs := func() map[common.Hash][]byte {138		return map[common.Hash][]byte{139			h2: randomAccount(),140		}141	}142	// Add some flipAccs-flopping layers on top143	parent := newDiffLayer(emptyLayer(), common.Hash{}, flipDrops(), flipAccs(), storage)144	child := parent.Update(common.Hash{}, flopDrops(), flopAccs(), storage)145	child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)146	child = child.Update(common.Hash{}, flopDrops(), flopAccs(), storage)147	child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)148	child = child.Update(common.Hash{}, flopDrops(), flopAccs(), storage)149	child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)150	if data, _ := child.Account(h1); data == nil {151		t.Errorf("last diff layer: expected %x account to be non-nil", h1)152	}153	if data, _ := child.Account(h2); data != nil {154		t.Errorf("last diff layer: expected %x account to be nil", h2)155	}156	if _, ok := child.destructSet[h1]; ok {157		t.Errorf("last diff layer: expected %x drop to be missing", h1)158	}159	if _, ok := child.destructSet[h2]; !ok {160		t.Errorf("last diff layer: expected %x drop to be present", h1)161	}162	// And flatten163	merged := (child.flatten()).(*diffLayer)164	if data, _ := merged.Account(h1); data == nil {165		t.Errorf("merged layer: expected %x account to be non-nil", h1)166	}167	if data, _ := merged.Account(h2); data != nil {168		t.Errorf("merged layer: expected %x account to be nil", h2)169	}170	if _, ok := merged.destructSet[h1]; !ok { // Note, drops stay alive until persisted to disk!171		t.Errorf("merged diff layer: expected %x drop to be present", h1)172	}173	if _, ok := merged.destructSet[h2]; !ok { // Note, drops stay alive until persisted to disk!174		t.Errorf("merged diff layer: expected %x drop to be present", h1)175	}176	// If we add more granular metering of memory, we can enable this again,177	// but it's not implemented for now178	//if have, want := merged.memory, child.memory; have != want {179	//	t.Errorf("mem wrong: have %d, want %d", have, want)180	//}181}182// This tests that if we create a new account, and set a slot, and then merge183// it, the lists will be correct.184func TestInsertAndMerge(t *testing.T) {185	// Fill up a parent186	var (187		acc    = common.HexToHash("0x01")188		slot   = common.HexToHash("0x02")189		parent *diffLayer190		child  *diffLayer191	)192	{193		var (194			destructs = make(map[common.Hash]struct{})195			accounts  = make(map[common.Hash][]byte)196			storage   = make(map[common.Hash]map[common.Hash][]byte)197		)198		parent = newDiffLayer(emptyLayer(), common.Hash{}, destructs, accounts, storage)199	}200	{201		var (202			destructs = make(map[common.Hash]struct{})203			accounts  = make(map[common.Hash][]byte)204			storage   = make(map[common.Hash]map[common.Hash][]byte)205		)206		accounts[acc] = randomAccount()207		storage[acc] = make(map[common.Hash][]byte)208		storage[acc][slot] = []byte{0x01}209		child = newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)210	}211	// And flatten212	merged := (child.flatten()).(*diffLayer)213	{ // Check that slot value is present214		have, _ := merged.Storage(acc, slot)215		if want := []byte{0x01}; !bytes.Equal(have, want) {216			t.Errorf("merged slot value wrong: have %x, want %x", have, want)217		}218	}219}220func emptyLayer() *diskLayer {221	return &diskLayer{222		diskdb: memorydb.New(),223		cache:  fastcache.New(500 * 1024),224	}225}226// BenchmarkSearch checks how long it takes to find a non-existing key227// BenchmarkSearch-6   	  200000	     10481 ns/op (1K per layer)228// BenchmarkSearch-6   	  200000	     10760 ns/op (10K per layer)229// BenchmarkSearch-6   	  100000	     17866 ns/op230//231// BenchmarkSearch-6   	  500000	      3723 ns/op (10k per layer, only top-level RLock()232func BenchmarkSearch(b *testing.B) {233	// First, we set up 128 diff layers, with 1K items each234	fill := func(parent snapshot) *diffLayer {235		var (236			destructs = make(map[common.Hash]struct{})237			accounts  = make(map[common.Hash][]byte)238			storage   = make(map[common.Hash]map[common.Hash][]byte)239		)240		for i := 0; i < 10000; i++ {241			accounts[randomHash()] = randomAccount()242		}243		return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)244	}245	var layer snapshot246	layer = emptyLayer()247	for i := 0; i < 128; i++ {248		layer = fill(layer)249	}250	key := crypto.Keccak256Hash([]byte{0x13, 0x38})251	b.ResetTimer()252	for i := 0; i < b.N; i++ {253		layer.AccountRLP(key)254	}255}256// BenchmarkSearchSlot checks how long it takes to find a non-existing key257// - Number of layers: 128258// - Each layers contains the account, with a couple of storage slots259// BenchmarkSearchSlot-6   	  100000	     14554 ns/op260// BenchmarkSearchSlot-6   	  100000	     22254 ns/op (when checking parent root using mutex)261// BenchmarkSearchSlot-6   	  100000	     14551 ns/op (when checking parent number using atomic)262// With bloom filter:263// BenchmarkSearchSlot-6   	 3467835	       351 ns/op264func BenchmarkSearchSlot(b *testing.B) {265	// First, we set up 128 diff layers, with 1K items each266	accountKey := crypto.Keccak256Hash([]byte{0x13, 0x37})267	storageKey := crypto.Keccak256Hash([]byte{0x13, 0x37})268	accountRLP := randomAccount()269	fill := func(parent snapshot) *diffLayer {270		var (271			destructs = make(map[common.Hash]struct{})272			accounts  = make(map[common.Hash][]byte)273			storage   = make(map[common.Hash]map[common.Hash][]byte)274		)275		accounts[accountKey] = accountRLP276		accStorage := make(map[common.Hash][]byte)277		for i := 0; i < 5; i++ {278			value := make([]byte, 32)279			rand.Read(value)280			accStorage[randomHash()] = value281			storage[accountKey] = accStorage282		}283		return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)284	}285	var layer snapshot286	layer = emptyLayer()287	for i := 0; i < 128; i++ {288		layer = fill(layer)289	}290	b.ResetTimer()291	for i := 0; i < b.N; i++ {292		layer.Storage(accountKey, storageKey)293	}294}295// With accountList and sorting296// BenchmarkFlatten-6   	      50	  29890856 ns/op297//298// Without sorting and tracking accountList299// BenchmarkFlatten-6   	     300	   5511511 ns/op300func BenchmarkFlatten(b *testing.B) {301	fill := func(parent snapshot) *diffLayer {302		var (303			destructs = make(map[common.Hash]struct{})304			accounts  = make(map[common.Hash][]byte)305			storage   = make(map[common.Hash]map[common.Hash][]byte)306		)307		for i := 0; i < 100; i++ {308			accountKey := randomHash()309			accounts[accountKey] = randomAccount()310			accStorage := make(map[common.Hash][]byte)311			for i := 0; i < 20; i++ {312				value := make([]byte, 32)313				rand.Read(value)314				accStorage[randomHash()] = value315			}316			storage[accountKey] = accStorage317		}318		return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)319	}320	b.ResetTimer()321	for i := 0; i < b.N; i++ {322		b.StopTimer()323		var layer snapshot324		layer = emptyLayer()325		for i := 1; i < 128; i++ {326			layer = fill(layer)327		}328		b.StartTimer()329		for i := 1; i < 128; i++ {330			dl, ok := layer.(*diffLayer)331			if !ok {332				break333			}334			layer = dl.flatten()335		}336		b.StopTimer()337	}338}339// This test writes ~324M of diff layers to disk, spread over340// - 128 individual layers,341// - each with 200 accounts342// - containing 200 slots343//344// BenchmarkJournal-6   	       1	1471373923 ns/ops345// BenchmarkJournal-6   	       1	1208083335 ns/op // bufio writer346func BenchmarkJournal(b *testing.B) {347	fill := func(parent snapshot) *diffLayer {348		var (349			destructs = make(map[common.Hash]struct{})350			accounts  = make(map[common.Hash][]byte)351			storage   = make(map[common.Hash]map[common.Hash][]byte)352		)353		for i := 0; i < 200; i++ {354			accountKey := randomHash()355			accounts[accountKey] = randomAccount()356			accStorage := make(map[common.Hash][]byte)357			for i := 0; i < 200; i++ {358				value := make([]byte, 32)359				rand.Read(value)360				accStorage[randomHash()] = value361			}362			storage[accountKey] = accStorage363		}364		return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)365	}366	layer := snapshot(new(diskLayer))367	for i := 1; i < 128; i++ {368		layer = fill(layer)369	}370	b.ResetTimer()371	for i := 0; i < b.N; i++ {372		layer.Journal(new(bytes.Buffer))373	}374}...

Full Screen

Full Screen

difficulty_test.go

Source:difficulty_test.go Github

copy

Full Screen

...4	"testing"5	. "github.com/pegnet/pegnet/opr"6)7// Just some sample vectors taken from 1 miner on a network8func TestEffectiveHashRate(t *testing.T) {9	vectors := []struct {10		WorstDiff uint6411		BestDiff  uint6412		HashRate  float6413		Height    uint6414	}{15		{WorstDiff: 18446586878042484810, BestDiff: 18446742466885828062, HashRate: 14399.63, Height: 204797},16		{WorstDiff: 18446633144064883604, BestDiff: 18446743743681176119, HashRate: 14337.81, Height: 204796},17		{WorstDiff: 18446587826224330316, BestDiff: 18446740789917189647, HashRate: 14319.85, Height: 204795},18		{WorstDiff: 18446595274788757708, BestDiff: 18446743410821960935, HashRate: 14265.29, Height: 204794},19		{WorstDiff: 18446611696110517928, BestDiff: 18446743091015240431, HashRate: 14235.62, Height: 204793},20		{WorstDiff: 18446572367808766797, BestDiff: 18446739418174104138, HashRate: 14299.25, Height: 204792},21		{WorstDiff: 18446608794088792336, BestDiff: 18446743251485133710, HashRate: 14335.20, Height: 204791},22		{WorstDiff: 18446610101229625045, BestDiff: 18446744035146642516, HashRate: 14332.53, Height: 204790},23		{WorstDiff: 18446625595880705622, BestDiff: 18446739129668856235, HashRate: 14356.89, Height: 204789},24	}25	for _, v := range vectors {26		ehr := EffectiveHashRate(v.WorstDiff, 50)27		diff := math.Abs(ehr - v.HashRate)28		if diff/v.HashRate > 0.25 {29			// 25% tolerance?30			t.Errorf("Hashrate calculated is over 20%% different from the actual. Difference is %.2f%%", diff/v.HashRate*100)31		}32		//fmt.Printf("%d %.4f, %.4f\n", v.Height, ehr, v.HashRate)33		expMin := ExpectedMinimumDifficulty(v.HashRate, 50)34		diff = math.Abs(float64(expMin) - float64(v.WorstDiff))35		if diff/float64(v.WorstDiff) > 0.25 {36			// 25% tolerance?37			t.Errorf("Minimum difficulty calculated is over 20%% different from the actual. Difference is %.2f%%", diff/float64(v.WorstDiff)*100)38		}39		//fm40		//fmt.Printf("%d %x, %x\n", v.Height, expMin, v.WorstDiff)41	}42}...

Full Screen

Full Screen

Hash

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3    fmt.Println("Hello, 世界")4}5import "fmt"6func main() {7    fmt.Println("Hello, 世界")8}9import "fmt"10func main() {11    fmt.Println("Hello, 世界")12}13import "fmt"14func main() {15    fmt.Println("Hello, 世界")16}17import "fmt"18func main() {19    fmt.Println("Hello, 世界")20}21import "fmt"22func main() {23    fmt.Println("Hello, 世界")24}25import "fmt"26func main() {27    fmt.Println("Hello, 世界")28}29import "fmt"30func main() {31    fmt.Println("Hello, 世界")32}33import "fmt"34func main() {35    fmt.Println("Hello, 世界")36}37import "fmt"38func main() {39    fmt.Println("Hello, 世界")40}41import "fmt"42func main() {43    fmt.Println("Hello, 世界")44}45import "fmt"46func main() {47    fmt.Println("Hello, 世界")48}49import "fmt"50func main() {51    fmt.Println("Hello,

Full Screen

Full Screen

Hash

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println("Enter the first string:")4	fmt.Scanln(&a)5	fmt.Println("Enter the second string:")6	fmt.Scanln(&b)7	fmt.Println("The hash value of first string is:", Hash(a))8	fmt.Println("The hash value of second string is:", Hash(b))9	if Hash(a) == Hash(b) {10		fmt.Println("The strings are equal.")11	} else {12		fmt.Println("The strings are not equal.")13	}14}15import (16func main() {17	fmt.Println("Enter the first string:")18	fmt.Scanln(&a)19	fmt.Println("Enter the second string:")20	fmt.Scanln(&b)21	fmt.Println("The hash value of first string is:", Hash(a))22	fmt.Println("The hash value of second string is:", Hash(b))23	if Hash(a) == Hash(b) {24		fmt.Println("The strings are equal.")25	} else {26		fmt.Println("The strings are not equal.")27	}28}

Full Screen

Full Screen

Hash

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	h := crc32.NewIEEE()4	h.Write([]byte("test"))5	v := h.Sum32()6	fmt.Println(v)7	h.Reset()8	h.Write([]byte("test"))9	v = h.Sum32()10	fmt.Println(v)11}

Full Screen

Full Screen

Hash

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	h := adler32.New()4	h.Write([]byte("test"))5	v := h.Sum32()6	fmt.Println(v)7	fmt.Println(adler32.Checksum([]byte("test")))8}9import (10func main() {11	h := adler32.New()12	h.Write([]byte("test"))13	v1 := h.Sum32()14	fmt.Println(v1)15	h.Write([]byte("test"))16	v2 := h.Sum32()17	fmt.Println(v2)18	fmt.Println(adler32.Checksum([]byte("testtest")))19}20import (21func main() {22	h := adler32.New()23	h.Write([]byte("test"))24	v1 := h.Sum32()25	fmt.Println(v1)

Full Screen

Full Screen

Hash

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    fmt.Println(diff.Hash("data"))4}5import (6func main() {7    fmt.Println(diff.Hash("data"))8}9import (10func main() {11    fmt.Println(diff.Hash("data"))12}13import (14func main() {15    fmt.Println(diff.Hash("data"))16}17import (18func main() {19    fmt.Println(diff.Hash("data"))20}21import (22func main() {23    fmt.Println(diff.Hash("data"))24}25import (26func main() {27    fmt.Println(diff.Hash("data"))28}29import (30func main() {31    fmt.Println(diff.Hash("data"))32}33import (34func main() {35    fmt.Println(diff.Hash("data"))36}37import (38func main() {39    fmt.Println(diff.Hash("data"))40}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful