Skip to content

Commit

Permalink
feat: add IsMature func
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Apr 6, 2021
1 parent 02abcd5 commit 54e135a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 8 additions & 3 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"context"
"errors"
"github.com/nervosnetwork/ckb-sdk-go/indexer"
"github.com/nervosnetwork/ckb-sdk-go/rpc"
"github.com/nervosnetwork/ckb-sdk-go/types"
"math/big"
Expand Down Expand Up @@ -89,7 +90,7 @@ func getCellbaseMaturity(client rpc.Client, ctx context.Context, cellbaseMaturit
if err != nil {
return nil, err
}
major, minor, _, err := ParseNodeVersion(nodeInfo.Version)
major, minor, _, err := parseNodeVersion(nodeInfo.Version)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,8 +142,7 @@ func isTipEpochLessThanCellbaseMaturity(tipEpochR, cellbaseMaturityR *big.Rat) b
return false
}

// ParseNodeVersion return ckb node version number
func ParseNodeVersion(nodeVersion string) (int, int, int, error) {
func parseNodeVersion(nodeVersion string) (int, int, int, error) {
reg, err := regexp.Compile("\\d+(\\.\\d+){0,2}")
if err != nil {
return 0, 0, 0, err
Expand All @@ -164,3 +164,8 @@ func ParseNodeVersion(nodeVersion string) (int, int, int, error) {
}
return major, minor, patch, nil
}

// IsMature check if a cellbase live cell is mature
func IsMature(cell *indexer.LiveCell, maxMatureBlockNumber uint64) bool {
return cell.TxIndex > 0 || cell.BlockNumber == 0 || cell.BlockNumber <= maxMatureBlockNumber
}
12 changes: 6 additions & 6 deletions utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Test_calcMaxMatureBlockNumber(t *testing.T) {
}
}

func TestParseNodeVersion(t *testing.T) {
func Test_parseNodeVersion(t *testing.T) {
type args struct {
nodeVersion string
}
Expand Down Expand Up @@ -161,19 +161,19 @@ func TestParseNodeVersion(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, err := ParseNodeVersion(tt.args.nodeVersion)
got, got1, got2, err := parseNodeVersion(tt.args.nodeVersion)
if (err != nil) != tt.wantErr {
t.Errorf("ParseNodeVersion() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("parseNodeVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseNodeVersion() got = %v, want %v", got, tt.want)
t.Errorf("parseNodeVersion() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("ParseNodeVersion() got1 = %v, want %v", got1, tt.want1)
t.Errorf("parseNodeVersion() got1 = %v, want %v", got1, tt.want1)
}
if got2 != tt.want2 {
t.Errorf("ParseNodeVersion() got2 = %v, want %v", got2, tt.want2)
t.Errorf("parseNodeVersion() got2 = %v, want %v", got2, tt.want2)
}
})
}
Expand Down

0 comments on commit 54e135a

Please sign in to comment.