Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:FactomProject/factomd into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSnow committed May 20, 2022
2 parents 9eb1876 + 83d99e6 commit 352dce7
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ _obj
_test
build
bin
pkg
.vagrant

# Architecture specific extensions/prefixes
Expand Down
42 changes: 42 additions & 0 deletions Utilities/snapshot/pkg/balances/balances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package balances

import (
"fmt"
"io"

"github.com/FactomProject/factomd/common/factoid"
"github.com/FactomProject/factomd/common/primitives"
)

type Balances struct {
Height uint32
// Use int64s because temporarily during the math, we might have a negative balance
FCTAddressMap map[[32]byte]int64
ECAddressMap map[[32]byte]int64
}

func NewBalances() *Balances {
return &Balances{
FCTAddressMap: make(map[[32]byte]int64),
ECAddressMap: make(map[[32]byte]int64),
}
}

func (bs *Balances) Dump(w io.Writer) error {
height := bs.Height
_, _ = fmt.Fprintf(w, "height %d\n", height)
for k, v := range bs.FCTAddressMap {
_, err := fmt.Fprintf(w, "%s: %d\n", primitives.ConvertFctAddressToUserStr(factoid.NewAddress(k[:])), v)
if err != nil {
return fmt.Errorf("write fct addr: %w", err)
}
}
for k, v := range bs.ECAddressMap {
_, err := fmt.Fprintf(w, "%s: %d\n", primitives.ConvertECAddressToUserStr(factoid.NewAddress(k[:])), v)
if err != nil {
return fmt.Errorf("write ec addr: %w", err)
}
}

return nil
}
59 changes: 59 additions & 0 deletions Utilities/snapshot/pkg/balances/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package balances

import (
"bufio"
"fmt"
"io"
"strconv"
"strings"

"github.com/FactomProject/factomd/common/primitives"
)

func LoadBalances(file io.Reader) (bal *Balances, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic, file format invalid :%v", r)
}
}()

bal = NewBalances()

scanner := bufio.NewScanner(file)
if !scanner.Scan() {
return nil, fmt.Errorf("no lines in the file")
}
first := scanner.Text()
parts := strings.Split(first, " ")
height, err := strconv.Atoi(strings.TrimSpace(parts[1]))
if err != nil {
return nil, fmt.Errorf("parse height: %w", err)
}
bal.Height = uint32(height)

for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
parts := strings.Split(line, ":")
addr := parts[0]
balPart := strings.TrimSpace(parts[1])
balance, err := strconv.ParseInt(balPart, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse balance (%s) %s: %w", addr, balPart, err)
}

var hash [32]byte
copy(hash[:], primitives.ConvertUserStrToAddress(addr)[:])
switch addr[:2] {
case "FA":
bal.FCTAddressMap[hash] = balance
case "EC":
bal.ECAddressMap[hash] = balance
default:
return nil, fmt.Errorf("addr %s not reconigzed", addr)
}
}
return bal, nil
}
108 changes: 108 additions & 0 deletions Utilities/snapshot/pkg/load/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package load

import (
"bufio"
"encoding/base64"
"fmt"
"io"
"strconv"
"strings"

"github.com/FactomProject/FactomCode/common"
)

type Eblock struct {
Height int
KeyMR string
Seq int64
// Indexes in the Entries slice [first:last]
FirstEntry int
LastEntry int
}

type Chain struct {
Height int
Eblocks []*Eblock
Entries []*common.Entry
Head *Eblock
}

func (c Chain) EblockEntries(eblock *Eblock) []*common.Entry {
return c.Entries[eblock.FirstEntry:eblock.LastEntry]
}

func LoadChain(file io.Reader) (chain *Chain, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic, file format invalid :%v", r)
}
}()

c := new(Chain)
c.Entries = make([]*common.Entry, 0)
c.Eblocks = make([]*Eblock, 0)

scanner := bufio.NewScanner(file)
// entIdx is the index of the NEXT entry in the chain.
// This idx has yet to actually be allocated.
var entIdx = 0
var lastEblock *Eblock

for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}

switch line[:2] {
case "eb":
data := strings.TrimPrefix(line, "eb:")
parts := strings.Split(data, " ")
height, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("parse height %s: %w", parts[0], err)
}
if lastEblock != nil {
lastEblock.LastEntry = entIdx
}

next := &Eblock{
Height: height,
KeyMR: parts[1],
FirstEntry: entIdx,
Seq: int64(len(c.Eblocks)),
}
c.Eblocks = append(c.Eblocks, next)
lastEblock = next
case "et":
data64 := strings.TrimPrefix(line, "et:")
data, err := base64.StdEncoding.DecodeString(data64)
if err != nil {
return nil, err
}

entry := common.NewEntry()
left, err := entry.UnmarshalBinaryData([]byte(data))
if err != nil {
return nil, fmt.Errorf("unmarshal entry: %w", err)
}
if len(left) > 0 {
return nil, fmt.Errorf("%d left over bytes", len(left))
}

c.Entries = append(c.Entries, entry)
entIdx++
}
}
if lastEblock != nil {
lastEblock.LastEntry = entIdx
}
c.Head = lastEblock

return c, nil
}

// readLine has some complexity as entries can contain newlines
func readLine(scanner *bufio.Scanner) {

}

0 comments on commit 352dce7

Please sign in to comment.