-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
106 lines (96 loc) · 2.58 KB
/
transaction.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
package blockchain
import "errors"
// Transaction represents a transaction in the blockchain
type Transaction struct {
ID string // Unique transaction identifier
Sender string // Address of the sender
Receiver string // Address of the receiver
Amount int // Amount transferred
}
// NewTransaction creates a new transaction
func NewTransaction(id, sender, receiver string, amount int) *Transaction {
return &Transaction{
ID: id,
Sender: sender,
Receiver: receiver,
Amount: amount,
}
}
// FindTransactionByID 根据交易ID查找交易
func (bc *Blockchain) FindTransactionByID(txID string) *Transaction {
for _, block := range bc.Blocks {
for _, tx := range block.Transactions {
if tx.ID == txID {
return &tx
}
}
}
return nil
}
// FromMap 将区块数据转换为 Block 结构体
func FromMap(data map[string]interface{}) (Block, error) {
block := Block{}
// 解析数据
if index, ok := data["index"].(float64); ok {
block.Index = int(index)
} else {
return block, errors.New("invalid index")
}
if timestamp, ok := data["timestamp"].(string); ok {
block.Timestamp = timestamp
} else {
return block, errors.New("invalid timestamp")
}
if transactions, ok := data["transactions"].([]interface{}); ok {
for _, txData := range transactions {
txMap, ok := txData.(map[string]interface{})
if !ok {
return block, errors.New("invalid transaction data")
}
tx, err := FromTransactionMap(txMap)
if err != nil {
return block, err
}
block.Transactions = append(block.Transactions, tx)
}
} else {
return block, errors.New("invalid transactions")
}
if previousHash, ok := data["previousHash"].(string); ok {
block.PreviousHash = previousHash
} else {
return block, errors.New("invalid previousHash")
}
if hash, ok := data["hash"].(string); ok {
block.Hash = hash
} else {
return block, errors.New("invalid hash")
}
return block, nil
}
// FromTransactionMap 将交易数据转换为 Transaction 结构体
func FromTransactionMap(data map[string]interface{}) (Transaction, error) {
tx := Transaction{}
// 解析数据
if id, ok := data["id"].(string); ok {
tx.ID = id
} else {
return tx, errors.New("invalid id")
}
if sender, ok := data["sender"].(string); ok {
tx.Sender = sender
} else {
return tx, errors.New("invalid sender")
}
if receiver, ok := data["receiver"].(string); ok {
tx.Receiver = receiver
} else {
return tx, errors.New("invalid receiver")
}
if amount, ok := data["amount"].(float64); ok {
tx.Amount = int(amount)
} else {
return tx, errors.New("invalid amount")
}
return tx, nil
}