forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Object Value and Type Persistence. Node persistence is simulated with the preprocessing of all files upon app restart.
- Loading branch information
Showing
63 changed files
with
4,327 additions
and
2,075 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package bank | ||
|
||
// NOTE: unexposed struct for security. | ||
type order struct { | ||
from Address | ||
to Address | ||
amount Coins | ||
processed bool | ||
} | ||
|
||
// NOTE: unexposed methods for security. | ||
func (ch *order) string() string { | ||
return "TODO" | ||
} | ||
|
||
// Wraps the internal *order for external use. | ||
type Order struct { | ||
*order | ||
} | ||
|
||
// XXX only exposed for demonstration. TODO unexpose, make full demo. | ||
func NewOrder(from Address, to Address, amount Coins) Order { | ||
return Order{ | ||
order: &order{ | ||
from: from, | ||
to: to, | ||
amount: amount, | ||
}, | ||
} | ||
} | ||
|
||
// Panics if error, or already processed. | ||
func (o Order) Execute() { | ||
if o.order.processed { | ||
panic("order already processed") | ||
} | ||
o.order.processed = true | ||
// TODO implemement. | ||
} | ||
|
||
func (o Order) IsZero() bool { | ||
return o.order == nil | ||
} | ||
|
||
func (o Order) From() Address { | ||
return o.order.from | ||
} | ||
|
||
func (o Order) To() Address { | ||
return o.order.to | ||
} | ||
|
||
func (o Order) Amount() Coins { | ||
return o.order.amount | ||
} | ||
|
||
func (o Order) Processed() bool { | ||
return o.order.processed | ||
} | ||
|
||
//---------------------------------------- | ||
// Escrow | ||
|
||
type EscrowTerms struct { | ||
PartyA Address | ||
PartyB Address | ||
AmountA Coins | ||
AmountB Coins | ||
} | ||
|
||
type EscrowContract struct { | ||
EscrowTerms | ||
OrderA Order | ||
OrderB Order | ||
} | ||
|
||
func CreateEscrow(terms EscrowTerms) *EscrowContract { | ||
return &EscrowContract{ | ||
EscrowTerms: terms, | ||
} | ||
} | ||
|
||
func (esc *EscrowContract) SetOrderA(order Order) { | ||
if !esc.OrderA.IsZero() { | ||
panic("order-a already set") | ||
} | ||
if esc.EscrowTerms.PartyA != order.From() { | ||
panic("invalid order-a:from mismatch") | ||
} | ||
if esc.EscrowTerms.PartyB != order.To() { | ||
panic("invalid order-a:to mismatch") | ||
} | ||
if esc.EscrowTerms.AmountA != order.Amount() { | ||
panic("invalid order-a amount") | ||
} | ||
esc.OrderA = order | ||
} | ||
|
||
func (esc *EscrowContract) SetOrderB(order Order) { | ||
if !esc.OrderB.IsZero() { | ||
panic("order-b already set") | ||
} | ||
if esc.EscrowTerms.PartyB != order.From() { | ||
panic("invalid order-b:from mismatch") | ||
} | ||
if esc.EscrowTerms.PartyA != order.To() { | ||
panic("invalid order-b:to mismatch") | ||
} | ||
if esc.EscrowTerms.AmountB != order.Amount() { | ||
panic("invalid order-b amount") | ||
} | ||
esc.OrderA = order | ||
} | ||
|
||
func (esc *EscrowContract) Execute() { | ||
if esc.OrderA.IsZero() { | ||
panic("order-a not yet set") | ||
} | ||
if esc.OrderB.IsZero() { | ||
panic("order-b not yet set") | ||
} | ||
// NOTE: succeeds atomically. | ||
esc.OrderA.Execute() | ||
esc.OrderB.Execute() | ||
} | ||
|
||
//---------------------------------------- | ||
// TODO: actually implement these in std package. | ||
|
||
type Address string | ||
type Coins []Coin | ||
type Coin struct { | ||
Denom bool | ||
Amount int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.