Skip to content

Commit

Permalink
Merge pull request #1194 from iost-official/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
jiqiren11 authored Jun 2, 2021
2 parents f703a93 + 07ab274 commit 77051a7
Show file tree
Hide file tree
Showing 69 changed files with 4,930 additions and 906 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ linters:
- typecheck
- unconvert
- gofmt
- unused
- varcheck
- asciicheck
- bodyclose
Expand Down Expand Up @@ -66,3 +65,4 @@ linters:
- gocognit
- goconst
- gocyclo
- unused
1 change: 0 additions & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ RUN mkdir /goroot && \
ENV CGO_ENABLED 1
ENV GOROOT /goroot
ENV PATH $GOROOT/bin:$PATH
ENV GO111MODULE on

# Install golangci-lint
RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /usr/local/bin v1.37.0
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ else
$(GO_TEST) -v ./...
endif

e2e_test_local:
$(TARGET_DIR)/itest run a_case
$(TARGET_DIR)/itest run t_case
$(TARGET_DIR)/itest run c_case

e2e_test: image
docker rm -f iserver || true
docker run -d --name iserver $(DOCKER_IMAGE)
Expand Down
100 changes: 59 additions & 41 deletions account/account.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,72 @@
package account

import (
"github.com/iost-official/go-iost/v3/common"
"github.com/iost-official/go-iost/v3/crypto"
)

