/
smart_contract_service_impl.go
401 lines (343 loc) · 12.4 KB
/
smart_contract_service_impl.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package service
import (
"errors"
"it-chain/domain"
"strings"
"os"
"time"
"context"
"docker.io/go-docker"
"io"
"docker.io/go-docker/api/types"
"docker.io/go-docker/api/types/container"
"encoding/json"
"bufio"
"os/exec"
"it-chain/common"
"fmt"
"github.com/spf13/viper"
)
var logger_s = common.GetLogger("smart_contract_service.go")
type SmartContract struct {
Name string
OriginReposPath string
SmartContractPath string
}
type SmartContractServiceImpl struct {
GithubID string
SmartContractHomePath string
SmartContractMap map[string]SmartContract
WorldStateDBPath string
WorldStateDBName string
}
func Init() {
}
func NewSmartContractService(githubID string, smartContractDirPath string) *SmartContractServiceImpl {
return &SmartContractServiceImpl{
GithubID: githubID,
SmartContractHomePath: smartContractDirPath,
SmartContractMap: make(map[string]SmartContract),
}
}
func (scs *SmartContractServiceImpl) PullAllSmartContracts(errorHandler func(error), completionHandler func()) {
GOPATH := os.Getenv("GOPATH")
//go func() {
repoList, err := domain.GetRepositoryList(scs.GithubID)
if err != nil {
errorHandler(errors.New("An error was occurred during getting repository list"))
return
}
for _, repo := range repoList {
localReposPath := GOPATH + "/src/it-chain" + scs.SmartContractHomePath + "/" + repo.Name
err = os.MkdirAll(localReposPath, 0755)
if err != nil {
errorHandler(errors.New("An error was occurred during making repository path"))
return
}
commits, err := domain.GetReposCommits(repo.FullName)
if err != nil {
errorHandler(errors.New("An error was occurred during getting commit logs"))
return
}
for _, commit := range commits {
err := domain.CloneReposWithName(repo.FullName, localReposPath, commit.Sha)
if err != nil {
errorHandler(errors.New("An error was occurred during cloning with name"))
return
}
err = domain.ResetWithSHA(localReposPath+"/"+commit.Sha, commit.Sha)
if err != nil {
errorHandler(errors.New("An error was occurred during resetting with SHA"))
return
}
}
}
if completionHandler != nil {
completionHandler()
}
return
//}()
}
func (scs *SmartContractServiceImpl) Deploy(ReposPath string) (string, error) {
origin_repos_name := strings.Split(ReposPath, "/")[1]
new_repos_name := strings.Replace(ReposPath, "/", "_", -1)
_, ok := scs.keyByValue(ReposPath)
if ok {
// 버전 업데이트 기능 추가 필요
return "", errors.New("Already exist smart contract ID")
}
repos, err := domain.GetRepos(ReposPath)
if err != nil {
return "", errors.New("An error occurred while getting repos!")
}
if repos.Message == "Bad credentials" {
return "", errors.New("Not Exist Repos!")
}
err = os.MkdirAll(scs.SmartContractHomePath+"/"+new_repos_name, 0755)
if err != nil {
return "", errors.New("An error occurred while make repository's directory!")
}
//todo gitpath이미 존재하는지 확인
err = domain.CloneRepos(ReposPath, scs.SmartContractHomePath+"/"+new_repos_name)
if err != nil {
return "", errors.New("An error occurred while cloning repos!")
}
common.Log.Println(viper.GetString("smartContract.githubID"))
_, err = domain.CreateRepos(new_repos_name, viper.GetString("smartContract.githubAccessToken"))
if err != nil {
return "", errors.New(err.Error()) //"An error occurred while creating repos!")
}
err = domain.ChangeRemote(scs.GithubID+"/"+new_repos_name, scs.SmartContractHomePath+"/"+new_repos_name+"/"+origin_repos_name)
if err != nil {
return "", errors.New("An error occurred while cloning repos!")
}
// 버전 관리를 위한 파일 추가
now := time.Now().Format("2006-01-02 15:04:05");
file, err := os.OpenFile(scs.SmartContractHomePath+"/"+new_repos_name+"/"+origin_repos_name+"/version", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return "", errors.New("An error occurred while creating or opening file!")
}
_, err = file.WriteString("Deployed at " + now + "\n")
if err != nil {
return "", errors.New("An error occurred while writing file!")
}
err = file.Close()
if err != nil {
return "", errors.New("An error occurred while closing file!")
}
err = domain.CommitAndPush(scs.SmartContractHomePath+"/"+new_repos_name+"/"+origin_repos_name, "It-Chain Smart Contract \""+new_repos_name+"\" Deploy")
if err != nil {
return "", errors.New(err.Error())
//return "", errors.New("An error occurred while committing and pushing!")
}
githubResponseCommits, err := domain.GetReposCommits(scs.GithubID + "/" + new_repos_name)
if err != nil {
return "", errors.New("An error occurred while getting commit log!")
}
reposDirPath := scs.SmartContractHomePath + "/" + new_repos_name + "/" + githubResponseCommits[0].Sha
err = os.Rename(scs.SmartContractHomePath+"/"+new_repos_name+"/"+origin_repos_name, reposDirPath)
if err != nil {
return "", errors.New("An error occurred while renaming directory!")
}
scs.SmartContractMap[githubResponseCommits[0].Sha] = SmartContract{new_repos_name, ReposPath, ""}
return githubResponseCommits[0].Sha, nil
}
/***************************************************
* 1. smartcontract 검사
* 2. smartcontract -> sc.tar : 애초에 풀 받을 때 압축해 둘 수 있음
* 3. go 버전에 맞는 docker image를 Create
* 4. sc.tar를 docker container로 복사
* 5. docker container Start
* 6. docker에서 smartcontract 실행
****************************************************/
func (scs *SmartContractServiceImpl) ValidateTransactionsOfBlock(block *domain.Block) (error) {
// 블럭 유효성 검사 필요?
if block.TransactionCount <= 0 {
return errors.New("No tx in block")
}
for i := 0; i < block.TransactionCount; i++ {
scs.ValidateTransaction(block.Transactions[i])
}
return nil
}
func (scs *SmartContractServiceImpl) ValidateTransaction(transaction *domain.Transaction) {
smartContractResponse, err := scs.RunTransactionOnDocker(transaction)
if err != nil {
logger_s.Errorln("An error occurred while validating smartcontract!")
transaction.TransactionStatus = domain.Status_TRANSACTION_UNCONFIRMED
} else {
if smartContractResponse.Result == domain.SUCCESS {
logger_s.Println("Running smartcontract is success")
transaction.TransactionStatus = domain.Status_TRANSACTION_CONFIRMED
} else if smartContractResponse.Result == domain.FAIL {
logger_s.Errorln("An error occurred while validating smartcontract!")
transaction.TransactionStatus = domain.Status_TRANSACTION_UNCONFIRMED
}
}
transaction.GenerateHash()
}
func (scs *SmartContractServiceImpl) RunTransactionsOfBlock(block domain.Block) (error) {
if block.TransactionCount <= 0 {
return errors.New("No tx in block")
}
var err error
for i := 0; i < block.TransactionCount; i++ {
err = scs.RunTransaction(block.Transactions[i])
if err != nil {
return err
}
}
return nil
}
func (scs *SmartContractServiceImpl) RunTransaction(transaction *domain.Transaction) (error) {
if transaction.TxData.Method == domain.Query {
//smartContractResponse, err := scs.RunTransactionOnDocker(transaction)
_, err := scs.RunTransactionOnDocker(transaction)
if err != nil {
logger_s.Errorln("An error occurred while running smartcontract!")
return err
}
} else if transaction.TxData.Method == domain.Invoke {
scs.Invoke(transaction)
}
return nil
}
func (scs *SmartContractServiceImpl) RunTransactionOnDocker(transaction *domain.Transaction) (*domain.SmartContractResponse, error) {
GOPATH := os.Getenv("GOPATH")
/*** Set Transaction Arg ***/
logger_s.Errorln("validateTransaction start")
txBytes, err := json.Marshal(transaction)
if err != nil {
return nil, errors.New("Tx Marshal Error")
}
sc, ok := scs.SmartContractMap[transaction.TxData.ContractID]
if !ok {
logger_s.Errorln("Not exist contract ID")
return nil, errors.New("Not exist contract ID")
}
_, err = os.Stat(GOPATH + "/src/it-chain" + sc.SmartContractPath + "/" + transaction.TxData.ContractID)
if os.IsNotExist(err) {
logger_s.Errorln("File or Directory Not Exist")
return nil, errors.New("File or Directory Not Exist")
}
/* Docker Code */
imageName := "docker.io/library/golang:1.9.2-alpine3.6"
ctx := context.Background()
cli, err := docker.NewEnvClient()
if err != nil {
logger_s.Errorln("An error occurred while creating new Docker Client!")
return nil, err
}
logger_s.Errorln("Pulling image")
out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
logger_s.Errorln("An error oeccured while pulling docker image!")
return nil, err
}
io.Copy(os.Stdout, out)
imageName_splited := strings.Split(imageName, "/")
image := imageName_splited[len(imageName_splited)-1]
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: image,
Cmd: []string{
"go", "run",
"/go/src/it-chain" + sc.SmartContractPath + "/" + transaction.TxData.ContractID + "/" + sc.Name + ".go",
string(txBytes), "/go/src/it-chain" + scs.WorldStateDBPath + "/" + scs.WorldStateDBName,
},
//Cmd: []string{"/go/src/it-chain/smartcontract/sample_smartcontract/" + sc.Name, string(txBytes), GOPATH + "/src" + scs.WorldStateDBPath + "/" + scs.WorldStateDBName},
Tty: true,
AttachStdout: true,
AttachStderr: true,
}, &container.HostConfig{
Binds: []string{GOPATH + "/src/it-chain:/go/src/it-chain"}, // {"/smartcontract/path:/smartcontract", "/worldstatedb/path:/worldstatedb", "/config:/config"},
}, nil, "")
if err != nil {
logger_s.Errorln("An error occurred while creating docker container!")
return nil, err
}
err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
logger_s.Errorln("An error occurred while starting the container!")
return nil, err
}
/* get docker output
----------------------*/
fmt.Println("============<Docker Output>=============")
reader, err := cli.ContainerLogs(context.Background(), resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
Timestamps: false,
})
if err != nil {
logger_s.Errorln("An error occurred while getting the output!")
return nil, err
}
defer reader.Close()
var output = ""
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
output += scanner.Text() + "\n"
}
fmt.Println(output)
smartContractResponse := &domain.SmartContractResponse{}
err = json.Unmarshal([]byte(output), smartContractResponse)
if err != nil {
logger_s.Errorln("An error occurred while unmarshalling the smartContractResponse!")
return nil, err
}
return smartContractResponse, nil
}
func (scs *SmartContractServiceImpl) Query() {
logger_s.Errorln("Query has to run on docker")
}
func (scs *SmartContractServiceImpl) Invoke(transaction *domain.Transaction) (*domain.SmartContractResponse, error) {
GOPATH := os.Getenv("GOPATH")
/*** Set Transaction Arg ***/
logger_s.Errorln("invoke start")
txBytes, err := json.Marshal(transaction)
if err != nil {
return nil, errors.New("Tx Marshal Error")
}
sc, ok := scs.SmartContractMap[transaction.TxData.ContractID]
if !ok {
logger_s.Errorln("Not exist contract ID")
return nil, errors.New("Not exist contract ID")
}
fmt.Println(GOPATH + "/src/it-chain" + sc.SmartContractPath + "/" + transaction.TxData.ContractID)
_, err = os.Stat(GOPATH + "/src/it-chain" + sc.SmartContractPath + "/" + transaction.TxData.ContractID)
if os.IsNotExist(err) {
logger_s.Errorln("File or Directory Not Exist")
return nil, errors.New("File or Directory Not Exist")
}
cmd := exec.Command(
"go", "run",
GOPATH + "/src/it-chain" + sc.SmartContractPath + "/" + transaction.TxData.ContractID + "/" + sc.Name + ".go",
string(txBytes), GOPATH + "/src/it-chain" + scs.WorldStateDBPath + "/" + scs.WorldStateDBName)
//cmd.Dir = sc.SmartContractPath + "/" + transaction.TxData.ContractID
output, err := cmd.Output()
if err != nil {
logger_s.Errorln("SmartContract running error")
return nil, err
}
fmt.Println("========< Output >========")
fmt.Println(string(output))
smartContractResponse := &domain.SmartContractResponse{}
err = json.Unmarshal(output, smartContractResponse)
if err != nil {
logger_s.Errorln("An error occurred while unmarshalling the smartContractResponse!")
return nil, err
}
return smartContractResponse, nil
}
func (scs *SmartContractServiceImpl) keyByValue(OriginReposPath string) (key string, ok bool) {
contractName := strings.Replace(OriginReposPath, "/", "^", -1)
for k, v := range scs.SmartContractMap {
if contractName == v.OriginReposPath {
key = k
ok = true
return key, ok
}
}
return "", false
}