summaryrefslogtreecommitdiff
path: root/ledger/eval/cow_creatables.go
blob: 00d90ab5b398de827731ce59dd971b5a4cafb062 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package eval

import (
	"fmt"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/ledgercore"
)

// These functions ensure roundCowState satisfies the methods for
// accessing asset and app data in the apply.Balances interface.

func (cs *roundCowState) GetAppParams(addr basics.Address, aidx basics.AppIndex) (ret basics.AppParams, ok bool, err error) {
	var d ledgercore.AppParamsDelta
	d, ok, err = cs.lookupAppParams(addr, aidx, false)
	if err != nil || !ok {
		return
	}
	if d.Deleted {
		ok = false
		return
	}
	if d.Params == nil {
		// found and not deleled => must exist. Err if not
		err = fmt.Errorf("GetAppParams got a nil entry for (%s, %d): %p, %v", addr.String(), aidx, d.Params, d.Deleted)
	}
	ret = *d.Params
	return
}

func (cs *roundCowState) GetAppLocalState(addr basics.Address, aidx basics.AppIndex) (ret basics.AppLocalState, ok bool, err error) {
	var d ledgercore.AppLocalStateDelta
	d, ok, err = cs.lookupAppLocalState(addr, aidx, false)
	if err != nil || !ok {
		return
	}
	if d.Deleted {
		ok = false
		return
	}
	if d.LocalState == nil {
		// found and not deleled => must exist. Err if not
		err = fmt.Errorf("GetAppLocalState got a nil entry for (%s, %d): %p, %v", addr.String(), aidx, d.LocalState, d.Deleted)
	}
	ret = *d.LocalState
	return
}

func (cs *roundCowState) GetAssetHolding(addr basics.Address, aidx basics.AssetIndex) (ret basics.AssetHolding, ok bool, err error) {
	var d ledgercore.AssetHoldingDelta
	d, ok, err = cs.lookupAssetHolding(addr, aidx, false)
	if err != nil || !ok {
		return
	}
	if d.Deleted {
		ok = false
		return
	}
	if d.Holding == nil {
		// found and not deleted => must exist. Err if not
		err = fmt.Errorf("GetAssetHolding got a nil entry for (%s, %d): %p, %v", addr, aidx, d.Holding, d.Deleted)
	}
	ret = *d.Holding
	return
}

func (cs *roundCowState) GetAssetParams(addr basics.Address, aidx basics.AssetIndex) (ret basics.AssetParams, ok bool, err error) {
	var d ledgercore.AssetParamsDelta
	d, ok, err = cs.lookupAssetParams(addr, aidx, false)
	if err != nil || !ok {
		return
	}
	if d.Deleted {
		ok = false
		return
	}
	if d.Params == nil {
		// found and not deleled => must exist. Err if not
		err = fmt.Errorf("GetAppLocalState got a nil entry for (%s, %d): %p, %v", addr.String(), aidx, d.Params, d.Deleted)
	}
	ret = *d.Params
	return
}

func (cs *roundCowState) PutAppParams(addr basics.Address, aidx basics.AppIndex, params basics.AppParams) error {
	return cs.putAppParams(addr, aidx, ledgercore.AppParamsDelta{Params: &params})
}

func (cs *roundCowState) putAppParams(addr basics.Address, aidx basics.AppIndex, params ledgercore.AppParamsDelta) error {
	state, _, err := cs.lookupAppLocalState(addr, aidx, true) // should be cached
	if err != nil {
		return err
	}
	cs.mods.Accts.UpsertAppResource(addr, aidx, params, state)
	return nil
}

func (cs *roundCowState) PutAppLocalState(addr basics.Address, aidx basics.AppIndex, state basics.AppLocalState) error {
	return cs.putAppLocalState(addr, aidx, ledgercore.AppLocalStateDelta{LocalState: &state})
}

