-
Notifications
You must be signed in to change notification settings - Fork 79
/
response.go
167 lines (147 loc) · 4.59 KB
/
response.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
package oracle
import (
"encoding/hex"
"errors"
gio "io"
"github.com/nspcc-dev/neo-go/pkg/core/fee"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"go.uber.org/zap"
)
func (o *Oracle) getResponse(reqID uint64, create bool) *incompleteTx {
o.respMtx.Lock()
defer o.respMtx.Unlock()
incTx, ok := o.responses[reqID]
if !ok && create && !o.removed[reqID] {
incTx = newIncompleteTx()
o.responses[reqID] = incTx
}
return incTx
}
// AddResponse handles an oracle response (transaction signature for some identified request) signed by the given key.
// sig is a response transaction signature.
func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) {
incTx := o.getResponse(reqID, true)
if incTx == nil {
return
}
incTx.Lock()
isBackup := false
if incTx.tx != nil {
ok := pub.VerifyHashable(txSig, uint32(o.Network), incTx.tx)
if !ok {
ok = pub.VerifyHashable(txSig, uint32(o.Network), incTx.backupTx)
if !ok {
o.Log.Debug("invalid response signature",
zap.String("pub", hex.EncodeToString(pub.Bytes())))
incTx.Unlock()
return
}
isBackup = true
}
}
incTx.addResponse(pub, txSig, isBackup)
readyTx, ready := incTx.finalize(o.getOracleNodes(), false)
if ready {
ready = !incTx.isSent
incTx.isSent = true
}
incTx.Unlock()
if ready {
o.sendTx(readyTx)
}
}
// ErrResponseTooLarge is returned when a response exceeds the max allowed size.
var ErrResponseTooLarge = errors.New("too big response")
func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) {
defer rc.Close()
buf := make([]byte, limit+1)
n, err := gio.ReadFull(rc, buf)
if err == gio.ErrUnexpectedEOF && n <= limit {
return buf[:n], nil
}
if err == nil || n > limit {
return nil, ErrResponseTooLarge
}
return nil, err
}
// CreateResponseTx creates an unsigned oracle response transaction.
func (o *Oracle) CreateResponseTx(gasForResponse int64, vub uint32, resp *transaction.OracleResponse) (*transaction.Transaction, error) {
tx := transaction.New(o.oracleResponse, 0)
tx.Nonce = uint32(resp.ID)
tx.ValidUntilBlock = vub
tx.Attributes = []transaction.Attribute{{
Type: transaction.OracleResponseT,
Value: resp,
}}
oracleSignContract := o.getOracleSignContract()
tx.Signers = []transaction.Signer{
{
Account: o.oracleHash,
Scopes: transaction.None,
},
{
Account: hash.Hash160(oracleSignContract),
Scopes: transaction.None,
},
}
tx.Scripts = []transaction.Witness{
{}, // native contract witness is fixed, second witness is set later.
}
// Calculate network fee.
size := io.GetVarSize(tx)
tx.Scripts = append(tx.Scripts, transaction.Witness{VerificationScript: oracleSignContract})
gasConsumed, ok := o.testVerify(tx)
if !ok {
return nil, errors.New("can't verify transaction")
}
tx.NetworkFee += gasConsumed
netFee, sizeDelta := fee.Calculate(o.Chain.GetBaseExecFee(), tx.Scripts[1].VerificationScript)
tx.NetworkFee += netFee
size += sizeDelta
currNetFee := tx.NetworkFee + int64(size)*o.Chain.FeePerByte()
if currNetFee > gasForResponse {
attrSize := io.GetVarSize(tx.Attributes)
resp.Code = transaction.InsufficientFunds
resp.Result = nil
size = size - attrSize + io.GetVarSize(tx.Attributes)
}
tx.NetworkFee += int64(size) * o.Chain.FeePerByte() // 233
// Calculate system fee.
tx.SystemFee = gasForResponse - tx.NetworkFee
return tx, nil
}
func (o *Oracle) testVerify(tx *transaction.Transaction) (int64, bool) {
// (*Blockchain).GetTestVM calls Hash() method of the provided transaction; once being called, this
// method caches transaction hash, but tx building is not yet completed and hash will be changed.
// So, make a copy of the tx to avoid wrong hash caching.
cp := *tx
ic := o.Chain.GetTestVM(trigger.Verification, &cp, nil)
ic.VM.GasLimit = o.Chain.GetMaxVerificationGAS()
ic.VM.LoadScriptWithHash(o.oracleScript, o.oracleHash, callflag.ReadOnly)
ic.VM.Context().Jump(o.verifyOffset)
ok := isVerifyOk(ic)
return ic.VM.GasConsumed(), ok
}
func isVerifyOk(ic *interop.Context) bool {
defer ic.Finalize()
if err := ic.VM.Run(); err != nil {
return false
}
if ic.VM.Estack().Len() != 1 {
return false
}
ok, err := ic.VM.Estack().Pop().Item().TryBool()
return err == nil && ok
}
func getFailedResponse(id uint64) *transaction.OracleResponse {
return &transaction.OracleResponse{
ID: id,
Code: transaction.Error,
}
}