-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
46 lines (38 loc) · 1014 Bytes
/
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
package main
import (
"crypto/sha256"
"fmt"
"sort"
"strings"
)
// RawAccountState stores an acccount's (address') state
type RawAccountState struct {
Balance uint64 `json:"b"`
Nonce uint64 `json:"n"`
Data string `json:"d"`
}
func (s *RawAccountState) AddBalance(amount uint64) {
// XXX: check for overflows
s.Balance += amount
}
func (s *RawAccountState) IncNonce() {
s.Nonce++
}
type AccountStates map[string]*RawAccountState
func (states AccountStates) getHash() []byte {
keys := []string{}
for k := range states {
keys = append(keys, k)
}
sort.Strings(keys)
jsonList := []string{}
for _, k := range keys {
jsonList = append(jsonList, fmt.Sprintf(`{"%s":{"b":%v,"n":%d,"d":%s}}`, k, states[k].Balance, states[k].Nonce, jsonifyWhatever(states[k].Data)))
}
jsonString := fmt.Sprintf("{%s}", strings.Join(jsonList, ","))
hash := sha256.Sum256([]byte(jsonString))
return hash[:]
}
func (states AccountStates) getStrHash() string {
return mustEncodeBase64URL(states.getHash())
}