forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain.go
161 lines (151 loc) · 4.78 KB
/
blockchain.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package neovm
import (
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/core/types"
"github.com/dnaproject2/DNA/errors"
vm "github.com/dnaproject2/DNA/vm/neovm"
)
// BlockChainGetHeight put blockchain's height to vm stack
func BlockChainGetHeight(service *NeoVmService, engine *vm.ExecutionEngine) error {
vm.PushData(engine, service.Store.GetCurrentBlockHeight())
return nil
}
// BlockChainGetHeader put blockchain's header to vm stack
func BlockChainGetHeader(service *NeoVmService, engine *vm.ExecutionEngine) error {
var (
header *types.Header
err error
)
data, err := vm.PopByteArray(engine)
if err != nil {
return err
}
l := len(data)
if l <= 5 {
b := common.BigIntFromNeoBytes(data)
height := uint32(b.Int64())
hash := service.Store.GetBlockHash(height)
header, err = service.Store.GetHeaderByHash(hash)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetHeader] GetHeader error!.")
}
} else if l == 32 {
hash, _ := common.Uint256ParseFromBytes(data)
header, err = service.Store.GetHeaderByHash(hash)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetHeader] GetHeader error!.")
}
} else {
return errors.NewErr("[BlockChainGetHeader] data invalid.")
}
vm.PushData(engine, header)
return nil
}
// BlockChainGetBlock put blockchain's block to vm stack
func BlockChainGetBlock(service *NeoVmService, engine *vm.ExecutionEngine) error {
if vm.EvaluationStackCount(engine) < 1 {
return errors.NewErr("[BlockChainGetBlock] Too few input parameters ")
}
data, err := vm.PopByteArray(engine)
if err != nil {
return err
}
var block *types.Block
l := len(data)
if l <= 5 {
b := common.BigIntFromNeoBytes(data)
height := uint32(b.Int64())
var err error
block, err = service.Store.GetBlockByHeight(height)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetBlock] GetBlock error!.")
}
} else if l == 32 {
hash, err := common.Uint256ParseFromBytes(data)
if err != nil {
return err
}
block, err = service.Store.GetBlockByHash(hash)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetBlock] GetBlock error!.")
}
} else {
return errors.NewErr("[BlockChainGetBlock] data invalid.")
}
vm.PushData(engine, block)
return nil
}
// BlockChainGetTransaction put blockchain's transaction to vm stack
func BlockChainGetTransaction(service *NeoVmService, engine *vm.ExecutionEngine) error {
d, err := vm.PopByteArray(engine)
if err != nil {
return err
}
hash, err := common.Uint256ParseFromBytes(d)
if err != nil {
return err
}
t, _, err := service.Store.GetTransaction(hash)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetTransaction] GetTransaction error!")
}
vm.PushData(engine, t)
return nil
}
// BlockChainGetContract put blockchain's contract to vm stack
func BlockChainGetContract(service *NeoVmService, engine *vm.ExecutionEngine) error {
if vm.EvaluationStackCount(engine) < 1 {
return errors.NewErr("[GetContract] Too few input parameters ")
}
b, err := vm.PopByteArray(engine)
if err != nil {
return err
}
address, err := common.AddressParseFromBytes(b)
if err != nil {
return err
}
item, err := service.Store.GetContractState(address)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[GetContract] GetAsset error!")
}
vm.PushData(engine, item)
return nil
}
// BlockChainGetTransactionHeight put transaction in block height to vm stack
func BlockChainGetTransactionHeight(service *NeoVmService, engine *vm.ExecutionEngine) error {
if vm.EvaluationStackCount(engine) < 1 {
return errors.NewErr("[BlockChainGetTransactionHeight] Too few input parameters ")
}
d, err := vm.PopByteArray(engine)
if err != nil {
return err
}
hash, err := common.Uint256ParseFromBytes(d)
if err != nil {
return err
}
_, h, err := service.Store.GetTransaction(hash)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[BlockChainGetTransaction] GetTransaction error!")
}
vm.PushData(engine, h)
return nil
}