Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
znn committed Jan 22, 2022
1 parent df3c89d commit aee7ab6
Show file tree
Hide file tree
Showing 49 changed files with 118,471 additions and 76 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ Reference Golang implementation of the Alphanet - Network of Momentum Phase 0.

## Building from source

Building `znnd` requires both a Go (version 1.16 or later) and a C compiler. You can install them using your favourite
package manager. Once the dependencies are installed, please run:
Building `znnd` requires both a Go (version 1.16 or later) and a C compiler. You can install them using your favourite package manager. Once the dependencies are installed, please run:

```shell
make znnd
```

## Running `znnd`

Since this is version `0.0.1`, `znnd` is not configured with a genesis file or default seeders.
Use [znn-controller](https://github.com/zenon-network/znn_controller_dart)
to configure your full node. For more information please consult the [Wiki](https://github.com/zenon-network/znn-wiki).
Since version `0.0.2`, `znnd` is configured with the Alphanet Genesis and default seeders.

Use [znn-controller](https://github.com/zenon-network/znn_controller_dart) to configure your full node. For more information please consult the [Wiki](https://github.com/zenon-network/znn-wiki).
24 changes: 24 additions & 0 deletions chain/account_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package chain

import (
"testing"

"github.com/zenon-network/go-zenon/chain/nom"
"github.com/zenon-network/go-zenon/common"
)

func TestAccountPool_filterBlocksToCommit(t *testing.T) {
ap := accountPool{}
MaxAccountBlocksInMomentum = 2
common.Expect(t, len(ap.filterBlocksToCommit([]*nom.AccountBlock{
{Height: 1, BlockType: nom.BlockTypeUserSend},
{Height: 2, BlockType: nom.BlockTypeUserSend},
{Height: 3, BlockType: nom.BlockTypeUserSend},
})), 2)

common.Expect(t, len(ap.filterBlocksToCommit([]*nom.AccountBlock{
{Height: 1, BlockType: nom.BlockTypeContractSend},
{Height: 2, BlockType: nom.BlockTypeContractSend},
{Height: 3, BlockType: nom.BlockTypeUserReceive},
})), 0)
}
3 changes: 2 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zenon-network/go-zenon/chain/store"
"github.com/zenon-network/go-zenon/common"
"github.com/zenon-network/go-zenon/common/db"
"github.com/zenon-network/go-zenon/common/types"
)

var (
Expand Down Expand Up @@ -50,7 +51,7 @@ func (c *chain) Init() error {
if err := c.checkGenesisCompatibility(); err != nil {
return err
}

types.SporkAddress = c.genesis.GetSporkAddress()
c.Register(c.accountPool)

frontierStore := c.GetFrontierMomentumStore()
Expand Down
29 changes: 11 additions & 18 deletions chain/genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"math/big"
"os"
"time"

"github.com/zenon-network/go-zenon/chain/store"
"github.com/zenon-network/go-zenon/common"
Expand All @@ -16,6 +15,13 @@ var (
log = common.ChainLogger.New("submodule", "genesis")
)

func MakeEmbeddedGenesisConfig() (store.Genesis, error) {
if embeddedGenesis == nil {
return nil, ErrNoEmbeddedGenesis
}
return NewGenesis(embeddedGenesis), nil
}

func ReadGenesisConfigFromFile(genesisFile string) (store.Genesis, error) {
defer func() {
if err := recover(); err != nil {
Expand Down Expand Up @@ -53,24 +59,11 @@ func ReadGenesisConfigFromFile(genesisFile string) (store.Genesis, error) {
}
}

// PollGenesisConfigFromFile tries to ReadGenesisConfigFromFile and retries after 10 seconds if file doesn't exist
func PollGenesisConfigFromFile(genesisFile string) (store.Genesis, error) {
for {
if genesisConfig, err := ReadGenesisConfigFromFile(genesisFile); err == ErrInvalidGenesisPath {
select {
case <-time.After(time.Second * 10):
}
} else {
return genesisConfig, err
}
}
}

type GenesisConfig struct {
ChainIdentifier uint64
ExtraData string
GenesisTimestampSec int64
GenesisAccountAddress *types.Address
ChainIdentifier uint64
ExtraData string
GenesisTimestampSec int64
SporkAddress *types.Address

PillarConfig *PillarContractConfig
TokenConfig *TokenContractConfig
Expand Down
83 changes: 83 additions & 0 deletions chain/genesis/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package genesis

import (
"os"
"path"
"testing"

"github.com/zenon-network/go-zenon/common"
"github.com/zenon-network/go-zenon/common/types"
)

var (
emptyGenesisJsonStr = []byte(`
{
"ChainIdentifier": 100,
"ExtraData": "",
"GenesisTimestampSec": 1000000000,
"SporkAddress": "z1qqv2fnc3avjg39dcste4c5lag7l42xyykjf49w",
"PillarConfig": {
"Pillars": [],
"Delegations": [],
"LegacyEntries": []
},
"TokenConfig": {
"Tokens": []
},
"PlasmaConfig": {
"Fusions": []
},
"SwapConfig": {
"Entries": []
},
"SporkConfig": {
"Sporks": []
},
"GenesisBlocks": {
"Blocks": []
}
}`)
emptyHash = types.HexToHashPanic("30a9c36aa27d0b441eff8328a277e417ae0f1661f298f6376858f6819492811a")
)

func TestGenesisToFromJson(t *testing.T) {
genesisFile := path.Join(t.TempDir(), "genesis.json")
err := os.WriteFile(genesisFile, emptyGenesisJsonStr, 777)
common.FailIfErr(t, err)

config, err := ReadGenesisConfigFromFile(genesisFile)
common.FailIfErr(t, err)
common.ExpectString(t, config.GetGenesisMomentum().Hash.String(), emptyHash.String())
}

func TestPartiallyWrittenFile(t *testing.T) {
for i := 0; i < len(emptyGenesisJsonStr)-5; i += 1 {
genesisFile := path.Join(t.TempDir(), "genesis.json")
if err := os.WriteFile(genesisFile, emptyGenesisJsonStr[0:i], 777); err != nil {
t.Fatal(err)
}
if _, err := ReadGenesisConfigFromFile(genesisFile); err != ErrIncompleteGenesisJson {
t.Fatalf("Unexpected error %v `%v`", err, emptyGenesisJsonStr[0:i])
}
}
}

func TestMalformedGenesis(t *testing.T) {
genesisFile := path.Join(t.TempDir(), "genesis.json")
if err := os.WriteFile(genesisFile, []byte(`{"a":"aaaa"]}`), 777); err != nil {
t.Fatal(err)
}
if _, err := ReadGenesisConfigFromFile(genesisFile); err != ErrInvalidGenesisJson {
t.Fatalf("Unexpected error %v", err)
}
}

func TestEmptyGenesis(t *testing.T) {
genesisFile := path.Join(t.TempDir(), "genesis.json")
if err := os.WriteFile(genesisFile, []byte(`{}`), 777); err != nil {
t.Fatal(err)
}
if _, err := ReadGenesisConfigFromFile(genesisFile); err != ErrInvalidGenesisConfig {
t.Fatalf("Unexpected error %v", err)
}
}
18 changes: 18 additions & 0 deletions chain/genesis/embedded_genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package genesis

import (
"encoding/json"

"github.com/zenon-network/go-zenon/common"
)

func init() {
if embeddedGenesis == nil && len(embeddedGenesisStr) != 0 {
embeddedGenesis = new(GenesisConfig)
common.DealWithErr(json.Unmarshal([]byte(embeddedGenesisStr), embeddedGenesis))
}
}

var (
embeddedGenesis *GenesisConfig
)
Loading

0 comments on commit aee7ab6

Please sign in to comment.