func (h *Hasher) writeNode(n *node, isLeft bool, s []byte) {
	var err error
	level := 1
	for {
		if n == nil {
			h.result <- s
			return
		}
		if isLeft {
			n.left = s
		} else {
			n.right = s
		}
		if n.toggle() {
			return
		}
		s, err = doHash(n.hasher, n.left, n.right)
		if err != nil {
			select {
			case h.errc <- err:
			default:
			}
			return
		}
		isLeft = n.isLeft
		n = n.parent
		level++
	}
}
writeNode pushes the data to the node.
if it is the first of 2 sisters written, the routine terminates.
if it is the second, it calculates the hash and writes it
to the parent node recursively.
since hashing the parent is synchronous the same hasher can be used.
package bmt
import (
	"encoding/binary"
	"hash"
	"github.com/ethsana/sana/pkg/swarm"
)
var _ Hash = (*Hasher)(nil)
var (
	zerospan    = make([]byte, 8)
	zerosection = make([]byte, 64)
)
type Hasher struct {
	*Conf               
	bmt    *tree        
	size   int          
	pos    int          
	offset int          
	result chan []byte  
	errc   chan error   
	span   []byte       
}
func (h *Hasher) Capacity() int {
	return h.maxSize
}
func LengthToSpan(length int64) []byte {
	span := make([]byte, SpanSize)
	binary.LittleEndian.PutUint64(span, uint64(length))
	return span
}
func (h *Hasher) SetHeaderInt64(length int64) {
	binary.LittleEndian.PutUint64(h.span, uint64(length))
}
func (h *Hasher) SetHeader(span []byte) {
	copy(h.span, span)
}
func (h *Hasher) Size() int {
	return h.segmentSize
}
func (h *Hasher) BlockSize() int {
	return 2 * h.segmentSize
}
func (h *Hasher) Hash(b []byte) ([]byte, error) {
	if h.size == 0 {
		return sha3hash(h.span, h.zerohashes[h.depth])
	}
	copy(h.bmt.buffer[h.size:], zerosection)
	go h.processSection(h.pos, true)
	select {
	case result := <-h.result:
		return sha3hash(h.span, result)
	case err := <-h.errc:
		return nil, err
	}
}
func (h *Hasher) Sum(b []byte) []byte {
	s, _ := h.Hash(b)
	return s
}
func (h *Hasher) Write(b []byte) (int, error) {
	l := len(b)
	max := h.maxSize - h.size
	if l > max {
		l = max
	}
	copy(h.bmt.buffer[h.size:], b)
	secsize := 2 * h.segmentSize
	from := h.size / secsize
	h.offset = h.size % secsize
	h.size += l
	to := h.size / secsize
	if l == max {
		to--
	}
	h.pos = to
	for i := from; i < to; i++ {
		go h.processSection(i, false)
	}
	return l, nil
}
func (h *Hasher) Reset() {
	h.pos = 0
	h.size = 0
	h.offset = 0
	copy(h.span, zerospan)
}
func (h *Hasher) processSection(i int, final bool) {
	secsize := 2 * h.segmentSize
	offset := i * secsize
	level := 1
	n := h.bmt.leaves[i]
	isLeft := n.isLeft
	hasher := n.hasher
	n = n.parent
	section, err := doHash(hasher, h.bmt.buffer[offset:offset+secsize])
	if err != nil {
		select {
		case h.errc <- err:
		default:
		}
		return
	}
	if final {
		h.writeFinalNode(level, n, isLeft, section)
	} else {
		h.writeNode(n, isLeft, section)
	}
}