func (cs *roundCowState) putAppLocalState(addr basics.Address, aidx basics.AppIndex, state ledgercore.AppLocalStateDelta) error {
	params, _, err := cs.lookupAppParams(addr, aidx, true) // should be cached
	if err != nil {
		return err
	}
	cs.mods.Accts.UpsertAppResource(addr, aidx, params, state)
	return nil
}

func (cs *roundCowState) PutAssetHolding(addr basics.Address, aidx basics.AssetIndex, data basics.AssetHolding) error {
	return cs.putAssetHolding(addr, aidx, ledgercore.AssetHoldingDelta{Holding: &data})
}

func (cs *roundCowState) putAssetHolding(addr basics.Address, aidx basics.AssetIndex, data ledgercore.AssetHoldingDelta) error {
	params, _, err := cs.lookupAssetParams(addr, aidx, true) // should be cached
	if err != nil {
		return err
	}
	cs.mods.Accts.UpsertAssetResource(addr, aidx, params, data)
	return nil
}

func (cs *roundCowState) PutAssetParams(addr basics.Address, aidx basics.AssetIndex, data basics.AssetParams) error {
	return cs.putAssetParams(addr, aidx, ledgercore.AssetParamsDelta{Params: &data})
}

func (cs *roundCowState) putAssetParams(addr basics.Address, aidx basics.AssetIndex, data ledgercore.AssetParamsDelta) error {
	holding, _, err := cs.lookupAssetHolding(addr, aidx, true) // should be cached
	if err != nil {
		return err
	}
	cs.mods.Accts.UpsertAssetResource(addr, aidx, data, holding)
	return nil
}

func (cs *roundCowState) DeleteAppParams(addr basics.Address, aidx basics.AppIndex) error {
	if _, ok := cs.mods.Accts.GetData(addr); !ok {
		return fmt.Errorf("DeleteAppParams: %s not found in deltas for %d", addr.String(), aidx)
	}

	return cs.putAppParams(addr, aidx, ledgercore.AppParamsDelta{Deleted: true})
}

func (cs *roundCowState) DeleteAppLocalState(addr basics.Address, aidx basics.AppIndex) error {
	if _, ok := cs.mods.Accts.GetData(addr); !ok {
		return fmt.Errorf("DeleteAppLocalState: %s not found in deltas for %d", addr.String(), aidx)
	}

	return cs.putAppLocalState(addr, aidx, ledgercore.AppLocalStateDelta{Deleted: true})
}

func (cs *roundCowState) DeleteAssetHolding(addr basics.Address, aidx basics.AssetIndex) error {
	if _, ok := cs.mods.Accts.GetData(addr); !ok {
		return fmt.Errorf("DeleteAssetHolding: %s not found in deltas for %d", addr.String(), aidx)
	}

	return cs.putAssetHolding(addr, aidx, ledgercore.AssetHoldingDelta{Deleted: true})
}

func (cs *roundCowState) DeleteAssetParams(addr basics.Address, aidx basics.AssetIndex) error {
	if _, ok := cs.mods.Accts.GetData(addr); !ok {
		return fmt.Errorf("DeleteAssetParams: %s not found in deltas for %d", addr.String(), aidx)
	}

	return cs.putAssetParams(addr, aidx, ledgercore.AssetParamsDelta{Deleted: true})
}

func (cs *roundCowState) HasAppLocalState(addr basics.Address, aidx basics.AppIndex) (ok bool, err error) {
	d, ok, err := cs.lookupAppLocalState(addr, aidx, false)
	if err != nil {
		return false, err
	}
	if d.Deleted || d.LocalState == nil {
		ok = false
	}
	return ok, nil
}

func (cs *roundCowState) HasAssetParams(addr basics.Address, aidx basics.AssetIndex) (ok bool, err error) {
	d, ok, err := cs.lookupAssetParams(addr, aidx, false)
	if err != nil {
		return false, err
	}
	if d.Deleted || d.Params == nil {
		ok = false
	}
	return ok, nil
}