Skip to content

Commit 5392a36

Browse files
committed
add
1 parent 0d8f2d7 commit 5392a36

File tree

11 files changed

+400
-10
lines changed

11 files changed

+400
-10
lines changed

conf/conf.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/viper"
6+
"github.com/fsnotify/fsnotify"
7+
)
8+
9+
func main() {
10+
viper.SetDefault("ContentDir", "content")
11+
viper.SetDefault("LayoutDir", "layouts")
12+
viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
13+
14+
viper.SetConfigName("config") // 设置配置文件名 (不带后缀)
15+
viper.AddConfigPath("/etc/appname/") // 第一个搜索路径
16+
viper.AddConfigPath("$HOME/.appname") // 可以多次调用添加路径
17+
viper.AddConfigPath(".") // 比如添加当前目录
18+
err := viper.ReadInConfig() // 搜索路径,并读取配置数据
19+
if err != nil {
20+
panic(fmt.Errorf("Fatal error config file: %s \n", err))
21+
}
22+
23+
viper.WatchConfig()
24+
viper.OnConfigChange(func(e fsnotify.Event) {
25+
fmt.Println("Config file changed:", e.Name)
26+
})
27+
}
28+
29+
30+
31+

conf/conf.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
statetransfer:
2+
recoverdamage: true
3+
blocksperrequest: 20
4+
maxdeltas: 200
5+
timeout:
6+
singleblock: 2s
7+
singlestatedelta: 2s
8+
fullstate: 60s
9+
peer:
10+
abcd: 3322d

conf/conf_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

copernicus/addr.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/bcext/cashutil"
5+
"github.com/bcext/gcash/chaincfg"
6+
)
7+
8+
func main() {
9+
addrs := "bchtest:qq0ae7jqqvr87gex4yk3ukppvnm0w7ftqqpzv0lcqa"
10+
addr, err := cashutil.DecodeAddress(addrs, &chaincfg.TestNet3Params)
11+
if err != nil {
12+
13+
}
14+
addr.EncodeAddress(true)
15+
}

copernicus/ecdsaLowS.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"github.com/btcsuite/btcd/wire"
6+
"bytes"
7+
"fmt"
8+
)
9+
10+
func main() {
11+
txstr := "0200000002438e2ab0cc34e1ec684a7d2119753ca088c46ee7574668d577b53eeb6926f752030000008b483045022100e4c82b4ed6c625a54cd935ddc0f5993a097800300807ae7fe3e20c5abce9bfcc02202dc3509b5a26a7c7b8cc55d757b1e067ecd3cb1f63e5b28d3371e2a67bf5be29414104968618754fd8d8e83909fa1430292c872a83a40d999c77636d6d4dc775c256ec33da744722d85f5d15717a4f6b7b983301122891ede5af2250e49b6a3e5b1cecffffffff106130ad4de4adbf2509ad1f18e3cf8f3ccf449ef8fd2a636a0f3c695b731f30020000008b483045022100ac1ac30304d8bca14d3aa1c61c9aa74190c0557cf328f2d594a758dc80fb24ac02201121c4c97decd7d4624f638d57b28429c3833d9464707ed3f9f65ad11c30f4534141040baa4271a82c5f1a09a5ea63d763697ca0545b6049c4dd8e8d099dd91f2da10eb11e829000a82047ac56969fb582433067a21c3171e569d1832c34fdd793cfc8ffffffff0320770e00000000001976a91439ab3c52b317e9906aaf9c91b6421c4f24bfd7c888ac0000000000000000466a445bd33ebccb1453afdd207ad23f6274c514f60f7ae46ede7cd82e417f6c42b42252f72669eb3eb577d5684657e76ec488a03c7519217d4a68ece134ccb02a8e4300000003cb82160b000000001976a9148b80536aa3c460258cda834b86a46787c9a2b0bf88ac00000000"
12+
b, err := hex.DecodeString(txstr)
13+
if err != nil {
14+
fmt.Errorf("err:%v",err)
15+
}
16+
17+
var tx wire.MsgTx
18+
err = tx.Deserialize(bytes.NewBuffer(b))
19+
if err != nil {
20+
fmt.Errorf("err:%v",err)
21+
}
22+
23+
opcodes, err := parseScript(tx.TxIn[0].SignatureScript)
24+
if err != nil {
25+
fmt.Errorf("err:%v",err)
26+
}
27+
28+
for _, op := range opcodes {
29+
fmt.Println(hex.EncodeToString(op.data))
30+
}
31+
}

