-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimisticrp.go
167 lines (154 loc) · 4.41 KB
/
optimisticrp.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 optimisticrp
import (
"bytes"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)
type OptimisticTrie struct {
*trie.Trie
}
func NewTrie(triedb *trie.Database) (*OptimisticTrie, error) {
tr, err := trie.New(common.Hash{}, triedb)
if err != nil {
return nil, err
}
return &OptimisticTrie{tr}, nil
}
func (ot *OptimisticTrie) GetAccount(address common.Address) (Account, error) {
fBytes := ot.Get(address.Bytes())
var acc SolidityAccount
if len(fBytes) == 0 {
return Account{}, &AccountNotFound{address}
}
err := rlp.DecodeBytes(fBytes, &acc)
if err != nil {
return Account{}, err
}
return acc.ToGolangFormat()
}
func (ot *OptimisticTrie) UpdateAccount(address common.Address, acc Account) common.Hash {
//val := acc.MarshalBinary()
val, err := rlp.EncodeToBytes(acc.SolidityFormat())
if err != nil {
panic(err)
}
//acc.Balance = new(big.Int).SetUint64(0e+18)
ot.Update(address.Bytes(), val)
return ot.Hash()
}
func (ot *OptimisticTrie) StateRoot() common.Hash {
return ot.Hash()
}
func (ot *OptimisticTrie) NewProve(address common.Address) ([][]byte, error) {
fBytes := ot.Get(address.Bytes())
if len(fBytes) == 0 {
return nil, &AccountNotFound{address}
}
it := trie.NewIterator(ot.NodeIterator(nil))
accounts := 0
for it.Next() {
accounts += 1
}
//log.Printf("Number of accounts in Trie: %v\n", accounts)
proof := memorydb.New()
formatProof := [][]byte{}
if it := trie.NewIterator(ot.NodeIterator(address.Bytes())); it.Next() && bytes.Equal(address.Bytes(), it.Key) {
for _, p := range it.Prove() {
formatProof = append(formatProof, p)
proof.Put(crypto.Keccak256(p), p)
}
}
toSend := make([][]byte, 4)
//key
toSend[0] = address.Bytes()
//value
toSend[1] = fBytes
//root
toSend[3] = ot.Hash().Bytes()
rlpProof, err := rlp.EncodeToBytes(formatProof)
if err != nil {
return nil, err
}
//rlp proof for onchain data https://github.com/ethereum-optimism/contracts/blob/c39fcc40aec235511a5a161c3e33a6d3bd24221c/test/helpers/trie/trie-test-generator.ts#L170
toSend[2] = rlpProof
val, err := trie.VerifyProof(ot.StateRoot(), address.Bytes(), proof)
if !bytes.Equal(val, fBytes) {
return nil, fmt.Errorf("Verified value mismatch for key %x: have %x, want %x", address, val, fBytes)
}
return toSend, err
}
//Additional helpers not linked to interface so you can use them as you wish
func (ot *OptimisticTrie) AddFunds(account common.Address, value *big.Int) error {
acc, err := ot.GetAccount(account)
switch err.(type) {
case nil:
case *AccountNotFound:
newAcc := Account{Balance: value, Nonce: 0}
ot.UpdateAccount(account, newAcc)
return nil
default:
return err
}
acc.Balance.Add(acc.Balance, value)
ot.UpdateAccount(account, acc)
return nil
}
func (ot *OptimisticTrie) RemoveFunds(account common.Address, value *big.Int) error {
acc, err := ot.GetAccount(account)
switch err.(type) {
case nil:
case *AccountNotFound:
newAcc := Account{Balance: value, Nonce: 0}
ot.UpdateAccount(account, newAcc)
return nil
default:
return err
}
acc.Balance.Sub(acc.Balance, value)
ot.UpdateAccount(account, acc)
return nil
}
func (ot *OptimisticTrie) ProcessTx(transaction Transaction) (common.Hash, error) {
fromAcc, err := ot.GetAccount(transaction.From)
if err != nil {
return common.Hash{}, err
}
toAcc, err := ot.GetAccount(transaction.To)
switch err.(type) {
case nil:
case *AccountNotFound:
toAcc = Account{Balance: new(big.Int).SetUint64(0), Nonce: 0}
ot.UpdateAccount(transaction.To, toAcc)
default:
return common.Hash{}, err
}
//tx Value must be higher than the account balance (fee are not included)
if fromAcc.Balance.Cmp(transaction.Value) == -1 {
return common.Hash{}, &InvalidBalance{transaction.From, fromAcc.Balance}
}
fromAcc.Balance.Sub(fromAcc.Balance, transaction.Value)
toAcc.Balance.Add(toAcc.Balance, transaction.Value)
fromAcc.Nonce++
ot.UpdateAccount(transaction.From, fromAcc)
return ot.UpdateAccount(transaction.To, toAcc), nil
}
func (ot *OptimisticTrie) Copy() (*OptimisticTrie, error) {
var (
diskdb = memorydb.New()
triedb = trie.NewDatabase(diskdb)
)
tr, err := trie.New(common.Hash{}, triedb)
if err != nil {
return nil, err
}
it := trie.NewIterator(ot.NodeIterator(nil))
for it.Next() {
tr.Update(it.Key, it.Value)
}
return &OptimisticTrie{tr}, nil
}