Move docs
|
Before Width: | Height: | Size: 243 KiB After Width: | Height: | Size: 243 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 106 KiB After Width: | Height: | Size: 106 KiB |
|
Before Width: | Height: | Size: 140 KiB After Width: | Height: | Size: 140 KiB |
|
Before Width: | Height: | Size: 442 KiB After Width: | Height: | Size: 442 KiB |
|
Before Width: | Height: | Size: 290 KiB After Width: | Height: | Size: 290 KiB |
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 60 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
109
context.go
@ -1,109 +0,0 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
wire "github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/go-wire/data"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
// Actor abstracts any address that can authorize actions, hold funds,
|
||||
// or initiate any sort of transaction.
|
||||
//
|
||||
// It doesn't just have to be a pubkey on this chain, it could stem from
|
||||
// another app (like multi-sig account), or even another chain (via IBC)
|
||||
type Actor struct {
|
||||
ChainID string `json:"chain"` // this is empty unless it comes from a different chain
|
||||
App string `json:"app"` // the app that the actor belongs to
|
||||
Address data.Bytes `json:"addr"` // arbitrary app-specific unique id
|
||||
}
|
||||
|
||||
// NewActor - create a new actor
|
||||
func NewActor(app string, addr []byte) Actor {
|
||||
return Actor{App: app, Address: addr}
|
||||
}
|
||||
|
||||
// Bytes makes a binary coding, useful for turning this into a key in the store
|
||||
func (a Actor) Bytes() []byte {
|
||||
return wire.BinaryBytes(a)
|
||||
}
|
||||
|
||||
// Equals checks if two actors are the same
|
||||
func (a Actor) Equals(b Actor) bool {
|
||||
return a.ChainID == b.ChainID &&
|
||||
a.App == b.App &&
|
||||
bytes.Equal(a.Address, b.Address)
|
||||
}
|
||||
|
||||
// Empty checks if the actor is not initialized
|
||||
func (a Actor) Empty() bool {
|
||||
return a.ChainID == "" && a.App == "" && len(a.Address) == 0
|
||||
}
|
||||
|
||||
// WithChain creates a copy of the actor with a different chainID
|
||||
func (a Actor) WithChain(chainID string) (b Actor) {
|
||||
b = a
|
||||
b.ChainID = chainID
|
||||
return
|
||||
}
|
||||
|
||||
type Actors []Actor
|
||||
|
||||
func (a Actors) AllHaveChain(chainID string) bool {
|
||||
for _, b := range a {
|
||||
if b.ChainID != chainID {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Context is an interface, so we can implement "secure" variants that
|
||||
// rely on private fields to control the actions
|
||||
type Context interface {
|
||||
// context.Context
|
||||
log.Logger
|
||||
WithPermissions(perms ...Actor) Context
|
||||
HasPermission(perm Actor) bool
|
||||
GetPermissions(chain, app string) []Actor
|
||||
IsParent(ctx Context) bool
|
||||
Reset() Context
|
||||
ChainID() string
|
||||
BlockHeight() uint64
|
||||
}
|
||||
|
||||
//////////////////////////////// Sort Interface
|
||||
// USAGE sort.Sort(ByAll(<actor instance>))
|
||||
|
||||
func (a Actor) String() string {
|
||||
return fmt.Sprintf("%x", a.Address)
|
||||
}
|
||||
|
||||
// ByAll implements sort.Interface for []Actor.
|
||||
// It sorts be the ChainID, followed by the App, followed by the Address
|
||||
type ByAll []Actor
|
||||
|
||||
// Verify the sort interface at compile time
|
||||
var _ sort.Interface = ByAll{}
|
||||
|
||||
func (a ByAll) Len() int { return len(a) }
|
||||
func (a ByAll) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByAll) Less(i, j int) bool {
|
||||
|
||||
if a[i].ChainID < a[j].ChainID {
|
||||
return true
|
||||
}
|
||||
if a[i].ChainID > a[j].ChainID {
|
||||
return false
|
||||
}
|
||||
if a[i].App < a[j].App {
|
||||
return true
|
||||
}
|
||||
if a[i].App > a[j].App {
|
||||
return false
|
||||
}
|
||||
return bytes.Compare(a[i].Address, a[j].Address) == -1
|
||||
}
|
||||
@ -1,77 +0,0 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk"
|
||||
"github.com/cosmos/cosmos-sdk/state"
|
||||
"github.com/cosmos/cosmos-sdk/util"
|
||||
)
|
||||
|
||||
func makeSignTx() interface{} {
|
||||
key := crypto.GenPrivKeyEd25519().Wrap()
|
||||
payload := cmn.RandBytes(32)
|
||||
tx := newSingle(payload)
|
||||
Sign(tx, key)
|
||||
return tx
|
||||
}
|
||||
|
||||
func makeMultiSignTx(cnt int) interface{} {
|
||||
payload := cmn.RandBytes(32)
|
||||
tx := newMulti(payload)
|
||||
for i := 0; i < cnt; i++ {
|
||||
key := crypto.GenPrivKeyEd25519().Wrap()
|
||||
Sign(tx, key)
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
func makeHandler() sdk.Handler {
|
||||
return sdk.ChainDecorators(
|
||||
Signatures{},
|
||||
).WithHandler(
|
||||
util.OKHandler{},
|
||||
)
|
||||
}
|
||||
|
||||
func BenchmarkCheckOneSig(b *testing.B) {
|
||||
tx := makeSignTx()
|
||||
h := makeHandler()
|
||||
store := state.NewMemKVStore()
|
||||
for i := 1; i <= b.N; i++ {
|
||||
ctx := util.MockContext("foo", 100)
|
||||
_, err := h.DeliverTx(ctx, store, tx)
|
||||
// never should error
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCheckMultiSig(b *testing.B) {
|
||||
sigs := []int{1, 3, 8, 20}
|
||||
for _, cnt := range sigs {
|
||||
label := fmt.Sprintf("%dsigs", cnt)
|
||||
b.Run(label, func(sub *testing.B) {
|
||||
benchmarkCheckMultiSig(sub, cnt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkCheckMultiSig(b *testing.B, cnt int) {
|
||||
tx := makeMultiSignTx(cnt)
|
||||
h := makeHandler()
|
||||
store := state.NewMemKVStore()
|
||||
for i := 1; i <= b.N; i++ {
|
||||
ctx := util.MockContext("foo", 100)
|
||||
_, err := h.DeliverTx(ctx, store, tx)
|
||||
// never should error
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
//nolint
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidSignature = fmt.Errorf("Invalid Signature") //move auth
|
||||
errTooManySignatures = fmt.Errorf("Too many signatures") //move auth
|
||||
|
||||
unauthorized = abci.CodeType_Unauthorized
|
||||
)
|
||||
|
||||
func ErrTooManySignatures() errors.TMError {
|
||||
return errors.WithCode(errTooManySignatures, unauthorized)
|
||||
}
|
||||
func IsTooManySignaturesErr(err error) bool {
|
||||
return errors.IsSameError(errTooManySignatures, err)
|
||||
}
|
||||
|
||||
func ErrInvalidSignature() errors.TMError {
|
||||
return errors.WithCode(errInvalidSignature, unauthorized)
|
||||
}
|
||||
func IsInvalidSignatureErr(err error) bool {
|
||||
return errors.IsSameError(errInvalidSignature, err)
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/errors"
|
||||
)
|
||||
|
||||
func TestChecks(t *testing.T) {
|
||||
// TODO: make sure the Is and Err methods match
|
||||
assert := assert.New(t)
|
||||
|
||||
cases := []struct {
|
||||
err error
|
||||
check func(error) bool
|
||||
match bool
|
||||
}{
|
||||
// unauthorized includes InvalidSignature, but not visa versa
|
||||
{ErrInvalidSignature(), IsInvalidSignatureErr, true},
|
||||
{ErrInvalidSignature(), errors.IsUnauthorizedErr, true},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
match := tc.check(tc.err)
|
||||
assert.Equal(tc.match, match, "%d", i)
|
||||
}
|
||||
}
|
||||