copernicus/structcopy.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"github.com/davecgh/go-spew/spew"
5+
"fmt"
6+
)
7+
8+
type People struct {
9+
Student
10+
}
11+
12+
type Student struct {
13+
name string
14+
age int
15+
sex bool
16+
tea *Teach //数值不会被拷贝
17+
}
18+
19+
type Teach struct {
20+
name string
21+
age int
22+
}
23+
24+
func main() {
25+
s := &Student{
26+
name: "wolf",
27+
age: 21,
28+
sex: true,
29+
tea: &Teach{
30+
name: "yujian",
31+
age: 12,
32+
},
33+
}
34+
35+
p := &People{*s}
36+
spew.Dump(p)
37+
38+
// tmp copy
39+
newP := *p
40+
newP.name = "qiwei"
41+
newP.age = 40
42+
newP.tea.age = 31 //如果不是基础类型 浅拷贝会污染原来的结构,需要注意
43+
44+
spew.Dump(newP)
45+
fmt.Println("浅copy之后")
46+
spew.Dump(p)
47+
48+
49+
fmt.Println("分割线")
50+
newp := &People{*s}
51+
52+
news := newp.deepCopy()
53+
news.tea.age = 31
54+
news.tea.name = "shabi"
55+
56+
fmt.Println("deep copy before")
57+
spew.Dump(newp)
58+
fmt.Println("tmp fix value")
59+
spew.Dump(news)
60+
fmt.Println("deep copy after")
61+
spew.Dump(newp)
62+
63+
}
64+
65+
func (p *People) deepCopy() *Student {
66+
s := &Student{name: p.name, age: p.age, sex: p.sex}
67+
newtea := &Teach{name: p.tea.name, age: p.tea.age}
68+
s.tea = newtea
69+
return s
70+
}

