forked from OpenAtomFoundation/xupercore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
153 lines (132 loc) · 3.85 KB
/
state.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
package evm
import (
"math/big"
"time"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/permission"
"github.com/superconsensus/matrixcore/kernel/contract/bridge"
)
type stateManager struct {
ctx *bridge.Context
}
func newStateManager(ctx *bridge.Context) *stateManager {
return &stateManager{
ctx: ctx,
}
}
// Get an account by its address return nil if it does not exist (which should not be an error)
func (s *stateManager) GetAccount(address crypto.Address) (*acm.Account, error) {
addr, addrType, err := DetermineEVMAddress(address)
if err != nil {
return nil, nil
}
var evmCode []byte
if addrType == contractNameType {
v, err := s.ctx.State.Get("contract", evmCodeKey(addr))
if err != nil {
return nil, nil
}
evmCode = v
}
// balance, err := s.ctx.Core.GetBalance(addr)
// if err != nil {
// return nil, nil
// }
var balance *big.Int
return &acm.Account{
Address: address,
Balance: balance,
EVMCode: evmCode,
Permissions: permission.AllAccountPermissions,
}, nil
}
// Retrieve a 32-byte value stored at key for the account at address, return Zero256 if key does not exist but
// error if address does not
func (s *stateManager) GetStorage(address crypto.Address, key binary.Word256) ([]byte, error) {
//log.Debug("get storage for evm", "contract", s.ctx.ContractName, "address", address.String(), "key", key.String())
contractName, err := DetermineContractNameFromEVM(address)
if err != nil {
return nil, nil
}
v, err := s.ctx.State.Get(contractName, key.Bytes())
if err != nil {
return binary.Zero256.Bytes(), nil
}
return v, nil
}
// Updates the fields of updatedAccount by address, creating the account
// if it does not exist
func (s *stateManager) UpdateAccount(updatedAccount *acm.Account) error {
return nil
}
// Remove the account at address
func (s *stateManager) RemoveAccount(address crypto.Address) error {
return nil
}
// Store a 32-byte value at key for the account at address, setting to Zero256 removes the key
func (s *stateManager) SetStorage(address crypto.Address, key binary.Word256, value []byte) error {
//log.Debug("set storage for evm", "contract", s.ctx.ContractName, "address", address.String(), "key", key.String())
contractName, err := DetermineContractNameFromEVM(address)
if err != nil {
return err
}
return s.ctx.State.Put(contractName, key.Bytes(), value)
}
// Transfer native token
func (s *stateManager) Transfer(from, to crypto.Address, amount *big.Int) error {
fromAddr, addrType, err := DetermineEVMAddress(from)
if err != nil {
return err
}
// return directly when from is xchain address or contract account
// only transfer from a contract name works
if addrType == contractAccountType || addrType == xchainAddrType {
return nil
}
toAddr, _, err := DetermineEVMAddress(to)
if err != nil {
return err
}
return s.ctx.State.Transfer(fromAddr, toAddr, amount)
}
type blockStateManager struct {
ctx *bridge.Context
}
func newBlockStateManager(ctx *bridge.Context) *blockStateManager {
return &blockStateManager{
ctx: ctx,
}
}
// LastBlockHeight
func (s *blockStateManager) LastBlockHeight() uint64 {
// TODO
return 0
//block, err := s.ctx.Core.QueryLastBlock()
//if err != nil {
// return 0
//}
//return uint64(block.GetHeight())
}
// LastBlockTime
func (s *blockStateManager) LastBlockTime() time.Time {
// TODO
return time.Time{}
//block, err := s.ctx.Core.QueryLastBlock()
//if err != nil {
// return time.Time{}
//}
//timestamp := block.GetTimestamp()
//return time.Unix(timestamp/1e9, timestamp%1e9)
}
// LastBlockHeight
func (s *blockStateManager) BlockHash(height uint64) ([]byte, error) {
return nil, nil
// TODO
//block, err := s.ctx.Core.QueryBlockByHeight(int64(height))
//if err != nil {
// return nil, err
//}
//return block.GetBlockid(), nil
}