// KeyPair account of the ios
type KeyPair struct {
Algorithm crypto.Algorithm
Pubkey []byte
Seckey []byte
// Account type of a permission tree
type Account struct {
ID string `json:"id"`
Referrer string `json:"referrer"`
Groups map[string]*Group `json:"groups"`
Permissions map[string]*Permission `json:"permissions"`
}

// NewKeyPair create an account
func NewKeyPair(seckey []byte, algo crypto.Algorithm) (*KeyPair, error) {
if seckey == nil {
seckey = algo.GenSeckey()
}

err := algo.CheckSeckey(seckey)
if err != nil {
return nil, err
}

pubkey := algo.GetPubkey(seckey)

account := &KeyPair{
Algorithm: algo,
Pubkey: pubkey,
Seckey: seckey,
}
return account, nil
// Item identity of a permission owner
type Item struct {
ID string `json:"id"` // key pair id
Permission string `json:"permission"`
IsKeyPair bool `json:"is_key_pair"`
Weight int `json:"weight"`
}

// Sign sign a tx
func (a *KeyPair) Sign(info []byte) *crypto.Signature {
return crypto.NewSignature(a.Algorithm, info, a.Seckey)
// Group group of permissions
type Group struct {
Name string `json:"name"`
Items []*Item `json:"items"`
}

// ReadablePubkey ...
func (a *KeyPair) ReadablePubkey() string {
return EncodePubkey(a.Pubkey)
// Permission permission struct
type Permission struct {
Name string `json:"name"`
Groups []string `json:"groups"`
Items []*Item `json:"items"`
Threshold int `json:"threshold"`
}

// EncodePubkey ...
func EncodePubkey(pubkey []byte) string {
return common.Base58Encode(pubkey)
// NewAccount a new empty account
func NewAccount(id string) *Account {
return &Account{
ID: id,
Groups: make(map[string]*Group),
Permissions: make(map[string]*Permission),
}
}

// DecodePubkey ...
func DecodePubkey(readablePubKey string) []byte {
return common.Base58Decode(readablePubKey)
// NewAccountFromKeys new account with owner and active key
func NewAccountFromKeys(id, ownerKey, activeKey string) *Account {
a := &Account{
ID: id,
Groups: make(map[string]*Group),
Permissions: make(map[string]*Permission),
}
a.Permissions["owner"] = &Permission{
Name: "owner",
Threshold: 1,
Items: []*Item{
{
ID: ownerKey,
IsKeyPair: true,
Weight: 1,
},
},
}
a.Permissions["active"] = &Permission{
Name: "active",
Threshold: 1,
Items: []*Item{
{
ID: activeKey,
IsKeyPair: true,
Weight: 1,
},
},
}
return a
}
54 changes: 54 additions & 0 deletions account/keypair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package account

import (
"github.com/iost-official/go-iost/v3/common"
"github.com/iost-official/go-iost/v3/crypto"
)

// KeyPair keypair of the iost account
type KeyPair struct {
Algorithm crypto.Algorithm
Pubkey []byte
Seckey []byte
}

// NewKeyPair create a keypair
func NewKeyPair(seckey []byte, algo crypto.Algorithm) (*KeyPair, error) {
if seckey == nil {
seckey = algo.GenSeckey()
}

err := algo.CheckSeckey(seckey)
if err != nil {
return nil, err
}

pubkey := algo.GetPubkey(seckey)

account := &KeyPair{
Algorithm: algo,
Pubkey: pubkey,
Seckey: seckey,
}
return account, nil
}

// Sign sign a tx
func (a *KeyPair) Sign(info []byte) *crypto.Signature {
return crypto.NewSignature(a.Algorithm, info, a.Seckey)
}

// ReadablePubkey ...
func (a *KeyPair) ReadablePubkey() string {
return EncodePubkey(a.Pubkey)
}

// EncodePubkey ...
func EncodePubkey(pubkey []byte) string {
return common.Base58Encode(pubkey)
}

// DecodePubkey ...
func DecodePubkey(readablePubKey string) []byte {
return common.Base58Decode(readablePubKey)
}
26 changes: 12 additions & 14 deletions account/account_test.go → account/keypair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package account

import (
"testing"

"bytes"

"fmt"

. "github.com/iost-official/go-iost/v3/common"
"github.com/iost-official/go-iost/v3/crypto"
. "github.com/smartystreets/goconvey/convey"
)

func TestMember(t *testing.T) {
func TestKeypair(t *testing.T) {
Convey("Test of KeyPair", t, func() {
m, err := NewKeyPair(nil, crypto.Secp256k1)
Convey("New member: ", func() {
Convey("New keypair: ", func() {
So(err, ShouldBeNil)
So(len(m.Pubkey), ShouldEqual, 33)
So(len(m.Seckey), ShouldEqual, 32)
Expand All @@ -39,21 +37,21 @@ func TestMember(t *testing.T) {
})
}

func TestPubkeyAndID(t *testing.T) {
func TestSeckeyToPubkey(t *testing.T) {
seckey := Base58Decode("1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB")
pubkey := crypto.Ed25519.GetPubkey(seckey)
fmt.Println("id >", EncodePubkey(pubkey))
}

func TestPubkeyEncodeDecode(t *testing.T) {
for i := 0; i < 10; i++ {
seckey := crypto.Secp256k1.GenSeckey()
pubkey := crypto.Secp256k1.GetPubkey(seckey)
id := EncodePubkey(pubkey)
pub2 := DecodePubkey(id)
id2 := EncodePubkey(pub2)
if id != id2 {
pubkeyStr := EncodePubkey(pubkey)
pubkeyStr2 := EncodePubkey(DecodePubkey(pubkeyStr))
if pubkeyStr != pubkeyStr2 {
t.Fail()
}
}
}

func TestID_Platform(t *testing.T) {
seckey := Base58Decode("1rANSfcRzr4HkhbUFZ7L1Zp69JZZHiDDq5v7dNSbbEqeU4jxy3fszV4HGiaLQEyqVpS1dKT9g7zCVRxBVzuiUzB")
pubkey := crypto.Ed25519.GetPubkey(seckey)
fmt.Println("id >", EncodePubkey(pubkey))
}
72 changes: 0 additions & 72 deletions account/permission.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/itest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func main() {
app := cli.NewApp()
app.Name = "itest"
app.Usage = "The cli tool for testing the IOST testnet"
app.Usage = "The cli tool for testing the IOST blockchain"
app.Version = "0.0.1"
app.Commands = []cli.Command{
create.Command,
Expand Down
53 changes: 53 additions & 0 deletions common/chainmode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package common

import (
"sync"

"github.com/iost-official/go-iost/v3/metrics"
)

// ModeType is the type of mode.
type ModeType uint

// Constant of mode type.
const (
ModeNormal ModeType = iota
ModeSync
ModeInit
)

var (
mode = ModeInit
modeMutex = new(sync.RWMutex)
modeGauge = metrics.NewGauge("iost_node_mode", nil)
)

func init() {
modeGauge.Set(float64(mode), nil)
}

// Mode will return the mode of iserver.
func Mode() string {
modeMutex.RLock()
defer modeMutex.RUnlock()

switch mode {
case ModeNormal:
return "ModeNormal"
case ModeSync:
return "ModeSync"
case ModeInit:
return "ModeInit"
default:
return "Undefined"
}
}

// SetMode will set the mode of iserver.
func SetMode(m ModeType) {
modeMutex.Lock()
defer modeMutex.Unlock()

mode = m
modeGauge.Set(float64(mode), nil)
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 77051a7

Please sign in to comment.