Skip to content

Commit

Permalink
Refactor store into separate package
Browse files Browse the repository at this point in the history
Was getting really messy with store and blocks sharing a package, split
them out and tidy up database access to prevent parallel access
  • Loading branch information
frankh committed Jan 28, 2018
1 parent 0c90ca2 commit 2cfaf43
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 201 deletions.
40 changes: 2 additions & 38 deletions blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const LiveGenesisBlockHash rai.BlockHash = "991CF190094C00F0B68E2E5F75F6BEE95A2E
const LiveGenesisSourceHash rai.BlockHash = "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA"

var GenesisAmount uint128.Uint128 = uint128.FromInts(0xffffffffffffffff, 0xffffffffffffffff)
var WorkThreshold = uint64(0xffffffc000000000)

const TestPrivateKey string = "34F0A37AAD20F4A260F0A5B3CB3D7FB50673212263E58A380BC10474BB039CE4"

Expand All @@ -41,17 +42,6 @@ var LiveGenesisBlock = FromJson([]byte(`{
"signature": "9F0C933C8ADE004D808EA1985FA746A7E95BA2A38F867640F53EC8F180BDFE9E2C1268DEAD7C2664F356E37ABA362BC58E46DBA03E523A7B5A19E4B6EB12BB02"
}`)).(*OpenBlock)

var LiveConfig = Config{
"db.sqlite",
LiveGenesisBlock,
0xffffffc000000000,
}
var TestConfig = Config{
":memory:",
TestGenesisBlock,
0xffffffc000000000,
}

type BlockType string

const (
Expand All @@ -63,12 +53,10 @@ const (

type Block interface {
Type() BlockType
GetBalance() uint128.Uint128
GetSignature() rai.Signature
GetWork() rai.Work
RootHash() rai.BlockHash
Hash() rai.BlockHash
Previous() Block
PreviousBlockHash() rai.BlockHash
}

Expand Down Expand Up @@ -120,30 +108,6 @@ func (b *SendBlock) Hash() rai.BlockHash {
return rai.BlockHashFromBytes(HashSend(b.PreviousHash, b.Destination, b.Balance))
}

func (b *OpenBlock) Source() *SendBlock {
return FetchBlock(b.SourceHash).(*SendBlock)
}

func (b *ReceiveBlock) Source() *SendBlock {
return FetchBlock(b.SourceHash).(*SendBlock)
}

func (b *ReceiveBlock) Previous() Block {
return FetchBlock(b.PreviousHash)
}

func (b *ChangeBlock) Previous() Block {
return FetchBlock(b.PreviousHash)
}

func (b *SendBlock) Previous() Block {
return FetchBlock(b.PreviousHash)
}

func (b *OpenBlock) Previous() Block {
return FetchBlock(b.SourceHash)
}

func (b *ReceiveBlock) PreviousBlockHash() rai.BlockHash {
return b.PreviousHash
}
Expand Down Expand Up @@ -351,7 +315,7 @@ func ValidateWork(block_hash []byte, work []byte) bool {
work_value := hash.Sum(nil)
work_value_int := binary.LittleEndian.Uint64(work_value)

return work_value_int >= Conf.WorkThreshold
return work_value_int >= WorkThreshold
}

func ValidateBlockWork(b Block) bool {
Expand Down
22 changes: 3 additions & 19 deletions blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import (
"testing"
)

var TestConfigLive = Config{
":memory:",
LiveGenesisBlock,
0xffffffc000000000,
}

var TestConfigTest = Config{
":memory:",
TestGenesisBlock,
0xff00000000000000,
}

func TestSignMessage(t *testing.T) {
test_private_key_data := "34F0A37AAD20F4A260F0A5B3CB3D7FB50673212263E58A380BC10474BB039CE4"

Expand All @@ -42,23 +30,19 @@ func TestSignMessage(t *testing.T) {
}

func TestGenerateWork(t *testing.T) {
Init(TestConfigTest)
WorkThreshold = 0xfff0000000000000
GenerateWork(LiveGenesisBlock)
}

func BenchmarkGenerateWork(b *testing.B) {
Init(Config{
":memory:",
LiveGenesisBlock,
0xfff0000000000000,
})
WorkThreshold = 0xfff0000000000000
for n := 0; n < b.N; n++ {
GenerateWork(LiveGenesisBlock)
}
}

func TestValidateWork(t *testing.T) {
Init(TestConfigLive)
WorkThreshold = 0xffffffc000000000

live_block_hash, _ := address.AddressToPub(LiveGenesisBlock.Account)
live_work_bytes, _ := hex.DecodeString(string(LiveGenesisBlock.Work))
Expand Down
3 changes: 2 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/frankh/rai/blocks"
"github.com/frankh/rai/store"
"log"
"net"
)
Expand Down Expand Up @@ -121,7 +122,7 @@ func handleMessage(buf *bytes.Buffer) {
log.Printf("Failed to read publish: %s", err)
} else {
log.Println("Read publish")
blocks.StoreBlock(m.ToBlock())
store.StoreBlock(m.ToBlock())
}
default:
log.Printf("Ignored message. Cannot handle message type %d\n", header.MessageType)
Expand Down
7 changes: 5 additions & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"github.com/frankh/rai"
"github.com/frankh/rai/blocks"
"github.com/frankh/rai/store"
"testing"
)

Expand Down Expand Up @@ -54,7 +55,8 @@ func TestReadWriteMessageKeepAlive(t *testing.T) {
}

func TestReadWriteMessageBody(t *testing.T) {
blocks.Init(blocks.TestConfig)
store.Init(store.TestConfig)

var message MessagePublishOpen
var header MessageHeader
buf := bytes.NewBuffer(publishSend)
Expand Down Expand Up @@ -148,11 +150,12 @@ func TestReadPublish(t *testing.T) {
}

func TestHandleMessage(t *testing.T) {
store.Init(store.TestConfig)
handleMessage(bytes.NewBuffer(publishTest))
}

func TestReadWriteHeader(t *testing.T) {
blocks.Init(blocks.TestConfig)
store.Init(store.TestConfig)
var message MessageHeader
buf := bytes.NewBuffer(publishOpen)
message.ReadHeader(buf)
Expand Down
Loading

0 comments on commit 2cfaf43

Please sign in to comment.