Skip to content

Commit 3c7ae32

Browse files
authored
Merge pull request #879 from CortexFoundation/dev
Miner & trie state & storage & network
2 parents 3da54b6 + 4fce9e3 commit 3c7ae32

File tree

210 files changed

+16017
-17604
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+16017
-17604
lines changed

accounts/scwallet/securechannel.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import (
2020
"bytes"
2121
"crypto/aes"
2222
"crypto/cipher"
23+
"crypto/elliptic"
2324
"crypto/rand"
2425
"crypto/sha256"
2526
"crypto/sha512"
2627
"fmt"
2728

2829
"github.com/CortexFoundation/CortexTheseus/crypto"
2930
pcsc "github.com/gballet/go-libpcsclite"
30-
"github.com/wsddn/go-ecdh"
3131
"golang.org/x/crypto/pbkdf2"
3232
"golang.org/x/text/unicode/norm"
3333
)
@@ -63,26 +63,19 @@ type SecureChannelSession struct {
6363
// NewSecureChannelSession creates a new secure channel for the given card and public key.
6464
func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) {
6565
// Generate an ECDSA keypair for ourselves
66-
gen := ecdh.NewEllipticECDH(crypto.S256())
67-
private, public, err := gen.GenerateKey(rand.Reader)
66+
key, err := crypto.GenerateKey()
6867
if err != nil {
6968
return nil, err
7069
}
71-
72-
cardPublic, ok := gen.Unmarshal(keyData)
73-
if !ok {
74-
return nil, fmt.Errorf("could not unmarshal public key from card")
75-
}
76-
77-
secret, err := gen.GenerateSharedSecret(private, cardPublic)
70+
cardPublic, err := crypto.UnmarshalPubkey(keyData)
7871
if err != nil {
79-
return nil, err
72+
return nil, fmt.Errorf("could not unmarshal public key from card: %v", err)
8073
}
81-
74+
secret, _ := key.Curve.ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes())
8275
return &SecureChannelSession{
8376
card: card,
84-
secret: secret,
85-
publicKey: gen.Marshal(public),
77+
secret: secret.Bytes(),
78+
publicKey: elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y),
8679
}, nil
8780
}
8881

cmd/bootnode/main.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/CortexFoundation/CortexTheseus/crypto"
2929
"github.com/CortexFoundation/CortexTheseus/log"
3030
"github.com/CortexFoundation/CortexTheseus/p2p/discover"
31-
"github.com/CortexFoundation/CortexTheseus/p2p/discv5"
3231
"github.com/CortexFoundation/CortexTheseus/p2p/enode"
3332
"github.com/CortexFoundation/CortexTheseus/p2p/nat"
3433
"github.com/CortexFoundation/CortexTheseus/p2p/netutil"
@@ -128,25 +127,16 @@ func main() {
128127

129128
db, _ := enode.OpenDB("")
130129
ln := enode.NewLocalNode(db, nodeKey)
131-
132-
var unhandled chan discover.ReadPacket
133-
var sconn *sharedUDPConn
134-
if *runv5 {
135-
unhandled = make(chan discover.ReadPacket, 100)
136-
sconn = &sharedUDPConn{conn, unhandled}
137-
}
138130
cfg := discover.Config{
139131
PrivateKey: nodeKey,
140132
NetRestrict: restrictList,
141-
Unhandled: unhandled,
142-
}
143-
144-
if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
145-
utils.Fatalf("%v", err)
146133
}
147-
148134
if *runv5 {
149-
if _, err = discv5.ListenUDP(nodeKey, sconn, "", restrictList); err != nil {
135+
if _, err := discover.ListenV5(conn, ln, cfg); err != nil {
136+
utils.Fatalf("%v", err)
137+
}
138+
} else {
139+
if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
150140
utils.Fatalf("%v", err)
151141
}
152142
}

cmd/cortex/chaincmd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/CortexFoundation/CortexTheseus/event"
3939
"github.com/CortexFoundation/CortexTheseus/log"
4040
"github.com/CortexFoundation/CortexTheseus/trie"
41-
//"github.com/syndtr/goleveldb/leveldb/util"
4241
"gopkg.in/urfave/cli.v1"
4342
)
4443

