Skip to content

Commit

Permalink
Merge branch 'base'
Browse files Browse the repository at this point in the history
  • Loading branch information
mksong76 committed Aug 24, 2023
2 parents e8e8729 + a7de989 commit 46a4d0b
Show file tree
Hide file tree
Showing 34 changed files with 1,308 additions and 307 deletions.
8 changes: 8 additions & 0 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ type chainTask interface {
Wait() error
}

type simpleTask interface {
chainTask
Run() error
}

type singleChain struct {
wallet module.Wallet

Expand Down Expand Up @@ -502,6 +507,9 @@ func (c *singleChain) releaseManagers() {
}

func (c *singleChain) _runTask(task chainTask, wait bool) error {
if st, ok := task.(simpleTask) ; ok {
return st.Run()
}
if err := c._setStartingTask(task); err != nil {
return err
}
Expand Down
131 changes: 131 additions & 0 deletions chain/taskpause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2023 Parameta Corp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package chain

import (
"encoding/json"

"github.com/icon-project/goloop/common/errors"
)

type taskPause struct {
chain *singleChain
result resultStore
}

var pauseStates = map[State]string{
Starting: "pausing",
Started: "paused",
Stopping: "stopping paused",
Failed: "fail to pause",
}

func (t *taskPause) String() string {
return "Pause"
}

func (t *taskPause) DetailOf(s State) string {
if name, ok := pauseStates[s]; ok {
return name
} else {
return s.String()
}
}

func (t *taskPause) Start() error {
if err := t.chain.prepareManagers(); err != nil {
t.result.SetValue(err)
return err
}
if err := t._start(t.chain); err != nil {
t.chain.releaseManagers()
t.result.SetValue(err)
return err
}
return nil
}

func (t *taskPause) _start(c *singleChain) error {
c.sm.Start()
//if err := c.cs.Start(); err != nil {
// return err
//}
c.srv.SetChain(c.cfg.Channel, c)
if err := c.nm.Start(); err != nil {
return err
}
return nil
}

func (t *taskPause) Stop() {
t.chain.srv.RemoveChain(t.chain.cfg.Channel)
t.chain.releaseManagers()
t.result.SetValue(errors.ErrInterrupted)
}

func (t *taskPause) Wait() error {
return t.result.Wait()
}

func newTaskPause(chain *singleChain, params json.RawMessage) (chainTask, error) {
return &taskPause{
chain: chain,
}, nil
}

type taskResume struct {
chain *singleChain
}

func (t *taskResume) String() string {
panic("invalid usage")
}

func (t *taskResume) DetailOf(s State) string {
panic("invalid usage")
}

func (t *taskResume) Start() error {
panic("invalid usage")
}

func (t *taskResume) Stop() {
panic("invalid usage")
}

func (t *taskResume) Wait() error {
panic("invalid usage")
}

func (t *taskResume) Run() error {
if _, ok := t.chain.task.(*taskPause) ; ok {
return t.chain.cs.Start()
} else {
return errors.InvalidStateError.New("Not in PAUSED state")
}
}

func newTaskResume(c *singleChain, params json.RawMessage) (chainTask, error) {
return &taskResume{
chain: c,
}, nil
}

func init() {
registerTaskFactory("pause", newTaskPause)
registerTaskFactory("resume", newTaskResume)
}
2 changes: 1 addition & 1 deletion cmd/cli/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func NewRpcCmd(parentCmd *cobra.Command, parentVc *viper.Viper) (*cobra.Command,
callFlags.Int("height", -1, "BlockHeight")
callFlags.String("method", "",
"Name of the function to invoke in SCORE, if '--raw' used, will overwrite")
callFlags.StringToString("params", nil,
callFlags.String("params", "",
"raw json string or '@<json file>' or '-' for stdin for parameter JSON. it overrides raw one ")
callFlags.StringToString("param", nil,
"key=value, Function parameters, if '--raw' used, will overwrite")
Expand Down
15 changes: 13 additions & 2 deletions cmd/txgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"log"
"os"

"github.com/icon-project/goloop/common/wallet"
"github.com/spf13/cobra"

"github.com/icon-project/goloop/common/wallet"
)

func main() {
var keyStoreFile string
var keyStorePass string
var keyStoreSecret string
var scorePath string
var tps int
var concurrent int
Expand All @@ -31,6 +33,7 @@ func main() {
flags := cmd.PersistentFlags()
flags.StringVarP(&keyStoreFile, "keystore", "k", "", "File path to keystore of base account (like GOD)")
flags.StringVarP(&keyStorePass, "password", "p", "gochain", "Password for the keystore")
flags.StringVar(&keyStoreSecret, "secret", "", "Secret file containing password for the keystore")
flags.IntVarP(&tps, "tps", "t", 1000, "Max transaction per a second")
flags.IntVarP(&concurrent, "concurrent", "c", 2, "Number of subroutines (threads)")
flags.IntVarP(&walletCount, "wallets", "w", 1000, "Number of temporal wallets")
Expand All @@ -57,8 +60,16 @@ func main() {
if err != nil {
log.Panicf("Fail to read KeyStore file=%s err=%+v", keyStoreFile, err)
}
var pass []byte;
if len(keyStoreSecret)>0 {
if pass, err = os.ReadFile(keyStoreSecret); err != nil {
log.Panicf("Fail to read secret for keyStore file=%s", keyStoreSecret);
}
} else {
pass = []byte(keyStorePass);
}

godWallet, err := wallet.NewFromKeyStore(ks, []byte(keyStorePass))
godWallet, err := wallet.NewFromKeyStore(ks, pass)
if err != nil {
log.Panicf("Fail to decrypt KeyStore err=%+v", err)
}
Expand Down
104 changes: 89 additions & 15 deletions common/codec/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,91 @@ package codec
import (
"bytes"
"io"
"sync"

"github.com/icon-project/goloop/common/log"
)

type bytesEncoder struct {
Encoder
buffer *bytes.Buffer
}

func (e *bytesEncoder) Reset() {
e.buffer.Reset()
}

func (e *bytesEncoder) Bytes() []byte {
return []byte(e.buffer.String())
}

type bytesDecoder struct {
Decoder
buffer *bytes.Buffer
}

func (d *bytesDecoder) Reset(bs []byte) {
d.buffer.Reset()
d.buffer.Write(bs)
d.Decoder.SetMaxBytes(len(bs))
}

func (d *bytesDecoder) Bytes() []byte {
return []byte(d.buffer.String())
}

type bytesWrapper struct {
codecImpl
encoders sync.Pool
decoders sync.Pool
}

func (c bytesWrapper) Marshal(w io.Writer, v interface{}) error {
func (c *bytesWrapper) Marshal(w io.Writer, v interface{}) error {
return c.NewEncoder(w).Encode(v)
}

func (c bytesWrapper) Unmarshal(r io.Reader, v interface{}) error {
func (c *bytesWrapper) Unmarshal(r io.Reader, v interface{}) error {
return c.NewDecoder(r).Decode(v)
}

func (c bytesWrapper) MarshalToBytes(v interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
if err := c.NewEncoder(buf).Encode(v); err != nil {
func bytesDup(bs []byte) []byte {
sz := len(bs)
if sz != 0 {
nbs := make([]byte,len(bs))
copy(nbs, bs)
return nbs
} else {
return []byte{}
}
}

func (c *bytesWrapper) MarshalToBytes(v interface{}) ([]byte, error) {
be := c.encoders.Get().(*bytesEncoder)

be.Reset()
if err := be.Encode(v) ; err != nil {
return nil, err
}
return buf.Bytes(), nil
remainder := bytesDup(be.Bytes())

c.encoders.Put(be)
return remainder, nil
}

func (c bytesWrapper) UnmarshalFromBytes(b []byte, v interface{}) ([]byte, error) {
buf := bytes.NewBuffer(b)
dec := c.NewDecoder(buf)
dec.SetMaxBytes(len(b))
if err := dec.Decode(v); err != nil {
func (c *bytesWrapper) UnmarshalFromBytes(b []byte, v interface{}) ([]byte, error) {
bd := c.decoders.Get().(*bytesDecoder)

bd.Reset(b)
if err := bd.Decode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
bs := bytesDup(bd.Bytes())

c.decoders.Put(bd)
return bs, nil
}

func (c bytesWrapper) MustMarshalToBytes(v interface{}) []byte {
func (c *bytesWrapper) MustMarshalToBytes(v interface{}) []byte {
bs, err := c.MarshalToBytes(v)
if err != nil {
log.Panicf("MustMarshalToBytes() fails for object=%T err=%+v", v, err)
Expand All @@ -47,7 +97,7 @@ func (c bytesWrapper) MustMarshalToBytes(v interface{}) []byte {
}
}

func (c bytesWrapper) MustUnmarshalFromBytes(b []byte, v interface{}) []byte {
func (c *bytesWrapper) MustUnmarshalFromBytes(b []byte, v interface{}) []byte {
bs, err := c.UnmarshalFromBytes(b, v)
if err != nil {
log.Panicf("MustUnmarshalFromBytes() fails for bytes=% x buffer=%T err=%+v", b, v, err)
Expand All @@ -66,9 +116,33 @@ func (w bytesWriter) Write(bs []byte) (int, error) {
return len(bs), nil
}

func (c bytesWrapper) NewEncoderBytes(b *[]byte) EncodeAndCloser {
func (c *bytesWrapper) NewEncoderBytes(b *[]byte) EncodeAndCloser {
if len(*b) > 0 {
*b = (*b)[:0]
}
return c.codecImpl.NewEncoder(&bytesWriter{b})
}

func bytesWrapperFrom(codec codecImpl) *bytesWrapper {
return &bytesWrapper{
codecImpl: codec,
encoders: sync.Pool{
New: func() interface{} {
buffer := bytes.NewBuffer(nil)
return &bytesEncoder{
Encoder: codec.NewEncoder(buffer),
buffer: buffer,
}
},
},
decoders: sync.Pool{
New: func() interface{} {
buffer := bytes.NewBuffer(nil)
return &bytesDecoder{
Decoder: codec.NewDecoder(buffer),
buffer: buffer,
}
},
},
}
}
Loading

0 comments on commit 46a4d0b

Please sign in to comment.