copernicus/t1.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func main() {
11+
crypto.InitSecp256()
1112
bs, err := hex.DecodeString("03ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1")
1213
pubkey, err := crypto.ParsePubKey(bs)
1314
if err != nil {

test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package main
22

3-
import (
4-
"strconv"
5-
"fmt"
6-
)
3+
import "fmt"
74

85
func main() {
9-
i, err := strconv.Atoi("-42")
10-
if err != nil {
11-
panic(err)
12-
}
13-
fmt.Println(i)
6+
7+
i := int64(-1)
8+
ii := uint64(i)
9+
fmt.Println(ii)
10+
11+
//i, err := strconv.Atoi("-42")
12+
//if err != nil {
13+
// panic(err)
14+
//}
15+
//fmt.Println(i)
1416
}

testcopernicus/loadblockindex.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"github.com/copernet/copernicus/model/utxo"
5+
"github.com/copernet/copernicus/model/blockindex"
6+
"github.com/copernet/copernicus/persist/blkdb"
7+
"github.com/copernet/copernicus/persist"
8+
"github.com/copernet/copernicus/model/chain"
9+
"sort"
10+
"github.com/copernet/copernicus/log"
11+
"github.com/copernet/copernicus/util"
12+
"fmt"
13+
"github.com/copernet/copernicus/conf"
14+
"github.com/copernet/copernicus/persist/db"
15+
"github.com/copernet/copernicus/logic/lblockindex"
16+
"github.com/copernet/copernicus/logic/lchain"
17+
"github.com/copernet/copernicus/crypto"
18+
"github.com/copernet/copernicus/model/mempool"
19+
"github.com/copernet/copernicus/persist/disk"
20+
"os"
21+
"path/filepath"
22+
"encoding/json"
23+
)
24+
25+
func appInitMain(args []string) {
26+
//init config
27+
conf.Cfg = conf.InitConfig(args)
28+
fmt.Println("Current data dir:\033[0;32m", conf.DataDir, "\033[0m")
29+
30+
//init log
31+
logDir := filepath.Join(conf.DataDir, log.DefaultLogDirname)
32+
if !conf.ExistDataDir(logDir) {
33+
err := os.MkdirAll(logDir, os.ModePerm)
34+
if err != nil {
35+
panic("logdir create failed: " + err.Error())
36+
}
37+
}
38+
39+
logConf := struct {
40+
FileName string `json:"filename"`
41+
Level int `json:"level"`
42+
Daily bool `json:"daily"`
43+
}{
44+
FileName: logDir + "/" + conf.Cfg.Log.FileName + ".log",
45+
Level: log.GetLevel(conf.Cfg.Log.Level),
46+
Daily: false,
47+
}
48+
49+
configuration, err := json.Marshal(logConf)
50+
if err != nil {
51+
panic(err)
52+
}
53+
log.Init(string(configuration))
54+
55+
// Init UTXO DB
56+
utxoConfig := utxo.UtxoConfig{Do: &db.DBOption{FilePath: conf.Cfg.DataDir + "/chainstate", CacheSize: (1 << 20) * 8}}
57+
utxo.InitUtxoLruTip(&utxoConfig)
58+
59+
chain.InitGlobalChain()
60+
61+
// Init blocktree DB
62+
blkdbCfg := blkdb.BlockTreeDBConfig{Do: &db.DBOption{FilePath: conf.Cfg.DataDir + "/blocks/index", CacheSize: (1 << 20) * 8}}
63+
blkdb.InitBlockTreeDB(&blkdbCfg)
64+
65+
persist.InitPersistGlobal()
66+
67+
// Load blockindex DB
68+
lblockindex.LoadBlockIndexDB()
69+
lchain.InitGenesisChain()
70+
71+
mempool.InitMempool()
72+
crypto.InitSecp256()
73+
}
74+
75+
func main() {
76+
args := []string{"--datadir=" + conf.DataDir, "--testnet"}
77+
appInitMain(args)
78+
gChain := chain.GetInstance()
79+
globalBlockIndexMap := make(map[util.Hash]*blockindex.BlockIndex)
80+
branch := make([]*blockindex.BlockIndex, 0, 20)
81+
82+
// Load blockindex from DB
83+
ok := blkdb.GetInstance().LoadBlockIndexGuts(globalBlockIndexMap, gChain.GetParams())
84+
fmt.Println("load block index guts: ", ok)
85+
86+
sortedByHeight := make([]*blockindex.BlockIndex, 0, len(globalBlockIndexMap))
87+
for _, index := range globalBlockIndexMap {
88+
sortedByHeight = append(sortedByHeight, index)
89+
90+
}
91+
//sort by decrease
92+
sort.SliceStable(sortedByHeight, func(i, j int) bool {
93+
return sortedByHeight[i].Height < sortedByHeight[j].Height
94+
})
95+
for _, index := range sortedByHeight {
96+
//fmt.Printf("the block index value is:%v\n", index)
97+
blk, ok := disk.ReadBlockFromDisk(index, gChain.GetParams())
98+
if ok {
99+
fmt.Printf("block height is:%v===block size value is:%v\n", index.Height, blk.EncodeSize())
100+
}
101+
}
102+
fmt.Printf("LoadBlockIndexDB, BlockIndexMap len:%d, Branch len:%d, Orphan len:%d\n",
103+
len(globalBlockIndexMap), len(branch), gChain.ChainOrphanLen())
104+
}

yuheng/day07/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
11
# 函数调用
22

3-
进程是一个资源单位,真正执行代码的是线程,线程启动的时候,先为他分配一段内存,分为两块,一个为栈-->执行栈(函数参数、返回值),一个为堆(new、malloc),堆上的内存需要free\GC
3+
## 调用的过程
44

5+
进程是一个资源单位,真正执行代码的是线程,线程启动的时候,先为他分配一段内存,
6+
分为两块,一个为栈-->执行栈(函数参数、返回值),一个为堆(new、malloc),堆上的内存需要free\GC
57

8+
函数调用主要是在栈上执行,栈和堆不同,堆是一个平面结构,栈是先进后出的一个空间,栈空间一般都追求重复使用,那么这里就有一个问题:
9+
栈如何知道哪一段空间是用来维护你这个main函数的执行逻辑,(这里说main函数主要是,我们一个项目main函数是唯一的入口)
10+
这里就需要用到两个寄存器,一个是BP一个是SP,BP寄存器指向栈的底部,SP指向栈的顶部,当调用的时候,SP会不断向上增长。
611

12+
注意BP这个栈指针是指向当前执行函数的底部。所以,当我们调用一个函数的时候,首先需要做现场保护,如栈指针的值,上下文的信息
13+
当调用完成之后,我们需要做现场恢复,所以这里就需要另一个寄存器,IP寄存器的值也需要保护起来,
14+
IP保存了执行完成某一个函数的时候,需要执行的下一条指令。
15+
16+
每一个函数在栈上的一个执行空间我们称为:桢(Frame),所有的这些栈桢合起来我们称为:调用堆栈(call stack)
17+
18+
### 总结:
19+
20+
当mian函数调用add函数的时候,步骤如下:
21+
22+
1. 调用call指令,然后push(保存)IP指令。
23+
2. 然后在函数头部,又把BP push(保护)进来。
24+
3. 挪动栈指针,将SP的值赋值给BP,`mov rsp rbp`
25+
4. 然后调用sub指令,向上移动SP的指针
26+
5. 然后把SP pop出来,使用的levelq这个系统命令,来完成现场恢复
27+
6. 把BP的值写给SP,那么刚才调用的那段空间就会被释放。
28+
7. 然后把保存BP的那段空间pop出来,BP回到初始位置
29+
8. 接下来还有一个retq指令,把IP寄存器pop出来,恢复到初始状态。
30+
31+
栈空间栈底的位置大,栈顶的位置小,所以SP要向上移动,就需要做减法往上移动栈的指针。
32+
33+
所有编译器的寻址都是基于BP寄存器来寻址的,做减法,减去偏移量。
34+
35+
栈上没有内存释放这么个说法,它只是通过移动BP和SP的位置来处理,要释放的话,就把整个栈空间释放
36+
所以,我们尽可能把对象分配到栈上。垃圾回收器只管堆上的内存,不管栈上的空间。
37+
38+
指针本身是一个标准的变量,它是可以被赋值的。
39+
40+
### 参数传递
41+
42+
假设我们需要分配0x30 这么大的空间,按照8字节对其,会分配6个8 这样的堆栈空间。
43+
44+
go不使用BP寄存器,它是基于SP寄存器做加法的。gcc是基于BP寄存器做加法。
45+
46+
47+
比如,一个简单的加法运算,x+y
48+
49+
* 先在一个格子里分配 x=100 ,继续分配 x=200 ,然后分别复制参数100、200
50+
* 执行add的函数调用
51+
* 在执行函数调用的时候,参数会被复制。
52+
* 由调用方准备参数使用的空间,初始过程先赋值为0,当拿到具体的值的时候,再去覆盖这个位置。
53+
54+
![image.png](https://upload-images.jianshu.io/upload_images/6967649-ea43ee8a9c07b0da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
55+
56+
栈空间分为两部分,一部分留给自己本地的局部变量使用,一部分留给函数调用的过程使用。

0 commit comments

Comments
 (0)