2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
2019-11-26 21:58:43 +00:00
|
|
|
"context"
|
2019-10-17 04:53:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
vchain "github.com/filecoin-project/chain-validation/pkg/chain"
|
|
|
|
vactors "github.com/filecoin-project/chain-validation/pkg/state/actors"
|
|
|
|
vaddress "github.com/filecoin-project/chain-validation/pkg/state/address"
|
|
|
|
vtypes "github.com/filecoin-project/chain-validation/pkg/state/types"
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2019-11-26 21:58:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMessageFactory(t *testing.T) {
|
|
|
|
ks := wallet.NewMemKeyStore()
|
|
|
|
wallet, err := wallet.NewWallet(ks)
|
|
|
|
require.NoError(t, err)
|
2019-11-26 21:58:43 +00:00
|
|
|
factory := NewMessageFactory(&walletWrapper{wallet})
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
gasPrice := vtypes.NewInt(1)
|
|
|
|
gasLimit := vtypes.GasUnit(1000)
|
|
|
|
p := vchain.NewMessageProducer(factory, gasLimit, gasPrice)
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
sender, err := wallet.GenerateKey(types.KTSecp256k1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
bfAddr := factory.FromSingletonAddress(vactors.BurntFundsAddress)
|
|
|
|
addr, err := vaddress.NewFromBytes(sender.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
m, err := p.Transfer(addr, bfAddr, 0, 1)
|
2019-10-17 04:53:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
messages := p.Messages()
|
|
|
|
assert.Equal(t, 1, len(messages))
|
|
|
|
msg := m.(*types.Message)
|
|
|
|
assert.Equal(t, m, msg)
|
|
|
|
assert.Equal(t, sender, msg.From)
|
|
|
|
assert.Equal(t, actors.BurntFundsAddress, msg.To)
|
|
|
|
assert.Equal(t, types.NewInt(1), msg.Value)
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
|
|
|
|
type walletWrapper struct {
|
|
|
|
w *wallet.Wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ww *walletWrapper) Sign(ctx context.Context, vaddr vaddress.Address, msg []byte) (*types.Signature, error) {
|
|
|
|
addr, err := address.NewFromBytes(vaddr.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ww.w.Sign(ctx, addr, msg)
|
|
|
|
}
|