Skip to content

Commit

Permalink
Merge branch 'base' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hijung committed Dec 14, 2021
2 parents 47f5643 + ee307f9 commit 730ecb2
Show file tree
Hide file tree
Showing 30 changed files with 1,083 additions and 356 deletions.
102 changes: 98 additions & 4 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,97 @@ func (m *manager) newConsensusInfo(blk module.Block) (module.ConsensusInfo, erro
return common.NewConsensusInfo(pblk.Proposer(), vl, voted), nil
}

func GetLastHeight(dbase db.Database) (int64, error) {
func GetBlockHeaderHashByHeight(
dbase db.Database,
c codec.Codec,
height int64,
) ([]byte, error) {
headerHashByHeight, err := db.NewCodedBucket(
dbase, db.BlockHeaderHashByHeight, c,
)
if err != nil {
return nil, err
}
return headerHashByHeight.GetBytes(height)
}

func GetBlockVersion(
dbase db.Database,
c codec.Codec,
height int64,
) (int, error) {
headerHashByHeight, err := db.NewCodedBucket(
dbase, db.BlockHeaderHashByHeight, c,
)
if err != nil {
return -1, err
}
hash, err := headerHashByHeight.GetBytes(height + 1)
if err != nil {
return -1, err
}
headerBytes, err := db.DoGetWithBucketID(dbase, db.BytesByHash, hash)
if err != nil {
return -1, err
}

br := bytes.NewReader(headerBytes)
dec := c.NewDecoder(br)
defer func() {
_ = dec.Close()
}()
d2, err := dec.DecodeList()
if err != nil {
return -1, err
}
var version int
if err = d2.Decode(&version); err != nil {
return -1, err
}
return version, nil
}

func GetCommitVoteListBytesByHeight(
dbase db.Database,
c codec.Codec,
height int64,
) ([]byte, error) {
headerHashByHeight, err := db.NewCodedBucket(
dbase, db.BlockHeaderHashByHeight, c,
)
if err != nil {
return nil, err
}
hash, err := headerHashByHeight.GetBytes(height + 1)
if err != nil {
return nil, err
}
headerBytes, err := db.DoGetWithBucketID(dbase, db.BytesByHash, hash)
if err != nil {
return nil, err
}

br := bytes.NewReader(headerBytes)
dec := c.NewDecoder(br)
defer func() {
_ = dec.Close()
}()

d2, err := dec.DecodeList()
if err != nil {
return nil, err
}
if err = d2.Skip(5); err != nil {
return nil, err
}
var votesHash []byte
if err = d2.Decode(&votesHash); err != nil {
return nil, err
}
return db.DoGetWithBucketID(dbase, db.BytesByHash, votesHash)
}

func GetLastHeightWithCodec(dbase db.Database, c codec.Codec) (int64, error) {
bk, err := dbase.GetBucket(db.ChainProperty)
if err != nil {
return 0, err
Expand All @@ -1733,23 +1823,27 @@ func GetLastHeight(dbase db.Database) (int64, error) {
return 0, err
}
var height int64
if _, err := dbCodec.UnmarshalFromBytes(bs, &height); err != nil {
if _, err := c.UnmarshalFromBytes(bs, &height); err != nil {
return 0, err
}
return height, nil
}

func GetLastHeight(dbase db.Database) (int64, error) {
return GetLastHeightWithCodec(dbase, dbCodec)
}

func GetLastHeightOf(dbase db.Database) int64 {
height, _ := GetLastHeight(dbase)
return height
}

func ResetDB(d db.Database, height int64) error {
func ResetDB(d db.Database, c codec.Codec, height int64) error {
bk, err := d.GetBucket(db.ChainProperty)
if err != nil {
return err
}
err = bk.Set([]byte(keyLastBlockHeight), codec.MustMarshalToBytes(height))
err = bk.Set([]byte(keyLastBlockHeight), c.MustMarshalToBytes(height))
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/cli/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,14 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman
return step, nil
}
} else {
save := vc.GetString("save")
rpcClientSendTx = func(w module.Wallet, p *v3.TransactionParam) (interface{}, error) {
txId, err := rpcClient.SendTransaction(w, p)
if len(save) > 0 {
if err := JsonPrettySaveFile(save, 0644, p); err != nil {
fmt.Fprintf(os.Stderr, "FAIL to save parameter file=%s err=%+v\n", save, err)
}
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -519,6 +525,7 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman
rootPFlags.Int("wait_interval", 1000, "Polling interval(msec) for wait transaction result")
rootPFlags.Int("wait_timeout", 10, "Timeout(sec) for wait transaction result")
rootPFlags.Bool("estimate", false, "Just estimate steps for the tx")
rootPFlags.String("save", "", "Store transaction to the file")
MarkAnnotationCustom(rootPFlags, "key_store", "nid")
BindPFlags(vc, rootCmd.PersistentFlags())
MarkAnnotationHidden(rootPFlags, "wait", "wait_interval", "wait_timeout")
Expand All @@ -533,7 +540,7 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman

rawCmd := &cobra.Command{
Use: "raw FILE",
Short: "Send transaction with json file filling nid,from,timestamp and signature",
Short: "Send transaction with json file filling nid,version,stepLimit,from and overwriting timestamp and signature",
Args: ArgsWithDefaultErrorFunc(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := readFile(args[0])
Expand Down Expand Up @@ -575,7 +582,7 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman

raw2Cmd := &cobra.Command{
Use: "raw2 FILE",
Short: "Send transaction with json file filling timestamp and signature",
Short: "Send transaction with json file overwriting timestamp and signature",
Args: ArgsWithDefaultErrorFunc(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := readFile(args[0])
Expand Down
77 changes: 58 additions & 19 deletions cmd/resetchain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ import (
"github.com/spf13/cobra"

"github.com/icon-project/goloop/block"
"github.com/icon-project/goloop/common/codec"
"github.com/icon-project/goloop/common/db"
cs "github.com/icon-project/goloop/consensus"
"github.com/icon-project/goloop/module"
)

var dbPath string
var walPath string
var dbType string
var codecType string

func codecForType(t string) codec.Codec {
if t == "mp" {
return codec.MP
}
return codec.RLP
}

func reset(height int64) error {
d, err := db.Open(dbPath, dbType, "")
Expand All @@ -45,48 +55,76 @@ func reset(height int64) error {
panic(err)
}
}()
curHeight, err := block.GetLastHeight(d)

cod := codecForType(codecType)
curHeight, err := block.GetLastHeightWithCodec(d, cod)
if curHeight <= height {
return errors.Errorf("invalid target height current=%d target=%d", curHeight, height)
}
ver, err := block.GetBlockVersion(d, cod, height)
if err != nil {
return err
}
if ver <= module.BlockVersion1 {
return errors.Errorf("unsupported block version=%d height=%d", ver, height)
}

fmt.Printf("Current block height : %v \n", curHeight)
fmt.Printf("Target block height : %v \n", height)
fmt.Printf("Confirm reset? (y/n) ")
reader := bufio.NewReader(os.Stdin)
confirm, err := reader.ReadString('\n')
if err != nil {
return nil
return err
}
confirm = strings.Replace(confirm, "\n", "", -1)
if confirm != "y" {
return nil
}
err = block.ResetDB(d, height)

bid, err := block.GetBlockHeaderHashByHeight(d, cod, height)
if err != nil {
return err
}
err = cs.ResetWAL(height, walPath)

cvlBytes, err := block.GetCommitVoteListBytesByHeight(d, cod, height)
if err != nil {
return err
}

vlmBytes, err := cs.WALRecordBytesFromCommitVoteListBytes(
cvlBytes, height, bid, cod,
)
if err != nil {
return err
}

err = block.ResetDB(d, cod, height)
if err != nil {
return err
}

err = cs.ResetWAL(height, walPath, vlmBytes)
if err != nil {
return err
}
return nil
}

func er(msg interface{}) {
fmt.Println("Error:", msg)
_, _ = fmt.Fprintln(os.Stderr, "Error:", msg)
os.Exit(1)
}

func main() {
rootCmd := &cobra.Command{
Use: os.Args[0] + " <height>",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("requires one height argument")
}
_, err := strconv.ParseInt(args[0], 0, 64)
if err != nil {
return err
}
return nil
},
rootCmd := &cobra.Command{Use: os.Args[0] + " <height>", Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("requires one height argument")
}
_, err := strconv.ParseInt(args[0], 0, 64)
if err != nil {
return err
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
height, _ := strconv.ParseInt(args[0], 0, 64)
err := reset(height)
Expand All @@ -101,6 +139,7 @@ func main() {
flag.StringVar(&walPath, "wal_path", "", "WAL path. For example, .chain/hxd81df51476cee82617f6fa658ebecc31d24ddce3/bfdc51/wal/)")
flag.StringVar(&dbType, "db_type", "goleveldb",
fmt.Sprintf("Name of database system (%s)", strings.Join(db.GetSupportedTypes(), ", ")))
flag.StringVar(&codecType, "codec", "rlp", "Name of data codec (rlp, mp)")
err := rootCmd.Execute()
if err != nil {
er(err)
Expand Down
8 changes: 8 additions & 0 deletions common/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type EncodeAndCloser interface {
}

type Decoder interface {
Skip(cnt int) error
Decode(o interface{}) error
DecodeMulti(objs ...interface{}) (int, error)
DecodeAll(objs ...interface{}) error
Expand Down Expand Up @@ -438,6 +439,13 @@ func (d *decoderImpl) Close() error {
return d.real.Close()
}

func (d *decoderImpl) Skip(n int) error {
if err := d.flush(); err != nil {
return err
}
return d.real.Skip(n)
}

func (d *decoderImpl) Decode(o interface{}) error {
if err := d.flush(); err != nil {
return err
Expand Down
22 changes: 22 additions & 0 deletions consensus/commitvotelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"bytes"
"encoding/binary"
"fmt"
"sort"

Expand Down Expand Up @@ -159,3 +160,24 @@ func NewCommitVoteSetFromBytes(bs []byte) module.CommitVoteSet {
}
return vl
}

func WALRecordBytesFromCommitVoteListBytes(
bs []byte, h int64, bid []byte, c codec.Codec,
) ([]byte, error) {
cvl := &commitVoteList{}
if bs != nil {
_, err := c.UnmarshalFromBytes(bs, cvl)
if err != nil {
return nil, err
}
}
vlm := newVoteListMessage()
vlm.VoteList = cvl.voteList(h, bid)
rec := make([]byte, 2, 32)
binary.BigEndian.PutUint16(rec, vlm.subprotocol())
writer := bytes.NewBuffer(rec)
if err := c.Marshal(writer, vlm); err != nil {
return nil, err
}
return writer.Bytes(), nil
}
Loading

0 comments on commit 730ecb2

Please sign in to comment.