forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
txnpool.go
130 lines (118 loc) · 3.55 KB
/
txnpool.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
/*
* 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 actor privides communication with other actor
package actor
import (
"errors"
"time"
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/common/log"
"github.com/dnaproject2/DNA/core/types"
ontErrors "github.com/dnaproject2/DNA/errors"
tcomn "github.com/dnaproject2/DNA/txnpool/common"
"github.com/ontio/ontology-eventbus/actor"
)
var txnPid *actor.PID
var txnPoolPid *actor.PID
var DisableSyncVerifyTx = false
func SetTxPid(actr *actor.PID) {
txnPid = actr
}
func SetTxnPoolPid(actr *actor.PID) {
txnPoolPid = actr
}
//append transaction to pool to txpool actor
func AppendTxToPool(txn *types.Transaction) (ontErrors.ErrCode, string) {
if DisableSyncVerifyTx {
txReq := &tcomn.TxReq{txn, tcomn.HttpSender, nil}
txnPid.Tell(txReq)
return ontErrors.ErrNoError, ""
}
//add Pre Execute Contract
_, err := PreExecuteContract(txn)
if err != nil {
return ontErrors.ErrUnknown, err.Error()
}
ch := make(chan *tcomn.TxResult, 1)
txReq := &tcomn.TxReq{txn, tcomn.HttpSender, ch}
txnPid.Tell(txReq)
if msg, ok := <-ch; ok {
return msg.Err, msg.Desc
}
return ontErrors.ErrUnknown, ""
}
//GetTxsFromPool from txpool actor
func GetTxsFromPool(byCount bool) map[common.Uint256]*types.Transaction {
future := txnPoolPid.RequestFuture(&tcomn.GetTxnPoolReq{ByCount: byCount}, REQ_TIMEOUT*time.Second)
result, err := future.Result()
if err != nil {
log.Errorf(ERR_ACTOR_COMM, err)
return nil
}
txpool, ok := result.(*tcomn.GetTxnPoolRsp)
if !ok {
return nil
}
txMap := make(map[common.Uint256]*types.Transaction)
for _, v := range txpool.TxnPool {
txMap[v.Tx.Hash()] = v.Tx
}
return txMap
}
//GetTxFromPool from txpool actor
func GetTxFromPool(hash common.Uint256) (tcomn.TXEntry, error) {
future := txnPid.RequestFuture(&tcomn.GetTxnReq{hash}, REQ_TIMEOUT*time.Second)
result, err := future.Result()
if err != nil {
log.Errorf(ERR_ACTOR_COMM, err)
return tcomn.TXEntry{}, err
}
rsp, ok := result.(*tcomn.GetTxnRsp)
if !ok {
return tcomn.TXEntry{}, errors.New("fail")
}
if rsp.Txn == nil {
return tcomn.TXEntry{}, errors.New("fail")
}
future = txnPid.RequestFuture(&tcomn.GetTxnStatusReq{hash}, REQ_TIMEOUT*time.Second)
result, err = future.Result()
if err != nil {
log.Errorf(ERR_ACTOR_COMM, err)
return tcomn.TXEntry{}, err
}
txStatus, ok := result.(*tcomn.GetTxnStatusRsp)
if !ok {
return tcomn.TXEntry{}, errors.New("fail")
}
txnEntry := tcomn.TXEntry{rsp.Txn, txStatus.TxStatus}
return txnEntry, nil
}
//GetTxnCount from txpool actor
func GetTxnCount() ([]uint32, error) {
future := txnPid.RequestFuture(&tcomn.GetTxnCountReq{}, REQ_TIMEOUT*time.Second)
result, err := future.Result()
if err != nil {
log.Errorf(ERR_ACTOR_COMM, err)
return []uint32{}, err
}
txnCnt, ok := result.(*tcomn.GetTxnCountRsp)
if !ok {
return []uint32{}, errors.New("fail")
}
return txnCnt.Count, nil
}