-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
121 lines (100 loc) · 3.58 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright IBM Corp. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package file
import (
"io"
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/orderer/common/bootstrap"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
)
type fileBootstrapper struct {
GenesisBlockFile string
}
// New returns a new static bootstrap helper.
func New(fileName string) bootstrap.Helper {
return &fileBootstrapper{
GenesisBlockFile: fileName,
}
}
// NewReplacer returns a new bootstrap replacer.
func NewReplacer(fileName string) bootstrap.Replacer {
return &fileBootstrapper{
GenesisBlockFile: fileName,
}
}
// GenesisBlock returns the genesis block to be used for bootstrapping.
func (b *fileBootstrapper) GenesisBlock() *cb.Block {
bootstrapFile, fileErr := ioutil.ReadFile(b.GenesisBlockFile)
if fileErr != nil {
panic(errors.Errorf("unable to bootstrap orderer. Error reading genesis block file: %v", fileErr))
}
genesisBlock := &cb.Block{}
unmarshallErr := proto.Unmarshal(bootstrapFile, genesisBlock)
if unmarshallErr != nil {
panic(errors.Errorf("unable to bootstrap orderer. Error unmarshalling genesis block: %v", unmarshallErr))
}
return genesisBlock
} // GenesisBlock
// ReplaceGenesisBlockFile creates a backup of the genesis block file, and then replaces
// it with the content of the given block.
// This is used during consensus-type migration in order to generate a bootstrap file that
// specifies the new consensus-type.
func (b *fileBootstrapper) ReplaceGenesisBlockFile(block *cb.Block) error {
buff, marshalErr := proto.Marshal(block)
if marshalErr != nil {
return errors.Wrap(marshalErr, "could not marshal block into a []byte")
}
genFileStat, statErr := os.Stat(b.GenesisBlockFile)
if statErr != nil {
return errors.Wrapf(statErr, "could not get the os.Stat of the genesis block file: %s", b.GenesisBlockFile)
}
if !genFileStat.Mode().IsRegular() {
return errors.Errorf("genesis block file: %s, is not a regular file", b.GenesisBlockFile)
}
backupFile := b.GenesisBlockFile + ".bak"
if err := backupGenesisFile(b.GenesisBlockFile, backupFile); err != nil {
return errors.Wrapf(err, "could not copy genesis block file (%s) into backup file: %s",
b.GenesisBlockFile, backupFile)
}
if err := ioutil.WriteFile(b.GenesisBlockFile, buff, genFileStat.Mode()); err != nil {
return errors.Wrapf(err, "could not write new genesis block into file: %s; use backup if necessary: %s",
b.GenesisBlockFile, backupFile)
}
return nil
}
func backupGenesisFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
}
func (b *fileBootstrapper) CheckReadWrite() error {
genFileStat, statErr := os.Stat(b.GenesisBlockFile)
if statErr != nil {
return errors.Wrapf(statErr, "could not get the os.Stat of the genesis block file: %s", b.GenesisBlockFile)
}
if !genFileStat.Mode().IsRegular() {
return errors.Errorf("genesis block file: %s, is not a regular file", b.GenesisBlockFile)
}
genFile, openErr := os.OpenFile(b.GenesisBlockFile, os.O_RDWR, genFileStat.Mode().Perm())
if openErr != nil {
if os.IsPermission(openErr) {
return errors.Wrapf(openErr, "genesis block file: %s, cannot be opened for read-write, check permissions", b.GenesisBlockFile)
} else {
return errors.Wrapf(openErr, "genesis block file: %s, cannot be opened for read-write", b.GenesisBlockFile)
}
}
genFile.Close()
return nil
}