Ejemplo n.º 1
0
// Reconstructs the indices from the underlying Array
func (self *UnspentPool) Rebuild(uxs UxArray) {
	self.Pool = make(map[cipher.SHA256]UxOut, len(uxs))
	xh := cipher.SHA256{}
	for i, _ := range uxs {
		h := uxs[i].Hash()
		self.Pool[h] = uxs[i]
		xh = xh.Xor(uxs[i].SnapshotHash())
	}
	self.XorHash = xh
	if len(self.Pool) != len(uxs) {
		log.Panic("Corrupt UnspentPool array: contains duplicate UxOut")
	}
}
Ejemplo n.º 2
0
func TestUnspentPoolRebuild(t *testing.T) {
	up := NewUnspentPool()
	arr := make(UxArray, 0)
	arr = append(arr, makeUxOut(t))
	arr = append(arr, makeUxOut(t))
	assert.Equal(t, len(up.Pool), 0)
	assert.Equal(t, up.XorHash, cipher.SHA256{})
	up.Rebuild(arr)
	assert.Equal(t, len(up.Pool), 2)
	for _, x := range arr {
		ux, ok := up.Pool[x.Hash()]
		assert.True(t, ok)
		assert.Equal(t, x, ux)
	}
	h := cipher.SHA256{}
	h = h.Xor(arr[0].SnapshotHash())
	h = h.Xor(arr[1].SnapshotHash())
	assert.Equal(t, up.XorHash, h)
	assert.NotEqual(t, up.XorHash, cipher.SHA256{})

	// Duplicate item in array causes panic
	arr = append(arr, arr[0])
	assert.Panics(t, func() { up.Rebuild(arr) })
}