aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/storage.go17
-rw-r--r--core/verification.go21
2 files changed, 28 insertions, 10 deletions
diff --git a/core/storage.go b/core/storage.go
index 8334271..d3dc3da 100644
--- a/core/storage.go
+++ b/core/storage.go
@@ -2,13 +2,13 @@ package core
import (
"bufio"
+ "bytes"
"encoding/binary"
"encoding/json"
"fmt"
+ bolt "go.etcd.io/bbolt"
"io"
"os"
-
- bolt "go.etcd.io/bbolt"
)
type Header struct {
@@ -141,6 +141,8 @@ func ReadDB(partition string) (string, error) {
db := make([]byte, header.TableSize*header.TableUnit)
n, err := io.ReadFull(reader, db)
if err != nil {
+ fmt.Println("failed reading db")
+ fmt.Println(header.TableSize * header.TableUnit)
return "", err
}
if n != header.TableSize*header.TableUnit {
@@ -195,3 +197,14 @@ func GetNode(checksum string, db *bolt.DB) (Node, error) {
}
return node, err
}
+
+func CopyByteArea(start int, end int, reader *bytes.Reader) ([]byte, error) {
+ bytes := make([]byte, end-start)
+ n, err := reader.ReadAt(bytes, int64(start))
+ if err != nil {
+ return nil, err
+ } else if n != end-start {
+ return nil, fmt.Errorf("Unable to read requested size. Got %d, expected %d", n, end-start)
+ }
+ return bytes, nil
+}
diff --git a/core/verification.go b/core/verification.go
index b0ce367..1193664 100644
--- a/core/verification.go
+++ b/core/verification.go
@@ -2,6 +2,7 @@ package core
import (
"bufio"
+ "bytes"
"fmt"
"os"
"strings"
@@ -11,7 +12,7 @@ import (
"github.com/tarm/serial"
)
-var TotalReadBlocks int = 0
+//var TotalReadBlocks int = 0
func fileReadKey() (string, error) {
if _, err := os.Stat(config.KeyLocation); os.IsNotExist(err) {
@@ -92,16 +93,20 @@ func ReadKey() (string, error) {
return "", nil
}
-func ReadBlock(node Node, part *bufio.Reader) ([]byte, error) {
+func ReadBlock(node Node, part *bytes.Reader, totalReadBlocks int) ([]byte, int, error) {
block := make([]byte, node.BlockEnd-node.BlockStart)
blockSize := node.BlockEnd - node.BlockStart
- _, err := part.Discard(node.BlockStart)
+ _, err := part.Seek(int64(node.BlockStart), 0)
if err != nil {
- return []byte{}, err
+ return []byte{}, -1, err
}
- block, err = part.Peek(blockSize)
- TotalReadBlocks = TotalReadBlocks + blockSize
- return block, err
+ n, err := part.Read(block)
+ if err != nil {
+ return block, -1, err
+ } else if n != blockSize {
+ return block, -1, fmt.Errorf("Did not read correct amount of bytes. Expected: %d, Got: %d", blockSize, n)
+ }
+ return block, totalReadBlocks + 1, err
}
func VerifySignature(key string, signature string, database string) (bool, error) {
@@ -127,7 +132,7 @@ func VerifyBlock(block []byte, node Node) error {
if strings.Compare(calculatedBlockHash, strings.TrimSpace(wantedBlockHash)) == 0 {
return nil
}
- return fmt.Errorf("Error: Node %s ranging from %d to %d does not match block", node.PrevNodeSum, node.BlockStart, node.BlockEnd)
+ return fmt.Errorf("Error: Node %s ranging from %d to %d does not match block. Expected %s, got %s.", node.PrevNodeSum, node.BlockStart, node.BlockEnd, wantedBlockHash, calculatedBlockHash)
}
func VerifyNode(node Node, nextNode Node) error {