diff --git a/chain/types/bitfield.go b/chain/types/bitfield.go index 1f46e9451..b4687f5e8 100644 --- a/chain/types/bitfield.go +++ b/chain/types/bitfield.go @@ -16,7 +16,10 @@ type BitField struct { } func NewBitField() BitField { - rle, _ := rlepluslazy.FromBuf([]byte{}) + rle, err := rlepluslazy.FromBuf([]byte{}) + if err != nil { + panic(err) + } return BitField{ rle: rle, bits: make(map[uint64]struct{}), @@ -70,7 +73,7 @@ func (bf BitField) Count() (uint64, error) { return rlepluslazy.Count(s) } -// All returns all set bits, in random order +// All returns all set bits func (bf BitField) All() ([]uint64, error) { runs, err := bf.sum() diff --git a/docs/rewards.m b/docs/rewards.m deleted file mode 100644 index 28dbe4760..000000000 --- a/docs/rewards.m +++ /dev/null @@ -1,29 +0,0 @@ - -HalvingPeriod = vpa(6 * 365 * 24 * 60 * 2); -AdjusmentPeriod = vpa(20160); -Decay = exp(log(vpa(0.5)) / HalvingPeriod * AdjusmentPeriod); -Total = vpa(1400e6); -IV = Total * (1-Decay) / AdjusmentPeriod; - - -syms R(x) -R(x) = IV * (Decay.^floor(x/AdjusmentPeriod)); - -Hmax = HalvingPeriod*5; -heights = linspace(0, Hmax, Hmax/AdjusmentPeriod); -Rewards = R(heights); - -R2 = zeros(size(heights)); - -coffer = Total; -for h = 1:size(heights,2) - k = IV*(coffer/Total); - coffer = coffer - k*AdjusmentPeriod; - R2(h) = k; -end -hYears = heights/2/60/24/365; -plot(hYears, Rewards, 'go', hYears, R2, 'r-') -legend('formula', 'incremental'); - -for6y = Rewards(1:HalvingPeriod/AdjusmentPeriod)*AdjusmentPeriod; -inc6t = R2(1:HalvingPeriod/AdjusmentPeriod)*AdjusmentPeriod; diff --git a/docs/rewards.m~ b/docs/rewards.m~ deleted file mode 100644 index 2f2f57c37..000000000 --- a/docs/rewards.m~ +++ /dev/null @@ -1,30 +0,0 @@ - -HalvingPeriod = vpa(6 * 365 * 24 * 60 * 2); -AdjusmentPeriod = vpa(20160); -Decay = exp(log(vpa(0.5)) / HalvingPeriod * AdjusmentPeriod); -Total = vpa(1400e6); -IV = Total * (1-Decay) / AdjusmentPeriod; - - - -syms R(x) -R(x) = IV * (Decay.^floor(x/AdjusmentPeriod)); - -Hmax = HalvingPeriod*10; -heights = linspace(0, Hmax, HalvingPeriod/AdjusmentPeriod); -Rewards = R(heights); - -plot(heights/2/60/24/365, Rewards, 'r-') - -R2 = zeros(size(heights)); - -coffer = Total; -for h = 1:size(heights,2) - - k = IV*(coffer/Total); - coffer = coffer - k*AdjusmentPeriod; - R2(h) = k; - -end -hYears = heights/2/60/24/365; -plot(hYears, Rewards, 'r-', hYears, R2, 'g-') diff --git a/docs/rewards1.jpg b/docs/rewards1.jpg deleted file mode 100644 index 7fd4bd73e..000000000 Binary files a/docs/rewards1.jpg and /dev/null differ diff --git a/docs/rewards2.m b/docs/rewards2.m deleted file mode 100644 index 67e9c8e29..000000000 --- a/docs/rewards2.m +++ /dev/null @@ -1,19 +0,0 @@ -syms height; - - -IV = 153; -HalvingPeriod = (6 * 365 * 24 * 60 * 2); -AdjusmentPeriod = vpa(20160); -Total = vpa(1400e6); - -syms coffer(height) -syms R(height); -syms h -%coffer(height) = piecewise(height <= 0, Total, height > 0, Total - symsum(R(h), h, 0, height)); -coffer(height) = piecewise(height < 0, Total, height >= 0, coffer(height-1) - R(height)); -R(height) = coffer(height-1)/Total * IV; -syms x -r = R(x); - -fplot(r, [0, 5]); -fibonacci \ No newline at end of file diff --git a/lib/rlepluslazy/bitvec.go.bak b/lib/rlepluslazy/bitvec.go.bak deleted file mode 100644 index da713bc43..000000000 --- a/lib/rlepluslazy/bitvec.go.bak +++ /dev/null @@ -1,63 +0,0 @@ -package rlepluslazy - -type rbitvec struct { - index int - - bits uint64 - bitCap byte - - vec []byte -} - -func readBitvec(vec []byte) *rbitvec { - bv := &rbitvec{vec: vec} - for n := 7; n >= 0; n-- { - var o uint64 - if len(bv.vec) > n { - o = uint64(bv.vec[n]) - } - - bv.bits = bv.bits<<8 | o - bv.index++ - } - bv.bitCap = 64 - return bv -} - -const ( - minCap = 8 -) - -var bitMasks = [9]byte{ - 0x0, - 0x1, - 0x3, - 0x7, - 0xF, - 0x1F, - 0x3F, - 0x7F, - 0xFF, -} - -func (bv *rbitvec) Get(count byte) byte { - res := byte(bv.bits) & bitMasks[count] - bv.bits = bv.bits >> count - bv.bitCap = bv.bitCap - count - - if bv.bitCap < minCap { - var add uint64 - for n := 6; n >= 0; n-- { - var o uint64 - if len(bv.vec) > bv.index+n { - o = uint64(bv.vec[bv.index+n]) - } - - add = add<<8 | o - } - bv.index = bv.index + 7 - bv.bits = bv.bits | add<<(bv.bitCap) - bv.bitCap = bv.bitCap + 7*8 - } - return res -} diff --git a/lib/rlepluslazy/rleplus.go b/lib/rlepluslazy/rleplus.go index 6bf8f3789..100007d65 100644 --- a/lib/rlepluslazy/rleplus.go +++ b/lib/rlepluslazy/rleplus.go @@ -45,150 +45,3 @@ func (rle *RLE) Count() (uint64, error) { } return Count(it) } - -/* - -type change struct { - set bool - reset bool - index uint64 -} -func (c change) valid() bool { - return c.reset || c.set -} - -func (rle *RLE) RunIterator() (RunIterator, error) { - if err != nil { - return nil, err - } - - if len(rle.changes) == 0 { - return source, nil - } - - ch := rle.changes - // using stable sort because we want to preserve change order - sort.SliceStable(ch, func(i int, j int) bool { - return ch[i].index < ch[j].index - }) - for i := range ch { - if i+1 >= len(ch) { - break - } - if ch[i].index == ch[i+1].index { - ch[i].set = false - ch[i].reset = false - } - } - - sets := make([]uint64, 0, len(ch)/2) - clears := make([]uint64, 0, len(ch)/2) - for _, c := range ch { - if c.set { - sets = append(sets, c.index) - } else if c.reset { - clears = append(clears, c.index) - } - } - - setRuns, err := RunsFromSlice(sets) - if err != nil { - return nil, err - } - clearRuns, err := RunsFromSlice(clears) - if err != nil { - return nil, err - } - - it := &chit{source: source, sets: setRuns, clears: clearRuns} - return nil, nil -} - -type chit struct { - source RunIterator - sets RunIterator - clears RunIterator - - next Run - - nextSource Run - nextSet Run - nextClear Run -} - -func (it *chit) prep() error { - var err error - if !it.nextSource.Valid() && it.source.HasNext() { - it.nextSource, err = it.source.NextRun() - if err != nil { - return err - } - } - - if !it.nextSet.Valid() && it.sets.HasNext() { - it.nextSet, err = it.sets.NextRun() - if err != nil { - return err - } - } - - if !it.nextClear.Valid() && it.clears.HasNext() { - it.nextClear, err = it.clears.NextRun() - if err != nil { - return err - } - } - - for len(it.changes) != 0 && !it.changes[0].valid() { - it.changes = it.changes[1:] - } - - var ch change - if len(it.changes) != 0 { - ch = it.changes[0] - } - - if it.source.HasNext() { - var err error - it.nextRun, err = it.source.NextRun() - if err != nil { - return err - } - } - - if ch.valid() && ch.index < it.runIndex+it.nextRun.Len { - if ch.set != it.nextRun.Val { - // split run - whole := it.nextRun - it.nextRun.Len = ch.index - it.runIndex - - } - - // if we are here then change was valid so len(it.changes) != 0 - it.changes = it.changes[1:] - } else { - it.runIndex = it.runIndex + it.nextRun.Len - } - return nil -} - -func (it *chit) HasNext() bool { - return it.nextRun.Valid() -} - -func (it *chit) NextRun() (Run, error) { - return it.nextRun, it.prep() -} - -func (rle *RLE) Set(index uint64) { - rle.changes = append(rle.changes, change{set: true, index: index}) -} - -func (rle *RLE) Clear(index uint64) { - rle.changes = append(rle.changes, change{reset: true, index: index}) -} - -func (rle *RLE) Merge(other *RLE) RunIterator { - return nil -} -*/ diff --git a/lib/rlepluslazy/rleplus_reader.go b/lib/rlepluslazy/rleplus_reader.go index 5fff2ccd6..0b3a77da0 100644 --- a/lib/rlepluslazy/rleplus_reader.go +++ b/lib/rlepluslazy/rleplus_reader.go @@ -1,8 +1,7 @@ package rlepluslazy import ( - "encoding/binary" - + "github.com/multiformats/go-varint" "golang.org/x/xerrors" ) @@ -64,7 +63,12 @@ func (it *rleIterator) prep() error { return xerrors.Errorf("run too long: %w", ErrDecode) } } - it.nextRun.Len, _ = binary.Uvarint(buf) + var err error + it.nextRun.Len, _, err = varint.FromUvarint(buf) + if err != nil { + return err + } + } } diff --git a/lib/rlepluslazy/runs.go b/lib/rlepluslazy/runs.go index be2ed5bcc..034a449c9 100644 --- a/lib/rlepluslazy/runs.go +++ b/lib/rlepluslazy/runs.go @@ -59,7 +59,7 @@ func (it *addIt) prep() error { return nil } - if !it.arun.Val && !it.brun.Val { + if !(it.arun.Val || it.brun.Val) { min := it.arun.Len if it.brun.Len < min { min = it.brun.Len