Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DARC attributes #2033

Merged
merged 10 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions byzcoin/contract_darc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func (c *contractSecureDarc) SetRegistry(r ReadOnlyContractRegistry) {
// need the Darc contract to opt in for deferred transaction because it is used
// by default when spawning new contracts.
func (c *contractSecureDarc) VerifyDeferredInstruction(rst ReadOnlyStateTrie, inst Instruction, ctxHash []byte) error {
if err := inst.VerifyWithOption(rst, ctxHash, false); err != nil {
return err
}
return nil
return inst.VerifyWithOption(rst, ctxHash, &VerificationOptions{IgnoreCounters: true})
}

func (c *contractSecureDarc) Spawn(rst ReadOnlyStateTrie, inst Instruction, coins []Coin) (sc []StateChange, cout []Coin, err error) {
Expand Down
2 changes: 1 addition & 1 deletion byzcoin/contract_deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (c *contractDeferred) VerifyDeferredInstruction(rst ReadOnlyStateTrie, inst
if inst.GetType() == DeleteType && uint64(rst.GetIndex()) >= c.DeferredData.ExpireBlockIndex {
return nil
}
if err := inst.VerifyWithOption(rst, ctxHash, false); err != nil {
if err := inst.VerifyWithOption(rst, ctxHash, &VerificationOptions{IgnoreCounters: true}); err != nil {
return errors.New("failed to verify deferred instruction: " + err.Error())
}
return nil
Expand Down
51 changes: 49 additions & 2 deletions byzcoin/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -196,7 +197,7 @@ func notImpl(what string) error { return fmt.Errorf("this contract does not impl
// VerifyInstruction offers the default implementation of verifying an instruction. Types
// which embed BasicContract may choose to override this implementation.
func (b BasicContract) VerifyInstruction(rst ReadOnlyStateTrie, inst Instruction, ctxHash []byte) error {
return inst.Verify(rst, ctxHash)
return inst.VerifyWithOption(rst, ctxHash, &VerificationOptions{EvalAttr: b.AttrInterpreters(rst, inst)})
}

// VerifyDeferredInstruction is not implemented in a BasicContract. Types which
Expand All @@ -206,6 +207,52 @@ func (b BasicContract) VerifyDeferredInstruction(rst ReadOnlyStateTrie, inst Ins
return notImpl("VerifyDeferredInstruction")
}

// AttrInterpreters provides one default attribute verification which check
// whether the transaction is sent after a certain block index and before
// another block index.
func (b BasicContract) AttrInterpreters(rst ReadOnlyStateTrie, inst Instruction) map[string]func(string) error {
kc1212 marked this conversation as resolved.
Show resolved Hide resolved
kc1212 marked this conversation as resolved.
Show resolved Hide resolved
cb := func(attr string) error {
vals, err := url.ParseQuery(attr)
if err != nil {
return err
}
beforeStr := vals.Get("before")
afterStr := vals.Get("after")

var before, after int

if len(beforeStr) == 0 {
// Set before to something higher than the current
// index so that it always passes.
before = rst.GetIndex() + 1
} else {
var err error
before, err = strconv.Atoi(beforeStr)
if err != nil {
return err
}
}

if len(afterStr) == 0 {
after = -1
} else {
var err error
after, err = strconv.Atoi(afterStr)
if err != nil {
return err
}
}

if after < rst.GetIndex() && rst.GetIndex() < before {
return nil
}
return errors.New("bad block interval")
kc1212 marked this conversation as resolved.
Show resolved Hide resolved
}
cbs := make(map[string]func(string) error)
cbs["block"] = cb
return cbs
kc1212 marked this conversation as resolved.
Show resolved Hide resolved
}

// Spawn is not implmented in a BasicContract. Types which embed BasicContract
// must override this method if they support spawning.
func (b BasicContract) Spawn(ReadOnlyStateTrie, Instruction, []Coin) (sc []StateChange, c []Coin, err error) {
Expand Down Expand Up @@ -297,7 +344,7 @@ func (c *contractConfig) VerifyDeferredInstruction(rst ReadOnlyStateTrie, inst I
return nil
}

return inst.VerifyWithOption(rst, msg, false)
return inst.VerifyWithOption(rst, msg, &VerificationOptions{IgnoreCounters: true})
}

// FormatMethod overrides the implementation from the BasicContract in order to
Expand Down
Loading