cmd/cortex/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var (
6161
utils.BootnodesV4Flag,
6262
// utils.BootnodesV5Flag,
6363
utils.DataDirFlag,
64+
utils.AncientFlag,
65+
utils.MinFreeDiskSpaceFlag,
6466
utils.KeyStoreDirFlag,
6567
utils.ExternalSignerFlag,
6668
// utils.NoUSBFlag,
@@ -337,7 +339,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
337339
debug.Memsize.Add("node", stack)
338340

339341
// Start up the node itself
340-
utils.StartNode(stack)
342+
utils.StartNode(ctx, stack)
341343
var unlocks []string
342344
inputs := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
343345
for _, input := range inputs {

cmd/cortex/usage.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ var AppHelpFlagGroups = []flagGroup{
6969
Flags: []cli.Flag{
7070
configFileFlag,
7171
utils.DataDirFlag,
72+
utils.AncientFlag,
73+
utils.MinFreeDiskSpaceFlag,
7274
utils.KeyStoreDirFlag,
7375
// utils.NoUSBFlag,
7476
utils.NetworkIdFlag,

cmd/utils/cmd.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ import (
2626
"runtime"
2727
"strings"
2828
"syscall"
29+
"time"
2930

3031
"github.com/CortexFoundation/CortexTheseus/common"
3132
"github.com/CortexFoundation/CortexTheseus/core"
3233
"github.com/CortexFoundation/CortexTheseus/core/rawdb"
3334
"github.com/CortexFoundation/CortexTheseus/core/types"
3435
"github.com/CortexFoundation/CortexTheseus/crypto"
36+
"github.com/CortexFoundation/CortexTheseus/ctxc"
3537
"github.com/CortexFoundation/CortexTheseus/ctxcdb"
3638
"github.com/CortexFoundation/CortexTheseus/internal/debug"
3739
"github.com/CortexFoundation/CortexTheseus/log"
3840
"github.com/CortexFoundation/CortexTheseus/node"
3941
"github.com/CortexFoundation/CortexTheseus/rlp"
42+
"gopkg.in/urfave/cli.v1"
4043
)
4144

4245
const (
@@ -63,14 +66,23 @@ func Fatalf(format string, args ...interface{}) {
6366
os.Exit(1)
6467
}
6568

66-
func StartNode(stack *node.Node) {
69+
func StartNode(ctx *cli.Context, stack *node.Node) {
6770
if err := stack.Start(); err != nil {
6871
Fatalf("Error starting protocol stack: %v", err)
6972
}
7073
go func() {
7174
sigc := make(chan os.Signal, 1)
7275
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
7376
defer signal.Stop(sigc)
77+
minFreeDiskSpace := ctxc.DefaultConfig.TrieDirtyCache
78+
if ctx.GlobalIsSet(MinFreeDiskSpaceFlag.Name) {
79+
minFreeDiskSpace = ctx.GlobalInt(MinFreeDiskSpaceFlag.Name)
80+
} else if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
81+
minFreeDiskSpace = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
82+
}
83+
if minFreeDiskSpace > 0 {
84+
go monitorFreeDiskSpace(sigc, stack.InstanceDir(), uint64(minFreeDiskSpace)*1024*1024)
85+
}
7486
<-sigc
7587
log.Info("Got interrupt, shutting down...")
7688
go stack.Stop()
@@ -85,6 +97,24 @@ func StartNode(stack *node.Node) {
8597
}()
8698
}
8799

100+
func monitorFreeDiskSpace(sigc chan os.Signal, path string, freeDiskSpaceCritical uint64) {
101+
for {
102+
freeSpace, err := getFreeDiskSpace(path)
103+
if err != nil {
104+
log.Warn("Failed to get free disk space", "path", path, "err", err)
105+
break
106+
}
107+
if freeSpace < freeDiskSpaceCritical {
108+
log.Error("Low disk space. Gracefully shutting down Geth to prevent database corruption.", "available", common.StorageSize(freeSpace))
109+
sigc <- syscall.SIGTERM
110+
break
111+
} else if freeSpace < 2*freeDiskSpaceCritical {
112+
log.Warn("Disk space is running low. Geth will shutdown if disk space runs below critical level.", "available", common.StorageSize(freeSpace), "critical_level", common.StorageSize(freeDiskSpaceCritical))
113+
}
114+
time.Sleep(60 * time.Second)
115+
}
116+
}
117+
88118
func ImportChain(chain *core.BlockChain, fn string) error {
89119
// Watch for Ctrl-C while the import is running.
90120
// If a signal is received, the import will stop at the next batch.

p2p/discv5/metrics.go renamed to cmd/utils/diskusage.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The CortexTheseus Authors
1+
// Copyright 2021 The CortexTheseus Authors
22
// This file is part of the CortexTheseus library.
33
//
44
// The CortexTheseus library is free software: you can redistribute it and/or modify
@@ -14,11 +14,22 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package discv5
17+
// +build !windows
1818

19-
import "github.com/CortexFoundation/CortexTheseus/metrics"
19+
package utils
2020

21-
var (
22-
ingressTrafficMeter = metrics.NewRegisteredMeter("discv5/InboundTraffic", nil)
23-
egressTrafficMeter = metrics.NewRegisteredMeter("discv5/OutboundTraffic", nil)
21+
import (
22+
"fmt"
23+
24+
"golang.org/x/sys/unix"
2425
)
26+
27+
func getFreeDiskSpace(path string) (uint64, error) {
28+
var stat unix.Statfs_t
29+
if err := unix.Statfs(path, &stat); err != nil {
30+
return 0, fmt.Errorf("failed to call Statfs: %v", err)
31+
}
32+
33+
// Available blocks * size per block = available space in bytes
34+
return stat.Bavail * uint64(stat.Bsize), nil
35+
}
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The CortexTheseus Authors
1+
// Copyright 2021 The CortexTheseus Authors
22
// This file is part of the CortexTheseus library.
33
//
44
// The CortexTheseus library is free software: you can redistribute it and/or modify
@@ -14,30 +14,25 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// +build go1.4,nacl,faketime_simulation
18-
19-
package discv5
17+
package utils
2018

2119
import (
22-
"os"
23-
"runtime"
24-
"testing"
25-
"unsafe"
20+
"fmt"
21+
22+
"golang.org/x/sys/windows"
2623
)
2724

28-
// Enable fake time mode in the runtime, like on the go playground.
29-
// There is a slight chance that this won't work because some go code
30-
// might have executed before the variable is set.
25+
func getFreeDiskSpace(path string) (uint64, error) {
3126

32-
//go:linkname faketime runtime.faketime
33-
var faketime = 1
27+
cwd, err := windows.UTF16PtrFromString(path)
28+
if err != nil {
29+
return 0, fmt.Errorf("failed to call UTF16PtrFromString: %v", err)
30+
}
3431

35-
func TestMain(m *testing.M) {
36-
// We need to use unsafe somehow in order to get access to go:linkname.
37-
_ = unsafe.Sizeof(0)
32+
var freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes uint64
33+
if err := windows.GetDiskFreeSpaceEx(cwd, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
34+
return 0, fmt.Errorf("failed to call GetDiskFreeSpaceEx: %v", err)
35+
}
3836

39-
// Run the actual test. runWithPlaygroundTime ensures that the only test
40-
// that runs is the one calling it.
41-
runtime.GOMAXPROCS(8)
42-
os.Exit(m.Run())
37+
return freeBytesAvailableToCaller, nil
4338
}

cmd/utils/flags.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import (
5454
"github.com/CortexFoundation/CortexTheseus/metrics/influxdb"
5555
"github.com/CortexFoundation/CortexTheseus/node"
5656
"github.com/CortexFoundation/CortexTheseus/p2p"
57-
"github.com/CortexFoundation/CortexTheseus/p2p/discv5"
5857
"github.com/CortexFoundation/CortexTheseus/p2p/enode"
5958
"github.com/CortexFoundation/CortexTheseus/p2p/nat"
6059
"github.com/CortexFoundation/CortexTheseus/p2p/netutil"
@@ -134,14 +133,18 @@ var (
134133
Usage: "Data directory for the databases and keystore",
135134
Value: DirectoryString{node.DefaultDataDir()},
136135
}
137-
KeyStoreDirFlag = DirectoryFlag{
138-
Name: "keystore",
139-
Usage: "Directory for the keystore (default = inside the datadir)",
140-
}
141136
AncientFlag = DirectoryFlag{
142137
Name: "datadir.ancient",
143138
Usage: "Data directory for ancient chain segments (default = inside chaindata)",
144139
}
140+
MinFreeDiskSpaceFlag = DirectoryFlag{
141+
Name: "datadir.minfreedisk",
142+
Usage: "Minimum free disk space in MB, once reached triggers auto shut down (default = --cache.gc converted to MB, 0 = disabled)",
143+
}
144+
KeyStoreDirFlag = DirectoryFlag{
145+
Name: "keystore",
146+
Usage: "Directory for the keystore (default = inside the datadir)",
147+
}
145148
// NoUSBFlag = cli.BoolFlag{
146149
// Name: "nousb",
147150
// Usage: "Disables monitoring for and managing USB hardware wallets",
@@ -901,26 +904,26 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
901904
// setBootstrapNodesV5 creates a list of bootstrap nodes from the command line
902905
// flags, reverting to pre-configured ones if none have been specified.
903906
func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
904-
urls := params.MainnetBootnodes
907+
urls := params.V5Bootnodes
905908
switch {
906909
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(BootnodesV5Flag.Name):
907910
if ctx.GlobalIsSet(BootnodesV5Flag.Name) {
908911
urls = splitAndTrim(ctx.GlobalString(BootnodesV5Flag.Name))
909912
} else {
910913
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
911914
}
912-
case ctx.GlobalBool(BernardFlag.Name):
913-
urls = params.BernardBootnodes
914-
case ctx.GlobalBool(DoloresFlag.Name):
915-
urls = params.BernardBootnodes
915+
//case ctx.GlobalBool(BernardFlag.Name):
916+
// urls = params.BernardBootnodes
917+
//case ctx.GlobalBool(DoloresFlag.Name):
918+
// urls = params.BernardBootnodes
916919
case cfg.BootstrapNodesV5 != nil:
917920
return // already set, don't apply defaults.
918921
}
919922

920-
cfg.BootstrapNodesV5 = make([]*discv5.Node, 0, len(urls))
923+
cfg.BootstrapNodesV5 = make([]*enode.Node, 0, len(urls))
921924
for _, url := range urls {
922925
if url != "" {
923-
node, err := discv5.ParseNode(url)
926+
node, err := enode.Parse(enode.ValidSchemes, url)
924927
if err != nil {
925928
log.Error("Bootstrap URL invalid", "enode", url, "err", err)
926929
continue
@@ -1370,7 +1373,6 @@ func SetCortexConfig(ctx *cli.Context, stack *node.Node, cfg *ctxc.Config) {
13701373
if ctx.GlobalIsSet(NetworkIdFlag.Name) {
13711374
cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name)
13721375
}
1373-
13741376
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) {
13751377
cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
13761378
}

common/mclock/mclock.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ package mclock
2020
import (
2121
"time"
2222

23-
"github.com/aristanetworks/goarista/monotime"
23+
_ "unsafe" // for go:linkname
2424
)
2525

26+
//go:noescape
27+
//go:linkname nanotime runtime.nanotime
28+
func nanotime() int64
29+
2630
// AbsTime represents absolute monotonic time.
27-
type AbsTime time.Duration
31+
type AbsTime int64
2832

2933
// Now returns the current absolute monotonic time.
3034
func Now() AbsTime {
31-
return AbsTime(monotime.Now())
35+
return AbsTime(nanotime())
3236
}
3337

3438
// Add returns t + d as absolute time.
@@ -76,11 +80,7 @@ type System struct {
7680

7781
// Now returns the current monotonic time.
7882
func (c System) Now() AbsTime {
79-
//return AbsTime(monotime.Now())
80-
return AbsTime(monotime.Now()) + c.offset
81-
}
82-
func (c *System) SetAbsTime(now AbsTime) {
83-
c.offset = now - AbsTime(monotime.Now())
83+
return Now()
8484
}
8585

8686
// Sleep blocks for the given duration.

0 commit comments

Comments
 (0)